Ensure wemo discovery is run in a background task (#112665)

This commit is contained in:
J. Nick Koston 2024-03-11 04:13:41 -10:00 committed by GitHub
parent e13d8200cc
commit 5e2edb6819
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -119,7 +119,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up a wemo config entry.""" """Set up a wemo config entry."""
wemo_data = async_wemo_data(hass) wemo_data = async_wemo_data(hass)
dispatcher = WemoDispatcher(entry) dispatcher = WemoDispatcher(entry)
discovery = WemoDiscovery(hass, dispatcher, wemo_data.static_config) discovery = WemoDiscovery(hass, dispatcher, wemo_data.static_config, entry)
wemo_data.config_entry_data = WemoConfigEntryData( wemo_data.config_entry_data = WemoConfigEntryData(
device_coordinators={}, device_coordinators={},
discovery=discovery, discovery=discovery,
@ -246,6 +246,7 @@ class WemoDiscovery:
hass: HomeAssistant, hass: HomeAssistant,
wemo_dispatcher: WemoDispatcher, wemo_dispatcher: WemoDispatcher,
static_config: Sequence[HostPortTuple], static_config: Sequence[HostPortTuple],
entry: ConfigEntry,
) -> None: ) -> None:
"""Initialize the WemoDiscovery.""" """Initialize the WemoDiscovery."""
self._hass = hass self._hass = hass
@ -253,7 +254,8 @@ class WemoDiscovery:
self._stop: CALLBACK_TYPE | None = None self._stop: CALLBACK_TYPE | None = None
self._scan_delay = 0 self._scan_delay = 0
self._static_config = static_config self._static_config = static_config
self._discover_job: HassJob[[datetime], Coroutine[Any, Any, None]] | None = None self._discover_job: HassJob[[datetime], None] | None = None
self._entry = entry
async def async_discover_and_schedule( async def async_discover_and_schedule(
self, event_time: datetime | None = None self, event_time: datetime | None = None
@ -274,13 +276,23 @@ class WemoDiscovery:
self.MAX_SECONDS_BETWEEN_SCANS, self.MAX_SECONDS_BETWEEN_SCANS,
) )
if not self._discover_job: if not self._discover_job:
self._discover_job = HassJob(self.async_discover_and_schedule) self._discover_job = HassJob(self._async_discover_and_schedule_callback)
self._stop = async_call_later( self._stop = async_call_later(
self._hass, self._hass,
self._scan_delay, self._scan_delay,
self._discover_job, self._discover_job,
) )
@callback
def _async_discover_and_schedule_callback(self, event_time: datetime) -> None:
"""Run the periodic background scanning."""
self._entry.async_create_background_task(
self._hass,
self.async_discover_and_schedule(),
name="wemo_discovery",
eager_start=True,
)
@callback @callback
def async_stop_discovery(self) -> None: def async_stop_discovery(self) -> None:
"""Stop the periodic background scanning.""" """Stop the periodic background scanning."""