diff --git a/homeassistant/components/zha/select.py b/homeassistant/components/zha/select.py index dfe9de24b40..fdb47b550fe 100644 --- a/homeassistant/components/zha/select.py +++ b/homeassistant/components/zha/select.py @@ -8,7 +8,7 @@ from typing import Any from homeassistant.components.select import SelectEntity from homeassistant.config_entries import ConfigEntry -from homeassistant.const import STATE_UNKNOWN, Platform +from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN, Platform from homeassistant.core import HomeAssistant, State, callback from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity_platform import AddEntitiesCallback @@ -69,7 +69,7 @@ class ZHAEnumSelectEntity(ZHAEntity, SelectEntity): @callback def restore_external_state_attributes(self, state: State) -> None: """Restore entity state.""" - if state.state and state.state != STATE_UNKNOWN: + if state.state and state.state not in (STATE_UNKNOWN, STATE_UNAVAILABLE): self.entity_data.entity.restore_external_state_attributes( state=state.state, ) diff --git a/tests/components/zha/test_select.py b/tests/components/zha/test_select.py index a39172b850e..f0f742503e3 100644 --- a/tests/components/zha/test_select.py +++ b/tests/components/zha/test_select.py @@ -13,12 +13,19 @@ from homeassistant.components.zha.helpers import ( get_zha_gateway, get_zha_gateway_proxy, ) -from homeassistant.const import STATE_UNKNOWN, EntityCategory, Platform -from homeassistant.core import HomeAssistant +from homeassistant.const import ( + STATE_UNAVAILABLE, + STATE_UNKNOWN, + EntityCategory, + Platform, +) +from homeassistant.core import HomeAssistant, State from homeassistant.helpers import entity_registry as er from .common import find_entity_id +from tests.common import mock_restore_cache + @pytest.fixture(autouse=True) def select_select_only(): @@ -103,3 +110,51 @@ async def test_select( state = hass.states.get(entity_id) assert state assert state.state == security.IasWd.Warning.WarningMode.Burglar.name + + +@pytest.mark.parametrize( + ("restored_state", "expected_state"), + [ + # Unavailable is not restored + (STATE_UNAVAILABLE, STATE_UNKNOWN), + # Normal state is + ( + security.IasWd.Warning.WarningMode.Burglar.name, + security.IasWd.Warning.WarningMode.Burglar.name, + ), + ], +) +async def test_select_restore_state( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + setup_zha, + zigpy_device_mock, + restored_state: str, + expected_state: str, +) -> None: + """Test ZHA select platform restore state.""" + entity_id = "select.fakemanufacturer_fakemodel_default_siren_tone" + + mock_restore_cache(hass, [State(entity_id, restored_state)]) + + await setup_zha() + + zigpy_device = zigpy_device_mock( + { + 1: { + SIG_EP_INPUT: [general.Basic.cluster_id, security.IasWd.cluster_id], + SIG_EP_OUTPUT: [], + SIG_EP_TYPE: zha.DeviceType.IAS_WARNING_DEVICE, + SIG_EP_PROFILE: zha.PROFILE_ID, + } + } + ) + + gateway = get_zha_gateway(hass) + gateway.get_or_create_device(zigpy_device) + await gateway.async_device_initialized(zigpy_device) + await hass.async_block_till_done(wait_background_tasks=True) + + state = hass.states.get(entity_id) + assert state + assert state.state == expected_state