MQTT doesn't autodiscover properly
This commit is contained in:
89
gauge.py
89
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
|
||||
@@ -830,9 +835,9 @@ def publish_discovery(client):
|
||||
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}",
|
||||
@@ -845,16 +850,14 @@ def publish_discovery(client):
|
||||
"unit_of_meas": g["unit"],
|
||||
"icon": "mdi:gauge",
|
||||
"dev": _dev_ref,
|
||||
}
|
||||
),
|
||||
retain=True,
|
||||
},
|
||||
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",
|
||||
@@ -870,16 +873,13 @@ def publish_discovery(client):
|
||||
"enabled_by_default": False,
|
||||
"entity_category": "config",
|
||||
"dev": _dev_ref,
|
||||
}
|
||||
),
|
||||
retain=True,
|
||||
},
|
||||
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",
|
||||
@@ -895,16 +895,15 @@ def publish_discovery(client):
|
||||
"enabled_by_default": False,
|
||||
"entity_category": "config",
|
||||
"dev": _dev_ref,
|
||||
}
|
||||
),
|
||||
retain=True,
|
||||
},
|
||||
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,15 +934,14 @@ 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",
|
||||
@@ -957,16 +954,14 @@ def publish_discovery(client):
|
||||
"icon": "mdi:led-strip",
|
||||
"dev": _dev_ref,
|
||||
"ret": True,
|
||||
}
|
||||
),
|
||||
retain=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