Skip to content

Commit

Permalink
feat: improve logging of scanner failures and time_since_last_detecti…
Browse files Browse the repository at this point in the history
…on (#155)
  • Loading branch information
bdraco authored Feb 22, 2025
1 parent 1d30743 commit f0ff045
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/habluetooth/base_scanner.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ cdef class BaseHaScanner:

cpdef tuple get_discovered_device_advertisement_data(self, str address)

cpdef float time_since_last_detection(self)


cdef class BaseHaRemoteScanner(BaseHaScanner):

Expand Down
11 changes: 8 additions & 3 deletions src/habluetooth/base_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ def __init__(
adapter=self.adapter,
)

def time_since_last_detection(self) -> float:
"""Return the time since the last detection."""
return monotonic_time_coarse() - self._last_detection

def async_setup(self) -> CALLBACK_TYPE:
"""Set up the scanner."""
self._loop = asyncio.get_running_loop()
Expand Down Expand Up @@ -123,7 +127,7 @@ def _async_call_scanner_watchdog(self) -> None:

def _async_watchdog_triggered(self) -> bool:
"""Check if the watchdog has been triggered."""
time_since_last_detection = monotonic_time_coarse() - self._last_detection
time_since_last_detection = self.time_since_last_detection()
_LOGGER.debug(
"%s: Scanner watchdog time_since_last_detection: %s",
self.name,
Expand All @@ -139,13 +143,14 @@ def _async_scanner_watchdog(self) -> None:
is triggered.
"""
if self._async_watchdog_triggered():
_LOGGER.info(
_LOGGER.log(
logging.WARNING if self.scanning else logging.DEBUG,
(
"%s: Bluetooth scanner has gone quiet for %ss, check logs on the"
" scanner device for more information"
),
self.name,
SCANNER_WATCHDOG_TIMEOUT,
self.time_since_last_detection(),
)
self.scanning = False
return
Expand Down
8 changes: 4 additions & 4 deletions src/habluetooth/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,10 +508,11 @@ def _async_scanner_watchdog(self) -> None:
self.name,
)
return
_LOGGER.info(
_LOGGER.log(
logging.WARNING if self.scanning else logging.DEBUG,
"%s: Bluetooth scanner has gone quiet for %ss, restarting",
self.name,
SCANNER_WATCHDOG_TIMEOUT,
self.time_since_last_detection(),
)
# Immediately mark the scanner as not scanning
# since the restart task will have to wait for the lock
Expand All @@ -521,7 +522,6 @@ def _async_scanner_watchdog(self) -> None:
async def _async_restart_scanner(self) -> None:
"""Restart the scanner."""
async with self._start_stop_lock:
time_since_last_detection = monotonic_time_coarse() - self._last_detection
# Stop the scanner but not the watchdog
# since we want to try again later if it's still quiet
await self._async_stop_scanner()
Expand All @@ -530,7 +530,7 @@ async def _async_restart_scanner(self) -> None:
# do the reset.
if (
self._start_time == self._last_detection
or time_since_last_detection > SCANNER_WATCHDOG_MULTIPLE
or self.time_since_last_detection() > SCANNER_WATCHDOG_MULTIPLE
):
await self._async_reset_adapter()
try:
Expand Down

0 comments on commit f0ff045

Please sign in to comment.