hc2mqtt: create a thread per device

This commit is contained in:
Trammell Hudson
2022-02-06 14:27:56 +01:00
parent c6407d2a28
commit 87914adffa

34
hc2mqtt
View File

@@ -1,11 +1,12 @@
#!/usr/bin/env python3
# Contact a Bosh-Siemens Home Connect device
# and connect it to the mqtt server
# 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
@@ -14,11 +15,6 @@ mqtt_prefix = "homeconnect/"
client = mqtt.Client()
client.connect("dashboard", 1883, 70)
device_name = 'dishwasher'
if len(sys.argv) > 1:
device_name = sys.argv[1]
devices = {
'clothes': {
"host": '10.1.0.145',
@@ -32,14 +28,7 @@ devices = {
},
}
device = devices.get(device_name, None)
if not device:
print(device_name, " not known", file=sys.stderr)
exit(1)
mqtt_topic = mqtt_prefix + device_name
# Map their value names to easier state names
topics = {
"OperationState": "state",
"DoorState": "door",
@@ -51,13 +40,19 @@ topics = {
"FatalErrorOccured": "error",
}
def client_connect(device_name, device):
mqtt_topic = mqtt_prefix + device_name
host = device["host"]
state = {}
for topic in topics:
state[topics[topic]] = None
while True:
try:
ws = HCSocket(device["host"], device["psk64"], device["iv64"])
ws = HCSocket(host, device["psk64"], device["iv64"])
dev = HCDevice(ws)
#ws.debug = True
@@ -85,6 +80,11 @@ while True:
client.publish(mqtt_topic + "/state", msg)
except Exception as e:
print("ERROR", device["host"], e, file=sys.stderr)
print("ERROR", host, e, file=sys.stderr)
time.sleep(5)
for device in devices:
thread = Thread(target=client_connect, args=(device, devices[device]))
thread.start()