feat: refactor hc2mqtt to take config as cli args, add click to requirements

This commit is contained in:
Jasper Seidel
2022-10-07 16:59:41 +02:00
parent a574219945
commit 8ab7997519
2 changed files with 28 additions and 21 deletions

48
hc2mqtt
View File

@@ -1,27 +1,36 @@
#!/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 sys
import time
import io
from threading import Thread
from HCSocket import HCSocket, now
from HCDevice import HCDevice
import click
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)
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()
# 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
@@ -38,8 +47,7 @@ topics = {
def client_connect(device):
mqtt_topic = mqtt_prefix + device["name"]
def client_connect(client, device, mqtt_topic):
host = device["host"]
state = {}
@@ -93,7 +101,5 @@ def client_connect(device):
time.sleep(5)
for device in devices:
thread = Thread(target=client_connect, args=(device,))
thread.start()
if __name__ == "__main__":
hc2mqtt()

View File

@@ -3,3 +3,4 @@ websocket-client
sslpsk
paho.mqtt
lxml
click