Compare commits
1 Commits
feature/sp
...
21b413eb57
| Author | SHA1 | Date | |
|---|---|---|---|
| 21b413eb57 |
382
gauge.py
382
gauge.py
@@ -756,10 +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_queue = []
|
||||||
_discovery_phase_idx = 0
|
_discovery_idx = 0
|
||||||
_last_discovery_ms = 0
|
_last_discovery_ms = 0
|
||||||
_DISCOVERY_INTERVAL_MS = 500
|
_DISCOVERY_INTERVAL_MS = 200
|
||||||
|
|
||||||
|
|
||||||
def check_mqtt():
|
def check_mqtt():
|
||||||
@@ -809,237 +809,208 @@ def check_mqtt():
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def _discovery_pause(client, count=5, delay_ms=25):
|
|
||||||
for _ in range(count):
|
|
||||||
client.check_msg()
|
|
||||||
utime.sleep_ms(delay_ms)
|
|
||||||
gc.collect()
|
|
||||||
|
|
||||||
|
|
||||||
def _publish_discovery_entity(client, topic, payload, log_msg):
|
def _publish_discovery_entity(client, topic, payload, log_msg):
|
||||||
client.publish(topic, ujson.dumps(payload), retain=True)
|
client.publish(topic, ujson.dumps(payload), retain=True)
|
||||||
info(log_msg)
|
info(log_msg)
|
||||||
|
|
||||||
|
|
||||||
def _publish_gauge_discovery(client, dev_ref):
|
def _append_gauge_discovery(entries, dev_ref):
|
||||||
for i, g in enumerate(gauges):
|
for i, g in enumerate(gauges):
|
||||||
gt = gauge_topics[i]
|
gt = gauge_topics[i]
|
||||||
_publish_discovery_entity(
|
entries.append(
|
||||||
client,
|
(
|
||||||
gt["disc"],
|
gt["disc"],
|
||||||
{
|
{
|
||||||
"name": g["entity_name"],
|
"name": g["entity_name"],
|
||||||
"unique_id": f"{MQTT_CLIENT_ID}_g{i}",
|
"unique_id": f"{MQTT_CLIENT_ID}_g{i}",
|
||||||
"cmd_t": gt["set"],
|
"cmd_t": gt["set"],
|
||||||
"stat_t": gt["state"],
|
"stat_t": gt["state"],
|
||||||
"avty_t": gt["status"],
|
"avty_t": gt["status"],
|
||||||
"min": g["min"],
|
"min": g["min"],
|
||||||
"max": g["max"],
|
"max": g["max"],
|
||||||
"step": 1,
|
"step": 1,
|
||||||
"unit_of_meas": g["unit"],
|
"unit_of_meas": g["unit"],
|
||||||
"icon": "mdi:gauge",
|
"icon": "mdi:gauge",
|
||||||
"dev": dev_ref,
|
"dev": dev_ref,
|
||||||
},
|
},
|
||||||
f"Discovery: gauge {i} ({g['name']})",
|
f"Discovery: gauge {i} ({g['name']})",
|
||||||
|
)
|
||||||
)
|
)
|
||||||
_discovery_pause(client)
|
|
||||||
|
|
||||||
|
|
||||||
def _publish_speed_discovery(client, dev_ref):
|
def _append_speed_discovery(entries, dev_ref):
|
||||||
for i, g in enumerate(gauges):
|
for i, g in enumerate(gauges):
|
||||||
gt = gauge_topics[i]
|
gt = gauge_topics[i]
|
||||||
_publish_discovery_entity(
|
entries.append(
|
||||||
client,
|
(
|
||||||
gt["speed_disc"],
|
gt["speed_disc"],
|
||||||
{
|
{
|
||||||
"name": f"{g['name']} Speed",
|
"name": f"{g['name']} Speed",
|
||||||
"unique_id": f"{MQTT_CLIENT_ID}_g{i}_speed",
|
"unique_id": f"{MQTT_CLIENT_ID}_g{i}_speed",
|
||||||
"cmd_t": gt["speed"],
|
"cmd_t": gt["speed"],
|
||||||
"stat_t": gt["speed_state"],
|
"stat_t": gt["speed_state"],
|
||||||
"avty_t": gt["status"],
|
"avty_t": gt["status"],
|
||||||
"min": 1,
|
"min": 1,
|
||||||
"max": 50000,
|
"max": 50000,
|
||||||
"step": 1,
|
"step": 1,
|
||||||
"mode": "box",
|
"mode": "box",
|
||||||
"unit_of_meas": "steps/s",
|
"unit_of_meas": "steps/s",
|
||||||
"icon": "mdi:speedometer",
|
"icon": "mdi:speedometer",
|
||||||
"entity_category": "config",
|
"entity_category": "config",
|
||||||
"dev": dev_ref,
|
"dev": dev_ref,
|
||||||
},
|
},
|
||||||
f"Discovery: gauge {i} speed",
|
f"Discovery: gauge {i} speed",
|
||||||
|
)
|
||||||
)
|
)
|
||||||
_discovery_pause(client)
|
|
||||||
|
|
||||||
|
|
||||||
def _publish_acceleration_discovery(client, dev_ref):
|
def _append_acceleration_discovery(entries, dev_ref):
|
||||||
for i, g in enumerate(gauges):
|
for i, g in enumerate(gauges):
|
||||||
gt = gauge_topics[i]
|
gt = gauge_topics[i]
|
||||||
_publish_discovery_entity(
|
entries.append(
|
||||||
client,
|
(
|
||||||
gt["acceleration_disc"],
|
gt["acceleration_disc"],
|
||||||
{
|
{
|
||||||
"name": f"{g['name']} Acceleration",
|
"name": f"{g['name']} Acceleration",
|
||||||
"unique_id": f"{MQTT_CLIENT_ID}_g{i}_acceleration",
|
"unique_id": f"{MQTT_CLIENT_ID}_g{i}_acceleration",
|
||||||
"cmd_t": gt["acceleration"],
|
"cmd_t": gt["acceleration"],
|
||||||
"stat_t": gt["acceleration_state"],
|
"stat_t": gt["acceleration_state"],
|
||||||
"avty_t": gt["status"],
|
"avty_t": gt["status"],
|
||||||
"min": 1,
|
"min": 1,
|
||||||
"max": 100000,
|
"max": 100000,
|
||||||
"step": 1,
|
"step": 1,
|
||||||
"mode": "box",
|
"mode": "box",
|
||||||
"unit_of_meas": "steps/s2",
|
"unit_of_meas": "steps/s2",
|
||||||
"icon": "mdi:chart-bell-curve-cumulative",
|
"icon": "mdi:chart-bell-curve-cumulative",
|
||||||
"entity_category": "config",
|
"entity_category": "config",
|
||||||
"dev": dev_ref,
|
"dev": dev_ref,
|
||||||
},
|
},
|
||||||
f"Discovery: gauge {i} acceleration",
|
f"Discovery: gauge {i} acceleration",
|
||||||
|
)
|
||||||
)
|
)
|
||||||
_discovery_pause(client)
|
|
||||||
|
|
||||||
|
|
||||||
def _publish_indicator_led_discovery(client, dev_ref):
|
def _append_indicator_led_discovery(entries, dev_ref):
|
||||||
for i, g in enumerate(gauges):
|
for i, g in enumerate(gauges):
|
||||||
gt = gauge_topics[i]
|
gt = gauge_topics[i]
|
||||||
_publish_discovery_entity(
|
entries.append(
|
||||||
client,
|
(
|
||||||
gt["led_red_disc"],
|
gt["led_red_disc"],
|
||||||
{
|
{
|
||||||
"name": f"{g['name']} Dial Red LED",
|
"name": f"{g['name']} Dial Red LED",
|
||||||
"uniq_id": f"{MQTT_CLIENT_ID}_g{i}_red",
|
"uniq_id": f"{MQTT_CLIENT_ID}_g{i}_red",
|
||||||
"cmd_t": gt["led_red"],
|
"cmd_t": gt["led_red"],
|
||||||
"stat_t": gt["led_red_state"],
|
"stat_t": gt["led_red_state"],
|
||||||
"schema": "json",
|
"schema": "json",
|
||||||
"supported_color_modes": ["onoff"],
|
"supported_color_modes": ["onoff"],
|
||||||
"effect": True,
|
"effect": True,
|
||||||
"effect_list": _EFFECT_LIST,
|
"effect_list": _EFFECT_LIST,
|
||||||
"icon": "mdi:led-on",
|
"icon": "mdi:led-on",
|
||||||
"dev": dev_ref,
|
"dev": dev_ref,
|
||||||
"ret": True,
|
"ret": True,
|
||||||
},
|
},
|
||||||
f"Discovery: gauge {i} red LED",
|
f"Discovery: gauge {i} red LED",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
entries.append(
|
||||||
|
(
|
||||||
|
gt["led_green_disc"],
|
||||||
|
{
|
||||||
|
"name": f"{g['name']} Dial Green LED",
|
||||||
|
"uniq_id": f"{MQTT_CLIENT_ID}_g{i}_green",
|
||||||
|
"cmd_t": gt["led_green"],
|
||||||
|
"stat_t": gt["led_green_state"],
|
||||||
|
"schema": "json",
|
||||||
|
"supported_color_modes": ["onoff"],
|
||||||
|
"effect": True,
|
||||||
|
"effect_list": _EFFECT_LIST,
|
||||||
|
"icon": "mdi:led-on",
|
||||||
|
"dev": dev_ref,
|
||||||
|
"ret": True,
|
||||||
|
},
|
||||||
|
f"Discovery: gauge {i} green LED",
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
_publish_discovery_entity(
|
|
||||||
client,
|
|
||||||
gt["led_green_disc"],
|
|
||||||
{
|
|
||||||
"name": f"{g['name']} Dial Green LED",
|
|
||||||
"uniq_id": f"{MQTT_CLIENT_ID}_g{i}_green",
|
|
||||||
"cmd_t": gt["led_green"],
|
|
||||||
"stat_t": gt["led_green_state"],
|
|
||||||
"schema": "json",
|
|
||||||
"supported_color_modes": ["onoff"],
|
|
||||||
"effect": True,
|
|
||||||
"effect_list": _EFFECT_LIST,
|
|
||||||
"icon": "mdi:led-on",
|
|
||||||
"dev": dev_ref,
|
|
||||||
"ret": True,
|
|
||||||
},
|
|
||||||
f"Discovery: gauge {i} green LED",
|
|
||||||
)
|
|
||||||
_discovery_pause(client)
|
|
||||||
|
|
||||||
|
def _append_backlight_status_discovery(entries, dev_ref):
|
||||||
def _publish_backlight_status_discovery(client, dev_ref):
|
|
||||||
for i, g in enumerate(gauges):
|
for i, g in enumerate(gauges):
|
||||||
gt = gauge_topics[i]
|
gt = gauge_topics[i]
|
||||||
_publish_discovery_entity(
|
entries.append(
|
||||||
client,
|
(
|
||||||
gt["led_bl_disc"],
|
gt["led_bl_disc"],
|
||||||
{
|
{
|
||||||
"name": f"{g['name']} Backlight",
|
"name": f"{g['name']} Backlight",
|
||||||
"uniq_id": f"{MQTT_CLIENT_ID}_g{i}_bl",
|
"uniq_id": f"{MQTT_CLIENT_ID}_g{i}_bl",
|
||||||
"cmd_t": gt["led_bl"],
|
"cmd_t": gt["led_bl"],
|
||||||
"stat_t": gt["led_bl_state"],
|
"stat_t": gt["led_bl_state"],
|
||||||
"schema": "json",
|
"schema": "json",
|
||||||
"supported_color_modes": ["rgb"],
|
"supported_color_modes": ["rgb"],
|
||||||
"effect": True,
|
"effect": True,
|
||||||
"effect_list": _EFFECT_LIST,
|
"effect_list": _EFFECT_LIST,
|
||||||
"icon": "mdi:led-strip",
|
"icon": "mdi:led-strip",
|
||||||
"dev": dev_ref,
|
"dev": dev_ref,
|
||||||
"ret": True,
|
"ret": True,
|
||||||
},
|
},
|
||||||
f"Discovery: gauge {i} backlight",
|
f"Discovery: gauge {i} backlight",
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
entries.append(
|
||||||
_publish_discovery_entity(
|
(
|
||||||
client,
|
gt["status_red_disc"],
|
||||||
gt["status_red_disc"],
|
{
|
||||||
{
|
"name": f"{g['name']} Channel Status Red",
|
||||||
"name": f"{g['name']} Channel Status Red",
|
"uniq_id": f"{MQTT_CLIENT_ID}_g{i}_status_red",
|
||||||
"uniq_id": f"{MQTT_CLIENT_ID}_g{i}_status_red",
|
"cmd_t": gt["status_red"],
|
||||||
"cmd_t": gt["status_red"],
|
"stat_t": gt["status_red_state"],
|
||||||
"stat_t": gt["status_red_state"],
|
"schema": "json",
|
||||||
"schema": "json",
|
"supported_color_modes": ["onoff"],
|
||||||
"supported_color_modes": ["onoff"],
|
"effect": True,
|
||||||
"effect": True,
|
"effect_list": _EFFECT_LIST,
|
||||||
"effect_list": _EFFECT_LIST,
|
"icon": "mdi:led-on",
|
||||||
"icon": "mdi:led-on",
|
"dev": dev_ref,
|
||||||
"dev": dev_ref,
|
"ret": True,
|
||||||
"ret": True,
|
},
|
||||||
},
|
f"Discovery: gauge {i} status red",
|
||||||
f"Discovery: gauge {i} status red",
|
)
|
||||||
)
|
)
|
||||||
|
entries.append(
|
||||||
_publish_discovery_entity(
|
(
|
||||||
client,
|
gt["status_green_disc"],
|
||||||
gt["status_green_disc"],
|
{
|
||||||
{
|
"name": f"{g['name']} Channel Status Green",
|
||||||
"name": f"{g['name']} Channel Status Green",
|
"uniq_id": f"{MQTT_CLIENT_ID}_g{i}_status_green",
|
||||||
"uniq_id": f"{MQTT_CLIENT_ID}_g{i}_status_green",
|
"cmd_t": gt["status_green"],
|
||||||
"cmd_t": gt["status_green"],
|
"stat_t": gt["status_green_state"],
|
||||||
"stat_t": gt["status_green_state"],
|
"schema": "json",
|
||||||
"schema": "json",
|
"supported_color_modes": ["onoff"],
|
||||||
"supported_color_modes": ["onoff"],
|
"effect": True,
|
||||||
"effect": True,
|
"effect_list": _EFFECT_LIST,
|
||||||
"effect_list": _EFFECT_LIST,
|
"icon": "mdi:led-on",
|
||||||
"icon": "mdi:led-on",
|
"dev": dev_ref,
|
||||||
"dev": dev_ref,
|
"ret": True,
|
||||||
"ret": True,
|
},
|
||||||
},
|
f"Discovery: gauge {i} status green",
|
||||||
f"Discovery: gauge {i} status green",
|
)
|
||||||
)
|
)
|
||||||
_discovery_pause(client)
|
|
||||||
|
|
||||||
|
|
||||||
def publish_discovery(client):
|
|
||||||
"""Publish all HA MQTT discovery payloads for gauges and LEDs."""
|
|
||||||
_dev_ref = _DEVICE
|
|
||||||
|
|
||||||
# Clear any previously registered switch entities (migration to light).
|
|
||||||
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)
|
|
||||||
|
|
||||||
_publish_gauge_discovery(client, _dev_ref)
|
|
||||||
_publish_speed_discovery(client, _dev_ref)
|
|
||||||
_publish_acceleration_discovery(client, _dev_ref)
|
|
||||||
_publish_indicator_led_discovery(client, _dev_ref)
|
|
||||||
_publish_backlight_status_discovery(client, _dev_ref)
|
|
||||||
|
|
||||||
|
|
||||||
def schedule_discovery():
|
def schedule_discovery():
|
||||||
global _discovery_phases, _discovery_phase_idx, _last_discovery_ms
|
global _discovery_queue, _discovery_idx, _last_discovery_ms
|
||||||
_dev_ref = _DEVICE
|
_dev_ref = _DEVICE
|
||||||
_discovery_phases = (
|
entries = []
|
||||||
lambda client: _clear_legacy_discovery(client),
|
_append_legacy_discovery(entries)
|
||||||
lambda client: _publish_gauge_discovery(client, _dev_ref),
|
_append_gauge_discovery(entries, _dev_ref)
|
||||||
lambda client: _publish_speed_discovery(client, _dev_ref),
|
_append_speed_discovery(entries, _dev_ref)
|
||||||
lambda client: _publish_acceleration_discovery(client, _dev_ref),
|
_append_acceleration_discovery(entries, _dev_ref)
|
||||||
lambda client: _publish_indicator_led_discovery(client, _dev_ref),
|
_append_indicator_led_discovery(entries, _dev_ref)
|
||||||
lambda client: _publish_backlight_status_discovery(client, _dev_ref),
|
_append_backlight_status_discovery(entries, _dev_ref)
|
||||||
)
|
_discovery_queue = entries
|
||||||
_discovery_phase_idx = 0
|
_discovery_idx = 0
|
||||||
_last_discovery_ms = 0
|
_last_discovery_ms = 0
|
||||||
|
|
||||||
|
|
||||||
def _clear_legacy_discovery(client):
|
def _append_legacy_discovery(entries):
|
||||||
for i in range(num_gauges):
|
for i in range(num_gauges):
|
||||||
for old_t in [
|
for old_t in [
|
||||||
f"homeassistant/switch/{MQTT_CLIENT_ID}_g{i}_red/config",
|
f"homeassistant/switch/{MQTT_CLIENT_ID}_g{i}_red/config",
|
||||||
@@ -1047,22 +1018,27 @@ def _clear_legacy_discovery(client):
|
|||||||
f"homeassistant/switch/{MQTT_CLIENT_ID}_g{i}_status_red/config",
|
f"homeassistant/switch/{MQTT_CLIENT_ID}_g{i}_status_red/config",
|
||||||
f"homeassistant/switch/{MQTT_CLIENT_ID}_g{i}_status_green/config",
|
f"homeassistant/switch/{MQTT_CLIENT_ID}_g{i}_status_green/config",
|
||||||
]:
|
]:
|
||||||
client.publish(old_t, b"", retain=True)
|
entries.append((old_t, b"", None))
|
||||||
_discovery_pause(client, count=2, delay_ms=15)
|
|
||||||
|
|
||||||
|
|
||||||
def service_discovery():
|
def service_discovery():
|
||||||
global _discovery_phase_idx, _last_discovery_ms
|
global _discovery_idx, _last_discovery_ms
|
||||||
if client_ref is None or _discovery_phase_idx >= len(_discovery_phases):
|
if client_ref is None or _discovery_idx >= len(_discovery_queue):
|
||||||
return
|
return
|
||||||
|
|
||||||
now = utime.ticks_ms()
|
now = utime.ticks_ms()
|
||||||
if _last_discovery_ms and utime.ticks_diff(now, _last_discovery_ms) < _DISCOVERY_INTERVAL_MS:
|
if _last_discovery_ms and utime.ticks_diff(now, _last_discovery_ms) < _DISCOVERY_INTERVAL_MS:
|
||||||
return
|
return
|
||||||
|
|
||||||
_discovery_phases[_discovery_phase_idx](client_ref)
|
topic, payload, log_msg = _discovery_queue[_discovery_idx]
|
||||||
_discovery_phase_idx += 1
|
if isinstance(payload, bytes):
|
||||||
|
client_ref.publish(topic, payload, retain=True)
|
||||||
|
else:
|
||||||
|
_publish_discovery_entity(client_ref, topic, payload, log_msg)
|
||||||
|
_discovery_idx += 1
|
||||||
_last_discovery_ms = utime.ticks_ms()
|
_last_discovery_ms = utime.ticks_ms()
|
||||||
|
if (_discovery_idx & 3) == 0:
|
||||||
|
gc.collect()
|
||||||
|
|
||||||
|
|
||||||
def publish_online(client):
|
def publish_online(client):
|
||||||
|
|||||||
Reference in New Issue
Block a user