diff --git a/TheengsGateway/ble_gateway.py b/TheengsGateway/ble_gateway.py index cfb7c4d3..bccaf612 100644 --- a/TheengsGateway/ble_gateway.py +++ b/TheengsGateway/ble_gateway.py @@ -70,7 +70,6 @@ "mic", "servicedata", "servicedatauuid", - "track", ) LOG_LEVEL = { @@ -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, diff --git a/TheengsGateway/discovery.py b/TheengsGateway/discovery.py index 54cd1290..cc888b45 100644 --- a/TheengsGateway/discovery.py +++ b/TheengsGateway/discovery.py @@ -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"] @@ -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