From 5f96ca9d85cfcfefc385cf18be1550ed2266e314 Mon Sep 17 00:00:00 2001 From: Patrick Huesmann Date: Wed, 24 May 2023 14:57:36 +0000 Subject: [PATCH] Fix discovery retry logic --- zytempmqtt/ZyTemp.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/zytempmqtt/ZyTemp.py b/zytempmqtt/ZyTemp.py index c51195b..0bbede0 100755 --- a/zytempmqtt/ZyTemp.py +++ b/zytempmqtt/ZyTemp.py @@ -1,10 +1,8 @@ # https://hackaday.io/project/5301-reverse-engineering-a-low-cost-usb-co-monitor -import hid import os -import sys -import time import logging as log +import hid from .config import ConfigFile CO2_USB_MFG = 'Holtek' @@ -19,9 +17,11 @@ _CO2MON_MAGIC_WORD = b'Htemp99e' _CO2MON_MAGIC_TABLE = (0, 0, 0, 0, 0, 0, 0, 0) + def list_to_longint(x): return sum([val << (i * 8) for i, val in enumerate(x[::-1])]) + def longint_to_list(x): return [(x >> i) & 0xFF for i in (56, 48, 40, 32, 24, 16, 8, 0)] @@ -74,6 +74,7 @@ def discovery(self): if self.discover_published: return + res = [] for meas in ZyTemp.MEASUREMENTS.values(): id = os.path.basename(self.cfg.mqtt_topic) config_content = { @@ -93,15 +94,22 @@ def discovery(self): 'value_template': '{{ value_json.%s }}' % meas['name'], 'icon': meas['ha_icon'] } - res = self.m.publish( + res_val = self.m.publish( os.path.join( self.cfg.discovery_prefix, 'sensor', config_content['unique_id'], 'config' ), config_content, retain=True ) - if res: - self.discover_published = True + res.append(res_val) + + if all(res): + l.log( + log.INFO, f'MQTT discovery published to {self.cfg.mqtt_host}') + self.discover_published = True + else: + l.log( + log.INFO, f'MQTT discovery to {self.cfg.mqtt_host} failed - retrying') self.m.run(0.1)