diff --git a/homeassistant/components/scene/__init__.py b/homeassistant/components/scene/__init__.py index 846c0fbc7c68d60b4a5f7a7e2a005912e17f263a..5dea5965d439cc4ad67660fa5d50e7c326da380a 100644 --- a/homeassistant/components/scene/__init__.py +++ b/homeassistant/components/scene/__init__.py @@ -10,7 +10,7 @@ import voluptuous as vol from homeassistant.components.light import ATTR_TRANSITION from homeassistant.config_entries import ConfigEntry -from homeassistant.const import CONF_PLATFORM, SERVICE_TURN_ON +from homeassistant.const import CONF_PLATFORM, SERVICE_TURN_ON, STATE_UNAVAILABLE from homeassistant.core import DOMAIN as HA_DOMAIN, HomeAssistant from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.restore_state import RestoreEntity @@ -117,7 +117,11 @@ class Scene(RestoreEntity): """Call when the scene is added to hass.""" await super().async_internal_added_to_hass() state = await self.async_get_last_state() - if state is not None and state.state is not None: + if ( + state is not None + and state.state is not None + and state.state != STATE_UNAVAILABLE + ): self.__last_activated = state.state def activate(self, **kwargs: Any) -> None: diff --git a/tests/components/scene/test_init.py b/tests/components/scene/test_init.py index 41b16261cd1ac165506f8a9360bef192fce1e403..3dd0cfce7b97bc199827ca65e412f9cf9b7463c1 100644 --- a/tests/components/scene/test_init.py +++ b/tests/components/scene/test_init.py @@ -9,6 +9,7 @@ from homeassistant.const import ( ATTR_ENTITY_ID, ENTITY_MATCH_ALL, SERVICE_TURN_ON, + STATE_UNAVAILABLE, STATE_UNKNOWN, ) from homeassistant.core import State @@ -177,6 +178,34 @@ async def test_restore_state(hass, entities, enable_custom_integrations): assert hass.states.get("scene.test").state == "2021-01-01T23:59:59+00:00" +async def test_restore_state_does_not_restore_unavailable( + hass, entities, enable_custom_integrations +): + """Test we restore state integration but ignore unavailable.""" + mock_restore_cache(hass, (State("scene.test", STATE_UNAVAILABLE),)) + + light_1, light_2 = await setup_lights(hass, entities) + + assert await async_setup_component( + hass, + scene.DOMAIN, + { + "scene": [ + { + "name": "test", + "entities": { + light_1.entity_id: "on", + light_2.entity_id: "on", + }, + } + ] + }, + ) + await hass.async_block_till_done() + + assert hass.states.get("scene.test").state == STATE_UNKNOWN + + async def activate(hass, entity_id=ENTITY_MATCH_ALL): """Activate a scene.""" data = {}