async def test_async_remove_with_platform_update_finishes(hass): """Remove an entity when an update finishes after its been removed.""" component = EntityComponent(_LOGGER, DOMAIN, hass) entity1 = MockEntity(name="test_1") async def _delayed_update(*args, **kwargs): await asyncio.sleep(0.01) entity1.async_update = _delayed_update # Add, remove, add, remove and make sure no updates # cause the entity to reappear after removal for i in range(2): await component.async_add_entities([entity1]) assert len(hass.states.async_entity_ids()) == 1 entity1.async_write_ha_state() assert hass.states.get(entity1.entity_id) is not None task = asyncio.create_task(entity1.async_update_ha_state(True)) await entity1.async_remove() assert len(hass.states.async_entity_ids()) == 0 await task assert len(hass.states.async_entity_ids()) == 0
async def test_update_entity(hass): """Test that we can update an entity with the helper.""" component = EntityComponent(_LOGGER, DOMAIN, hass) entity = MockEntity() entity.async_write_ha_state = Mock() entity.async_update_ha_state = AsyncMock(return_value=None) await component.async_add_entities([entity]) # Called as part of async_add_entities assert len(entity.async_write_ha_state.mock_calls) == 1 await hass.helpers.entity_component.async_update_entity(entity.entity_id) assert len(entity.async_update_ha_state.mock_calls) == 1 assert entity.async_update_ha_state.mock_calls[-1][1][0] is True