From ec4b8d6ea95e7101ea77e36e346c32ee022b9171 Mon Sep 17 00:00:00 2001 From: DigiH Date: Sun, 21 Jan 2024 13:12:53 +0100 Subject: [PATCH] General Presence implementation General Presence with present/absent messages - Discovery independent --- TheengsGateway/ble_gateway.py | 31 +++++++++++++++++++++++++++---- TheengsGateway/config.py | 7 +++++++ docs/use/use.md | 6 +++++- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/TheengsGateway/ble_gateway.py b/TheengsGateway/ble_gateway.py index bcf333ce..750c6ebb 100644 --- a/TheengsGateway/ble_gateway.py +++ b/TheengsGateway/ble_gateway.py @@ -338,21 +338,25 @@ async def update_clock_times(self) -> None: def check_tracker_timeout(self) -> None: """Check if tracker timeout is over timeout limit.""" + # If the timestamp is later than current time minus tracker_timeout + # Publish offline message for address, timestamp in self.discovered_trackers.copy().items(): if ( round(time()) - timestamp >= self.configuration["tracker_timeout"] and timestamp != 0 + and ( + self.configuration["discovery"] + or self.configuration["general_presence"] + ) ): - # If the timestamp is later than current time minus tracker_timeout - # Publish offline message - message = json.dumps({"id": address, "state": "offline"}) + message = json.dumps({"id": address, "presence": "absent"}) + self.publish( message, self.configuration["publish_topic"] + "/" + address.replace(":", ""), ) - self.discovered_trackers[address] = 0 async def ble_scan_loop(self) -> None: @@ -546,6 +550,25 @@ def publish_json( decoded: bool, # noqa: FBT001 ) -> None: """Publish JSON data to MQTT.""" + # publish general presence "present" if tracker and general_presence true + if "track" in data_json: + if ( + data_json["id"] not in self.discovered_trackers + or ( + data_json["id"] in self.discovered_trackers + and self.discovered_trackers[str(data_json["id"])] == 0 + ) + ) and self.configuration["general_presence"]: + message = json.dumps({"id": data_json["id"], "presence": "present"}) + self.publish( + message, + self.configuration["publish_topic"] + + "/" + + get_address(data_json).replace(":", ""), + ) + self.discovered_trackers[str(data_json["id"])] = round(time()) + logger.debug("Discovered Trackers: %s", self.discovered_trackers) + # Remove "track" if PUBLISH_ADVDATA is 0 if not self.configuration["publish_advdata"] and "track" in data_json: data_json.pop("track", None) diff --git a/TheengsGateway/config.py b/TheengsGateway/config.py index d3f6c1ef..544ed4e5 100644 --- a/TheengsGateway/config.py +++ b/TheengsGateway/config.py @@ -31,6 +31,7 @@ "log_level": "INFO", "discovery": 1, "hass_discovery": 1, + "general_presence": 0, "discovery_topic": "homeassistant", "discovery_device_name": "TheengsGateway", "discovery_filter": [ @@ -111,6 +112,12 @@ def parse_args() -> argparse.Namespace: type=str, help="MQTT Discovery topic", ) + parser.add_argument( + "-Gp", + "--general_presence", + type=int, + help="Enable (1) or disable (0) general present/absent presence when --discovery: 0 (default: 0)", # noqa: E501 + ) parser.add_argument( "-H", "--host", diff --git a/docs/use/use.md b/docs/use/use.md index e5d2c1d7..29fa60c8 100644 --- a/docs/use/use.md +++ b/docs/use/use.md @@ -96,7 +96,8 @@ usage: TheengsGateway [-h] [-a ADAPTER] [-b BLE] [-bk ADDRESS [BINDKEY ...]] [-c CONFIG] [-D DISCOVERY] [-Df DISCOVERY_FILTER [DISCOVERY_FILTER ...]] [-Dh HASS_DISCOVERY] [-Dn DISCOVERY_NAME] - [-Dt DISCOVERY_TOPIC] [-H HOST] [-id ADDRESS [IRK ...]] + [-Dt DISCOVERY_TOPIC] [-Gp GENERAL_PRESENCE] [-H HOST] + [-id ADDRESS [IRK ...]] [-Lt LWT_TOPIC] [-ll {DEBUG,INFO,WARNING,ERROR,CRITICAL}] [-P PORT] [-p PASS] [-pa PUBLISH_ALL] [-padv PUBLISH_ADVDATA] [-pr PRESENCE] [-prt PRESENCE_TOPIC] [-pt PUB_TOPIC] @@ -125,6 +126,8 @@ options: Device name for Home Assistant -Dt DISCOVERY_TOPIC, --discovery-topic DISCOVERY_TOPIC MQTT Discovery topic + -Gp GENERAL_PRESENCE, --general_presence GENERAL_PRESENCE + Enable (1) or disable (0) general present/absent presence when --discovery: 0 -H HOST, --host HOST MQTT host address -id ADDRESS [IRK ...], --identities ADDRESS [IRK ...] Identity addresses and their IRKs: ADDR1 IRK1 ADDR2 IRK2 @@ -184,6 +187,7 @@ docker run --rm \ -e SCAN_TIME=60 \ -e LOG_LEVEL=INFO \ -e HAAS_DISCOVERY=true \ + -e GENERAL_PRESENCE=false \ -e DISCOVERY=true \ -e DISCOVERY_TOPIC=homeassistant \ -e DISCOVERY_DEVICE_NAME=TheengsGateway \