-
Notifications
You must be signed in to change notification settings - Fork 1
/
hm-mqtt-bridge.py
executable file
·525 lines (441 loc) · 19.5 KB
/
hm-mqtt-bridge.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#!/usr/bin/env python3
#
# Copyright 2022 Andreas Oberritter
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
import argparse
import asyncio
import json
import logging
import ssl
import sys
from functools import partial
from typing import Optional, Union
from urllib.parse import urlparse
from asyncio_mqtt import Client, MqttError, Will
from pyhomematic import HMConnection
from pyhomematic.devicetypes.actors import GenericBlind, GenericSwitch
logger = logging.getLogger(__name__)
logger.addHandler(logging.StreamHandler())
HM_COVER_CHANNEL_MAP = {4: 3}
MQTT_PREFIX = "Homematic"
HM_INTERFACE_ID = "mqttbridge"
HM_REMOTE = "default"
BINARY_SENSOR_TYPES = {
"MAINTENANCE",
}
COVER_TYPES = {"SHUTTER_VIRTUAL_RECEIVER"}
DEVICE_TRIGGER_TYPES = {"KEY_TRANSCEIVER"}
SENSOR_TYPES = {
"ALARM_COND_SWITCH_TRANSMITTER",
"BLIND_WEEK_PROFILE",
"COND_SWITCH_TRANSMITTER",
"ENERGIE_METER_TRANSMITTER",
"ROTARY_HANDLE_TRANSCEIVER",
"SHUTTER_TRANSMITTER",
"SMOKE_DETECTOR",
"SWITCH_TRANSMITTER",
"SWITCH_WEEK_PROFILE",
}
SWITCH_TYPES = {
"SWITCH_VIRTUAL_RECEIVER",
}
SENSOR_UNITS = {
"SHUTTER_TRANSMITTER": "%",
}
MAINTENANCE_FLAGS = {
flag.lower()
for flag in (
"ACTUAL_TEMPERATURE_STATUS",
"CONFIG_PENDING",
"DUTY_CYCLE",
"ERROR_CODE",
"ERROR_OVERHEAT",
"LOW_BAT",
"OPERATING_VOLTAGE_STATUS",
"SABOTAGE",
"TIME_OF_OPERATION_STATUS",
"UNREACH",
)
}
ROTARY_HANDLE_VALUES = {
0: "closed",
1: "tilted",
2: "open",
}
SMOKE_DETECTOR_VALUES = {
0: "off",
1: "primary",
2: "intrusion",
3: "secondary",
}
class HomematicMqttBridge:
def __init__(self):
self._loop = asyncio.get_running_loop()
self._ha_devices = {}
self._ha_attributes = {}
def _subscribe(self, mqtt, topic: str) -> None:
asyncio.run_coroutine_threadsafe(mqtt.subscribe(topic), self._loop)
def _publish(self, mqtt, topic: str, message: Union[bool, bytes, dict, float, int, str], retain: Optional[bool] = True) -> None:
if isinstance(message, dict):
message = json.dumps(message)
elif isinstance(message, bool):
message = [b"OFF", b"ON"][message]
elif isinstance(message, (float, int)):
message = str(message)
if isinstance(message, str):
message = message.encode("utf-8")
assert isinstance(message, bytes)
asyncio.run_coroutine_threadsafe(
mqtt.publish(topic, message, qos=2, retain=retain), self._loop
)
def _publish_discovery(self, mqtt, component: str, node_id: str, object_id: str, config: dict, retain: Optional[bool] = True) -> None:
discovery_prefix = "homeassistant"
topic = f"{discovery_prefix}/{component}/{node_id}/{object_id}/config"
self._publish(mqtt, topic, config, retain=retain)
def _publish_availability(self, mqtt, address: str, unreach: bool):
if unreach:
value = "offline"
else:
value = "online"
base_topic = f"{MQTT_PREFIX}/{address}"
self._publish(mqtt, "%s/availability" % base_topic, value)
def _check_interface_id(self, interface_id: str) -> bool:
return interface_id == HM_INTERFACE_ID + "-" + HM_REMOTE
def _event_callback(self, mqtt, interface_id, address, value_key, value):
logger.debug(f"event_callback({interface_id}, {address}, {value_key}, {value})")
if not self._check_interface_id(interface_id):
logger.error("Invalid interface ID: %s", interface_id)
return
attrs = self._ha_attributes.get(address)
if not attrs:
logger.error("Invalid address: %s", address)
return
parent, index = address.split(":", 1)
base_topic = f"{MQTT_PREFIX}/{parent}/{index}"
key = value_key
lc_key = key.lower()
old_value = attrs.get(lc_key)
if old_value != value:
attrs[lc_key] = value
self._publish(mqtt, "%s/attributes" % base_topic, attrs)
chan_type = attrs["type"]
if chan_type == "MAINTENANCE":
if key == "UNREACH":
self._publish_availability(mqtt, parent, value)
if lc_key in MAINTENANCE_FLAGS:
state = sum(attrs[flag] for flag in MAINTENANCE_FLAGS if flag in attrs)
self._publish(mqtt, "%s/state" % base_topic, bool(state))
# HmIP-BROLL(1,2)
elif chan_type == "KEY_TRANSCEIVER" and key in ("PRESS_SHORT", "PRESS_LONG"):
assert isinstance(value, bool)
self._publish(mqtt, f"{base_topic}/{key}", value, retain=False)
# HmIP-BROLL(3)
elif chan_type == "SHUTTER_TRANSMITTER" and key == "LEVEL":
self._publish(mqtt, "%s/state" % base_topic, int(round(value * 100)))
# HmIP-BROLL(4,5,6)
elif chan_type == "SHUTTER_VIRTUAL_RECEIVER" and key == "LEVEL":
self._publish(mqtt, "%s/state" % base_topic, int(round(value * 100)))
# HmIP-BROLL(7), HmIP-BSM(9)
elif chan_type.endswith("_WEEK_PROFILE") and key == "WEEK_PROGRAM_CHANNEL_LOCKS":
self._publish(mqtt, "%s/state" % base_topic, value)
# HmIP-SRH(1)
elif chan_type == "ROTARY_HANDLE_TRANSCEIVER" and key == "STATE":
text = ROTARY_HANDLE_VALUES.get(value, "unknown")
self._publish(mqtt, "%s/state" % base_topic, text)
# HmIP-SWSD(1)
elif chan_type == "SMOKE_DETECTOR" and key == "SMOKE_DETECTOR_ALARM_STATUS":
text = SMOKE_DETECTOR_VALUES.get(value, "unknown")
self._publish(mqtt, "%s/state" % base_topic, text)
# HmIP-BSM(3,4,5,6)
elif chan_type in ("SWITCH_TRANSMITTER", "SWITCH_VIRTUAL_RECEIVER") and key == "STATE":
self._publish(mqtt, "%s/state" % base_topic, value)
def _new_devices(self, mqtt, devices) -> None:
logger.debug("new_devices()")
for dev in devices:
address = dev.get("ADDRESS")
devtype = dev.get("TYPE")
if address and devtype:
self._ha_attributes[address] = {
k.lower(): v for k, v in dev.items() if v not in ("", [])
}
index = dev.get("INDEX")
parent = dev.get("PARENT")
parent_type = dev.get("PARENT_TYPE")
if not parent:
self._ha_devices[address] = {
"name": f"{devtype}_{address}",
"identifiers": [address],
"manufacturer": "eQ-3",
"model": devtype,
}
firmware = dev.get("FIRMWARE")
if firmware:
self._ha_devices[address]["sw_version"] = firmware
logger.debug("New parent: %s", self._ha_devices[address])
elif parent in self._ha_devices and index is not None:
node_id = f"{parent_type}_{parent}"
object_id = f"{index}-{devtype}"
parent_topic = f"{MQTT_PREFIX}/{parent}"
base_topic = f"{parent_topic}/{index}"
config = {
"device": self._ha_devices[parent],
}
if devtype in BINARY_SENSOR_TYPES:
# https://www.home-assistant.io/integrations/binary_sensor.mqtt/
config["availability_topic"] = "%s/availability" % parent_topic
config["json_attributes_topic"] = "%s/attributes" % base_topic
config["name"] = f"{parent_type} {devtype} {address}"
config["state_topic"] = "%s/state" % base_topic
config["unique_id"] = "Homematic-%s" % address
self._publish_discovery(mqtt, "binary_sensor", node_id, object_id, config)
elif devtype in SENSOR_TYPES:
# https://www.home-assistant.io/integrations/sensor.mqtt/
config["availability_topic"] = "%s/availability" % parent_topic
config["json_attributes_topic"] = "%s/attributes" % base_topic
config["name"] = f"{parent_type} {devtype} {address}"
config["state_topic"] = "%s/state" % base_topic
config["unique_id"] = "Homematic-%s" % address
unit_of_measurement = SENSOR_UNITS.get(devtype)
if unit_of_measurement:
config["unit_of_measurement"] = unit_of_measurement
self._publish_discovery(mqtt, "sensor", node_id, object_id, config)
elif devtype in DEVICE_TRIGGER_TYPES:
# https://www.home-assistant.io/integrations/device_trigger.mqtt/
component = "device_automation"
config["automation_type"] = "trigger"
config["subtype"] = "button_%s" % index
config["topic"] = "%s/PRESS_SHORT" % base_topic
config["type"] = "button_short_press"
self._publish_discovery(mqtt, component, node_id, object_id + "-short", config)
config["topic"] = "%s/PRESS_LONG" % base_topic
config["type"] = "button_long_press"
self._publish_discovery(mqtt, component, node_id, object_id + "-long", config)
elif devtype in COVER_TYPES:
# https://www.home-assistant.io/integrations/cover.mqtt/
config["availability_topic"] = "%s/availability" % parent_topic
config["command_topic"] = "%s/action" % base_topic
self._subscribe(mqtt, config["command_topic"])
config["device_class"] = "shutter"
config["json_attributes_topic"] = "%s/attributes" % base_topic
config["name"] = f"{parent_type} {devtype} {address}"
config["payload_close"] = "move_down"
config["payload_open"] = "move_up"
config["payload_stop"] = "stop"
config["position_closed"] = 0
config["position_open"] = 100
new_index = HM_COVER_CHANNEL_MAP.get(index)
if new_index is not None:
map_topic = f"{MQTT_PREFIX}/{parent}/%s" % new_index
config["position_topic"] = "%s/state" % map_topic
else:
config["position_topic"] = "%s/state" % base_topic
config["set_position_template"] = "{{ position / 100 }}"
config["set_position_topic"] = "%s/set_level" % base_topic
self._subscribe(mqtt, config["set_position_topic"])
config["unique_id"] = "Homematic-%s" % address
self._publish_discovery(mqtt, "cover", node_id, object_id, config)
elif devtype in SWITCH_TYPES:
# https://www.home-assistant.io/integrations/switch.mqtt/
config["availability_topic"] = "%s/availability" % parent_topic
config["command_topic"] = "%s/action" % base_topic
self._subscribe(mqtt, config["command_topic"])
config["json_attributes_topic"] = "%s/attributes" % base_topic
config["name"] = f"{parent_type} {devtype} {address}"
config["payload_off"] = "off"
config["payload_on"] = "on"
config["state_off"] = "OFF"
config["state_on"] = "ON"
config["state_topic"] = "%s/state" % base_topic
config["unique_id"] = "Homematic-%s" % address
self._publish_discovery(mqtt, "switch", node_id, object_id, config)
else:
logger.warning("Unhandled channel: %s", devtype)
else:
logger.error("Parent not found!")
def _system_callback(self, mqtt, src, *args):
if src == "newDevices" and len(args) >= 2:
if self._check_interface_id(args[0]):
self._new_devices(mqtt, args[1])
async def _process_packet(self, message, homematic):
try:
prefix, address, channel, name = message.topic.split("/")
except ValueError:
logger.error("Invalid topic: %s", message.topic)
return
if prefix != MQTT_PREFIX:
logger.error("Invalid prefix: %s", prefix)
return
try:
channel = int(channel)
except ValueError:
logger.error("Invalid channel: %s", channel)
return
try:
data = message.payload.decode("utf-8")
except UnicodeDecodeError:
logger.error("Invalid payload: %s", message.payload)
return
hmdevice = homematic.devices[HM_REMOTE].get(address)
if not hmdevice:
logger.error("Unable to find Homematic device %s", address)
return
hmchannel = hmdevice.CHANNELS.get(channel)
if not hmchannel:
logger.error("Invalid channel: %s", channel)
return
if isinstance(hmdevice, GenericBlind) and hmchannel.TYPE in COVER_TYPES:
if name == "action":
if data not in ("move_up", "move_down", "stop"):
logger.error("Invalid action: %s", data)
return
logger.debug("%s:%s: %s()", address, channel, data)
getattr(hmdevice, data)(channel)
elif name == "set_level":
try:
level = float(data)
except ValueError:
logger.error("Invalid level: %s", data)
return
if not 0 <= level <= 1:
logger.error("Invalid level: %s", level)
return
logger.debug("%s:%d: set_level(%s)", address, channel, level)
hmdevice.set_level(level, channel)
elif isinstance(hmdevice, GenericSwitch) and hmchannel.TYPE in SWITCH_TYPES:
if name == "action":
if data not in ("on", "off"):
logger.error("Invalid action: %s", data)
return
logger.debug("%s:%s: %s()", address, channel, data)
getattr(hmdevice, data)(channel)
def _xmlrpc_listen_url(self, url):
if "://" not in url:
url = f"//{url}"
p = urlparse(url, scheme="xmlrpc")
if p.scheme == "xmlrpc" and p.hostname:
return p
raise ValueError
def _xmlrpc_connect_url(self, url):
p = self._xmlrpc_listen_url(url)
if p.port:
return p
raise ValueError
async def run(self, broker: str, listen: str, connect: str) -> None:
p = urlparse(broker, scheme="mqtt")
if p.scheme not in ("mqtt", "mqtts") or not p.hostname:
raise ValueError
tls_context = None
if p.scheme == "mqtts":
tls_context = ssl.create_default_context()
will = None
async with Client(
p.hostname,
port=p.port or p.scheme == "mqtt" and 1883 or 8883,
username=p.username,
password=p.password,
logger=logger,
tls_context=tls_context,
will=will,
) as mqtt:
xmlrpc_local = self._xmlrpc_listen_url(listen)
xmlrpc_remote = self._xmlrpc_connect_url(connect)
homematic = HMConnection(
interface_id=HM_INTERFACE_ID,
local=xmlrpc_local.hostname,
localport=xmlrpc_local.port or 0,
remotes={
HM_REMOTE: {
"ip": xmlrpc_remote.hostname,
"port": xmlrpc_remote.port,
"path": xmlrpc_remote.path or "",
"username": xmlrpc_remote.username or "Admin",
"password": xmlrpc_remote.password or "",
}
},
eventcallback=partial(self._event_callback, mqtt),
systemcallback=partial(self._system_callback, mqtt),
)
try:
homematic.start()
except AttributeError:
sys.exit(1)
async with mqtt.unfiltered_messages() as messages:
async for message in messages:
await self._process_packet(message, homematic)
homematic.stop()
async def async_main(cfg: dict) -> None:
if cfg["debug"]:
logger.setLevel(logging.DEBUG)
await HomematicMqttBridge().run(cfg["broker"], cfg["listen"], cfg["connect"])
def options() -> dict:
cfg = {
"config": "/var/lib/hm-mqtt-bridge/config.json",
"broker": "mqtt://localhost",
"listen": "xmlrpc://0.0.0.0",
}
parser = argparse.ArgumentParser()
parser.add_argument(
"--config",
help=f"Location of config file (default: {cfg['config']})",
)
parser.add_argument(
"--broker",
help=f"MQTT broker (default: {cfg['broker']})",
)
parser.add_argument(
"--listen",
help=f"Where to listen for connections from CCU (default: {cfg['listen']})",
)
parser.add_argument(
"--connect",
help="XML-RPC server of CCU, e.g. xmlrpc://ccu.local:2010",
)
parser.add_argument(
"--debug",
action="store_true",
help="Enable logging of debug messages",
)
args = parser.parse_args()
filename = args.config or cfg["config"]
try:
with open(filename, "r") as f:
cfg.update(json.load(f))
except OSError as exc:
if args.config or not isinstance(exc, FileNotFoundError):
logger.error("Failed to open configuration file: %s", exc)
sys.exit(1)
except json.JSONDecodeError as exc:
logger.error("Failed to parse configuration file: %s", exc)
sys.exit(1)
for key, value in vars(args).items():
if value is not None:
cfg[key] = value
return cfg
def main():
try:
asyncio.run(async_main(options()))
except MqttError as exc:
logger.critical("MQTT error: %s", exc)
sys.exit(1)
except KeyboardInterrupt:
pass
if __name__ == "__main__":
main()