catch Mqtt errors

- we catch mqtt errors, so we can forward messages to
  the tsun cloud even if the mqtt broker is not running
This commit is contained in:
Stefan Allius
2023-10-23 21:17:17 +02:00
parent ff0979663e
commit 50977d5afd

View File

@@ -2,6 +2,8 @@ import asyncio, logging, traceback, json
from config import Config from config import Config
from async_stream import AsyncStream from async_stream import AsyncStream
from mqtt import Mqtt from mqtt import Mqtt
from aiomqtt import MqttCodeError
#import gc #import gc
#logger = logging.getLogger('conn') #logger = logging.getLogger('conn')
@@ -34,8 +36,9 @@ class Inverter(AsyncStream):
if self.remoteStream: if self.remoteStream:
logging.debug ("disconnect client connection") logging.debug ("disconnect client connection")
self.remoteStream.disc() self.remoteStream.disc()
try:
await self.__async_publ_mqtt_packet('proxy') await self.__async_publ_mqtt_packet('proxy')
except: pass
async def client_loop(self, addr): async def client_loop(self, addr):
'''Loop for receiving messages from the TSUN cloud (client-side)''' '''Loop for receiving messages from the TSUN cloud (client-side)'''
@@ -74,14 +77,22 @@ class Inverter(AsyncStream):
async def async_publ_mqtt(self) -> None: async def async_publ_mqtt(self) -> None:
'''puplish data to MQTT broker''' '''puplish data to MQTT broker'''
# check if new inverter or collector infos are available or when the home assistant has changed the status back to online # check if new inverter or collector infos are available or when the home assistant has changed the status back to online
if (('inverter' in self.new_data and self.new_data['inverter']) or try:
('collector' in self.new_data and self.new_data['collector']) or if (('inverter' in self.new_data and self.new_data['inverter']) or
self.mqtt.ha_restarts != self.ha_restarts): ('collector' in self.new_data and self.new_data['collector']) or
await self.__register_home_assistant() self.mqtt.ha_restarts != self.ha_restarts):
self.ha_restarts = self.mqtt.ha_restarts await self.__register_home_assistant()
self.ha_restarts = self.mqtt.ha_restarts
for key in self.new_data: for key in self.new_data:
await self.__async_publ_mqtt_packet(key) await self.__async_publ_mqtt_packet(key)
except MqttCodeError as error:
logging.error(f'Mqtt except: {error}')
except Exception:
logging.error(
f"Inverter: Exception:\n"
f"{traceback.format_exc()}")
async def __async_publ_mqtt_packet(self, key): async def __async_publ_mqtt_packet(self, key):
db = self.db.db db = self.db.db
@@ -101,14 +112,9 @@ class Inverter(AsyncStream):
async def __register_home_assistant(self) -> None: async def __register_home_assistant(self) -> None:
'''register all our topics at home assistant''' '''register all our topics at home assistant'''
try: for data_json, component, node_id, id in self.db.ha_confs(self.entity_prfx, self.node_id, self.unique_id, self.proxy_node_id, self.proxy_unique_id, self.sug_area):
for data_json, component, node_id, id in self.db.ha_confs(self.entity_prfx, self.node_id, self.unique_id, self.proxy_node_id, self.proxy_unique_id, self.sug_area): logger_mqtt.debug(f"MQTT Register: cmp:'{component}' node_id:'{node_id}' {data_json}")
logger_mqtt.debug(f"MQTT Register: cmp:'{component}' node_id:'{node_id}' {data_json}") await self.mqtt.publish(f"{self.discovery_prfx}{component}/{node_id}{id}/config", data_json)
await self.mqtt.publish(f"{self.discovery_prfx}{component}/{node_id}{id}/config", data_json)
except Exception:
logging.error(
f"Inverter: Exception:\n"
f"{traceback.format_exc()}")
def close(self) -> None: def close(self) -> None:
logging.debug(f'Inverter.close() {self.addr}') logging.debug(f'Inverter.close() {self.addr}')