From dc4008c5189ee3245733ab343b3ea739e89a8fb1 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 20 Feb 2024 20:10:25 -0600 Subject: [PATCH] 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 --- homeassistant/helpers/entity_component.py | 9 ++++----- homeassistant/helpers/entity_platform.py | 3 ++- tests/common.py | 5 +++-- tests/helpers/test_entity_platform.py | 2 +- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/homeassistant/helpers/entity_component.py b/homeassistant/helpers/entity_component.py index 5020c5c4271..d748cf5ed86 100644 --- a/homeassistant/helpers/entity_component.py +++ b/homeassistant/helpers/entity_component.py @@ -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() diff --git a/homeassistant/helpers/entity_platform.py b/homeassistant/helpers/entity_platform.py index 9b05e4939ba..6759a0f8369 100644 --- a/homeassistant/helpers/entity_platform.py +++ b/homeassistant/helpers/entity_platform.py @@ -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() diff --git a/tests/common.py b/tests/common.py index 3ed865af00b..f1ed002bf8e 100644 --- a/tests/common.py +++ b/tests/common.py @@ -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) diff --git a/tests/helpers/test_entity_platform.py b/tests/helpers/test_entity_platform.py index f16b5c16b5a..dace6515a38 100644 --- a/tests/helpers/test_entity_platform.py +++ b/tests/helpers/test_entity_platform.py @@ -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