Merge branch 'main' into domain_suffix
This commit is contained in:
@@ -57,7 +57,7 @@ def now():
|
|||||||
|
|
||||||
|
|
||||||
class HCDevice:
|
class HCDevice:
|
||||||
def __init__(self, ws, device):
|
def __init__(self, ws, device, debug=False):
|
||||||
self.ws = ws
|
self.ws = ws
|
||||||
self.features_lock = threading.Lock()
|
self.features_lock = threading.Lock()
|
||||||
self.features = device.get("features")
|
self.features = device.get("features")
|
||||||
@@ -66,7 +66,7 @@ class HCDevice:
|
|||||||
self.tx_msg_id = None
|
self.tx_msg_id = None
|
||||||
self.device_name = "hcpy"
|
self.device_name = "hcpy"
|
||||||
self.device_id = "0badcafe"
|
self.device_id = "0badcafe"
|
||||||
self.debug = False
|
self.debug = debug
|
||||||
self.services_initialized = False
|
self.services_initialized = False
|
||||||
self.services = {}
|
self.services = {}
|
||||||
self.token = None
|
self.token = None
|
||||||
@@ -259,7 +259,7 @@ class HCDevice:
|
|||||||
if isinstance(data, list) is False:
|
if isinstance(data, list) is False:
|
||||||
data = [data]
|
data = [data]
|
||||||
|
|
||||||
if action == "POST":
|
if action == "POST" and self.debug is False:
|
||||||
if resource == "/ro/values":
|
if resource == "/ro/values":
|
||||||
# Raises exceptions on failure
|
# Raises exceptions on failure
|
||||||
self.test_feature(data)
|
self.test_feature(data)
|
||||||
@@ -270,6 +270,8 @@ class HCDevice:
|
|||||||
msg["data"] = data
|
msg["data"] = data
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
if self.debug:
|
||||||
|
self.print(f"TX: {msg}")
|
||||||
self.ws.send(msg)
|
self.ws.send(msg)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(self.name, "Failed to send", e, msg, traceback.format_exc())
|
print(self.name, "Failed to send", e, msg, traceback.format_exc())
|
||||||
|
|||||||
10
hc2mqtt.py
10
hc2mqtt.py
@@ -28,6 +28,7 @@ from HCSocket import HCSocket, now
|
|||||||
@click.option("--mqtt_keyfile")
|
@click.option("--mqtt_keyfile")
|
||||||
@click.option("--mqtt_clientname", default="hcpy1")
|
@click.option("--mqtt_clientname", default="hcpy1")
|
||||||
@click.option("--domain_suffix", default="")
|
@click.option("--domain_suffix", default="")
|
||||||
|
@click.option("--debug/--no-debug", default=False)
|
||||||
@click_config_file.configuration_option()
|
@click_config_file.configuration_option()
|
||||||
def hc2mqtt(
|
def hc2mqtt(
|
||||||
devices_file: str,
|
devices_file: str,
|
||||||
@@ -42,6 +43,7 @@ def hc2mqtt(
|
|||||||
mqtt_keyfile: str,
|
mqtt_keyfile: str,
|
||||||
mqtt_clientname: str,
|
mqtt_clientname: str,
|
||||||
domain_suffix: str,
|
domain_suffix: str,
|
||||||
|
debug: bool,
|
||||||
):
|
):
|
||||||
|
|
||||||
def on_connect(client, userdata, flags, rc):
|
def on_connect(client, userdata, flags, rc):
|
||||||
@@ -103,7 +105,7 @@ def hc2mqtt(
|
|||||||
f"Hello {devices_file=} {mqtt_host=} {mqtt_prefix=} "
|
f"Hello {devices_file=} {mqtt_host=} {mqtt_prefix=} "
|
||||||
f"{mqtt_port=} {mqtt_username=} {mqtt_password=} "
|
f"{mqtt_port=} {mqtt_username=} {mqtt_password=} "
|
||||||
f"{mqtt_ssl=} {mqtt_cafile=} {mqtt_certfile=} {mqtt_keyfile=} {mqtt_clientname=}"
|
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:
|
with open(devices_file, "r") as f:
|
||||||
@@ -133,7 +135,7 @@ def hc2mqtt(
|
|||||||
|
|
||||||
for device in devices:
|
for device in devices:
|
||||||
mqtt_topic = mqtt_prefix + device["name"]
|
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()
|
thread.start()
|
||||||
|
|
||||||
client.loop_forever()
|
client.loop_forever()
|
||||||
@@ -143,7 +145,7 @@ global dev
|
|||||||
dev = {}
|
dev = {}
|
||||||
|
|
||||||
|
|
||||||
def client_connect(client, device, mqtt_topic, domain_suffix):
|
def client_connect(client, device, mqtt_topic, domain_suffix, debug):
|
||||||
host = device["host"]
|
host = device["host"]
|
||||||
name = device["name"]
|
name = device["name"]
|
||||||
|
|
||||||
@@ -196,7 +198,7 @@ def client_connect(client, device, mqtt_topic, domain_suffix):
|
|||||||
try:
|
try:
|
||||||
print(now(), name, f"connecting to {host}")
|
print(now(), name, f"connecting to {host}")
|
||||||
ws = HCSocket(host, device["key"], device.get("iv", None), domain_suffix)
|
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)
|
dev[name].run_forever(on_message=on_message, on_open=on_open, on_close=on_close)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(now(), device["name"], "ERROR", e, file=sys.stderr)
|
print(now(), device["name"], "ERROR", e, file=sys.stderr)
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ options:
|
|||||||
HCPY_MQTT_KEYFILE: ""
|
HCPY_MQTT_KEYFILE: ""
|
||||||
HCPY_MQTT_CLIENTNAME: "hcpy1"
|
HCPY_MQTT_CLIENTNAME: "hcpy1"
|
||||||
HCPY_DOMAIN_SUFFIX: ""
|
HCPY_DOMAIN_SUFFIX: ""
|
||||||
|
HCPY_DEBUG: false
|
||||||
schema:
|
schema:
|
||||||
HCPY_DEVICES_FILE: str
|
HCPY_DEVICES_FILE: str
|
||||||
HCPY_MQTT_HOST: str
|
HCPY_MQTT_HOST: str
|
||||||
@@ -41,4 +42,5 @@ schema:
|
|||||||
HCPY_MQTT_KEYFILE: str
|
HCPY_MQTT_KEYFILE: str
|
||||||
HCPY_MQTT_CLIENTNAME: str
|
HCPY_MQTT_CLIENTNAME: str
|
||||||
HCPY_DOMAIN_SUFFIX: str
|
HCPY_DOMAIN_SUFFIX: str
|
||||||
|
HCPY_DEBUG: bool?
|
||||||
startup: services
|
startup: services
|
||||||
|
|||||||
Reference in New Issue
Block a user