-
Notifications
You must be signed in to change notification settings - Fork 32
/
hc2mqtt
executable file
·106 lines (80 loc) · 2.33 KB
/
hc2mqtt
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
#!/usr/bin/env python3
# Contact Bosh-Siemens Home Connect devices
# and connect their messages to the mqtt server
import json
import sys
import time
from threading import Thread
import click
import paho.mqtt.client as mqtt
from HCDevice import HCDevice
from HCSocket import HCSocket, now
@click.command()
@click.argument("config_file")
@click.option("-h", "--mqtt_host", default="localhost")
@click.option("-p", "--mqtt_prefix", default="homeconnect/")
def hc2mqtt(config_file: str, mqtt_host: str, mqtt_prefix: str):
click.echo(f"Hello {config_file=} {mqtt_host=} {mqtt_prefix=}")
with open(config_file, "r") as f:
devices = json.load(f)
client = mqtt.Client()
client.connect(host=mqtt_host, port=1883, keepalive=70)
for device in devices:
mqtt_topic = mqtt_prefix + device["name"]
thread = Thread(target=client_connect, args=(client, device, mqtt_topic))
thread.start()
client.loop_forever()
# Map their value names to easier state names
topics = {
"OperationState": "state",
"DoorState": "door",
"RemainingProgramTime": "remaining",
"PowerState": "power",
"LowWaterPressure": "lowwaterpressure",
"AquaStopOccured": "aquastop",
"InternalError": "error",
"FatalErrorOccured": "error",
}
def client_connect(client, device, mqtt_topic):
host = device["host"]
state = {}
for topic in topics:
state[topics[topic]] = None
while True:
try:
ws = HCSocket(host, device["key"], device.get("iv",None))
dev = HCDevice(ws, device.get("features", None))
#ws.debug = True
ws.reconnect()
while True:
msg = dev.recv()
if msg is None:
break
if len(msg) > 0:
print(now(), msg)
update = False
for topic in topics:
value = msg.get(topic, None)
if value is None:
continue
# Convert "On" to True, "Off" to False
if value == "On":
value = True
elif value == "Off":
value = False
new_topic = topics[topic]
if new_topic == "remaining":
state["remainingseconds"] = value
value = "%d:%02d" % (value / 60 / 60, (value / 60) % 60)
state[new_topic] = value
update = True
if not update:
continue
msg = json.dumps(state)
print("publish", mqtt_topic, msg)
client.publish(mqtt_topic + "/state", msg)
except Exception as e:
print("ERROR", host, e, file=sys.stderr)
time.sleep(5)
if __name__ == "__main__":
hc2mqtt()