MQTT doesn't autodiscover properly
This commit is contained in:
199
gauge.py
199
gauge.py
@@ -812,6 +812,11 @@ def _discovery_pause(client, count=5, delay_ms=25):
|
||||
gc.collect()
|
||||
|
||||
|
||||
def _publish_discovery_entity(client, topic, payload, log_msg):
|
||||
client.publish(topic, ujson.dumps(payload), retain=True)
|
||||
info(log_msg)
|
||||
|
||||
|
||||
def publish_discovery(client):
|
||||
"""Publish all HA MQTT discovery payloads for gauges and LEDs."""
|
||||
_dev_ref = _DEVICE
|
||||
@@ -825,86 +830,80 @@ def publish_discovery(client):
|
||||
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)
|
||||
_discovery_pause(client, count=2, delay_ms=15)
|
||||
|
||||
for i, g in enumerate(gauges):
|
||||
gt = gauge_topics[i]
|
||||
|
||||
client.publish(
|
||||
_publish_discovery_entity(
|
||||
client,
|
||||
gt["disc"],
|
||||
ujson.dumps(
|
||||
{
|
||||
"name": g["entity_name"],
|
||||
"unique_id": f"{MQTT_CLIENT_ID}_g{i}",
|
||||
"cmd_t": gt["set"],
|
||||
"stat_t": gt["state"],
|
||||
"avty_t": gt["status"],
|
||||
"min": g["min"],
|
||||
"max": g["max"],
|
||||
"step": 1,
|
||||
"unit_of_meas": g["unit"],
|
||||
"icon": "mdi:gauge",
|
||||
"dev": _dev_ref,
|
||||
}
|
||||
),
|
||||
retain=True,
|
||||
{
|
||||
"name": g["entity_name"],
|
||||
"unique_id": f"{MQTT_CLIENT_ID}_g{i}",
|
||||
"cmd_t": gt["set"],
|
||||
"stat_t": gt["state"],
|
||||
"avty_t": gt["status"],
|
||||
"min": g["min"],
|
||||
"max": g["max"],
|
||||
"step": 1,
|
||||
"unit_of_meas": g["unit"],
|
||||
"icon": "mdi:gauge",
|
||||
"dev": _dev_ref,
|
||||
},
|
||||
f"Discovery: gauge {i} ({g['name']})",
|
||||
)
|
||||
info(f"Discovery: gauge {i} ({g['name']})")
|
||||
_discovery_pause(client)
|
||||
|
||||
client.publish(
|
||||
_publish_discovery_entity(
|
||||
client,
|
||||
gt["speed_disc"],
|
||||
ujson.dumps(
|
||||
{
|
||||
"name": f"{g['name']} Speed",
|
||||
"unique_id": f"{MQTT_CLIENT_ID}_g{i}_speed",
|
||||
"cmd_t": gt["speed"],
|
||||
"stat_t": gt["speed_state"],
|
||||
"avty_t": gt["status"],
|
||||
"min": 1,
|
||||
"max": 50000,
|
||||
"step": 1,
|
||||
"mode": "box",
|
||||
"unit_of_meas": "steps/s",
|
||||
"icon": "mdi:speedometer",
|
||||
"enabled_by_default": False,
|
||||
"entity_category": "config",
|
||||
"dev": _dev_ref,
|
||||
}
|
||||
),
|
||||
retain=True,
|
||||
{
|
||||
"name": f"{g['name']} Speed",
|
||||
"unique_id": f"{MQTT_CLIENT_ID}_g{i}_speed",
|
||||
"cmd_t": gt["speed"],
|
||||
"stat_t": gt["speed_state"],
|
||||
"avty_t": gt["status"],
|
||||
"min": 1,
|
||||
"max": 50000,
|
||||
"step": 1,
|
||||
"mode": "box",
|
||||
"unit_of_meas": "steps/s",
|
||||
"icon": "mdi:speedometer",
|
||||
"enabled_by_default": False,
|
||||
"entity_category": "config",
|
||||
"dev": _dev_ref,
|
||||
},
|
||||
f"Discovery: gauge {i} speed",
|
||||
)
|
||||
info(f"Discovery: gauge {i} speed")
|
||||
_discovery_pause(client)
|
||||
|
||||
client.publish(
|
||||
_publish_discovery_entity(
|
||||
client,
|
||||
gt["acceleration_disc"],
|
||||
ujson.dumps(
|
||||
{
|
||||
"name": f"{g['name']} Acceleration",
|
||||
"unique_id": f"{MQTT_CLIENT_ID}_g{i}_acceleration",
|
||||
"cmd_t": gt["acceleration"],
|
||||
"stat_t": gt["acceleration_state"],
|
||||
"avty_t": gt["status"],
|
||||
"min": 1,
|
||||
"max": 100000,
|
||||
"step": 1,
|
||||
"mode": "box",
|
||||
"unit_of_meas": "steps/s2",
|
||||
"icon": "mdi:chart-bell-curve-cumulative",
|
||||
"enabled_by_default": False,
|
||||
"entity_category": "config",
|
||||
"dev": _dev_ref,
|
||||
}
|
||||
),
|
||||
retain=True,
|
||||
{
|
||||
"name": f"{g['name']} Acceleration",
|
||||
"unique_id": f"{MQTT_CLIENT_ID}_g{i}_acceleration",
|
||||
"cmd_t": gt["acceleration"],
|
||||
"stat_t": gt["acceleration_state"],
|
||||
"avty_t": gt["status"],
|
||||
"min": 1,
|
||||
"max": 100000,
|
||||
"step": 1,
|
||||
"mode": "box",
|
||||
"unit_of_meas": "steps/s2",
|
||||
"icon": "mdi:chart-bell-curve-cumulative",
|
||||
"enabled_by_default": False,
|
||||
"entity_category": "config",
|
||||
"dev": _dev_ref,
|
||||
},
|
||||
f"Discovery: gauge {i} acceleration",
|
||||
)
|
||||
info(f"Discovery: gauge {i} acceleration")
|
||||
_discovery_pause(client)
|
||||
|
||||
client.publish(
|
||||
_publish_discovery_entity(
|
||||
client,
|
||||
gt["led_red_disc"],
|
||||
ujson.dumps({
|
||||
{
|
||||
"name": f"{g['name']} Red LED",
|
||||
"uniq_id": f"{MQTT_CLIENT_ID}_g{i}_red",
|
||||
"cmd_t": gt["led_red"],
|
||||
@@ -916,15 +915,14 @@ def publish_discovery(client):
|
||||
"icon": "mdi:led-on",
|
||||
"dev": _dev_ref,
|
||||
"ret": True,
|
||||
}),
|
||||
retain=True,
|
||||
},
|
||||
f"Discovery: gauge {i} red LED",
|
||||
)
|
||||
info(f"Discovery: gauge {i} red LED")
|
||||
_discovery_pause(client)
|
||||
|
||||
client.publish(
|
||||
_publish_discovery_entity(
|
||||
client,
|
||||
gt["led_green_disc"],
|
||||
ujson.dumps({
|
||||
{
|
||||
"name": f"{g['name']} Green LED",
|
||||
"uniq_id": f"{MQTT_CLIENT_ID}_g{i}_green",
|
||||
"cmd_t": gt["led_green"],
|
||||
@@ -936,37 +934,34 @@ def publish_discovery(client):
|
||||
"icon": "mdi:led-on",
|
||||
"dev": _dev_ref,
|
||||
"ret": True,
|
||||
}),
|
||||
retain=True,
|
||||
},
|
||||
f"Discovery: gauge {i} green LED",
|
||||
)
|
||||
info(f"Discovery: gauge {i} green LED")
|
||||
_discovery_pause(client)
|
||||
|
||||
client.publish(
|
||||
_publish_discovery_entity(
|
||||
client,
|
||||
gt["led_bl_disc"],
|
||||
ujson.dumps(
|
||||
{
|
||||
"name": f"{g['name']} Backlight",
|
||||
"uniq_id": f"{MQTT_CLIENT_ID}_g{i}_bl",
|
||||
"cmd_t": gt["led_bl"],
|
||||
"stat_t": gt["led_bl_state"],
|
||||
"schema": "json",
|
||||
"supported_color_modes": ["rgb"],
|
||||
"effect": True,
|
||||
"effect_list": _EFFECT_LIST,
|
||||
"icon": "mdi:led-strip",
|
||||
"dev": _dev_ref,
|
||||
"ret": True,
|
||||
}
|
||||
),
|
||||
retain=True,
|
||||
{
|
||||
"name": f"{g['name']} Backlight",
|
||||
"uniq_id": f"{MQTT_CLIENT_ID}_g{i}_bl",
|
||||
"cmd_t": gt["led_bl"],
|
||||
"stat_t": gt["led_bl_state"],
|
||||
"schema": "json",
|
||||
"supported_color_modes": ["rgb"],
|
||||
"effect": True,
|
||||
"effect_list": _EFFECT_LIST,
|
||||
"icon": "mdi:led-strip",
|
||||
"dev": _dev_ref,
|
||||
"ret": True,
|
||||
},
|
||||
f"Discovery: gauge {i} backlight",
|
||||
)
|
||||
info(f"Discovery: gauge {i} backlight")
|
||||
_discovery_pause(client)
|
||||
|
||||
client.publish(
|
||||
_publish_discovery_entity(
|
||||
client,
|
||||
gt["status_red_disc"],
|
||||
ujson.dumps({
|
||||
{
|
||||
"name": f"{g['name']} Status Red",
|
||||
"uniq_id": f"{MQTT_CLIENT_ID}_g{i}_status_red",
|
||||
"cmd_t": gt["status_red"],
|
||||
@@ -978,15 +973,14 @@ def publish_discovery(client):
|
||||
"icon": "mdi:led-on",
|
||||
"dev": _dev_ref,
|
||||
"ret": True,
|
||||
}),
|
||||
retain=True,
|
||||
},
|
||||
f"Discovery: gauge {i} status red",
|
||||
)
|
||||
info(f"Discovery: gauge {i} status red")
|
||||
_discovery_pause(client)
|
||||
|
||||
client.publish(
|
||||
_publish_discovery_entity(
|
||||
client,
|
||||
gt["status_green_disc"],
|
||||
ujson.dumps({
|
||||
{
|
||||
"name": f"{g['name']} Status Green",
|
||||
"uniq_id": f"{MQTT_CLIENT_ID}_g{i}_status_green",
|
||||
"cmd_t": gt["status_green"],
|
||||
@@ -998,10 +992,9 @@ def publish_discovery(client):
|
||||
"icon": "mdi:led-on",
|
||||
"dev": _dev_ref,
|
||||
"ret": True,
|
||||
}),
|
||||
retain=True,
|
||||
},
|
||||
f"Discovery: gauge {i} status green",
|
||||
)
|
||||
info(f"Discovery: gauge {i} status green")
|
||||
_discovery_pause(client)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user