From c111df49cb3c5d0a10032945dc5a387550a50ecb Mon Sep 17 00:00:00 2001 From: James Muscat Date: Sat, 20 Jul 2024 23:21:42 +0100 Subject: [PATCH] Publish state changes. At the moment this publishes all entity states every time one thing changes, which is not optimal. --- HADiscovery.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/HADiscovery.py b/HADiscovery.py index 925cbcb..9ca0de4 100644 --- a/HADiscovery.py +++ b/HADiscovery.py @@ -13,7 +13,10 @@ HA_DISCOVERY_PREFIX = "homeassistant" def publish_ha_states(state, client, mqtt_topic): - pass + for key, value in state.items(): + state_topic = f"{mqtt_topic}/{key}" + print(f"{now()} Publishing state for {key} at {state_topic}") + client.publish(state_topic, json.dumps(value)) def publish_ha_discovery(device, client, mqtt_topic): @@ -45,12 +48,12 @@ def publish_ha_discovery(device, client, mqtt_topic): if available and (access == "read" or access == "readWrite"): name_parts = feature["name"].split(".") name = name_parts[-1] - feature_type = name_parts[-2] + # feature_type = name_parts[-2] component_type = "sensor" # TODO use appropriate types discovery_topic = f"{HA_DISCOVERY_PREFIX}/{component_type}/hcpy/{device_ident}_{name}/config" - state_topic = f"{mqtt_topic}/{feature_type}/{name}" + state_topic = f"{mqtt_topic}/{name}" # print(discovery_topic, state_topic) discovery_payload = json.dumps({ @@ -63,4 +66,4 @@ def publish_ha_discovery(device, client, mqtt_topic): print(discovery_topic) # print(discovery_payload) - client.publish(discovery_topic, discovery_payload, retain=False) # TODO make retain=True when stable + client.publish(discovery_topic, discovery_payload, retain=True)