From fd8a165c97b60bae0d52b7541a37a902f5cda763 Mon Sep 17 00:00:00 2001 From: honzup <5564623+honzup@users.noreply.github.com> Date: Tue, 8 Sep 2026 10:43:45 +0200 Subject: [PATCH] Tolerate unloading a never-loaded config entry in EntityComponent (#176594) Co-authored-by: Claude Fable 5 Co-authored-by: Erik Montnemery --- homeassistant/helpers/entity_component.py | 13 ++++++++++++- tests/helpers/test_entity_component.py | 14 ++++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/homeassistant/helpers/entity_component.py b/homeassistant/helpers/entity_component.py index d1989bb8377a..9f8bbcc0837b 100644 --- a/homeassistant/helpers/entity_component.py +++ b/homeassistant/helpers/entity_component.py @@ -198,7 +198,18 @@ class EntityComponent[_EntityT: entity.Entity = entity.Entity]: key = config_entry.entry_id if (platform := self._platforms.pop(key, None)) is None: - raise ValueError("Config entry was never loaded!") + self.logger.warning( + ( + "Ignored unload request for config entry %s (%s) in %s.%s; " + "no platform is loaded, it was never set up " + "or has already been unloaded" + ), + config_entry.title, + key, + config_entry.domain, + self.domain, + ) + return True await platform.async_reset() return True diff --git a/tests/helpers/test_entity_component.py b/tests/helpers/test_entity_component.py index 0fe257e3166b..64efde060592 100644 --- a/tests/helpers/test_entity_component.py +++ b/tests/helpers/test_entity_component.py @@ -420,13 +420,19 @@ async def test_unload_entry_resets_platform(hass: HomeAssistant) -> None: assert len(hass.states.async_entity_ids()) == 0 -async def test_unload_entry_fails_if_never_loaded(hass: HomeAssistant) -> None: - """.""" +async def test_unload_entry_tolerates_never_loaded( + hass: HomeAssistant, caplog: pytest.LogCaptureFixture +) -> None: + """Test unloading an entry that was never loaded succeeds with a warning.""" component = EntityComponent(_LOGGER, DOMAIN, hass) entry = MockConfigEntry(domain="entry_domain") - with pytest.raises(ValueError): - await component.async_unload_entry(entry) + assert await component.async_unload_entry(entry) + assert ( + f"Ignored unload request for config entry Mock Title ({entry.entry_id}) " + f"in entry_domain.{DOMAIN}; no platform is loaded, it was never set up " + "or has already been unloaded" + ) in caplog.text async def test_update_entity(hass: HomeAssistant) -> None: