From 64f47e12a1aed652b362ed8d8af59f88433e3395 Mon Sep 17 00:00:00 2001 From: Meatballs1 Date: Mon, 13 May 2024 12:00:38 +0100 Subject: [PATCH 1/4] Add debug options --- HCDevice.py | 12 ++++++++---- hc2mqtt.py | 9 ++++++--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/HCDevice.py b/HCDevice.py index e69a71f..c52cea9 100755 --- a/HCDevice.py +++ b/HCDevice.py @@ -57,7 +57,7 @@ def now(): class HCDevice: - def __init__(self, ws, device): + def __init__(self, ws, device, debug=False): self.ws = ws self.features_lock = threading.Lock() self.features = device.get("features") @@ -66,7 +66,7 @@ class HCDevice: self.tx_msg_id = None self.device_name = "hcpy" self.device_id = "0badcafe" - self.debug = False + self.debug = debug self.services_initialized = False self.services = {} self.token = None @@ -262,14 +262,18 @@ class HCDevice: if action == "POST": if resource == "/ro/values": # Raises exceptions on failure - self.test_feature(data) + if self.debug is False: + self.test_feature(data) elif resource == "/ro/activeProgram": # Raises exception on failure - self.test_program_data(data) + if self.debug is False: + self.test_program_data(data) msg["data"] = data try: + if self.debug: + self.print(f"TX: {msg}") self.ws.send(msg) except Exception as e: print(self.name, "Failed to send", e, msg, traceback.format_exc()) diff --git a/hc2mqtt.py b/hc2mqtt.py index 083a592..dda11ee 100755 --- a/hc2mqtt.py +++ b/hc2mqtt.py @@ -27,6 +27,7 @@ from HCSocket import HCSocket, now @click.option("--mqtt_certfile") @click.option("--mqtt_keyfile") @click.option("--mqtt_clientname", default="hcpy1") +@click.option("--debug/--no-debug", default=False) @click_config_file.configuration_option() def hc2mqtt( devices_file: str, @@ -40,6 +41,7 @@ def hc2mqtt( mqtt_certfile: str, mqtt_keyfile: str, mqtt_clientname: str, + debug: bool ): def on_connect(client, userdata, flags, rc): @@ -101,6 +103,7 @@ def hc2mqtt( f"Hello {devices_file=} {mqtt_host=} {mqtt_prefix=} " f"{mqtt_port=} {mqtt_username=} {mqtt_password=} " f"{mqtt_ssl=} {mqtt_cafile=} {mqtt_certfile=} {mqtt_keyfile=} {mqtt_clientname=}" + f"{debug=}" ) with open(devices_file, "r") as f: @@ -130,7 +133,7 @@ def hc2mqtt( for device in devices: mqtt_topic = mqtt_prefix + device["name"] - thread = Thread(target=client_connect, args=(client, device, mqtt_topic)) + thread = Thread(target=client_connect, args=(client, device, mqtt_topic, debug)) thread.start() client.loop_forever() @@ -140,7 +143,7 @@ global dev dev = {} -def client_connect(client, device, mqtt_topic): +def client_connect(client, device, mqtt_topic, debug): host = device["host"] name = device["name"] @@ -193,7 +196,7 @@ def client_connect(client, device, mqtt_topic): try: print(now(), name, f"connecting to {host}") ws = HCSocket(host, device["key"], device.get("iv", None)) - dev[name] = HCDevice(ws, device) + dev[name] = HCDevice(ws, device, debug) dev[name].run_forever(on_message=on_message, on_open=on_open, on_close=on_close) except Exception as e: print(now(), device["name"], "ERROR", e, file=sys.stderr) From 90a4de82d539a6f183b7ee58c26e4e6efb3b4b2d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 13 May 2024 11:03:25 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- hc2mqtt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hc2mqtt.py b/hc2mqtt.py index dda11ee..b877d30 100755 --- a/hc2mqtt.py +++ b/hc2mqtt.py @@ -41,7 +41,7 @@ def hc2mqtt( mqtt_certfile: str, mqtt_keyfile: str, mqtt_clientname: str, - debug: bool + debug: bool, ): def on_connect(client, userdata, flags, rc): From fa2be65cc82c7b5e61f70827d5e336d4534ac609 Mon Sep 17 00:00:00 2001 From: Meatballs1 Date: Mon, 13 May 2024 12:04:37 +0100 Subject: [PATCH 3/4] No need for 2 debug checks --- HCDevice.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/HCDevice.py b/HCDevice.py index c52cea9..2665f05 100755 --- a/HCDevice.py +++ b/HCDevice.py @@ -259,15 +259,13 @@ class HCDevice: if isinstance(data, list) is False: data = [data] - if action == "POST": + if action == "POST" and self.debug is False: if resource == "/ro/values": # Raises exceptions on failure - if self.debug is False: - self.test_feature(data) + self.test_feature(data) elif resource == "/ro/activeProgram": # Raises exception on failure - if self.debug is False: - self.test_program_data(data) + self.test_program_data(data) msg["data"] = data From 1119e1e26fa98519f8a3bb01a482bb5adaa20dc2 Mon Sep 17 00:00:00 2001 From: Meatballs1 Date: Mon, 13 May 2024 12:21:53 +0100 Subject: [PATCH 4/4] Add debug option to Addon --- home-assistant-addon/config.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/home-assistant-addon/config.yaml b/home-assistant-addon/config.yaml index d6d528f..cd16fef 100644 --- a/home-assistant-addon/config.yaml +++ b/home-assistant-addon/config.yaml @@ -27,6 +27,7 @@ options: HCPY_MQTT_CERTFILE: "" HCPY_MQTT_KEYFILE: "" HCPY_MQTT_CLIENTNAME: "hcpy1" + HCPY_DEBUG: false schema: HCPY_DEVICES_FILE: str HCPY_MQTT_HOST: str @@ -39,4 +40,5 @@ schema: HCPY_MQTT_CERTFILE: str HCPY_MQTT_KEYFILE: str HCPY_MQTT_CLIENTNAME: str + HCPY_DEBUG: bool? startup: services