Skip to content
Snippets Groups Projects
Commit 9dff7ab6 authored by J. Nick Koston's avatar J. Nick Koston Committed by Paulus Schoutsen
Browse files

Adjust time to remove stale connectable devices from the esphome ble to closer match bluez (#81356)

parent d0ddbb5f
No related branches found
No related tags found
No related merge requests found
...@@ -6,6 +6,7 @@ import datetime ...@@ -6,6 +6,7 @@ import datetime
from datetime import timedelta from datetime import timedelta
import re import re
import time import time
from typing import Final
from aioesphomeapi import BluetoothLEAdvertisement from aioesphomeapi import BluetoothLEAdvertisement
from bleak.backends.device import BLEDevice from bleak.backends.device import BLEDevice
...@@ -23,6 +24,15 @@ from homeassistant.util.dt import monotonic_time_coarse ...@@ -23,6 +24,15 @@ from homeassistant.util.dt import monotonic_time_coarse
TWO_CHAR = re.compile("..") TWO_CHAR = re.compile("..")
# The maximum time between advertisements for a device to be considered
# stale when the advertisement tracker can determine the interval for
# connectable devices.
#
# BlueZ uses 180 seconds by default but we give it a bit more time
# to account for the esp32's bluetooth stack being a bit slower
# than BlueZ's.
CONNECTABLE_FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS: Final = 195
class ESPHomeScanner(BaseHaScanner): class ESPHomeScanner(BaseHaScanner):
"""Scanner for esphome.""" """Scanner for esphome."""
...@@ -45,8 +55,12 @@ class ESPHomeScanner(BaseHaScanner): ...@@ -45,8 +55,12 @@ class ESPHomeScanner(BaseHaScanner):
self._connector = connector self._connector = connector
self._connectable = connectable self._connectable = connectable
self._details: dict[str, str | HaBluetoothConnector] = {"source": scanner_id} self._details: dict[str, str | HaBluetoothConnector] = {"source": scanner_id}
self._fallback_seconds = FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS
if connectable: if connectable:
self._details["connector"] = connector self._details["connector"] = connector
self._fallback_seconds = (
CONNECTABLE_FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS
)
@callback @callback
def async_setup(self) -> CALLBACK_TYPE: def async_setup(self) -> CALLBACK_TYPE:
...@@ -61,7 +75,7 @@ class ESPHomeScanner(BaseHaScanner): ...@@ -61,7 +75,7 @@ class ESPHomeScanner(BaseHaScanner):
expired = [ expired = [
address address
for address, timestamp in self._discovered_device_timestamps.items() for address, timestamp in self._discovered_device_timestamps.items()
if now - timestamp > FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS if now - timestamp > self._fallback_seconds
] ]
for address in expired: for address in expired:
del self._discovered_device_advertisement_datas[address] del self._discovered_device_advertisement_datas[address]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment