Start publishing HA auto-discovery information.
This commit is contained in:
15
hc2mqtt.py
15
hc2mqtt.py
@@ -13,6 +13,7 @@ import paho.mqtt.client as mqtt
|
||||
|
||||
from HCDevice import HCDevice
|
||||
from HCSocket import HCSocket, now
|
||||
from HADiscovery import publish_ha_states, publish_ha_discovery
|
||||
|
||||
|
||||
@click.command()
|
||||
@@ -29,6 +30,7 @@ from HCSocket import HCSocket, now
|
||||
@click.option("--mqtt_clientname", default="hcpy1")
|
||||
@click.option("--domain_suffix", default="")
|
||||
@click.option("--debug/--no-debug", default=False)
|
||||
@click.option("--ha-discovery", is_flag=True)
|
||||
@click_config_file.configuration_option()
|
||||
def hc2mqtt(
|
||||
devices_file: str,
|
||||
@@ -44,6 +46,7 @@ def hc2mqtt(
|
||||
mqtt_clientname: str,
|
||||
domain_suffix: str,
|
||||
debug: bool,
|
||||
ha_discovery: bool
|
||||
):
|
||||
|
||||
def on_connect(client, userdata, flags, rc):
|
||||
@@ -74,6 +77,8 @@ def hc2mqtt(
|
||||
now(), device["name"], f"program topic: {mqtt_selected_program_topic}"
|
||||
)
|
||||
client.subscribe(mqtt_selected_program_topic)
|
||||
if ha_discovery:
|
||||
publish_ha_discovery(device, client, mqtt_topic)
|
||||
else:
|
||||
print(now(), f"ERROR MQTT connection failed: {rc}")
|
||||
|
||||
@@ -117,7 +122,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=} {debug=}"
|
||||
f"{domain_suffix=} {debug=} {ha_discovery=}"
|
||||
)
|
||||
|
||||
with open(devices_file, "r") as f:
|
||||
@@ -146,9 +151,9 @@ def hc2mqtt(
|
||||
client.connect(host=mqtt_host, port=mqtt_port, keepalive=70)
|
||||
|
||||
for device in devices:
|
||||
mqtt_topic = mqtt_prefix + device["name"]
|
||||
mqtt_topic = mqtt_prefix + device["host"]
|
||||
thread = Thread(
|
||||
target=client_connect, args=(client, device, mqtt_topic, domain_suffix, debug)
|
||||
target=client_connect, args=(client, device, mqtt_topic, domain_suffix, debug, ha_discovery)
|
||||
)
|
||||
thread.start()
|
||||
|
||||
@@ -159,7 +164,7 @@ global dev
|
||||
dev = {}
|
||||
|
||||
|
||||
def client_connect(client, device, mqtt_topic, domain_suffix, debug):
|
||||
def client_connect(client, device, mqtt_topic, domain_suffix, debug, ha_discovery):
|
||||
host = device["host"]
|
||||
name = device["name"]
|
||||
|
||||
@@ -193,6 +198,8 @@ def client_connect(client, device, mqtt_topic, domain_suffix, debug):
|
||||
msg = json.dumps(state)
|
||||
print(now(), name, f"publish to {mqtt_topic} with {msg}")
|
||||
client.publish(f"{mqtt_topic}/state", msg, retain=True)
|
||||
if ha_discovery:
|
||||
publish_ha_states(state, client, mqtt_topic)
|
||||
else:
|
||||
print(
|
||||
now(),
|
||||
|
||||
Reference in New Issue
Block a user