Skip to content

Commit

Permalink
[DISC] Isolate "track" advanced property publishing (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
DigiH authored Jan 11, 2024
1 parent 4c73555 commit 2fd7940
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
5 changes: 4 additions & 1 deletion TheengsGateway/ble_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
"mic",
"servicedata",
"servicedatauuid",
"track",
)

LOG_LEVEL = {
Expand Down Expand Up @@ -514,6 +513,10 @@ def publish_json(
decoded: bool, # noqa: FBT001
) -> None:
"""Publish JSON data to MQTT."""
# Remove "track" if PUBLISH_ADVDATA is 0
if not self.configuration["publish_advdata"] and "track" in data_json:
data_json.pop("track", None)

message = json.dumps(data_json)
self.publish(
message,
Expand Down
14 changes: 13 additions & 1 deletion TheengsGateway/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ def __init__(self, configuration: dict) -> None:
def publish_device_info(self, pub_device) -> None: # noqa: ANN001
"""Publish sensor directly to Home Assistant via MQTT discovery."""
pub_device_uuid = pub_device["id"].replace(":", "")
device_data = json.dumps(pub_device)

pub_device_copy = self.copy_pub_device(pub_device)

device_data = json.dumps(pub_device_copy)
if (
pub_device_uuid in self.discovered_entities
or pub_device["model_id"] in self.configuration["discovery_filter"]
Expand Down Expand Up @@ -251,3 +254,12 @@ def publish_device_tracker(
tracker["device"] = hadevice # type: ignore[assignment]

self.publish(json.dumps(tracker), config_topic, retain=True)

def copy_pub_device(self, device: dict) -> dict:
"""Copy pub_device and remove "track" if publish_advdata is false."""
pub_device_copy = device.copy()
# Remove "track" if PUBLISH_ADVDATA is 0
if not self.configuration["publish_advdata"] and "track" in pub_device_copy:
pub_device_copy.pop("track", None)

return pub_device_copy

0 comments on commit 2fd7940

Please sign in to comment.