mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Keep updating collection helper entities after an entity ID change (#184631)
This commit is contained in:
@@ -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}
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user