async def test_attach_entity_component_collection(hass): """Test attaching collection to entity component.""" ent_comp = entity_component.EntityComponent(_LOGGER, "test", hass) coll = collection.ObservableCollection(_LOGGER) collection.sync_entity_lifecycle(hass, "test", "test", ent_comp, coll, MockEntity) await coll.notify_changes([ collection.CollectionChangeSet( collection.CHANGE_ADDED, "mock_id", { "id": "mock_id", "state": "initial", "name": "Mock 1" }, ) ], ) assert hass.states.get("test.mock_1").name == "Mock 1" assert hass.states.get("test.mock_1").state == "initial" await coll.notify_changes([ collection.CollectionChangeSet( collection.CHANGE_UPDATED, "mock_id", { "id": "mock_id", "state": "second", "name": "Mock 1 updated" }, ) ], ) assert hass.states.get("test.mock_1").name == "Mock 1 updated" assert hass.states.get("test.mock_1").state == "second" await coll.notify_changes([ collection.CollectionChangeSet(collection.CHANGE_REMOVED, "mock_id", None) ], ) assert hass.states.get("test.mock_1") is None
async def test_observable_collection(): """Test observerable collection.""" coll = collection.ObservableCollection(_LOGGER) assert coll.async_items() == [] coll.data["bla"] = 1 assert coll.async_items() == [1] changes = track_changes(coll) await coll.notify_changes( [collection.CollectionChangeSet("mock_type", "mock_id", {"mock": "item"})] ) assert len(changes) == 1 assert changes[0] == ("mock_type", "mock_id", {"mock": "item"})
async def test_entity_component_collection_entity_removed(hass): """Test entity removal is handled.""" ent_comp = entity_component.EntityComponent(_LOGGER, "test", hass) coll = collection.ObservableCollection(_LOGGER) async_update_config_calls = [] async_remove_calls = [] class MockMockEntity(MockEntity): """Track calls to async_update_config and async_remove.""" async def async_update_config(self, config): nonlocal async_update_config_calls async_update_config_calls.append(None) await super().async_update_config() async def async_remove(self, *, force_remove: bool = False): nonlocal async_remove_calls async_remove_calls.append(None) await super().async_remove() collection.sync_entity_lifecycle( hass, "test", "test", ent_comp, coll, MockMockEntity ) entity_registry = er.async_get(hass) entity_registry.async_get_or_create( "test", "test", "mock_id", suggested_object_id="mock_1" ) await coll.notify_changes( [ collection.CollectionChangeSet( collection.CHANGE_ADDED, "mock_id", {"id": "mock_id", "state": "initial", "name": "Mock 1"}, ) ], ) assert hass.states.get("test.mock_1").name == "Mock 1" assert hass.states.get("test.mock_1").state == "initial" entity_registry.async_remove("test.mock_1") await hass.async_block_till_done() assert hass.states.get("test.mock_1") is None assert len(async_remove_calls) == 1 await coll.notify_changes( [ collection.CollectionChangeSet( collection.CHANGE_UPDATED, "mock_id", {"id": "mock_id", "state": "second", "name": "Mock 1 updated"}, ) ], ) assert hass.states.get("test.mock_1") is None assert len(async_update_config_calls) == 0 await coll.notify_changes( [collection.CollectionChangeSet(collection.CHANGE_REMOVED, "mock_id", None)], ) assert hass.states.get("test.mock_1") is None assert len(async_remove_calls) == 1