From 283bc2257be834db551135410c15c3d6b31a60a7 Mon Sep 17 00:00:00 2001 From: Stefan Allius Date: Mon, 2 Oct 2023 19:31:12 +0200 Subject: [PATCH 1/4] send autoconfig on HA restart Fixes #5 --- app/src/async_stream.py | 4 +++- app/src/mqtt.py | 7 ++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/app/src/async_stream.py b/app/src/async_stream.py index 5d3a51c..74f076c 100644 --- a/app/src/async_stream.py +++ b/app/src/async_stream.py @@ -136,8 +136,10 @@ class AsyncStream(Message): if self.server_side: db = self.db.db - if self.new_data.keys() & {'inverter', 'collector'}: + # check if new inverter or collevtor are available or if home assistant is online again + if (self.new_data.keys() & {'inverter', 'collector'}) or self.mqtt.home_assistant_restarted: await self.register_home_assistant() + self.mqtt.home_assistant_restarted = False # clear flag for key in self.new_data: if self.new_data[key] and key in db: diff --git a/app/src/mqtt.py b/app/src/mqtt.py index 913f357..0387f50 100644 --- a/app/src/mqtt.py +++ b/app/src/mqtt.py @@ -21,6 +21,7 @@ class Mqtt(metaclass=Singleton): logger_mqtt.debug(f'MQTT: __init__') loop = asyncio.get_event_loop() self.task = loop.create_task(self.__loop()) + self.home_assistant_restarted = False def __del__(self): @@ -55,7 +56,11 @@ class Mqtt(metaclass=Singleton): async with self.client.messages() as messages: await self.client.subscribe(f"{ha['auto_conf_prefix']}/status") async for message in messages: - logger_mqtt.info(f'Home-Assistant Status: {message.payload.decode("UTF-8")}') + status = message.payload.decode("UTF-8") + logger_mqtt.info(f'Home-Assistant Status: {status}') + if status == 'online': + self.home_assistant_restarted = True # set flag to force MQTT registering + except aiomqtt.MqttError: logger_mqtt.info(f"Connection lost; Reconnecting in {interval} seconds ...") await asyncio.sleep(interval) From 414eb19ffbc433a130929e761bab983b6d5a8784 Mon Sep 17 00:00:00 2001 From: Stefan Allius Date: Mon, 2 Oct 2023 19:35:59 +0200 Subject: [PATCH 2/4] clarify comment --- app/src/async_stream.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/async_stream.py b/app/src/async_stream.py index 74f076c..f58205b 100644 --- a/app/src/async_stream.py +++ b/app/src/async_stream.py @@ -136,7 +136,7 @@ class AsyncStream(Message): if self.server_side: db = self.db.db - # check if new inverter or collevtor are available or if home assistant is online again + # check if new inverter or collector infos are available or if home assistant changed state to online again if (self.new_data.keys() & {'inverter', 'collector'}) or self.mqtt.home_assistant_restarted: await self.register_home_assistant() self.mqtt.home_assistant_restarted = False # clear flag From 464e542a47aecdd7d0d8d2aac510fb09fa297f79 Mon Sep 17 00:00:00 2001 From: Stefan Allius Date: Mon, 2 Oct 2023 19:38:34 +0200 Subject: [PATCH 3/4] clearify comment --- app/src/async_stream.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/async_stream.py b/app/src/async_stream.py index f58205b..f3b0e22 100644 --- a/app/src/async_stream.py +++ b/app/src/async_stream.py @@ -136,7 +136,7 @@ class AsyncStream(Message): if self.server_side: db = self.db.db - # check if new inverter or collector infos are available or if home assistant changed state to online again + # check if new inverter or collector infos are available or when the home assistant has changed the status back to online if (self.new_data.keys() & {'inverter', 'collector'}) or self.mqtt.home_assistant_restarted: await self.register_home_assistant() self.mqtt.home_assistant_restarted = False # clear flag From e5d19ce07df2f4e40fb57e8833e70017a273d43d Mon Sep 17 00:00:00 2001 From: Stefan Allius Date: Mon, 2 Oct 2023 19:42:42 +0200 Subject: [PATCH 4/4] Force MQTT registration - when the home assistant has set the status to online again --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index be74d5c..08efc9e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] - Bump aiomqtt to version 1.2.1 +- Force MQTT registration when the home assistant has set the status to online again ## [0.0.5] - 2023-10-01