From 89049bc0222cd3de998a2a59a893457abc26bbd4 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 8 May 2024 16:59:37 -0500 Subject: [PATCH] Fix config entry _async_process_on_unload being called for forwarded platforms (#117084) --- homeassistant/components/wemo/__init__.py | 4 ++++ homeassistant/components/wemo/wemo_device.py | 2 ++ homeassistant/config_entries.py | 13 +++++++------ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/wemo/__init__.py b/homeassistant/components/wemo/__init__.py index 7d068cbd5bf..97c487fc41d 100644 --- a/homeassistant/components/wemo/__init__.py +++ b/homeassistant/components/wemo/__init__.py @@ -144,6 +144,10 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: dispatcher = wemo_data.config_entry_data.dispatcher if unload_ok := await dispatcher.async_unload_platforms(hass): + for coordinator in list( + wemo_data.config_entry_data.device_coordinators.values() + ): + await coordinator.async_shutdown() assert not wemo_data.config_entry_data.device_coordinators wemo_data.config_entry_data = None # type: ignore[assignment] return unload_ok diff --git a/homeassistant/components/wemo/wemo_device.py b/homeassistant/components/wemo/wemo_device.py index 8b99203e280..fcecf1027a6 100644 --- a/homeassistant/components/wemo/wemo_device.py +++ b/homeassistant/components/wemo/wemo_device.py @@ -142,6 +142,8 @@ class DeviceCoordinator(DataUpdateCoordinator[None]): # pylint: disable=hass-en async def async_shutdown(self) -> None: """Unregister push subscriptions and remove from coordinators dict.""" + if self._shutdown_requested: + return await super().async_shutdown() if TYPE_CHECKING: # mypy doesn't known that the device_id is set in async_setup. diff --git a/homeassistant/config_entries.py b/homeassistant/config_entries.py index 3741f6638b5..de0fda400b2 100644 --- a/homeassistant/config_entries.py +++ b/homeassistant/config_entries.py @@ -753,7 +753,7 @@ class ConfigEntry(Generic[_DataT]): component = await integration.async_get_component() - if integration.domain == self.domain: + if domain_is_integration := self.domain == integration.domain: if not self.state.recoverable: return False @@ -765,7 +765,7 @@ class ConfigEntry(Generic[_DataT]): supports_unload = hasattr(component, "async_unload_entry") if not supports_unload: - if integration.domain == self.domain: + if domain_is_integration: self._async_set_state( hass, ConfigEntryState.FAILED_UNLOAD, "Unload not supported" ) @@ -777,15 +777,16 @@ class ConfigEntry(Generic[_DataT]): assert isinstance(result, bool) # Only adjust state if we unloaded the component - if result and integration.domain == self.domain: - self._async_set_state(hass, ConfigEntryState.NOT_LOADED, None) + if domain_is_integration: + if result: + self._async_set_state(hass, ConfigEntryState.NOT_LOADED, None) - await self._async_process_on_unload(hass) + await self._async_process_on_unload(hass) except Exception as exc: _LOGGER.exception( "Error unloading entry %s for %s", self.title, integration.domain ) - if integration.domain == self.domain: + if domain_is_integration: self._async_set_state( hass, ConfigEntryState.FAILED_UNLOAD, str(exc) or "Unknown error" )