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
pull/109093/head
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 datetime import timedelta
from functools import partial
from itertools import chain
import logging
from types import ModuleType
from typing import Any, Generic
@ -394,8 +393,8 @@ class EntityComponent(Generic[_EntityT]):
entity_platform.async_prepare()
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."""
await asyncio.gather(
*(platform.async_shutdown() for platform in chain(self._platforms.values()))
)
for platform in self._platforms.values():
platform.async_shutdown()

View File

@ -284,7 +284,8 @@ class EntityPlatform:
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."""
self.async_cancel_retry_setup()
self.async_unsub_polling()

View File

@ -885,8 +885,9 @@ class MockEntityPlatform(entity_platform.EntityPlatform):
entity_namespace=entity_namespace,
)
async def _async_on_stop(_: Event) -> None:
await self.async_shutdown()
@callback
def _async_on_stop(_: Event) -> None:
self.async_shutdown()
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 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 ent_platform._async_unsub_polling is None