diff --git a/HCDevice.py b/HCDevice.py index e69a71f..2665f05 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 @@ -259,7 +259,7 @@ 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 self.test_feature(data) @@ -270,6 +270,8 @@ class HCDevice: 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 d840573..b21718e 100755 --- a/hc2mqtt.py +++ b/hc2mqtt.py @@ -28,6 +28,7 @@ from HCSocket import HCSocket, now @click.option("--mqtt_keyfile") @click.option("--mqtt_clientname", default="hcpy1") @click.option("--domain_suffix", default="") +@click.option("--debug/--no-debug", default=False) @click_config_file.configuration_option() def hc2mqtt( devices_file: str, @@ -42,6 +43,7 @@ def hc2mqtt( mqtt_keyfile: str, mqtt_clientname: str, domain_suffix: str, + debug: bool, ): def on_connect(client, userdata, flags, rc): @@ -103,7 +105,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"{domain_suffix=}" + f"{domain_suffix=} {debug=}" ) with open(devices_file, "r") as f: @@ -133,7 +135,7 @@ def hc2mqtt( for device in devices: mqtt_topic = mqtt_prefix + device["name"] - thread = Thread(target=client_connect, args=(client, device, mqtt_topic, domain_suffix)) + thread = Thread(target=client_connect, args=(client, device, mqtt_topic, domain_suffix, debug)) thread.start() client.loop_forever() @@ -143,7 +145,7 @@ global dev dev = {} -def client_connect(client, device, mqtt_topic, domain_suffix): +def client_connect(client, device, mqtt_topic, domain_suffix, debug): host = device["host"] name = device["name"] @@ -196,7 +198,7 @@ def client_connect(client, device, mqtt_topic, domain_suffix): try: print(now(), name, f"connecting to {host}") ws = HCSocket(host, device["key"], device.get("iv", None), domain_suffix) - 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) diff --git a/home-assistant-addon/config.yaml b/home-assistant-addon/config.yaml index 2a35c15..dd3e5f2 100644 --- a/home-assistant-addon/config.yaml +++ b/home-assistant-addon/config.yaml @@ -28,6 +28,7 @@ options: HCPY_MQTT_KEYFILE: "" HCPY_MQTT_CLIENTNAME: "hcpy1" HCPY_DOMAIN_SUFFIX: "" + HCPY_DEBUG: false schema: HCPY_DEVICES_FILE: str HCPY_MQTT_HOST: str @@ -41,4 +42,5 @@ schema: HCPY_MQTT_KEYFILE: str HCPY_MQTT_CLIENTNAME: str HCPY_DOMAIN_SUFFIX: str + HCPY_DEBUG: bool? startup: services