From 70afdf1857a1f0af65fd07856b754d1e660e9453 Mon Sep 17 00:00:00 2001 From: Edward Brown Date: Sun, 23 Nov 2025 11:54:24 +0000 Subject: [PATCH] Add Control.DATA for static announce data --- haco/base.py | 4 +++- haco/button.py | 13 +++++++++---- haco/client.py | 6 +++--- haco/climate.py | 6 +++++- haco/control.py | 4 ++-- haco/pulldown.py | 22 +++++++++++----------- haco/sb3.py | 4 +++- haco/switch.py | 6 ++++-- 8 files changed, 40 insertions(+), 25 deletions(-) diff --git a/haco/base.py b/haco/base.py index 68e5560..2828af6 100644 --- a/haco/base.py +++ b/haco/base.py @@ -6,8 +6,10 @@ from typing import Any @dataclass(kw_only=True) class Base: + DATA = dict() + def model_dump(self) -> dict[str, Any]: - data = {} + data = {} | self.DATA for fld in fields(self): meta = fld.metadata if meta.get('exclude', False): diff --git a/haco/button.py b/haco/button.py index e67400b..93d8a70 100644 --- a/haco/button.py +++ b/haco/button.py @@ -1,15 +1,20 @@ from dataclasses import dataclass -from typing import Literal from control import Control +from haco.capabilities import Capability @dataclass(kw_only=True) class Button(Control): - platform: Literal["climate"] = "button" + DATA = dict( + platform='button' + ) def command(self, value): raise NotImplementedError() - def state(self, value): - raise NotImplementedError() + @classmethod + def get_capabilities(cls): + return [ + Capability(name=None, state=None) + ] diff --git a/haco/client.py b/haco/client.py index 209777e..2147286 100644 --- a/haco/client.py +++ b/haco/client.py @@ -1,6 +1,5 @@ -import asyncio - import aiomqtt +import asyncio from fmtr.tools import mqtt, Constants from fmtr.tools.json_tools import to_json @@ -40,7 +39,8 @@ class ClientHaco(mqtt.Client): # Echo back as new state topic_state = topic_command.state - await topic_state.wrap_back(echo_val) + if topic_state: + await topic_state.wrap_back(echo_val) async def start(self): while True: diff --git a/haco/climate.py b/haco/climate.py index 2cd4050..3a27c38 100644 --- a/haco/climate.py +++ b/haco/climate.py @@ -7,7 +7,11 @@ from control import Control @dataclass(kw_only=True) class Climate(Control): - platform: Literal["climate"] = "climate" + DATA = dict( + platform='climate' + ) + + temperature_unit: Literal["C", "F"] = "C" temperature_min: float = 0 temperature_max: float = 40 diff --git a/haco/control.py b/haco/control.py index 4094e47..0664d52 100644 --- a/haco/control.py +++ b/haco/control.py @@ -14,7 +14,7 @@ if TYPE_CHECKING: @dataclass(kw_only=True) class Control(Base): name: str - platform: str + device: Device = field(default=None, init=False) capabilities: None | list[Capability] = field(default=None, metadata=dict(exclude=True)) @@ -69,7 +69,7 @@ class Control(Base): @property def announce_topic(self): - return f"homeassistant/{self.platform}/{self.unique_id}/config" + return f"homeassistant/{self.DATA['platform']}/{self.unique_id}/config" def get_availability_topic(self) -> str: return self.device.client.will.topic diff --git a/haco/pulldown.py b/haco/pulldown.py index 5c51951..1bd6c8b 100644 --- a/haco/pulldown.py +++ b/haco/pulldown.py @@ -1,18 +1,18 @@ -from typing import List +from dataclasses import dataclass -from haco.control import Control +from control import Control +@dataclass(kw_only=True) class Select(Control): - NAME = 'select' + DATA = dict( + platform='select' + ) - def __init__(self, name, options: List[str], icon=None): - self.options = options - super().__init__(name, icon=icon) + options: list[str] - def get_config_ha_ex(self): - data = { - 'options': self.options, - } + def command(self, value): + raise NotImplementedError() - return data + def state(self, value): + raise NotImplementedError() diff --git a/haco/sb3.py b/haco/sb3.py index aa8c81e..1fa7c5c 100644 --- a/haco/sb3.py +++ b/haco/sb3.py @@ -5,6 +5,7 @@ from haco.client import ClientHaco from haco.climate import Climate from haco.constants import MQTT_HOST from haco.device import Device +from haco.pulldown import Select from haco.switch import Switch @@ -15,7 +16,8 @@ async def main(): b1 = Button(name="Dev Button 1") s1 = Switch(name="Dev Switch 1") - device = Device(name="Dev Device", controls=[climate_a, climate_b, b1, s1]) + sel1 = Select(name="Dev Select 1", options=['a', 'b', 'c']) + device = Device(name="Dev Device", controls=[climate_a, climate_b, b1, s1, sel1]) client = ClientHaco(hostname=MQTT_HOST, device=device) diff --git a/haco/switch.py b/haco/switch.py index d35d474..b9cc1a4 100644 --- a/haco/switch.py +++ b/haco/switch.py @@ -1,12 +1,14 @@ from dataclasses import dataclass -from typing import Literal from control import Control @dataclass(kw_only=True) class Switch(Control): - platform: Literal["climate"] = "switch" + DATA = dict( + platform='switch' + ) + def command(self, value): raise NotImplementedError()