Skip to content

Commit f0ff045

Browse files
authored
feat: improve logging of scanner failures and time_since_last_detection (#155)
1 parent 1d30743 commit f0ff045

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

src/habluetooth/base_scanner.pxd

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ cdef class BaseHaScanner:
3030

3131
cpdef tuple get_discovered_device_advertisement_data(self, str address)
3232

33+
cpdef float time_since_last_detection(self)
34+
3335

3436
cdef class BaseHaRemoteScanner(BaseHaScanner):
3537

src/habluetooth/base_scanner.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ def __init__(
8888
adapter=self.adapter,
8989
)
9090

91+
def time_since_last_detection(self) -> float:
92+
"""Return the time since the last detection."""
93+
return monotonic_time_coarse() - self._last_detection
94+
9195
def async_setup(self) -> CALLBACK_TYPE:
9296
"""Set up the scanner."""
9397
self._loop = asyncio.get_running_loop()
@@ -123,7 +127,7 @@ def _async_call_scanner_watchdog(self) -> None:
123127

124128
def _async_watchdog_triggered(self) -> bool:
125129
"""Check if the watchdog has been triggered."""
126-
time_since_last_detection = monotonic_time_coarse() - self._last_detection
130+
time_since_last_detection = self.time_since_last_detection()
127131
_LOGGER.debug(
128132
"%s: Scanner watchdog time_since_last_detection: %s",
129133
self.name,
@@ -139,13 +143,14 @@ def _async_scanner_watchdog(self) -> None:
139143
is triggered.
140144
"""
141145
if self._async_watchdog_triggered():
142-
_LOGGER.info(
146+
_LOGGER.log(
147+
logging.WARNING if self.scanning else logging.DEBUG,
143148
(
144149
"%s: Bluetooth scanner has gone quiet for %ss, check logs on the"
145150
" scanner device for more information"
146151
),
147152
self.name,
148-
SCANNER_WATCHDOG_TIMEOUT,
153+
self.time_since_last_detection(),
149154
)
150155
self.scanning = False
151156
return

src/habluetooth/scanner.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -508,10 +508,11 @@ def _async_scanner_watchdog(self) -> None:
508508
self.name,
509509
)
510510
return
511-
_LOGGER.info(
511+
_LOGGER.log(
512+
logging.WARNING if self.scanning else logging.DEBUG,
512513
"%s: Bluetooth scanner has gone quiet for %ss, restarting",
513514
self.name,
514-
SCANNER_WATCHDOG_TIMEOUT,
515+
self.time_since_last_detection(),
515516
)
516517
# Immediately mark the scanner as not scanning
517518
# since the restart task will have to wait for the lock
@@ -521,7 +522,6 @@ def _async_scanner_watchdog(self) -> None:
521522
async def _async_restart_scanner(self) -> None:
522523
"""Restart the scanner."""
523524
async with self._start_stop_lock:
524-
time_since_last_detection = monotonic_time_coarse() - self._last_detection
525525
# Stop the scanner but not the watchdog
526526
# since we want to try again later if it's still quiet
527527
await self._async_stop_scanner()
@@ -530,7 +530,7 @@ async def _async_restart_scanner(self) -> None:
530530
# do the reset.
531531
if (
532532
self._start_time == self._last_detection
533-
or time_since_last_detection > SCANNER_WATCHDOG_MULTIPLE
533+
or self.time_since_last_detection() > SCANNER_WATCHDOG_MULTIPLE
534534
):
535535
await self._async_reset_adapter()
536536
try:

0 commit comments

Comments
 (0)