forked from hcpy2-0/hcpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HADiscovery.py
240 lines (215 loc) · 9.39 KB
/
HADiscovery.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import json
import re
from HCSocket import now
def decamelcase(str):
split = re.findall(r"[A-Z](?:[a-z]+|[A-Z]*(?=[A-Z]|$))", str)
return f"{split[0]} {' '.join(split[1:]).lower()}".strip()
HA_DISCOVERY_PREFIX = "homeassistant"
# These "magic overrides" provide HA MQTT autodiscovery data that is injected
# into devices.json when `hc-login.py` is run; the data is then read from
# devices.json at runtime.
#
# Note: keys should be integer (not string) taken from a device's feature
# mapping.
MAGIC_OVERRIDES = {
3: { # BSH.Common.Setting.AllowBackendConnection
"component_type": "binary_sensor",
"payload_values": {"payload_on": True, "payload_off": False},
},
5: { # BSH.Common.Status.BackendConnected
"component_type": "binary_sensor",
"payload_values": {
"device_class": "connectivity",
"payload_on": True,
"payload_off": False,
},
},
21: { # BSH.Common.Event.SoftwareUpdateAvailable
"component_type": "binary_sensor",
"payload_values": {"device_class": "update", "payload_on": "Present"},
},
517: { # BSH.Common.Status.RemoteControlStartAllowed
"component_type": "binary_sensor",
"payload_values": {"payload_on": True, "payload_off": False},
},
523: { # BSH.Common.Status.RemoteControlActive
"component_type": "binary_sensor",
"payload_values": {"payload_on": True, "payload_off": False},
},
524: { # BSH.Common.Setting.ChildLock
"component_type": "binary_sensor",
"payload_values": {
"device_class": "lock",
"payload_on": False, # Lock "on" means "unlocked" to HA
"payload_off": True, # Lock "off" means "locked"
},
},
525: { # BSH.Common.Event.AquaStopOccured
"component_type": "binary_sensor",
"payload_values": {"device_class": "problem", "payload_on": "Present"},
},
527: { # BSH.Common.Status.DoorState
"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"},
},
542: {"payload_values": {"unit_of_measurement": "%"}}, # BSH.Common.Option.ProgramProgress
543: { # BSH.Common.Event.LowWaterPressure
"component_type": "binary_sensor",
"payload_values": {"device_class": "problem", "payload_on": "Present"},
},
544: { # BSH.Common.Option.RemainingProgramTime
"payload_values": {"unit_of_measurement": "s", "device_class": "duration"}
},
549: { # BSH.Common.Option.RemainingProgramTimeIsEstimated
"component_type": "binary_sensor",
"payload_values": {"payload_on": True, "payload_off": False},
},
558: { # BSH.Common.Option.StartInRelative
"payload_values": {"unit_of_measurement": "s", "device_class": "duration"}
},
4101: { # Dishcare.Dishwasher.Status.SilenceOnDemandRemainingTime
"payload_values": {"unit_of_measurement": "s", "device_class": "duration"}
},
4103: { # Dishcare.Dishwasher.Status.EcoDryActive
"component_type": "binary_sensor",
"payload_values": {"payload_on": True, "payload_off": False},
},
4382: { # Dishcare.Dishwasher.Status.SilenceOnDemandDefaultTime
"payload_values": {"unit_of_measurement": "s", "device_class": "duration"}
},
4384: { # Dishcare.Dishwasher.Setting.SpeedOnDemand
"component_type": "binary_sensor",
"payload_values": {"payload_on": True, "payload_off": False},
},
4608: { # Dishcare.Dishwasher.Event.InternalError
"component_type": "binary_sensor",
"payload_values": {"device_class": "problem", "payload_on": "Present"},
},
4609: { # Dishcare.Dishwasher.Event.CheckFilterSystem
"component_type": "binary_sensor",
"payload_values": {"device_class": "problem", "payload_on": "Present"},
},
4610: { # Dishcare.Dishwasher.Event.DrainingNotPossible
"component_type": "binary_sensor",
"payload_values": {"device_class": "problem", "payload_on": "Present"},
},
4611: { # Dishcare.Dishwasher.Event.DrainPumpBlocked
"component_type": "binary_sensor",
"payload_values": {"device_class": "problem", "payload_on": "Present"},
},
4612: { # Dishcare.Dishwasher.Event.WaterheaterCalcified
"component_type": "binary_sensor",
"payload_values": {"device_class": "problem", "payload_on": "Present"},
},
4613: { # Dishcare.Dishwasher.Event.LowVoltage
"component_type": "binary_sensor",
"payload_values": {"device_class": "problem", "payload_on": "Present"},
},
4624: { # Dishcare.Dishwasher.Event.SaltLack
"component_type": "binary_sensor",
"payload_values": {"device_class": "problem", "payload_on": "Present"},
},
4625: { # Dishcare.Dishwasher.Event.RinseAidLack
"component_type": "binary_sensor",
"payload_values": {"device_class": "problem", "payload_on": "Present"},
},
4626: { # Dishcare.Dishwasher.Event.SaltNearlyEmpty
"component_type": "binary_sensor",
"payload_values": {"device_class": "problem", "payload_on": "Present"},
},
4627: { # Dishcare.Dishwasher.Event.RinseAidNearlyEmpty
"component_type": "binary_sensor",
"payload_values": {"device_class": "problem", "payload_on": "Present"},
},
5121: { # Dishcare.Dishwasher.Option.ExtraDry
"component_type": "binary_sensor",
"payload_values": {"payload_on": True, "payload_off": False},
},
5124: { # Dishcare.Dishwasher.Option.HalfLoad
"component_type": "binary_sensor",
"payload_values": {"payload_on": True, "payload_off": False},
},
5127: { # Dishcare.Dishwasher.Option.VarioSpeedPlus
"component_type": "binary_sensor",
"payload_values": {"payload_on": True, "payload_off": False},
},
5136: { # Dishcare.Dishwasher.Option.SilenceOnDemand
"component_type": "binary_sensor",
"payload_values": {"payload_on": True, "payload_off": False},
},
}
def augment_device_features(features):
for id, feature in features.items():
if id in MAGIC_OVERRIDES:
feature["discovery"] = MAGIC_OVERRIDES[id]
# else:
# print(f"No discovery information for: {id} => {feature['name']}", file=sys.stderr)
return features
def publish_ha_discovery(device, client, mqtt_topic):
print(f"{now()} Publishing HA discovery for {device}")
device_ident = device["name"]
device_description = device.get("description", {})
version_parts = filter(
lambda d: d is not None,
[device_description.get("version"), device_description.get("revision")],
)
device_info = {
"identifiers": [device_ident],
"name": device_ident,
"manufacturer": device_description.get("brand"),
"model": device_description.get("model"),
"sw_version": ".".join(version_parts),
}
for feature in device["features"].values():
name_parts = feature["name"].split(".")
name = name_parts[-1]
feature_type = name_parts[-2]
access = feature.get("access", "none")
available = feature.get("available", False)
if (
(
feature_type == "Setting"
and available
and (access == "read" or access == "readWrite")
)
or (
feature_type == "Status"
and available
and (access == "read" or access == "readWrite")
)
or feature_type == "Event"
or feature_type == "Option"
):
default_component_type = "binary_sensor" if feature_type == "Event" else "sensor"
overrides = feature.get("discovery", {})
component_type = overrides.get("component_type", default_component_type)
extra_payload_values = overrides.get("payload_values", {})
discovery_topic = (
f"{HA_DISCOVERY_PREFIX}/{component_type}/hcpy/{device_ident}_{name}/config"
)
# print(discovery_topic, state_topic)
discovery_payload = {
"name": decamelcase(name),
"device": device_info,
"state_topic": f"{mqtt_topic}/state",
# "availability_topic": f"{mqtt_topic}/LWT",
# # I found the behaviour of `availability_topic` unsatisfactory -
# # since it would set all device attributes to "unavailable"
# # then back to their correct values on every disconnect/
# # reconnect. This leaves a lot of noise in the HA history, so
# # I felt things were better off without an `availability_topic`.
"value_template": "{{value_json." + name + " | default('unavailable')}}",
"object_id": f"{device_ident}_{name}",
"unique_id": f"{device_ident}_{name}",
**extra_payload_values,
}
if component_type == "binary_sensor":
discovery_payload.setdefault("payload_on", "On")
discovery_payload.setdefault("payload_off", "Off")
# print(discovery_topic)
# print(discovery_payload)
client.publish(discovery_topic, json.dumps(discovery_payload), retain=True)