Convert debouncer async_shutdown to be a normal function (#111257)

* Convert debouncer async_shutdown to be a normal function

nothing was being awaited here and the shutdown call was only used
in integrations marked internal and other internals. Its possible
that a custom component might have been using the method but it
seemed uncommon enough that it did not warrent marking as a breaking
change. The update coordinator is no longer awaiting anything in
async_shutdown either now but it seemed likely that this use
would get subclassed.

* fix
This commit is contained in:
J. Nick Koston 2024-02-23 21:37:33 -10:00 committed by GitHub
parent e0490a3ade
commit ff0e0b3e77
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 20 additions and 14 deletions

View file

@ -173,9 +173,10 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
function=_async_rediscover_adapters,
)
async def _async_shutdown_debouncer(_: Event) -> None:
@hass_callback
def _async_shutdown_debouncer(_: Event) -> None:
"""Shutdown debouncer."""
await discovery_debouncer.async_shutdown()
discovery_debouncer.async_shutdown()
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_shutdown_debouncer)

View file

@ -93,9 +93,10 @@ class DebouncedEntryReloader:
LOGGER.debug("Calling debouncer to get a reload after cooldown")
await self._debounced_reload.async_call()
async def async_shutdown(self) -> None:
@callback
def async_shutdown(self) -> None:
"""Cancel any pending reload."""
await self._debounced_reload.async_shutdown()
self._debounced_reload.async_shutdown()
async def _async_reload_entry(self) -> None:
"""Reload entry."""

View file

@ -213,10 +213,11 @@ class USBDiscovery:
"""Start USB Discovery and run a manual scan."""
await self._async_scan_serial()
async def async_stop(self, event: Event) -> None:
@hass_callback
def async_stop(self, event: Event) -> None:
"""Stop USB Discovery."""
if self._request_debouncer:
await self._request_debouncer.async_shutdown()
self._request_debouncer.async_shutdown()
async def _async_start_monitor(self) -> None:
"""Start monitoring hardware with pyudev."""

View file

@ -1052,12 +1052,13 @@ class ConfigEntriesFlowManager(data_entry_flow.FlowManager):
init_done.set_result(None)
return flow, result
async def async_shutdown(self) -> None:
@callback
def async_shutdown(self) -> None:
"""Cancel any initializing flows."""
for future_list in self._initialize_futures.values():
for future in future_list:
future.set_result(None)
await self._discovery_debouncer.async_shutdown()
self._discovery_debouncer.async_shutdown()
async def async_finish_flow(
self, flow: data_entry_flow.FlowHandler, result: data_entry_flow.FlowResult
@ -1418,11 +1419,12 @@ class ConfigEntries:
self._async_dispatch(ConfigEntryChange.REMOVED, entry)
return {"require_restart": not unload_success}
async def _async_shutdown(self, event: Event) -> None:
@callback
def _async_shutdown(self, event: Event) -> None:
"""Call when Home Assistant is stopping."""
for entry in self._entries.values():
entry.async_shutdown()
await self.flow.async_shutdown()
self.flow.async_shutdown()
async def async_initialize(self) -> None:
"""Initialize config entry config."""

View file

@ -137,7 +137,8 @@ class Debouncer(Generic[_R_co]):
# Schedule a new timer to prevent new runs during cooldown
self._schedule_timer()
async def async_shutdown(self) -> None:
@callback
def async_shutdown(self) -> None:
"""Cancel any scheduled call, and prevent new runs."""
self._shutdown_requested = True
self.async_cancel()

View file

@ -187,7 +187,7 @@ class DataUpdateCoordinator(BaseDataUpdateCoordinatorProtocol, Generic[_DataT]):
self._shutdown_requested = True
self._async_unsub_refresh()
self._async_unsub_shutdown()
await self._debounced_refresh.async_shutdown()
self._debounced_refresh.async_shutdown()
@callback
def _unschedule_refresh(self) -> None:

View file

@ -484,7 +484,7 @@ async def test_shutdown(hass: HomeAssistant, caplog: pytest.LogCaptureFixture) -
# Ensure shutdown during a run doesn't create a cooldown timer
hass.async_create_task(debouncer.async_call())
await asyncio.sleep(0.01)
await debouncer.async_shutdown()
debouncer.async_shutdown()
future.set_result(True)
await hass.async_block_till_done()
assert len(calls) == 1

View file

@ -4199,7 +4199,7 @@ async def test_initializing_flows_canceled_on_shutdown(
manager.flow.async_init("test", context={"source": "reauth"})
)
await hass.async_block_till_done()
await manager.flow.async_shutdown()
manager.flow.async_shutdown()
with pytest.raises(asyncio.exceptions.CancelledError):
await task