Fix Homekit error handling alarm state unknown or unavailable (#130311)

This commit is contained in:
G Johansson 2024-11-10 23:40:23 +01:00 committed by GitHub
parent e040eb0ff2
commit 85bf8d1374
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 43 additions and 6 deletions

View File

@ -18,6 +18,8 @@ from homeassistant.const import (
SERVICE_ALARM_ARM_HOME,
SERVICE_ALARM_ARM_NIGHT,
SERVICE_ALARM_DISARM,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
)
from homeassistant.core import State, callback
@ -152,12 +154,12 @@ class SecuritySystem(HomeAccessory):
@callback
def async_update_state(self, new_state: State) -> None:
"""Update security state after state changed."""
hass_state = None
if new_state and new_state.state == "None":
# Bail out early for no state
hass_state: str | AlarmControlPanelState = new_state.state
if hass_state in {"None", STATE_UNKNOWN, STATE_UNAVAILABLE}:
# Bail out early for no state, unknown or unavailable
return
if new_state and new_state.state is not None:
hass_state = AlarmControlPanelState(new_state.state)
if hass_state is not None:
hass_state = AlarmControlPanelState(hass_state)
if (
hass_state
and (current_state := HASS_TO_HOMEKIT_CURRENT.get(hass_state)) is not None

View File

@ -10,7 +10,12 @@ from homeassistant.components.alarm_control_panel import (
)
from homeassistant.components.homekit.const import ATTR_VALUE
from homeassistant.components.homekit.type_security_systems import SecuritySystem
from homeassistant.const import ATTR_CODE, ATTR_ENTITY_ID, STATE_UNKNOWN
from homeassistant.const import (
ATTR_CODE,
ATTR_ENTITY_ID,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
)
from homeassistant.core import Event, HomeAssistant
from tests.common import async_mock_service
@ -307,3 +312,33 @@ async def test_supported_states(hass: HomeAssistant, hk_driver) -> None:
for val in valid_target_values.values():
assert val in test_config.get("target_values")
@pytest.mark.parametrize(
("state"),
[
(None),
("None"),
(STATE_UNKNOWN),
(STATE_UNAVAILABLE),
],
)
async def test_handle_non_alarm_states(
hass: HomeAssistant, hk_driver, events: list[Event], state: str
) -> None:
"""Test we can handle states that should not raise."""
code = "1234"
config = {ATTR_CODE: code}
entity_id = "alarm_control_panel.test"
hass.states.async_set(entity_id, state)
await hass.async_block_till_done()
acc = SecuritySystem(hass, hk_driver, "SecuritySystem", entity_id, 2, config)
acc.run()
await hass.async_block_till_done()
assert acc.aid == 2
assert acc.category == 11 # AlarmSystem
assert acc.char_current_state.value == 3
assert acc.char_target_state.value == 3