Discovery readded for every boot with different strategy
This commit is contained in:
@@ -211,7 +211,6 @@ T_SET = f"{MQTT_PREFIX}/set"
|
|||||||
T_STATE = f"{MQTT_PREFIX}/state"
|
T_STATE = f"{MQTT_PREFIX}/state"
|
||||||
T_STATUS = f"{MQTT_PREFIX}/status"
|
T_STATUS = f"{MQTT_PREFIX}/status"
|
||||||
T_ZERO = f"{MQTT_PREFIX}/zero"
|
T_ZERO = f"{MQTT_PREFIX}/zero"
|
||||||
T_RESET_DISCOVERY = f"{MQTT_PREFIX}/resetdiscovery"
|
|
||||||
|
|
||||||
T_DISC_GAUGE = f"homeassistant/number/{MQTT_CLIENT_ID}/config"
|
T_DISC_GAUGE = f"homeassistant/number/{MQTT_CLIENT_ID}/config"
|
||||||
T_DISC_RED = f"homeassistant/switch/{MQTT_CLIENT_ID}_red/config"
|
T_DISC_RED = f"homeassistant/switch/{MQTT_CLIENT_ID}_red/config"
|
||||||
@@ -564,16 +563,7 @@ def on_message(topic, payload):
|
|||||||
warn(f"Invalid set value: '{payload}'")
|
warn(f"Invalid set value: '{payload}'")
|
||||||
return
|
return
|
||||||
|
|
||||||
if topic == T_RESET_DISCOVERY:
|
|
||||||
info("Reset discovery triggered")
|
|
||||||
try:
|
|
||||||
import uos
|
|
||||||
uos.remove(".discovery_ok")
|
|
||||||
info("Discovery flag file removed")
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
import machine
|
|
||||||
machine.reset()
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
@@ -584,7 +574,6 @@ def on_message(topic, payload):
|
|||||||
def _subscribe_all(c):
|
def _subscribe_all(c):
|
||||||
c.subscribe(f"{MQTT_PREFIX}/set")
|
c.subscribe(f"{MQTT_PREFIX}/set")
|
||||||
c.subscribe(f"{MQTT_PREFIX}/zero")
|
c.subscribe(f"{MQTT_PREFIX}/zero")
|
||||||
c.subscribe(f"{MQTT_PREFIX}/resetdiscovery")
|
|
||||||
for i in range(num_gauges):
|
for i in range(num_gauges):
|
||||||
prefix = f"{MQTT_PREFIX}/gauge{i}"
|
prefix = f"{MQTT_PREFIX}/gauge{i}"
|
||||||
c.subscribe(f"{prefix}/set")
|
c.subscribe(f"{prefix}/set")
|
||||||
@@ -612,8 +601,6 @@ def connect_mqtt():
|
|||||||
client.set_callback(on_message)
|
client.set_callback(on_message)
|
||||||
client.connect()
|
client.connect()
|
||||||
client_ref = client
|
client_ref = client
|
||||||
_subscribe_all(client)
|
|
||||||
|
|
||||||
_mqtt_connected = True
|
_mqtt_connected = True
|
||||||
info(f"MQTT connected client_id={MQTT_CLIENT_ID}")
|
info(f"MQTT connected client_id={MQTT_CLIENT_ID}")
|
||||||
|
|
||||||
@@ -831,45 +818,19 @@ def main():
|
|||||||
|
|
||||||
connect_wifi(WIFI_SSID, WIFI_PASSWORD)
|
connect_wifi(WIFI_SSID, WIFI_PASSWORD)
|
||||||
|
|
||||||
# Check if we need to publish discovery
|
# Connect MQTT (no subscriptions yet — keeps broker silent during discovery)
|
||||||
discovery_published = False
|
|
||||||
try:
|
|
||||||
with open(".discovery_ok", "r") as f:
|
|
||||||
content = f.read()
|
|
||||||
if content.strip() == "ok":
|
|
||||||
discovery_published = True
|
|
||||||
info("Discovery flag exists - skipping discovery")
|
|
||||||
except:
|
|
||||||
info("No discovery flag - will publish discovery")
|
|
||||||
|
|
||||||
info(f"Discovery: {discovery_published}")
|
|
||||||
|
|
||||||
# Connect MQTT first (needed for discovery)
|
|
||||||
connect_mqtt()
|
connect_mqtt()
|
||||||
|
|
||||||
# Give MQTT time to process subscriptions
|
# Publish discovery — broker has nothing to send back yet
|
||||||
info("Processing initial MQTT messages...")
|
info("Publishing discovery...")
|
||||||
for i in range(50):
|
publish_discovery(client_ref)
|
||||||
|
|
||||||
|
# Subscribe now — retained messages will start arriving from here
|
||||||
|
_subscribe_all(client_ref)
|
||||||
|
info("Draining initial retained messages...")
|
||||||
|
for _ in range(50):
|
||||||
client_ref.check_msg()
|
client_ref.check_msg()
|
||||||
utime.sleep_ms(20)
|
utime.sleep_ms(20)
|
||||||
info("Done with initial processing")
|
|
||||||
|
|
||||||
# Publish discovery if needed (before gauge initialization)
|
|
||||||
if not discovery_published:
|
|
||||||
info("Publishing discovery...")
|
|
||||||
publish_discovery(client_ref)
|
|
||||||
|
|
||||||
# Write flag file
|
|
||||||
try:
|
|
||||||
with open(".discovery_ok", "w") as f:
|
|
||||||
f.write("ok")
|
|
||||||
info("Discovery flag file created")
|
|
||||||
except Exception as e:
|
|
||||||
warn(f"Could not write discovery flag: {e}")
|
|
||||||
|
|
||||||
info("Discovery published, resetting...")
|
|
||||||
import machine
|
|
||||||
machine.reset()
|
|
||||||
|
|
||||||
# Now initialize gauges
|
# Now initialize gauges
|
||||||
info("Zeroing gauges on startup ...")
|
info("Zeroing gauges on startup ...")
|
||||||
|
|||||||
Reference in New Issue
Block a user