haco/haco/topics.py

208 lines
5.4 KiB
Python

from __future__ import annotations
from functools import cached_property
from typing import ClassVar, TYPE_CHECKING, Callable
from pydantic import Field
from corio import aio, Constants
from haco.base import Base
from haco.obs import logger
from haco.utils import get_prefix
if TYPE_CHECKING:
from haco.capabilities import Capability
class AnnounceTopic(Base):
"""
Base class for MQTT topics used for discovery and communication.
"""
IO: ClassVar[str | None] = None
KEY_DEFAULTS: ClassVar[dict] = {True: '{capability}_{io}_topic', False: '{io}_topic'}
key: str | None = None
value: str = '{path}/{capability}/{io}'
parent: Capability | None = Field(default=None, exclude=True, repr=False)
announce: dict | None = Field(default=None, exclude=True, repr=False)
subscriptions: dict | None = Field(default=None, exclude=True, repr=False)
control_method: Callable | None = Field(default=None, exclude=True, repr=False)
def __init__(self, **kwargs):
from haco.capabilities import Capability
Capability == Capability
super().__init__(**kwargs)
def set_parent(self, capability: Capability):
"""
Set the parent capability for the topic and link it to the control's method.
"""
self.parent = capability
if self.key is None:
self.key = self.KEY_DEFAULTS[bool(self.capability.name)]
method_name = self.callback_name
method = getattr(self.capability.control, method_name, None)
self.control_method = method
setattr(self.capability.control, method_name, self.handle)
def fill(self, mask):
return mask.format(**self.fills)
@property
def fills(self):
return dict(
path=str(self.capability.control.topic),
capability=self.capability.name or 'default',
io=self.IO
)
@cached_property
def topic_key(self):
return self.fill(self.key)
@cached_property
def topic(self):
return self.fill(self.value)
def get_announce(self) -> dict:
"""
Get the discovery announcement data for the topic.
"""
return {self.topic_key: self.topic}
@property
def callback_name(self):
if not self.capability.name:
return self.IO
return f'{self.capability.name}_{self.IO}'
@property
def callback_class_method_name(self):
return f'{self.capability.control.__class__.__name__}.{self.callback_name}'
def get_subscriptions(self) -> dict:
"""
Get the MQTT topic subscriptions for this topic (empty for base class).
"""
return {}
@cached_property
def capability(self):
return self.parent
async def handle(self, value):
raise NotImplementedError()
class AnnounceTopicState(AnnounceTopic):
"""
MQTT topic for reporting state to Home Assistant.
"""
IO: ClassVar[str] = 'state'
def get_subscriptions(self) -> dict:
"""
Get the MQTT topic subscriptions for this state topic (empty as it is output only).
"""
return {}
async def handle(self, value=None):
"""
Handle state reporting. Calls the linked control method and publishes the result.
"""
if not self.control_method:
logger.error(f'Incomplete base class: {self.callback_class_method_name}')
return
is_async = aio.is_async(self.control_method)
try:
if is_async:
value = await self.control_method(value)
else:
value = self.control_method(value)
except NotImplementedError:
logger.warning(f'Subclass has not implemented method: {self.callback_class_method_name} {self.topic} {value}')
return
value_raw = self.capability.converters.state(value)
await self.capability.control.device.client.publish(self.topic, value_raw, retain=True)
logger.info(f'{get_prefix(self.IO)}: {self.topic} {Constants.ARROW_RIGHT} {value_raw}')
return value_raw
class AnnounceTopicCommand(AnnounceTopic):
"""
MQTT topic for receiving commands from Home Assistant.
"""
IO: ClassVar[str] = 'command'
@property
def state(self):
return self.capability.state
async def handle(self, message):
"""
Handle incoming command messages. Decodes, converts, calls the linked control method, and updates state.
"""
value = message.payload.decode('utf-8')
value = self.capability.converters.command(value)
logger.info(f'{get_prefix(self.IO)}: {value} {Constants.ARROW_LEFT} {self.topic}')
if not self.control_method:
logger.error(f'Incomplete base class: {self.callback_class_method_name}')
return
is_async = aio.is_async(self.control_method)
try:
if is_async:
value_raw = await self.control_method(value)
else:
value_raw = self.control_method(value)
except NotImplementedError:
logger.warning(f'Subclass has not implemented method: {self.callback_class_method_name} {self.topic} {value}')
return
topic_state = self.state
if topic_state:
await topic_state.handle(value_raw)
def get_subscriptions(self) -> dict:
"""
Get the MQTT topic subscriptions for this command topic.
"""
return {self.topic: self}