Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General Presence implementation #233

Merged
merged 1 commit into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions TheengsGateway/ble_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions TheengsGateway/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"log_level": "INFO",
"discovery": 1,
"hass_discovery": 1,
"general_presence": 0,
"discovery_topic": "homeassistant",
"discovery_device_name": "TheengsGateway",
"discovery_filter": [
Expand Down Expand Up @@ -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",
Expand Down
6 changes: 5 additions & 1 deletion docs/use/use.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 \
Expand Down
Loading