From afb23c5c9c2ffbdbaccdc26c06a6855bbe553a5d Mon Sep 17 00:00:00 2001 From: saveriol Date: Thu, 26 Sep 2024 16:34:10 +0200 Subject: [PATCH 1/7] Add basic controls to HADiscovery --- HADiscovery.py | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/HADiscovery.py b/HADiscovery.py index e1ee8ab..80fe544 100644 --- a/HADiscovery.py +++ b/HADiscovery.py @@ -6,6 +6,8 @@ def decamelcase(str): split = re.findall(r"[A-Z](?:[a-z]+|[A-Z]*(?=[A-Z]|$))", str) + if len(split) == 0: + return str return f"{split[0]} {' '.join(split[1:]).lower()}".strip() @@ -59,10 +61,7 @@ def decamelcase(str): "component_type": "binary_sensor", "payload_values": {"device_class": "door", "payload_on": "Open", "payload_off": "Closed"}, }, - 539: { # BSH.Common.Setting.PowerState - "component_type": "binary_sensor", - "payload_values": {"device_class": "power"}, - }, + 539: {"component_type": "switch"}, # BSH.Common.Setting.PowerState 542: {"payload_values": {"unit_of_measurement": "%"}}, # BSH.Common.Option.ProgramProgress 543: { # BSH.Common.Event.LowWaterPressure "component_type": "binary_sensor", @@ -179,12 +178,13 @@ def publish_ha_discovery(device, client, mqtt_topic): "sw_version": ".".join(version_parts), } - for feature in device["features"].values(): + for uid, feature in device["features"].items(): name_parts = feature["name"].split(".") name = name_parts[-1] - feature_type = name_parts[-2] + feature_type = name_parts[2] access = feature.get("access", "none") available = feature.get("available", False) + uid = int(uid) if ( ( @@ -199,9 +199,15 @@ def publish_ha_discovery(device, client, mqtt_topic): ) or feature_type == "Event" or feature_type == "Option" + or feature_type == "Program" ): - default_component_type = "binary_sensor" if feature_type == "Event" else "sensor" + if feature_type == "Event": + default_component_type = "binary_sensor" + elif feature_type == "Program": + default_component_type = "button" + else: + default_component_type = "sensor" overrides = feature.get("discovery", {}) @@ -234,6 +240,19 @@ def publish_ha_discovery(device, client, mqtt_topic): discovery_payload.setdefault("payload_on", "On") discovery_payload.setdefault("payload_off", "Off") + if component_type == "switch": + discovery_payload.setdefault("payload_on", "On") + discovery_payload.setdefault("payload_off", "Off") + discovery_payload.setdefault("command_topic", f"{mqtt_topic}/set") + discovery_payload.setdefault( + "command_template", + f'{{ "uid":{uid}, "value": {{{{ iif(value=="On", 2,1) }}}} }}', + ) + + if component_type == "button": + discovery_payload.setdefault("command_topic", f"{mqtt_topic}/activeProgram") + discovery_payload.setdefault("command_template", f'{{ "program":{uid} }}') + # print(discovery_topic) # print(discovery_payload) From 4b2c2d3ebe39cd427b7f4081c8cd115886fb6be7 Mon Sep 17 00:00:00 2001 From: saveriol Date: Thu, 26 Sep 2024 16:59:36 +0200 Subject: [PATCH 2/7] Update README.md to reflect HADiscovery basic control --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index bdbe381..86fd7f4 100644 --- a/README.md +++ b/README.md @@ -468,8 +468,7 @@ for each recognised property of your devices. ### Limitations -Discovery messages currently only contain a `state` topic, not a `command` topic, -so autodiscovered devices will be read-only. You can still use the method +Command topic has been implemented only for Power control and Programs. If you need to control anything else you can still use the method described in `Posting to the appliance` above. ### Customising the discovery messages From b7ad8621f0ffc30b340b51db3d96a67a7d96086c Mon Sep 17 00:00:00 2001 From: saveriol Date: Thu, 10 Oct 2024 15:07:39 +0200 Subject: [PATCH 3/7] Add availability to buttons --- HADiscovery.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/HADiscovery.py b/HADiscovery.py index 80fe544..31b5b7e 100644 --- a/HADiscovery.py +++ b/HADiscovery.py @@ -252,6 +252,8 @@ def publish_ha_discovery(device, client, mqtt_topic): if component_type == "button": discovery_payload.setdefault("command_topic", f"{mqtt_topic}/activeProgram") discovery_payload.setdefault("command_template", f'{{ "program":{uid} }}') + discovery_payload.setdefault("availability_topic ", f"{mqtt_topic}/state") + discovery_payload.setdefault("availability_template", "{{ 'online' if value_json.OperationState == 'Ready' else 'offline' }}") # print(discovery_topic) # print(discovery_payload) From 86a2517068133c7ed12f709fdb84b4cc7245308f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 10 Oct 2024 13:09:23 +0000 Subject: [PATCH 4/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- HADiscovery.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/HADiscovery.py b/HADiscovery.py index 31b5b7e..a39807c 100644 --- a/HADiscovery.py +++ b/HADiscovery.py @@ -253,7 +253,10 @@ def publish_ha_discovery(device, client, mqtt_topic): discovery_payload.setdefault("command_topic", f"{mqtt_topic}/activeProgram") discovery_payload.setdefault("command_template", f'{{ "program":{uid} }}') discovery_payload.setdefault("availability_topic ", f"{mqtt_topic}/state") - discovery_payload.setdefault("availability_template", "{{ 'online' if value_json.OperationState == 'Ready' else 'offline' }}") + discovery_payload.setdefault( + "availability_template", + "{{ 'online' if value_json.OperationState == 'Ready' else 'offline' }}", + ) # print(discovery_topic) # print(discovery_payload) From 3dfbd7e6b305981631b49fddfb49b27a14e7a391 Mon Sep 17 00:00:00 2001 From: saveriol Date: Thu, 10 Oct 2024 15:10:53 +0200 Subject: [PATCH 5/7] Fix typo - remove space --- HADiscovery.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HADiscovery.py b/HADiscovery.py index a39807c..50b26d7 100644 --- a/HADiscovery.py +++ b/HADiscovery.py @@ -252,7 +252,7 @@ def publish_ha_discovery(device, client, mqtt_topic): if component_type == "button": discovery_payload.setdefault("command_topic", f"{mqtt_topic}/activeProgram") discovery_payload.setdefault("command_template", f'{{ "program":{uid} }}') - discovery_payload.setdefault("availability_topic ", f"{mqtt_topic}/state") + discovery_payload.setdefault("availability_topic", f"{mqtt_topic}/state") discovery_payload.setdefault( "availability_template", "{{ 'online' if value_json.OperationState == 'Ready' else 'offline' }}", From 35625de2624780f25b53700bc8c734444af9c891 Mon Sep 17 00:00:00 2001 From: saveriol Date: Mon, 14 Oct 2024 18:26:08 +0200 Subject: [PATCH 6/7] Check for "Ready" in BSH.Common.Status.OperationState If it's a Program, check if BSH.Common.Status.OperationState has "Ready" as available option. If not, programs are read only. --- HADiscovery.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/HADiscovery.py b/HADiscovery.py index 50b26d7..e7655c1 100644 --- a/HADiscovery.py +++ b/HADiscovery.py @@ -199,7 +199,8 @@ def publish_ha_discovery(device, client, mqtt_topic): ) or feature_type == "Event" or feature_type == "Option" - or feature_type == "Program" + # If it's a Program, check if BSH.Common.Status.OperationState has "Ready" as available option. If not, programs are read only. + or ( feature_type == "Program" and "1" in device["features"]["552"]["values"]) ): if feature_type == "Event": From 2b35c4cdc0f84d5d085ffe9730bed032682a525d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 14 Oct 2024 16:26:14 +0000 Subject: [PATCH 7/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- HADiscovery.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HADiscovery.py b/HADiscovery.py index e7655c1..ae674e2 100644 --- a/HADiscovery.py +++ b/HADiscovery.py @@ -200,7 +200,7 @@ def publish_ha_discovery(device, client, mqtt_topic): or feature_type == "Event" or feature_type == "Option" # If it's a Program, check if BSH.Common.Status.OperationState has "Ready" as available option. If not, programs are read only. - or ( feature_type == "Program" and "1" in device["features"]["552"]["values"]) + or (feature_type == "Program" and "1" in device["features"]["552"]["values"]) ): if feature_type == "Event":