Test changes
This commit is contained in:
17
hc2mqtt.py
17
hc2mqtt.py
@@ -26,7 +26,7 @@ from HCSocket import HCSocket, now
|
|||||||
@click.option("--mqtt_cafile")
|
@click.option("--mqtt_cafile")
|
||||||
@click.option("--mqtt_certfile")
|
@click.option("--mqtt_certfile")
|
||||||
@click.option("--mqtt_keyfile")
|
@click.option("--mqtt_keyfile")
|
||||||
@click.option("--mqtt_clientname", default="hcpy")
|
@click.option("--mqtt_clientname", default="hcpy11233")
|
||||||
@click_config_file.configuration_option()
|
@click_config_file.configuration_option()
|
||||||
def hc2mqtt(
|
def hc2mqtt(
|
||||||
devices_file: str,
|
devices_file: str,
|
||||||
@@ -64,7 +64,7 @@ def hc2mqtt(
|
|||||||
print(now(), f"ERROR MQTT connection failed: {rc}")
|
print(now(), f"ERROR MQTT connection failed: {rc}")
|
||||||
|
|
||||||
def on_disconnect(client, userdata, rc):
|
def on_disconnect(client, userdata, rc):
|
||||||
print(now(), "ERROR MQTT client disconnected")
|
print(now(), f"ERROR MQTT client disconnected {rc}")
|
||||||
|
|
||||||
def on_message(client, userdata, msg):
|
def on_message(client, userdata, msg):
|
||||||
mqtt_state = msg.payload.decode()
|
mqtt_state = msg.payload.decode()
|
||||||
@@ -168,7 +168,7 @@ def client_connect(client, device, mqtt_topic):
|
|||||||
client.subscribe(mqtt_set_topic)
|
client.subscribe(mqtt_set_topic)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
time.sleep(20)
|
time.sleep(3)
|
||||||
try:
|
try:
|
||||||
print(now(), device["name"], f"connecting to {host}")
|
print(now(), device["name"], f"connecting to {host}")
|
||||||
ws = HCSocket(host, device["key"], device.get("iv", None))
|
ws = HCSocket(host, device["key"], device.get("iv", None))
|
||||||
@@ -177,9 +177,14 @@ def client_connect(client, device, mqtt_topic):
|
|||||||
# ws.debug = True
|
# ws.debug = True
|
||||||
ws.reconnect()
|
ws.reconnect()
|
||||||
|
|
||||||
|
while True:
|
||||||
|
if client.is_connected():
|
||||||
|
client.publish(f"{mqtt_topic}/LWT", "", retain=True)
|
||||||
|
break
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
msg = dev[device["name"]].recv()
|
msg = dev[device["name"]].recv()
|
||||||
client.publish(F"{mqtt_topic}/LWT", "online")
|
client.publish(f"{mqtt_topic}/LWT", "online")
|
||||||
if msg is None:
|
if msg is None:
|
||||||
break
|
break
|
||||||
if len(msg) > 0:
|
if len(msg) > 0:
|
||||||
@@ -201,7 +206,7 @@ def client_connect(client, device, mqtt_topic):
|
|||||||
if client.is_connected():
|
if client.is_connected():
|
||||||
msg = json.dumps(state)
|
msg = json.dumps(state)
|
||||||
print(now(), device["name"], f"publish to {mqtt_topic} with {msg}")
|
print(now(), device["name"], f"publish to {mqtt_topic} with {msg}")
|
||||||
client.publish(f"{mqtt_topic}/state", msg)
|
client.publish(f"{mqtt_topic}/state", msg, retain=True)
|
||||||
else:
|
else:
|
||||||
print(
|
print(
|
||||||
now(),
|
now(),
|
||||||
@@ -211,7 +216,7 @@ def client_connect(client, device, mqtt_topic):
|
|||||||
|
|
||||||
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)
|
||||||
client.publish(F"{mqtt_topic}/LWT", "offline", retain=True)
|
client.publish(f"{mqtt_topic}/LWT", "offline", retain=True)
|
||||||
|
|
||||||
time.sleep(40)
|
time.sleep(40)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user