Skip to content

Commit

Permalink
[DISC] Align discovery
Browse files Browse the repository at this point in the history
Handle binary and text sensors
Filter mac
  • Loading branch information
1technophile committed Dec 8, 2023
1 parent 2b08151 commit 1ea6b2a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion TheengsGateway/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
27 changes: 22 additions & 5 deletions TheengsGateway/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,7 @@ def publish_device_info(self, pub_device) -> None: # noqa: ANN001
hadevice["name"] = pub_device["model"]
hadevice["via_device"] = self.configuration["discovery_device_name"]

discovery_topic = (
self.configuration["discovery_topic"] + "/" + pub_device_uuid
)
discovery_topic = self.configuration["discovery_topic"]
state_topic = (
self.configuration["publish_topic"] + "/" + pub_device_uuid
)
Expand All @@ -156,25 +154,44 @@ 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"]
if pub_device["properties"][k]["unit"] in ha_dev_units:
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)

Expand Down
2 changes: 1 addition & 1 deletion docs/use/use.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down

0 comments on commit 1ea6b2a

Please sign in to comment.