send autoconfig on HA restart

Fixes #5
This commit is contained in:
Stefan Allius
2023-10-02 19:31:12 +02:00
parent 198146b5f4
commit 283bc2257b
2 changed files with 9 additions and 2 deletions

View File

@@ -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:

View File

@@ -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)