diff --git a/hc2mqtt b/hc2mqtt index 5125892..43d71ef 100755 --- a/hc2mqtt +++ b/hc2mqtt @@ -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() diff --git a/requirements.txt b/requirements.txt index 13ed067..ace8426 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ websocket-client sslpsk paho.mqtt lxml +click