Tolerate unloading a never-loaded config entry in EntityComponent (#176594)

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
honzup
2026-09-08 10:43:45 +02:00
committed by GitHub
co-authored by Claude Fable 5 Erik Montnemery
parent 08c4f686bf
commit fd8a165c97
2 changed files with 22 additions and 5 deletions
+12 -1
View File
@@ -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
+10 -4
View File
@@ -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: