Skip to content

Commit cc2031e

Browse files
committed
Adding proper via_device details including state of bridge in HA discovery. This will also now allow multiple instances to run correctly while using shared MQTT base topic.
1 parent bf35a3c commit cc2031e

File tree

1 file changed

+52
-16
lines changed

1 file changed

+52
-16
lines changed

wyzesense2mqtt/wyzesense2mqtt.py

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import wyzesense
1717
from retrying import retry
1818

19+
WYZESENSE2MQTT_VERSION = "3.1"
1920

2021
# Configuration File Locations
2122
CONFIG_PATH = "config"
@@ -122,6 +123,7 @@
122123

123124
INITIALIZED = False
124125

126+
125127
# Read data from YAML file
126128
def read_yaml_file(filename):
127129
try:
@@ -254,7 +256,8 @@ def init_mqtt_client():
254256
time.sleep(1)
255257

256258
# Make sure the service stays marked as offline until everything is initialized
257-
mqtt_publish(f"{CONFIG['self_topic_root']}/status", "offline", is_json=False)
259+
mqtt_publish(f"{CONFIG['self_topic_root']}/bridge_{WYZESENSE_DONGLE.MAC}/status", "offline", is_json=False)
260+
258261

259262
# Retry forever on IO Error
260263
def retry_if_io_error(exception):
@@ -285,6 +288,36 @@ def init_wyzesense_dongle():
285288
LOGGER.error(f"No device found on path {CONFIG['usb_dongle']}: {str(error)}")
286289

287290

291+
# Initialize bridge discovery
292+
def init_bridge_discovery(wait=True):
293+
if(CONFIG['hass_discovery']):
294+
connection_state_payload = {
295+
'default_entity_id': 'binary_sensor.wyzesense2mqtt_bridge_connection_state',
296+
'device': {
297+
'hw_version': {WYZESENSE_DONGLE.Version},
298+
'identifiers': [f"wyzesense2mqtt_bridge_{WYZESENSE_DONGLE.MAC}"],
299+
'manufacturer': 'Raetha',
300+
'model': 'Bridge',
301+
'name': 'WyzeSense2MQTT Bridge',
302+
'sw_version': {WYZESENSE2MQTT_VERSION}
303+
},
304+
'device_class': 'connectivity',
305+
'entity_category': 'diagnostic',
306+
'name': 'Connection state',
307+
'object_id': 'wyzesense2mqtt_bridge_connection_state',
308+
'origin': {
309+
'name': 'WyzeSense2MQTT Bridge',
310+
'sw_version': {WYZESENSE2MQTT_VERSION},
311+
'support_url': 'https://github.com/raetha/wyzesense2mqtt'
312+
},
313+
'payload_off': 'offline',
314+
'payload_on': 'online',
315+
'state_topic': 'wyzesense2mqtt/bridge_{WYZESENSE_DONGLE.MAC}/status',
316+
'unique_id': f"wyzesense2mqtt_bridge_{WYZESENSE_DONGLE.MAC}_connection_state"
317+
}
318+
mqtt_publish(f"{CONFIG['hass_topic_root']}/binary_sensor/wyzesense_{WYZESENSE_DONGLE.MAC}/connection_state/config", connection_state_payload, wait=wait)
319+
320+
288321
# Initialize sensor configuration
289322
def init_sensors(wait=True):
290323
# Initialize sensor dictionary
@@ -488,7 +521,7 @@ def send_discovery_topics(sensor_mac, wait=True):
488521
'hw_version': attr['hw_version'],
489522
'name': attr['name'],
490523
'sw_version': attr['sw_version'],
491-
'via_device': "wyzesense2mqtt"
524+
'via_device': "wyzesense2mqtt_bridge_{WYZESENSE_DONGLE.MAC}"
492525
}
493526
}
494527

@@ -507,7 +540,7 @@ def send_discovery_topics(sensor_mac, wait=True):
507540
'hw_version': attr['hw_version'],
508541
'name': attr['name'],
509542
'sw_version': attr['sw_version'],
510-
'via_device': "wyzesense2mqtt"
543+
'via_device': "wyzesense2mqtt_bridge_{WYZESENSE_DONGLE.MAC}"
511544
}
512545
}
513546

@@ -525,7 +558,7 @@ def send_discovery_topics(sensor_mac, wait=True):
525558
'hw_version': attr['hw_version'],
526559
'name': attr['name'],
527560
'sw_version': attr['sw_version'],
528-
'via_device': "wyzesense2mqtt"
561+
'via_device': "wyzesense2mqtt_bridge_{WYZESENSE_DONGLE.MAC}"
529562
}
530563
}
531564

@@ -542,7 +575,7 @@ def send_discovery_topics(sensor_mac, wait=True):
542575
'hw_version': attr['hw_version'],
543576
'name': attr['name'],
544577
'sw_version': attr['sw_version'],
545-
'via_device': "wyzesense2mqtt"
578+
'via_device': "wyzesense2mqtt_bridge_{WYZESENSE_DONGLE.MAC}"
546579
}
547580
}
548581

@@ -560,7 +593,7 @@ def send_discovery_topics(sensor_mac, wait=True):
560593
'hw_version': attr['hw_version'],
561594
'name': attr['name'],
562595
'sw_version': attr['sw_version'],
563-
'via_device': "wyzesense2mqtt"
596+
'via_device': "wyzesense2mqtt_bridge_{WYZESENSE_DONGLE.MAC}"
564597
}
565598
}
566599

@@ -577,7 +610,7 @@ def send_discovery_topics(sensor_mac, wait=True):
577610
'hw_version': attr['hw_version'],
578611
'name': attr['name'],
579612
'sw_version': attr['sw_version'],
580-
'via_device': "wyzesense2mqtt"
613+
'via_device': "wyzesense2mqtt_bridge_{WYZESENSE_DONGLE.MAC}"
581614
}
582615
}
583616
else:
@@ -597,7 +630,7 @@ def send_discovery_topics(sensor_mac, wait=True):
597630
'hw_version': attr['hw_version'],
598631
'name': attr['name'],
599632
'sw_version': attr['sw_version'],
600-
'via_device': "wyzesense2mqtt"
633+
'via_device': "wyzesense2mqtt_bridge_{WYZESENSE_DONGLE.MAC}"
601634
}
602635
}
603636
entity_payloads['battery'] = {
@@ -612,13 +645,13 @@ def send_discovery_topics(sensor_mac, wait=True):
612645
'hw_version': attr['hw_version'],
613646
'name': attr['name'],
614647
'sw_version': attr['sw_version'],
615-
'via_device': "wyzesense2mqtt"
648+
'via_device': "wyzesense2mqtt_bridge_{WYZESENSE_DONGLE.MAC}"
616649
}
617650
}
618651

619652
availability_topics = [
620653
{ 'topic': f"{CONFIG['self_topic_root']}/{sensor_mac}/status" },
621-
{ 'topic': f"{CONFIG['self_topic_root']}/status" }
654+
{ 'topic': f"{CONFIG['self_topic_root']}/bridge_{WYZESENSE_DONGLE.MAC}/status" }
622655
]
623656

624657
for entity, entity_payload in entity_payloads.items():
@@ -809,11 +842,11 @@ def on_event(WYZESENSE_DONGLE, event):
809842
LOGGER.warning(f"Event data: {event}")
810843

811844
def Stop():
845+
mqtt_publish(f"{CONFIG['self_topic_root']}/bridge_{WYZESENSE_DONGLE.MAC}/status", "offline", is_json=False)
846+
812847
# Stop the dongle first, letting this thread finish anything it might be busy doing, like handling an event
813848
WYZESENSE_DONGLE.Stop()
814849

815-
mqtt_publish(f"{CONFIG['self_topic_root']}/status", "offline", is_json=False)
816-
817850
# All event handling should now be done, close the mqtt connection
818851
MQTT_CLIENT.loop_stop()
819852
MQTT_CLIENT.disconnect()
@@ -840,11 +873,14 @@ def Stop():
840873
REMOVE_TOPIC = f"{CONFIG['self_topic_root']}/remove"
841874
RELOAD_TOPIC = f"{CONFIG['self_topic_root']}/reload"
842875

876+
# Initialize USB dongle
877+
init_wyzesense_dongle()
878+
843879
# Initialize MQTT client connection
844880
init_mqtt_client()
845881

846-
# Initialize USB dongle
847-
init_wyzesense_dongle()
882+
# Initialize bridge discovery topics
883+
init_bridge_discovery()
848884

849885
# Initialize sensor configuration
850886
init_sensors()
@@ -853,7 +889,7 @@ def Stop():
853889
INITIALIZED = True
854890

855891
# And mark the service as online
856-
mqtt_publish(f"{CONFIG['self_topic_root']}/status", "online", is_json=False)
892+
mqtt_publish(f"{CONFIG['self_topic_root']}/bridge_{WYZESENSE_DONGLE.MAC}/status", "online", is_json=False)
857893

858894
# Loop forever until keyboard interrupt or SIGINT
859895
try:
@@ -867,7 +903,7 @@ def Stop():
867903
MQTT_CLIENT.reconnect()
868904

869905
if MQTT_CLIENT.connected_flag:
870-
mqtt_publish(f"{CONFIG['self_topic_root']}/status", "online", is_json=False)
906+
mqtt_publish(f"{CONFIG['self_topic_root']}/bridge_{WYZESENSE_DONGLE.MAC}/status", "online", is_json=False)
871907

872908

873909
# Check for availability of the devices

0 commit comments

Comments
 (0)