Skip to content

Commit

Permalink
Fix discovery retry logic
Browse files Browse the repository at this point in the history
  • Loading branch information
patrislav1 committed May 24, 2023
1 parent 7b28e66 commit 5f96ca9
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions zytempmqtt/ZyTemp.py
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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)]

Expand Down Expand Up @@ -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 = {
Expand All @@ -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)

Expand Down

0 comments on commit 5f96ca9

Please sign in to comment.