Skip to content

Commit 85bf8d1

Browse files
Fix Homekit error handling alarm state unknown or unavailable (#130311)
1 parent e040eb0 commit 85bf8d1

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

homeassistant/components/homekit/type_security_systems.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
SERVICE_ALARM_ARM_HOME,
1919
SERVICE_ALARM_ARM_NIGHT,
2020
SERVICE_ALARM_DISARM,
21+
STATE_UNAVAILABLE,
22+
STATE_UNKNOWN,
2123
)
2224
from homeassistant.core import State, callback
2325

@@ -152,12 +154,12 @@ def set_security_state(self, value: int) -> None:
152154
@callback
153155
def async_update_state(self, new_state: State) -> None:
154156
"""Update security state after state changed."""
155-
hass_state = None
156-
if new_state and new_state.state == "None":
157-
# Bail out early for no state
157+
hass_state: str | AlarmControlPanelState = new_state.state
158+
if hass_state in {"None", STATE_UNKNOWN, STATE_UNAVAILABLE}:
159+
# Bail out early for no state, unknown or unavailable
158160
return
159-
if new_state and new_state.state is not None:
160-
hass_state = AlarmControlPanelState(new_state.state)
161+
if hass_state is not None:
162+
hass_state = AlarmControlPanelState(hass_state)
161163
if (
162164
hass_state
163165
and (current_state := HASS_TO_HOMEKIT_CURRENT.get(hass_state)) is not None

tests/components/homekit/test_type_security_systems.py

+36-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
)
1111
from homeassistant.components.homekit.const import ATTR_VALUE
1212
from homeassistant.components.homekit.type_security_systems import SecuritySystem
13-
from homeassistant.const import ATTR_CODE, ATTR_ENTITY_ID, STATE_UNKNOWN
13+
from homeassistant.const import (
14+
ATTR_CODE,
15+
ATTR_ENTITY_ID,
16+
STATE_UNAVAILABLE,
17+
STATE_UNKNOWN,
18+
)
1419
from homeassistant.core import Event, HomeAssistant
1520

1621
from tests.common import async_mock_service
@@ -307,3 +312,33 @@ async def test_supported_states(hass: HomeAssistant, hk_driver) -> None:
307312

308313
for val in valid_target_values.values():
309314
assert val in test_config.get("target_values")
315+
316+
317+
@pytest.mark.parametrize(
318+
("state"),
319+
[
320+
(None),
321+
("None"),
322+
(STATE_UNKNOWN),
323+
(STATE_UNAVAILABLE),
324+
],
325+
)
326+
async def test_handle_non_alarm_states(
327+
hass: HomeAssistant, hk_driver, events: list[Event], state: str
328+
) -> None:
329+
"""Test we can handle states that should not raise."""
330+
code = "1234"
331+
config = {ATTR_CODE: code}
332+
entity_id = "alarm_control_panel.test"
333+
334+
hass.states.async_set(entity_id, state)
335+
await hass.async_block_till_done()
336+
acc = SecuritySystem(hass, hk_driver, "SecuritySystem", entity_id, 2, config)
337+
acc.run()
338+
await hass.async_block_till_done()
339+
340+
assert acc.aid == 2
341+
assert acc.category == 11 # AlarmSystem
342+
343+
assert acc.char_current_state.value == 3
344+
assert acc.char_target_state.value == 3

0 commit comments

Comments
 (0)