From 74f967f7f939484aa7dcc06b3a718393cb6922ee Mon Sep 17 00:00:00 2001 From: Edward Brown Date: Wed, 10 Dec 2025 15:19:44 +0000 Subject: [PATCH] Stop automatically publishing state on announce, and add separate initialise method for announce+state --- haco/client.py | 11 +++-------- haco/control.py | 11 +++++------ haco/device.py | 5 ++++- haco/version | 2 +- 4 files changed, 13 insertions(+), 16 deletions(-) diff --git a/haco/client.py b/haco/client.py index ce7abcb..671bf4b 100644 --- a/haco/client.py +++ b/haco/client.py @@ -34,7 +34,7 @@ class ClientHaco(mqtt.Client): payload = message.payload.decode() logger.info(f"{message.topic.value}{Constants.ARROW}{payload}") topic_command = self.device.subscriptions[message.topic.value] - echo_val = await topic_command.handle(message) + await topic_command.handle(message) @@ -42,15 +42,10 @@ class ClientHaco(mqtt.Client): while True: try: async with self: - - await self.device.announce() - + await self.device.initialise() await self.publish(self.will.topic, self.ONLINE, retain=True) - - # await self.publish('haco/development/3012edb1a6d4-1ed9c3469373/dev-device/dev-climate-b/power/command', "OFF", retain=True) - await self.handle() except aiomqtt.MqttError as e: - logger.info(f"⚠️ MQTT disconnected: {e}; retrying in 5s") + logger.info(f"MQTT disconnected: {e}; retrying in 5s") await asyncio.sleep(5) diff --git a/haco/control.py b/haco/control.py index b14cdb0..f1e458b 100644 --- a/haco/control.py +++ b/haco/control.py @@ -98,7 +98,11 @@ class Control(Base, Generic[DeviceT]): return data async def initialise(self): - ... + await self.announce() + for capability in self.capabilities: + if not capability.state: + continue + await capability.state.handle(value=None) async def announce(self): """ @@ -111,11 +115,6 @@ class Control(Base, Generic[DeviceT]): logger.info(f'Announcing {topic} with {data_json}') await self.device.client.publish(topic, data_json, retain=True) - for capability in self.capabilities: - if not capability.state: - continue - await capability.state.handle(value=None) - if __name__ == "__main__": diff --git a/haco/device.py b/haco/device.py index cc25594..718cc60 100644 --- a/haco/device.py +++ b/haco/device.py @@ -24,7 +24,6 @@ class Device(Base): controls: List[Control] = field(default_factory=list, metadata=dict(exclude=True)) parent: ClientHaco | None = field(metadata=dict(exclude=True), init=False) - # announce: dict | None = field(default=None, metadata=dict(exclude=True), init=False) subscriptions: dict | None = field(default=None, metadata=dict(exclude=True), init=False) @@ -52,6 +51,10 @@ class Device(Base): for control in self.controls: await control.announce() + async def initialise(self): + for control in self.controls: + await control.initialise() + @property def client(self): diff --git a/haco/version b/haco/version index 7693c96..446ba66 100644 --- a/haco/version +++ b/haco/version @@ -1 +1 @@ -0.1.3 \ No newline at end of file +0.1.4 \ No newline at end of file