From 69e12df43000664d3d1899984de8b70f3c522185 Mon Sep 17 00:00:00 2001 From: Meatballs Date: Tue, 19 Mar 2024 12:20:25 +0000 Subject: [PATCH] Use a config.ini file --- .gitignore | 1 + README.md | 20 ++++++++++++++++++-- hc2mqtt | 14 ++++++++------ requirements.txt | 1 + 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index dd6fc16..47f41b0 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ __* *.json *.zip *.sh +*.ini diff --git a/README.md b/README.md index 364504c..dd9ad62 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ Installing `sslpsk` needs some extra steps: ![laptop in a clothes washer with a display DoorState:Closed](images/doorclose.jpg) ```bash -hc-login $USERNAME $PASSWORD > config.json +hc-login $USERNAME $PASSWORD > devices.json ``` The `hc-login` script perfoms the OAuth process to login to your @@ -58,8 +58,24 @@ your mDNS or DNS server resolves the names correctly. ## Home Connect to MQTT +Use the following config/config.ini example: + +``` +devices_file = "/config/devices.json" +mqtt_host = "localhost" +mqtt_username = "mqtt" +mqtt_password = "password" +mqtt_port = 1883 +mqtt_prefix = "homeconnect/" +mqtt_ssl = False +mqtt_cafile = None +mqtt_certfile = None +mqtt_keyfile = None +mqtt_clientname="hcpy" +``` + ```bash -hc2mqtt config.json +hc2mqtt --config config/config.ini ``` This tool will establish websockets to the local devices and diff --git a/hc2mqtt b/hc2mqtt index c6f4d81..e2ab22e 100755 --- a/hc2mqtt +++ b/hc2mqtt @@ -7,13 +7,14 @@ import time from threading import Thread import click +import click_config_file import paho.mqtt.client as mqtt from HCDevice import HCDevice from HCSocket import HCSocket, now @click.command() -@click.argument("config_file") +@click.option("-d", "--devices_file", default="config/devices.json") @click.option("-h", "--mqtt_host", default="localhost") @click.option("-p", "--mqtt_prefix", default="homeconnect/") @click.option("--mqtt_port", default=1883, type=int) @@ -24,13 +25,14 @@ from HCSocket import HCSocket, now @click.option("--mqtt_certfile") @click.option("--mqtt_keyfile") @click.option("--mqtt_clientname", default="hcpy") +@click_config_file.configuration_option() -def hc2mqtt(config_file: str, mqtt_host: str, mqtt_prefix: str, mqtt_port: int, mqtt_username: str, +def hc2mqtt(devices_file: str, mqtt_host: str, mqtt_prefix: str, mqtt_port: int, mqtt_username: str, mqtt_password: str, mqtt_ssl: bool, mqtt_cafile: str, mqtt_certfile: str, mqtt_keyfile: str, mqtt_clientname: str): - click.echo(f"Hello {config_file=} {mqtt_host=} {mqtt_prefix=} {mqtt_port=} {mqtt_username=} {mqtt_password=} " + click.echo(f"Hello {devices_file=} {mqtt_host=} {mqtt_prefix=} {mqtt_port=} {mqtt_username=} {mqtt_password=} " f"{mqtt_ssl=} {mqtt_cafile=} {mqtt_certfile=} {mqtt_keyfile=} {mqtt_clientname=}") - with open(config_file, "r") as f: + with open(devices_file, "r") as f: devices = json.load(f) client = mqtt.Client(mqtt_clientname) @@ -120,8 +122,8 @@ def client_connect(client, device, mqtt_topic): # new_topic = topics[topic] # if new_topic == "remaining": - # state["remainingseconds"] = value - # value = "%d:%02d" % (value / 60 / 60, (value / 60) % 60) + # state["remainingseconds"] = value + # value = "%d:%02d" % (value / 60 / 60, (value / 60) % 60) new_topic = device_topics[topic] state[new_topic] = value diff --git a/requirements.txt b/requirements.txt index 1a331e2..493ebbc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,4 +6,5 @@ sslpsk paho.mqtt==1.6.1 lxml click +click-config-file requests