From 94d1375be3895c1076833cebb445250e74ea0ff7 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 27 Dec 2022 16:00:24 -1000 Subject: [PATCH] Avoid creating a task in the bluetooth watchdog when everything is OK (#84669) --- homeassistant/components/bluetooth/base_scanner.py | 3 ++- homeassistant/components/bluetooth/scanner.py | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/bluetooth/base_scanner.py b/homeassistant/components/bluetooth/base_scanner.py index a4c3af2398d..d522d69cdbe 100644 --- a/homeassistant/components/bluetooth/base_scanner.py +++ b/homeassistant/components/bluetooth/base_scanner.py @@ -103,7 +103,8 @@ class BaseHaScanner(ABC): ) 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. Override this method if you need to do something else when the watchdog is triggered. diff --git a/homeassistant/components/bluetooth/scanner.py b/homeassistant/components/bluetooth/scanner.py index da2a4d930f0..a80386c25ef 100644 --- a/homeassistant/components/bluetooth/scanner.py +++ b/homeassistant/components/bluetooth/scanner.py @@ -309,7 +309,8 @@ class HaScanner(BaseHaScanner): self._async_setup_scanner_watchdog() 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.""" if not self._async_watchdog_triggered(): return @@ -324,6 +325,10 @@ class HaScanner(BaseHaScanner): self.name, 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: time_since_last_detection = MONOTONIC_TIME() - self._last_detection # Stop the scanner but not the watchdog