100 lines
2.1 KiB
Python
Executable File
100 lines
2.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# Contact Bosh-Siemens Home Connect devices
|
|
# and connect their messages to the mqtt server
|
|
import sys
|
|
import json
|
|
import re
|
|
import time
|
|
import io
|
|
from threading import Thread
|
|
from HCSocket import HCSocket, now
|
|
from HCDevice import HCDevice
|
|
import paho.mqtt.client as mqtt
|
|
|
|
if len(sys.argv) < 2:
|
|
print("Usage: hc2mqtt config.json", file=sys.stderr)
|
|
exit(1)
|
|
with open(sys.argv[1], "r") as f:
|
|
config_json = f.read()
|
|
devices = json.loads(config_json)
|
|
|
|
# these should probably be in the config too
|
|
mqtt_prefix = "homeconnect/"
|
|
client = mqtt.Client()
|
|
client.connect("dashboard", 1883, 70)
|
|
|
|
|
|
# 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(device):
|
|
mqtt_topic = mqtt_prefix + device["name"]
|
|
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)
|
|
|
|
for device in devices:
|
|
thread = Thread(target=client_connect, args=(device,))
|
|
thread.start()
|
|
|