mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
bosch_shc: add alarm control panel platform (#184472)
Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
4685252c81
commit
f07b42444b
@@ -16,6 +16,7 @@ from homeassistant.helpers import device_registry as dr
|
||||
from .const import CONF_SSL_CERTIFICATE, CONF_SSL_KEY, DOMAIN
|
||||
|
||||
PLATFORMS = [
|
||||
Platform.ALARM_CONTROL_PANEL,
|
||||
Platform.BINARY_SENSOR,
|
||||
Platform.BUTTON,
|
||||
Platform.COVER,
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
"""Platform for alarm control panel integration."""
|
||||
|
||||
from typing import override
|
||||
|
||||
from boschshcpy import SHCIntrusionSystem
|
||||
|
||||
from homeassistant.components.alarm_control_panel import (
|
||||
AlarmControlPanelEntity,
|
||||
AlarmControlPanelEntityFeature,
|
||||
AlarmControlPanelState,
|
||||
CodeFormat,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from . import BoschConfigEntry
|
||||
from .const import DOMAIN
|
||||
|
||||
PARALLEL_UPDATES = 1
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: BoschConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the SHC alarm control panel platform."""
|
||||
session = config_entry.runtime_data
|
||||
async_add_entities(
|
||||
[
|
||||
IntrusionSystemAlarmControlPanel(
|
||||
hass=hass,
|
||||
device=session.intrusion_system,
|
||||
entry_id=config_entry.entry_id,
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
class IntrusionSystemAlarmControlPanel(AlarmControlPanelEntity):
|
||||
"""Representation of the SHC intrusion detection system."""
|
||||
|
||||
_attr_should_poll = False
|
||||
_attr_has_entity_name = True
|
||||
_attr_name = None
|
||||
_attr_code_format: CodeFormat | None = None
|
||||
_attr_code_arm_required = False
|
||||
_attr_supported_features = (
|
||||
AlarmControlPanelEntityFeature.ARM_AWAY
|
||||
| AlarmControlPanelEntityFeature.ARM_HOME
|
||||
| AlarmControlPanelEntityFeature.ARM_CUSTOM_BYPASS
|
||||
)
|
||||
|
||||
def __init__(
|
||||
self, hass: HomeAssistant, device: SHCIntrusionSystem, entry_id: str
|
||||
) -> None:
|
||||
"""Initialize the intrusion detection system."""
|
||||
self._device = device
|
||||
self._attr_unique_id = f"{device.root_device_id}_{device.id}"
|
||||
device_info = DeviceInfo(
|
||||
identifiers={(DOMAIN, self._attr_unique_id)},
|
||||
translation_key="intrusion_system",
|
||||
manufacturer=device.manufacturer,
|
||||
model=device.device_model,
|
||||
)
|
||||
if device.root_device_id is not None and (
|
||||
hub := dr.async_get(hass).async_get_device_by_identifier(
|
||||
(DOMAIN, device.root_device_id), entry_id
|
||||
)
|
||||
):
|
||||
device_info["via_device_id"] = hub.id
|
||||
self._attr_device_info = device_info
|
||||
|
||||
@override
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""Subscribe to SHC events."""
|
||||
await super().async_added_to_hass()
|
||||
|
||||
def on_state_changed() -> None:
|
||||
self.schedule_update_ha_state()
|
||||
|
||||
self._device.subscribe_callback(self.entity_id, on_state_changed)
|
||||
|
||||
@override
|
||||
async def async_will_remove_from_hass(self) -> None:
|
||||
"""Unsubscribe from SHC events."""
|
||||
await super().async_will_remove_from_hass()
|
||||
self._device.unsubscribe_callback(self.entity_id)
|
||||
|
||||
@property
|
||||
@override
|
||||
def available(self) -> bool:
|
||||
"""Return false if the system is unavailable."""
|
||||
return bool(self._device.system_availability)
|
||||
|
||||
@property
|
||||
@override
|
||||
def alarm_state(self) -> AlarmControlPanelState | None:
|
||||
"""Return the state of the alarm system."""
|
||||
alarm = self._device.alarm_state
|
||||
if alarm in (
|
||||
SHCIntrusionSystem.AlarmState.ALARM_ON,
|
||||
SHCIntrusionSystem.AlarmState.ALARM_MUTED,
|
||||
):
|
||||
return AlarmControlPanelState.TRIGGERED
|
||||
if alarm is SHCIntrusionSystem.AlarmState.PRE_ALARM:
|
||||
return AlarmControlPanelState.PENDING
|
||||
|
||||
arming = self._device.arming_state
|
||||
if arming is SHCIntrusionSystem.ArmingState.SYSTEM_ARMING:
|
||||
return AlarmControlPanelState.ARMING
|
||||
if arming is SHCIntrusionSystem.ArmingState.SYSTEM_DISARMED:
|
||||
return AlarmControlPanelState.DISARMED
|
||||
|
||||
profile = self._device.active_configuration_profile
|
||||
if profile is SHCIntrusionSystem.Profile.FULL_PROTECTION:
|
||||
return AlarmControlPanelState.ARMED_AWAY
|
||||
if profile is SHCIntrusionSystem.Profile.PARTIAL_PROTECTION:
|
||||
return AlarmControlPanelState.ARMED_HOME
|
||||
# Custom or unrecognized profiles must not be reported as unknown
|
||||
# while the system is armed.
|
||||
return AlarmControlPanelState.ARMED_CUSTOM_BYPASS
|
||||
|
||||
@override
|
||||
def alarm_disarm(self, code: str | None = None) -> None:
|
||||
"""Disarm the system."""
|
||||
self._device.disarm()
|
||||
|
||||
@override
|
||||
def alarm_arm_away(self, code: str | None = None) -> None:
|
||||
"""Arm the system with full protection."""
|
||||
self._device.arm_full_protection()
|
||||
|
||||
@override
|
||||
def alarm_arm_home(self, code: str | None = None) -> None:
|
||||
"""Arm the system with partial protection."""
|
||||
self._device.arm_partial_protection()
|
||||
|
||||
@override
|
||||
def alarm_arm_custom_bypass(self, code: str | None = None) -> None:
|
||||
"""Arm the system with individual protection."""
|
||||
self._device.arm_individual_protection()
|
||||
@@ -42,6 +42,11 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"device": {
|
||||
"intrusion_system": {
|
||||
"name": "Alarm system"
|
||||
}
|
||||
},
|
||||
"entity": {
|
||||
"button": {
|
||||
"reset_tamper": {
|
||||
|
||||
@@ -20,6 +20,7 @@ from boschshcpy import (
|
||||
SHCCamera360,
|
||||
SHCCameraEyes,
|
||||
SHCCameraOutdoorGen2,
|
||||
SHCIntrusionSystem,
|
||||
SHCLightSwitchBSM,
|
||||
SHCMicromoduleBlinds,
|
||||
SHCMicromoduleRelay,
|
||||
@@ -138,6 +139,7 @@ def mock_session(device_buckets: dict[str, Any]) -> Generator[MagicMock]:
|
||||
session.information.updateState.name = "UP_TO_DATE"
|
||||
session.information.version = "2.0"
|
||||
session.device_helper = SimpleNamespace(**device_buckets)
|
||||
session.intrusion_system = intrusion_system_device()
|
||||
with patch("homeassistant.components.bosch_shc.SHCSession", return_value=session):
|
||||
yield session
|
||||
|
||||
@@ -416,6 +418,26 @@ def thermostat_gen2_device(
|
||||
return device
|
||||
|
||||
|
||||
def intrusion_system_device(
|
||||
arming_state: SHCIntrusionSystem.ArmingState = SHCIntrusionSystem.ArmingState.SYSTEM_DISARMED,
|
||||
alarm_state: SHCIntrusionSystem.AlarmState = SHCIntrusionSystem.AlarmState.ALARM_OFF,
|
||||
profile: SHCIntrusionSystem.Profile = SHCIntrusionSystem.Profile.FULL_PROTECTION,
|
||||
) -> SHCIntrusionSystem:
|
||||
"""Build a minimal double for session.intrusion_system."""
|
||||
device = create_autospec(SHCIntrusionSystem, instance=True, spec_set=True)
|
||||
device.id = "com.bosch.tt.intrusion.system"
|
||||
device.name = "Intrusion Detection System"
|
||||
device.root_device_id = "test-mac"
|
||||
device.manufacturer = "Bosch"
|
||||
device.device_model = "IDS"
|
||||
device.deleted = False
|
||||
device.system_availability = True
|
||||
device.arming_state = arming_state
|
||||
device.alarm_state = alarm_state
|
||||
device.active_configuration_profile = profile
|
||||
return device
|
||||
|
||||
|
||||
def micromodule_relay_device(
|
||||
device_id: str = "hdm:ZigBee:relay1",
|
||||
name: str = "Relay",
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
"""Tests for the bosch_shc alarm control panel platform."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from boschshcpy import SHCIntrusionSystem
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.alarm_control_panel import (
|
||||
DOMAIN as ALARM_DOMAIN,
|
||||
AlarmControlPanelState,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
SERVICE_ALARM_ARM_AWAY,
|
||||
SERVICE_ALARM_ARM_CUSTOM_BYPASS,
|
||||
SERVICE_ALARM_ARM_HOME,
|
||||
SERVICE_ALARM_DISARM,
|
||||
Platform,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .conftest import intrusion_system_device, setup_integration
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
ENTITY_ID = "alarm_control_panel.alarm_system"
|
||||
ArmingState = SHCIntrusionSystem.ArmingState
|
||||
AlarmState = SHCIntrusionSystem.AlarmState
|
||||
Profile = SHCIntrusionSystem.Profile
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def platforms() -> Generator[None]:
|
||||
"""Restrict bosch_shc setup to the alarm control panel platform."""
|
||||
with patch(
|
||||
"homeassistant.components.bosch_shc.PLATFORMS", [Platform.ALARM_CONTROL_PANEL]
|
||||
):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("kwargs", "expected"),
|
||||
[
|
||||
({}, AlarmControlPanelState.DISARMED),
|
||||
(
|
||||
{"arming_state": ArmingState.SYSTEM_ARMING},
|
||||
AlarmControlPanelState.ARMING,
|
||||
),
|
||||
(
|
||||
{"arming_state": ArmingState.SYSTEM_ARMED},
|
||||
AlarmControlPanelState.ARMED_AWAY,
|
||||
),
|
||||
(
|
||||
{
|
||||
"arming_state": ArmingState.SYSTEM_ARMED,
|
||||
"profile": Profile.PARTIAL_PROTECTION,
|
||||
},
|
||||
AlarmControlPanelState.ARMED_HOME,
|
||||
),
|
||||
(
|
||||
{
|
||||
"arming_state": ArmingState.SYSTEM_ARMED,
|
||||
"profile": Profile.CUSTOM_PROTECTION,
|
||||
},
|
||||
AlarmControlPanelState.ARMED_CUSTOM_BYPASS,
|
||||
),
|
||||
(
|
||||
{
|
||||
"arming_state": ArmingState.SYSTEM_ARMED,
|
||||
"profile": Profile.UNKNOWN,
|
||||
},
|
||||
AlarmControlPanelState.ARMED_CUSTOM_BYPASS,
|
||||
),
|
||||
(
|
||||
{"alarm_state": AlarmState.PRE_ALARM},
|
||||
AlarmControlPanelState.PENDING,
|
||||
),
|
||||
(
|
||||
{"alarm_state": AlarmState.ALARM_ON},
|
||||
AlarmControlPanelState.TRIGGERED,
|
||||
),
|
||||
(
|
||||
{"alarm_state": AlarmState.ALARM_MUTED},
|
||||
AlarmControlPanelState.TRIGGERED,
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_alarm_state(
|
||||
hass: HomeAssistant,
|
||||
mock_session: MagicMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
kwargs: dict,
|
||||
expected: AlarmControlPanelState,
|
||||
) -> None:
|
||||
"""The SHC arming, alarm and profile state map to the panel state."""
|
||||
mock_session.intrusion_system = intrusion_system_device(**kwargs)
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
assert state is not None
|
||||
assert state.state == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("service", "method"),
|
||||
[
|
||||
(SERVICE_ALARM_DISARM, "disarm"),
|
||||
(SERVICE_ALARM_ARM_AWAY, "arm_full_protection"),
|
||||
(SERVICE_ALARM_ARM_HOME, "arm_partial_protection"),
|
||||
(SERVICE_ALARM_ARM_CUSTOM_BYPASS, "arm_individual_protection"),
|
||||
],
|
||||
)
|
||||
async def test_services(
|
||||
hass: HomeAssistant,
|
||||
mock_session: MagicMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
service: str,
|
||||
method: str,
|
||||
) -> None:
|
||||
"""Each alarm service calls the matching SHC command."""
|
||||
device = intrusion_system_device()
|
||||
mock_session.intrusion_system = device
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
await hass.services.async_call(
|
||||
ALARM_DOMAIN, service, {ATTR_ENTITY_ID: ENTITY_ID}, blocking=True
|
||||
)
|
||||
|
||||
getattr(device, method).assert_called_once_with()
|
||||
|
||||
|
||||
async def test_unavailable(
|
||||
hass: HomeAssistant,
|
||||
mock_session: MagicMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""The panel is unavailable when the system reports unavailable."""
|
||||
device = intrusion_system_device()
|
||||
device.system_availability = False
|
||||
mock_session.intrusion_system = device
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
assert hass.states.get(ENTITY_ID).state == "unavailable"
|
||||
|
||||
|
||||
async def test_push_update(
|
||||
hass: HomeAssistant,
|
||||
mock_session: MagicMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""A pushed SHC state change updates the panel state."""
|
||||
device = intrusion_system_device()
|
||||
mock_session.intrusion_system = device
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
assert hass.states.get(ENTITY_ID).state == AlarmControlPanelState.DISARMED
|
||||
|
||||
device.alarm_state = AlarmState.ALARM_ON
|
||||
device.subscribe_callback.call_args.args[1]()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get(ENTITY_ID).state == AlarmControlPanelState.TRIGGERED
|
||||
Reference in New Issue
Block a user