From 2d0b11f18e8aad8187e2065977947656bc79d90d Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 20 Aug 2022 13:41:25 -1000 Subject: [PATCH] Add a new constant for multiple bluetooth watchdog failure hits (#77081) --- homeassistant/components/bluetooth/scanner.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/bluetooth/scanner.py b/homeassistant/components/bluetooth/scanner.py index ad6341910dd..96a0fe572c6 100644 --- a/homeassistant/components/bluetooth/scanner.py +++ b/homeassistant/components/bluetooth/scanner.py @@ -56,6 +56,15 @@ SCANNING_MODE_TO_BLEAK = { BluetoothScanningMode.PASSIVE: "passive", } +# The minimum number of seconds to know +# the adapter has not had advertisements +# and we already tried to restart the scanner +# without success when the first time the watch +# dog hit the failure path. +SCANNER_WATCHDOG_MULTIPLE = ( + SCANNER_WATCHDOG_TIMEOUT + SCANNER_WATCHDOG_INTERVAL.total_seconds() +) + class ScannerStartError(HomeAssistantError): """Error to indicate that the scanner failed to start.""" @@ -276,9 +285,13 @@ class HaScanner: # Stop the scanner but not the watchdog # since we want to try again later if it's still quiet await self._async_stop_scanner() - if self._start_time == self._last_detection or ( - time_since_last_detection - ) > (SCANNER_WATCHDOG_TIMEOUT + SCANNER_WATCHDOG_INTERVAL.total_seconds()): + # If there have not been any valid advertisements, + # or the watchdog has hit the failure path multiple times, + # do the reset. + if ( + self._start_time == self._last_detection + or time_since_last_detection > SCANNER_WATCHDOG_MULTIPLE + ): await self._async_reset_adapter() try: await self._async_start()