Skip to content

Commit

Permalink
Fix Homekit error handling alarm state unknown or unavailable (#130311)
Browse files Browse the repository at this point in the history
  • Loading branch information
gjohansson-ST authored Nov 10, 2024
1 parent e040eb0 commit 85bf8d1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
12 changes: 7 additions & 5 deletions homeassistant/components/homekit/type_security_systems.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
SERVICE_ALARM_ARM_HOME,
SERVICE_ALARM_ARM_NIGHT,
SERVICE_ALARM_DISARM,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
)
from homeassistant.core import State, callback

Expand Down Expand Up @@ -152,12 +154,12 @@ def set_security_state(self, value: int) -> None:
@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
Expand Down
37 changes: 36 additions & 1 deletion tests/components/homekit/test_type_security_systems.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
)
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
Expand Down Expand Up @@ -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

0 comments on commit 85bf8d1

Please sign in to comment.