MQTT troubleshooting
This commit is contained in:
54
gauge.py
54
gauge.py
@@ -756,6 +756,10 @@ def connect_mqtt():
|
|||||||
|
|
||||||
_mqtt_check_interval_ms = 30000
|
_mqtt_check_interval_ms = 30000
|
||||||
_last_mqtt_check = 0
|
_last_mqtt_check = 0
|
||||||
|
_discovery_phases = ()
|
||||||
|
_discovery_phase_idx = 0
|
||||||
|
_last_discovery_ms = 0
|
||||||
|
_DISCOVERY_INTERVAL_MS = 500
|
||||||
|
|
||||||
|
|
||||||
def check_mqtt():
|
def check_mqtt():
|
||||||
@@ -791,7 +795,7 @@ def check_mqtt():
|
|||||||
client_ref.connect()
|
client_ref.connect()
|
||||||
_mqtt_connected = True
|
_mqtt_connected = True
|
||||||
info("MQTT reconnected!")
|
info("MQTT reconnected!")
|
||||||
publish_discovery(client_ref)
|
schedule_discovery()
|
||||||
_subscribe_all(client_ref)
|
_subscribe_all(client_ref)
|
||||||
publish_online(client_ref)
|
publish_online(client_ref)
|
||||||
publish_state(client_ref)
|
publish_state(client_ref)
|
||||||
@@ -1020,6 +1024,47 @@ def publish_discovery(client):
|
|||||||
_publish_backlight_status_discovery(client, _dev_ref)
|
_publish_backlight_status_discovery(client, _dev_ref)
|
||||||
|
|
||||||
|
|
||||||
|
def schedule_discovery():
|
||||||
|
global _discovery_phases, _discovery_phase_idx, _last_discovery_ms
|
||||||
|
_dev_ref = _DEVICE
|
||||||
|
_discovery_phases = (
|
||||||
|
lambda client: _clear_legacy_discovery(client),
|
||||||
|
lambda client: _publish_gauge_discovery(client, _dev_ref),
|
||||||
|
lambda client: _publish_speed_discovery(client, _dev_ref),
|
||||||
|
lambda client: _publish_acceleration_discovery(client, _dev_ref),
|
||||||
|
lambda client: _publish_indicator_led_discovery(client, _dev_ref),
|
||||||
|
lambda client: _publish_backlight_status_discovery(client, _dev_ref),
|
||||||
|
)
|
||||||
|
_discovery_phase_idx = 0
|
||||||
|
_last_discovery_ms = 0
|
||||||
|
|
||||||
|
|
||||||
|
def _clear_legacy_discovery(client):
|
||||||
|
for i in range(num_gauges):
|
||||||
|
for old_t in [
|
||||||
|
f"homeassistant/switch/{MQTT_CLIENT_ID}_g{i}_red/config",
|
||||||
|
f"homeassistant/switch/{MQTT_CLIENT_ID}_g{i}_green/config",
|
||||||
|
f"homeassistant/switch/{MQTT_CLIENT_ID}_g{i}_status_red/config",
|
||||||
|
f"homeassistant/switch/{MQTT_CLIENT_ID}_g{i}_status_green/config",
|
||||||
|
]:
|
||||||
|
client.publish(old_t, b"", retain=True)
|
||||||
|
_discovery_pause(client, count=2, delay_ms=15)
|
||||||
|
|
||||||
|
|
||||||
|
def service_discovery():
|
||||||
|
global _discovery_phase_idx, _last_discovery_ms
|
||||||
|
if client_ref is None or _discovery_phase_idx >= len(_discovery_phases):
|
||||||
|
return
|
||||||
|
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
if _last_discovery_ms and utime.ticks_diff(now, _last_discovery_ms) < _DISCOVERY_INTERVAL_MS:
|
||||||
|
return
|
||||||
|
|
||||||
|
_discovery_phases[_discovery_phase_idx](client_ref)
|
||||||
|
_discovery_phase_idx += 1
|
||||||
|
_last_discovery_ms = utime.ticks_ms()
|
||||||
|
|
||||||
|
|
||||||
def publish_online(client):
|
def publish_online(client):
|
||||||
for i in range(num_gauges):
|
for i in range(num_gauges):
|
||||||
client.publish(gauge_topics[i]["status"], b"online", retain=True)
|
client.publish(gauge_topics[i]["status"], b"online", retain=True)
|
||||||
@@ -1052,11 +1097,9 @@ def main():
|
|||||||
connect_wifi(WIFI_SSID, WIFI_PASSWORD)
|
connect_wifi(WIFI_SSID, WIFI_PASSWORD)
|
||||||
|
|
||||||
connect_mqtt()
|
connect_mqtt()
|
||||||
|
|
||||||
info("Publishing discovery...")
|
|
||||||
publish_discovery(client_ref)
|
|
||||||
|
|
||||||
_subscribe_all(client_ref)
|
_subscribe_all(client_ref)
|
||||||
|
schedule_discovery()
|
||||||
|
|
||||||
apply_motion_defaults()
|
apply_motion_defaults()
|
||||||
info("Draining initial retained messages...")
|
info("Draining initial retained messages...")
|
||||||
for _ in range(50):
|
for _ in range(50):
|
||||||
@@ -1099,6 +1142,7 @@ def main():
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
client_ref.check_msg()
|
client_ref.check_msg()
|
||||||
|
service_discovery()
|
||||||
arduino_recv()
|
arduino_recv()
|
||||||
_flush_backlight_state()
|
_flush_backlight_state()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user