Skip to content

Commit

Permalink
Update discovery.py
Browse files Browse the repository at this point in the history
  • Loading branch information
1technophile committed Dec 8, 2023
1 parent 55fedae commit 9237abd
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions TheengsGateway/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,8 @@ def publish_device_info(self, pub_device) -> None: # noqa: ANN001
hadevice = self.prepare_hadevice(pub_device_uuid, pub_device)

discovery_topic = self.configuration["discovery_topic"]
state_topic = (
self.configuration["publish_topic"] + "/" + pub_device_uuid
)
state_topic = re.sub(
r".+?/",
"+/",
state_topic,
count=len(re.findall(r"/", state_topic)) - 1,
)
state_topic = self.build_state_topic(pub_device)

data = getProperties(pub_device["model_id"])
data = json.loads(data)
data = data["properties"]
Expand Down Expand Up @@ -196,13 +189,26 @@ def publish_device_info(self, pub_device) -> None: # noqa: ANN001

def prepare_hadevice(self, uuid: str, device: dict) -> dict:
"""Prepare Home Assistant device configuration."""
hadevice = {
return {
"identifiers": [uuid],
"connections": [["mac", uuid]],
"manufacturer": device.get("brand", "Unknown"),
"model": device.get("model_id", "Unknown"),
"name": device.get("name", device.get("model", "Unknown")),
"via_device": self.configuration.get("discovery_device_name", "Unknown"),
"via_device": self.configuration.get(
"discovery_device_name", "Unknown"
),
}

return hadevice
def build_state_topic(self, device: dict) -> str:
"""Build state topic."""
state_topic = (
self.configuration["publish_topic"] + "/" + device["id"].replace(":", "")
)
state_topic = re.sub(
r".+?/",
"+/",
state_topic,
count=len(re.findall(r"/", state_topic)) - 1,
)
return state_topic

0 comments on commit 9237abd

Please sign in to comment.