Merge pull request #15 from jawsper/hc2mqtt-with-click

Use click to parse CLI args in hc2mqtt
This commit is contained in:
Trammell Hudson
2023-03-17 14:03:41 +01:00
committed by GitHub
2 changed files with 28 additions and 21 deletions

48
hc2mqtt
View File

@@ -1,27 +1,36 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# Contact Bosh-Siemens Home Connect devices # Contact Bosh-Siemens Home Connect devices
# and connect their messages to the mqtt server # and connect their messages to the mqtt server
import sys
import json import json
import re import sys
import time import time
import io
from threading import Thread from threading import Thread
from HCSocket import HCSocket, now
from HCDevice import HCDevice import click
import paho.mqtt.client as mqtt import paho.mqtt.client as mqtt
if len(sys.argv) < 2: from HCDevice import HCDevice
print("Usage: hc2mqtt config.json", file=sys.stderr) from HCSocket import HCSocket, now
exit(1)
with open(sys.argv[1], "r") as f:
config_json = f.read() @click.command()
devices = json.loads(config_json) @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 # Map their value names to easier state names
@@ -38,8 +47,7 @@ topics = {
def client_connect(device): def client_connect(client, device, mqtt_topic):
mqtt_topic = mqtt_prefix + device["name"]
host = device["host"] host = device["host"]
state = {} state = {}
@@ -93,7 +101,5 @@ def client_connect(device):
time.sleep(5) time.sleep(5)
for device in devices: if __name__ == "__main__":
thread = Thread(target=client_connect, args=(device,)) hc2mqtt()
thread.start()

View File

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