Merge pull request #12 from s-allius/s-allius/issue5

S allius/issue5
This commit is contained in:
Stefan Allius
2023-10-02 19:49:46 +02:00
committed by GitHub
3 changed files with 10 additions and 2 deletions

View File

@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
- Bump aiomqtt to version 1.2.1 - 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 ## [0.0.5] - 2023-10-01

View File

@@ -136,8 +136,10 @@ class AsyncStream(Message):
if self.server_side: if self.server_side:
db = self.db.db db = self.db.db
if self.new_data.keys() & {'inverter', 'collector'}: # 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() await self.register_home_assistant()
self.mqtt.home_assistant_restarted = False # clear flag
for key in self.new_data: for key in self.new_data:
if self.new_data[key] and key in db: if self.new_data[key] and key in db:

View File

@@ -21,6 +21,7 @@ class Mqtt(metaclass=Singleton):
logger_mqtt.debug(f'MQTT: __init__') logger_mqtt.debug(f'MQTT: __init__')
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
self.task = loop.create_task(self.__loop()) self.task = loop.create_task(self.__loop())
self.home_assistant_restarted = False
def __del__(self): def __del__(self):
@@ -55,7 +56,11 @@ class Mqtt(metaclass=Singleton):
async with self.client.messages() as messages: async with self.client.messages() as messages:
await self.client.subscribe(f"{ha['auto_conf_prefix']}/status") await self.client.subscribe(f"{ha['auto_conf_prefix']}/status")
async for message in messages: 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: except aiomqtt.MqttError:
logger_mqtt.info(f"Connection lost; Reconnecting in {interval} seconds ...") logger_mqtt.info(f"Connection lost; Reconnecting in {interval} seconds ...")
await asyncio.sleep(interval) await asyncio.sleep(interval)