Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic controls to HADiscovery #97

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
39 changes: 32 additions & 7 deletions HADiscovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #103 PowerState should be an enum with On/Off/Standby

542: {"payload_values": {"unit_of_measurement": "%"}}, # BSH.Common.Option.ProgramProgress
543: { # BSH.Common.Event.LowWaterPressure
"component_type": "binary_sensor",
Expand Down Expand Up @@ -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 (
(
Expand All @@ -199,9 +199,16 @@ 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"])
):

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", {})

Expand Down Expand Up @@ -234,6 +241,24 @@ 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} }}')
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)

Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down