Fix ZHA select platform state restoration (#121646)
* Ensure `select` entities do not restore state if they were unavailable * Add a unit test
This commit is contained in:
parent
c81d5a1ac2
commit
020961d2d8
2 changed files with 59 additions and 4 deletions
|
@ -8,7 +8,7 @@ from typing import Any
|
||||||
|
|
||||||
from homeassistant.components.select import SelectEntity
|
from homeassistant.components.select import SelectEntity
|
||||||
from homeassistant.config_entries import ConfigEntry
|
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.core import HomeAssistant, State, callback
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
@ -69,7 +69,7 @@ class ZHAEnumSelectEntity(ZHAEntity, SelectEntity):
|
||||||
@callback
|
@callback
|
||||||
def restore_external_state_attributes(self, state: State) -> None:
|
def restore_external_state_attributes(self, state: State) -> None:
|
||||||
"""Restore entity state."""
|
"""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(
|
self.entity_data.entity.restore_external_state_attributes(
|
||||||
state=state.state,
|
state=state.state,
|
||||||
)
|
)
|
||||||
|
|
|
@ -13,12 +13,19 @@ from homeassistant.components.zha.helpers import (
|
||||||
get_zha_gateway,
|
get_zha_gateway,
|
||||||
get_zha_gateway_proxy,
|
get_zha_gateway_proxy,
|
||||||
)
|
)
|
||||||
from homeassistant.const import STATE_UNKNOWN, EntityCategory, Platform
|
from homeassistant.const import (
|
||||||
from homeassistant.core import HomeAssistant
|
STATE_UNAVAILABLE,
|
||||||
|
STATE_UNKNOWN,
|
||||||
|
EntityCategory,
|
||||||
|
Platform,
|
||||||
|
)
|
||||||
|
from homeassistant.core import HomeAssistant, State
|
||||||
from homeassistant.helpers import entity_registry as er
|
from homeassistant.helpers import entity_registry as er
|
||||||
|
|
||||||
from .common import find_entity_id
|
from .common import find_entity_id
|
||||||
|
|
||||||
|
from tests.common import mock_restore_cache
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
def select_select_only():
|
def select_select_only():
|
||||||
|
@ -103,3 +110,51 @@ async def test_select(
|
||||||
state = hass.states.get(entity_id)
|
state = hass.states.get(entity_id)
|
||||||
assert state
|
assert state
|
||||||
assert state.state == security.IasWd.Warning.WarningMode.Burglar.name
|
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
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue