From 8ce30d9559ee3042acac2d23739e5d34dc74ce84 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Fri, 4 Jul 2025 16:21:48 +0200 Subject: [PATCH] Add tests of legacy entity without platform writing state (#148109) --- tests/helpers/test_entity.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/helpers/test_entity.py b/tests/helpers/test_entity.py index 24205870779..30b25e9725d 100644 --- a/tests/helpers/test_entity.py +++ b/tests/helpers/test_entity.py @@ -2713,6 +2713,41 @@ async def test_platform_state( assert hass.states.get("test.test") is None +async def test_platform_state_no_platform(hass: HomeAssistant) -> None: + """Test platform state for entities which are not added by an entity platform.""" + + class MockEntity(entity.Entity): + entity_id = "test.test" + + def async_set_state(self, state: str) -> None: + self._attr_state = state + self.async_write_ha_state() + + ent = MockEntity() + ent.hass = hass + assert hass.states.get("test.test") is None + + # The attempt to write when in state NOT_ADDED should be allowed + assert ent._platform_state == entity.EntityPlatformState.NOT_ADDED + ent.async_set_state("not_added") + assert hass.states.get("test.test").state == "not_added" + + # The attempt to write when in state ADDING should be allowed + ent._platform_state = entity.EntityPlatformState.ADDING + ent.async_set_state("adding") + assert hass.states.get("test.test").state == "adding" + + # The attempt to write when in state ADDED should be allowed + ent._platform_state = entity.EntityPlatformState.ADDED + ent.async_set_state("added") + assert hass.states.get("test.test").state == "added" + + # The attempt to write when in state REMOVED should be ignored + ent._platform_state = entity.EntityPlatformState.REMOVED + ent.async_set_state("removed") + assert hass.states.get("test.test").state == "added" + + async def test_platform_state_fail_to_add( hass: HomeAssistant, entity_registry: er.EntityRegistry ) -> None: