Avoid creating a task in the bluetooth watchdog when everything is OK (#84669)

This commit is contained in:
J. Nick Koston 2022-12-27 16:00:24 -10:00 committed by GitHub
parent 4296f227cf
commit 94d1375be3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 2 deletions

View File

@ -103,7 +103,8 @@ class BaseHaScanner(ABC):
) )
return time_since_last_detection > SCANNER_WATCHDOG_TIMEOUT return time_since_last_detection > SCANNER_WATCHDOG_TIMEOUT
async def _async_scanner_watchdog(self, now: datetime.datetime) -> None: @hass_callback
def _async_scanner_watchdog(self, now: datetime.datetime) -> None:
"""Check if the scanner is running. """Check if the scanner is running.
Override this method if you need to do something else when the watchdog is triggered. Override this method if you need to do something else when the watchdog is triggered.

View File

@ -309,7 +309,8 @@ class HaScanner(BaseHaScanner):
self._async_setup_scanner_watchdog() self._async_setup_scanner_watchdog()
await restore_discoveries(self.scanner, self.adapter) await restore_discoveries(self.scanner, self.adapter)
async def _async_scanner_watchdog(self, now: datetime) -> None: @hass_callback
def _async_scanner_watchdog(self, now: datetime) -> None:
"""Check if the scanner is running.""" """Check if the scanner is running."""
if not self._async_watchdog_triggered(): if not self._async_watchdog_triggered():
return return
@ -324,6 +325,10 @@ class HaScanner(BaseHaScanner):
self.name, self.name,
SCANNER_WATCHDOG_TIMEOUT, SCANNER_WATCHDOG_TIMEOUT,
) )
self.hass.async_create_task(self._async_restart_scanner())
async def _async_restart_scanner(self) -> None:
"""Restart the scanner."""
async with self._start_stop_lock: async with self._start_stop_lock:
time_since_last_detection = MONOTONIC_TIME() - self._last_detection time_since_last_detection = MONOTONIC_TIME() - self._last_detection
# Stop the scanner but not the watchdog # Stop the scanner but not the watchdog