Add tests of legacy entity without platform writing state (#148109)

This commit is contained in:
Erik Montnemery 2025-07-04 16:21:48 +02:00 committed by GitHub
parent cde17fc0ca
commit 8ce30d9559
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2713,6 +2713,41 @@ async def test_platform_state(
assert hass.states.get("test.test") is None 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( async def test_platform_state_fail_to_add(
hass: HomeAssistant, entity_registry: er.EntityRegistry hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None: ) -> None: