From 426e0f696be3ffb1480c81883fd45b17bbdb18dc Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Wed, 7 Oct 2026 11:50:48 +0200 Subject: [PATCH] Keep updating collection helper entities after an entity ID change (#184631) --- homeassistant/helpers/collection.py | 14 +++++++++- tests/helpers/test_collection.py | 43 +++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/homeassistant/helpers/collection.py b/homeassistant/helpers/collection.py index 07d089b11592..4e985f6349cc 100644 --- a/homeassistant/helpers/collection.py +++ b/homeassistant/helpers/collection.py @@ -489,8 +489,20 @@ class _CollectionLifeCycle[_EntityT: Entity = Entity]: # the entity registry event handled by Entity._async_registry_updated entities.pop(item_id, None) + def _get_entity(self, item_id: str) -> CollectionEntity | None: + """Return the live entity for an item.""" + # Changing the entity ID removes and re-adds the entity, which drops it + # from self.entities, so look it up through the registry first + if entity_id := self.ent_reg.async_get_entity_id( + self.domain, self.platform, item_id + ): + entity = self.entity_component.get_entity(entity_id) + return entity if isinstance(entity, CollectionEntity) else None + + return self.entities.get(item_id) + async def _update_entity(self, change_set: CollectionChange) -> None: - if entity := self.entities.get(change_set.item_id): + if entity := self._get_entity(change_set.item_id): if change_set.item_hash: self.ent_reg.async_update_entity_options( entity.entity_id, "collection", {"hash": change_set.item_hash} diff --git a/tests/helpers/test_collection.py b/tests/helpers/test_collection.py index 9d78b5202051..afc8f16ead9a 100644 --- a/tests/helpers/test_collection.py +++ b/tests/helpers/test_collection.py @@ -373,6 +373,49 @@ async def test_attach_entity_component_collection(hass: HomeAssistant) -> None: assert hass.states.get("test.mock_1") is None +async def test_entity_component_collection_update_after_entity_id_change( + hass: HomeAssistant, entity_registry: er.EntityRegistry +) -> None: + """Test updates still reach the entity after its entity ID changed.""" + ent_comp = entity_component.EntityComponent(_LOGGER, "test", hass) + await ent_comp.async_setup({}) + coll = MockObservableCollection(None) + collection.sync_entity_lifecycle(hass, "test", "test", ent_comp, coll, MockEntity) + + await coll.notify_changes( + [ + collection.CollectionChange( + collection.CHANGE_ADDED, + "mock_id", + {"id": "mock_id", "state": "initial", "name": "Mock 1"}, + ) + ], + ) + + entity_registry.async_update_entity("test.mock_1", new_entity_id="test.renamed") + await hass.async_block_till_done() + assert hass.states.get("test.renamed").state == "initial" + + await coll.notify_changes( + [ + collection.CollectionChange( + collection.CHANGE_UPDATED, + "mock_id", + {"id": "mock_id", "state": "second", "name": "Mock 1 updated"}, + ) + ], + ) + + assert hass.states.get("test.renamed").name == "Mock 1 updated" + assert hass.states.get("test.renamed").state == "second" + + await coll.notify_changes( + [collection.CollectionChange(collection.CHANGE_REMOVED, "mock_id", None)], + ) + + assert hass.states.get("test.renamed") is None + + async def test_entity_component_collection_abort( hass: HomeAssistant, entity_registry: er.EntityRegistry ) -> None: