Discovery missing effects
This commit is contained in:
96
gauge.py
96
gauge.py
@@ -931,75 +931,9 @@ _last_discovery_ms = 0
|
|||||||
_DISCOVERY_INTERVAL_MS = 350
|
_DISCOVERY_INTERVAL_MS = 350
|
||||||
|
|
||||||
|
|
||||||
def _compact_discovery_payload(payload):
|
|
||||||
"""Trim optional HA discovery fields when RAM is tight."""
|
|
||||||
compact = dict(payload)
|
|
||||||
|
|
||||||
# Light entities are the largest payloads because they repeat effect metadata.
|
|
||||||
# Keep core functionality, but omit optional effect declarations to reduce heap use.
|
|
||||||
if compact.get("schema") == "json":
|
|
||||||
compact.pop("effect", None)
|
|
||||||
compact.pop("effect_list", None)
|
|
||||||
|
|
||||||
return compact
|
|
||||||
|
|
||||||
|
|
||||||
def check_mqtt():
|
|
||||||
global client_ref, _mqtt_connected, _last_mqtt_check
|
|
||||||
now = utime.ticks_ms()
|
|
||||||
if utime.ticks_diff(now, _last_mqtt_check) < _mqtt_check_interval_ms:
|
|
||||||
return _mqtt_connected
|
|
||||||
_last_mqtt_check = now
|
|
||||||
|
|
||||||
if client_ref is None:
|
|
||||||
return False
|
|
||||||
|
|
||||||
try:
|
|
||||||
client_ref.ping()
|
|
||||||
_mqtt_connected = True
|
|
||||||
return True
|
|
||||||
except Exception as e:
|
|
||||||
log_err(f"MQTT connection lost: {e}")
|
|
||||||
_mqtt_connected = False
|
|
||||||
|
|
||||||
log_err("Attempting MQTT reconnection...")
|
|
||||||
from umqtt.robust import MQTTClient
|
|
||||||
for attempt in range(3):
|
|
||||||
try:
|
|
||||||
client_ref = MQTTClient(
|
|
||||||
client_id=MQTT_CLIENT_ID,
|
|
||||||
server=MQTT_BROKER,
|
|
||||||
port=MQTT_PORT,
|
|
||||||
user=MQTT_USER,
|
|
||||||
password=MQTT_PASSWORD,
|
|
||||||
keepalive=30,
|
|
||||||
)
|
|
||||||
client_ref.set_callback(on_message)
|
|
||||||
client_ref.connect()
|
|
||||||
_mqtt_connected = True
|
|
||||||
info("MQTT reconnected!")
|
|
||||||
schedule_discovery()
|
|
||||||
_subscribe_all(client_ref)
|
|
||||||
publish_online(client_ref)
|
|
||||||
publish_state(client_ref)
|
|
||||||
publish_backlight_states(client_ref)
|
|
||||||
return True
|
|
||||||
except Exception as e2:
|
|
||||||
log_err(f"MQTT reconnect attempt {attempt + 1} failed: {e2}")
|
|
||||||
try:
|
|
||||||
client_ref.sock.close()
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
gc.collect()
|
|
||||||
utime.sleep_ms(2000)
|
|
||||||
|
|
||||||
log_err("MQTT reconnection failed after 3 attempts")
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def _publish_discovery_entity(client, topic, payload, log_msg):
|
def _publish_discovery_entity(client, topic, payload, log_msg):
|
||||||
gc.collect()
|
gc.collect()
|
||||||
client.publish(topic, ujson.dumps(_compact_discovery_payload(payload)), retain=True)
|
client.publish(topic, ujson.dumps(payload), retain=True)
|
||||||
info(log_msg)
|
info(log_msg)
|
||||||
|
|
||||||
|
|
||||||
@@ -1307,6 +1241,29 @@ def apply_motion_defaults():
|
|||||||
send_vfd_state()
|
send_vfd_state()
|
||||||
|
|
||||||
|
|
||||||
|
def _restore_led_states():
|
||||||
|
for i in range(num_gauges):
|
||||||
|
gt = gauge_topics[i]
|
||||||
|
info(f" red={_red_effect[i]} green={_green_effect[i]} status_r={_status_red_effect[i]} status_g={_status_green_effect[i]}")
|
||||||
|
for led_key, led_idx, color, effect_arr, state_topic in [
|
||||||
|
("red", _LED_RED, gauges[i]["ws2812_red"], _red_effect, gt["led_red_state"]),
|
||||||
|
("green", _LED_GREEN, gauges[i]["ws2812_green"], _green_effect, gt["led_green_state"]),
|
||||||
|
("status_red", _LED_STATUS_RED, gauges[i]["ws2812_red"], _status_red_effect, gt["status_red_state"]),
|
||||||
|
("status_green", _LED_STATUS_GREEN, gauges[i]["ws2812_green"], _status_green_effect, gt["status_green_state"]),
|
||||||
|
]:
|
||||||
|
if effect_arr[i]:
|
||||||
|
pub = {"state": "ON", "effect": effect_arr[i]}
|
||||||
|
_publish(state_topic, ujson.dumps(pub), retain=True)
|
||||||
|
if _red_effect[i]:
|
||||||
|
_apply_blink_or_led(i, _LED_RED, gauges[i]["ws2812_red"], _red_effect[i])
|
||||||
|
if _green_effect[i]:
|
||||||
|
_apply_blink_or_led(i, _LED_GREEN, gauges[i]["ws2812_green"], _green_effect[i])
|
||||||
|
if _status_red_effect[i]:
|
||||||
|
_apply_blink_or_led(i, _LED_STATUS_RED, gauges[i]["ws2812_red"], _status_red_effect[i])
|
||||||
|
if _status_green_effect[i]:
|
||||||
|
_apply_blink_or_led(i, _LED_STATUS_GREEN, gauges[i]["ws2812_green"], _status_green_effect[i])
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# Main
|
# Main
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
@@ -1347,6 +1304,7 @@ def main():
|
|||||||
_subscribe_all(client_ref)
|
_subscribe_all(client_ref)
|
||||||
schedule_discovery()
|
schedule_discovery()
|
||||||
|
|
||||||
|
publish_backlight_states(client_ref)
|
||||||
apply_motion_defaults()
|
apply_motion_defaults()
|
||||||
info("Draining initial retained messages...")
|
info("Draining initial retained messages...")
|
||||||
for _ in range(50):
|
for _ in range(50):
|
||||||
@@ -1359,6 +1317,10 @@ def main():
|
|||||||
gauge_last_rezero[i] = utime.ticks_ms()
|
gauge_last_rezero[i] = utime.ticks_ms()
|
||||||
info("Home command sent")
|
info("Home command sent")
|
||||||
|
|
||||||
|
utime.sleep_ms(100)
|
||||||
|
_restore_led_states()
|
||||||
|
info("LED effects restored")
|
||||||
|
|
||||||
info("Publishing state...")
|
info("Publishing state...")
|
||||||
publish_online(client_ref)
|
publish_online(client_ref)
|
||||||
publish_state(client_ref)
|
publish_state(client_ref)
|
||||||
|
|||||||
Reference in New Issue
Block a user