Avoid creating tasks to shutdown entity platforms (#111026)

* Avoid creating tasks to shutdown entity platforms

Nothing needed to be awaited here

* fix mocking

* missed one test
This commit is contained in:
J. Nick Koston 2024-02-20 20:10:25 -06:00 committed by GitHub
parent 98d5f2fc01
commit dc4008c518
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 10 additions and 9 deletions

View file

@ -5,7 +5,6 @@ import asyncio
from collections.abc import Callable, Iterable from collections.abc import Callable, Iterable
from datetime import timedelta from datetime import timedelta
from functools import partial from functools import partial
from itertools import chain
import logging import logging
from types import ModuleType from types import ModuleType
from typing import Any, Generic from typing import Any, Generic
@ -394,8 +393,8 @@ class EntityComponent(Generic[_EntityT]):
entity_platform.async_prepare() entity_platform.async_prepare()
return entity_platform return entity_platform
async def _async_shutdown(self, event: Event) -> None: @callback
def _async_shutdown(self, event: Event) -> None:
"""Call when Home Assistant is stopping.""" """Call when Home Assistant is stopping."""
await asyncio.gather( for platform in self._platforms.values():
*(platform.async_shutdown() for platform in chain(self._platforms.values())) platform.async_shutdown()
)

View file

@ -284,7 +284,8 @@ class EntityPlatform:
await self._async_setup_platform(async_create_setup_task) await self._async_setup_platform(async_create_setup_task)
async def async_shutdown(self) -> None: @callback
def async_shutdown(self) -> None:
"""Call when Home Assistant is stopping.""" """Call when Home Assistant is stopping."""
self.async_cancel_retry_setup() self.async_cancel_retry_setup()
self.async_unsub_polling() self.async_unsub_polling()

View file

@ -885,8 +885,9 @@ class MockEntityPlatform(entity_platform.EntityPlatform):
entity_namespace=entity_namespace, entity_namespace=entity_namespace,
) )
async def _async_on_stop(_: Event) -> None: @callback
await self.async_shutdown() def _async_on_stop(_: Event) -> None:
self.async_shutdown()
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_on_stop) hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_on_stop)

View file

@ -979,7 +979,7 @@ async def test_stop_shutdown_cancels_retry_setup_and_interval_listener(
assert len(mock_call_later.return_value.mock_calls) == 0 assert len(mock_call_later.return_value.mock_calls) == 0
assert ent_platform._async_cancel_retry_setup is not None assert ent_platform._async_cancel_retry_setup is not None
await ent_platform.async_shutdown() ent_platform.async_shutdown()
assert len(mock_call_later.return_value.mock_calls) == 1 assert len(mock_call_later.return_value.mock_calls) == 1
assert ent_platform._async_unsub_polling is None assert ent_platform._async_unsub_polling is None