From 1ff1fd3b9019bf966ea0c8787b782fc6e64e40cc Mon Sep 17 00:00:00 2001 From: Florian <1technophile@users.noreply.github.com> Date: Fri, 8 Dec 2023 10:54:13 -0600 Subject: [PATCH] [DISC] Align discovery Handle binary and text sensors Filter mac --- TheengsGateway/config.py | 2 +- TheengsGateway/discovery.py | 27 +++++++++++++++++++++++---- docs/use/use.md | 2 +- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/TheengsGateway/config.py b/TheengsGateway/config.py index 79830aa1..1c0d76bd 100644 --- a/TheengsGateway/config.py +++ b/TheengsGateway/config.py @@ -29,7 +29,7 @@ "log_level": "INFO", "discovery": 1, "hass_discovery": 1, - "discovery_topic": "homeassistant/sensor", + "discovery_topic": "homeassistant", "discovery_device_name": "TheengsGateway", "discovery_filter": [ "IBEACON", diff --git a/TheengsGateway/discovery.py b/TheengsGateway/discovery.py index 613ed09c..6301f159 100644 --- a/TheengsGateway/discovery.py +++ b/TheengsGateway/discovery.py @@ -142,7 +142,7 @@ def publish_device_info(self, pub_device) -> None: # noqa: ANN001 hadevice["via_device"] = self.configuration["discovery_device_name"] discovery_topic = ( - self.configuration["discovery_topic"] + "/" + pub_device_uuid + self.configuration["discovery_topic"] ) state_topic = ( self.configuration["publish_topic"] + "/" + pub_device_uuid @@ -156,10 +156,14 @@ def publish_device_info(self, pub_device) -> None: # noqa: ANN001 data = getProperties(pub_device["model_id"]) data = json.loads(data) data = data["properties"] + entity_type = "sensor" for k in data: device = {} device["stat_t"] = state_topic + # If the properties key is mac address, skip it + if k in {"mac", "device"}: + continue if k in pub_device["properties"]: if pub_device["properties"][k]["name"] in ha_dev_classes: device["dev_cla"] = pub_device["properties"][k]["name"] @@ -167,14 +171,29 @@ def publish_device_info(self, pub_device) -> None: # noqa: ANN001 device["unit_of_meas"] = pub_device["properties"][k][ "unit" ] + device["state_class"] = "measurement" + entity_type = "sensor" + elif pub_device["properties"][k]["unit"] == "status": + entity_type = "binary_sensor" + device["pl_on"] = "True" + device["pl_off"] = "False" device["name"] = pub_device["model_id"] + "-" + k device["uniq_id"] = pub_device_uuid + "-" + k if self.configuration["hass_discovery"]: device["val_tpl"] = "{{ value_json." + k + " | is_defined }}" else: device["val_tpl"] = "{{ value_json." + k + " }}" - device["state_class"] = "measurement" - config_topic = discovery_topic + "-" + k + "/config" + + config_topic = ( + discovery_topic + + "/" + + entity_type + + "/" + + pub_device_uuid + + "-" + + k + + "/config" + ) device["device"] = hadevice self.publish(json.dumps(device), config_topic, retain=True) @@ -184,4 +203,4 @@ def publish_device_info(self, pub_device) -> None: # noqa: ANN001 self.configuration["publish_topic"] + "/" + pub_device_uuid, ) if self.configuration["presence"]: - self.publish(device_data, self.configuration["presence_topic"]) + self.publish(device_data, self.configuration["presence_topic"]) \ No newline at end of file diff --git a/docs/use/use.md b/docs/use/use.md index 813f4699..9b2f5271 100644 --- a/docs/use/use.md +++ b/docs/use/use.md @@ -128,7 +128,7 @@ docker run --rm \ -e LOG_LEVEL=INFO \ -e HAAS_DISCOVERY=true \ -e DISCOVERY=true \ - -e DISCOVERY_TOPIC=homeassistant/sensor \ + -e DISCOVERY_TOPIC=homeassistant \ -e DISCOVERY_DEVICE_NAME=TheengsGateway \ -e DISCOVERY_FILTER="[IBEACON,GAEN,MS-CDP,APPLE_CONT,APPLE_CONTAT]" \ -e SCANNING_MODE=active \