Merge branch 'main' into mqtt_lwt

This commit is contained in:
Meatballs1
2024-03-20 13:32:00 +00:00
5 changed files with 114 additions and 48 deletions

48
hc2mqtt
View File

@@ -111,21 +111,40 @@ def client_connect(client, device, mqtt_topic):
print(msg.topic)
mqtt_state = msg.payload.decode()
mqtt_topic = msg.topic.split("/")
print(now(), f"received mqtt message {mqtt_state}")
print(now(), f"{msg.topic} received mqtt message {mqtt_state}")
try:
msg = json.loads(mqtt_state)
if "uid" in msg:
dev[mqtt_topic[-2]].get("/ro/values", 1, "POST", msg)
if len(mqtt_topic) >= 2:
device_name = mqtt_topic[-2]
topic = mqtt_topic[-1]
else:
raise Exception(f"Payload {msg} is not correctly formatted")
raise Exception(f"Invalid mqtt topic {msg.topic}.")
try:
msg = json.loads(mqtt_state)
except ValueError as e:
raise ValueError(f"Invalid JSON in message: {mqtt_state}.") from e
if topic == "set":
resource = "/ro/values"
elif topic == "activeProgram":
resource = "/ro/activeProgram"
else:
raise Exception(f"Payload topic {topic} is unknown.")
dev[device_name].get(resource, 1, "POST", msg)
except Exception as e:
print("ERROR", e, file=sys.stderr)
print(now(), device_name, "ERROR", e, file=sys.stderr)
host = device["host"]
device_topics = topics
client.on_message = on_message
active_program = False
for value in device["features"]:
# If the device has the ActiveProgram feature it allows programs to be started and
# scheduled via /ro/activeProgram
if "BSH.Common.Root.ActiveProgram" == device["features"][value]["name"]:
active_program = True
if (
"access" in device["features"][value]
and "read" in device["features"][value]["access"].lower()
@@ -141,6 +160,16 @@ def client_connect(client, device, mqtt_topic):
if not topic.isdigit(): # We only want the named topics
state[device_topics[topic]] = None
mqtt_set_topic = mqtt_topic + "/set"
print(now(), device["name"], f"set topic: {mqtt_set_topic}")
client.subscribe(mqtt_set_topic)
if active_program:
mqtt_active_program_topic = mqtt_topic + "/activeProgram"
print(now(), device["name"], f"program topic: {mqtt_active_program_topic}")
client.subscribe(mqtt_active_program_topic)
client.on_message = on_message
while True:
try:
@@ -164,11 +193,6 @@ def client_connect(client, device, mqtt_topic):
if value is None:
continue
# new_topic = topics[topic]
# if new_topic == "remaining":
# state["remainingseconds"] = value
# value = "%d:%02d" % (value / 60 / 60, (value / 60) % 60)
new_topic = device_topics[topic]
state[new_topic] = value
update = True