Home Assistant Unofficial Reference 2024.12.1
alarm_control_panel.py
Go to the documentation of this file.
1 """Support for Homekit Alarm Control Panel."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from aiohomekit.model.characteristics import CharacteristicsTypes
8 from aiohomekit.model.services import Service, ServicesTypes
9 
11  AlarmControlPanelEntity,
12  AlarmControlPanelEntityFeature,
13  AlarmControlPanelState,
14 )
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.const import ATTR_BATTERY_LEVEL, Platform
17 from homeassistant.core import HomeAssistant, callback
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 
20 from . import KNOWN_DEVICES
21 from .connection import HKDevice
22 from .entity import HomeKitEntity
23 
24 CURRENT_STATE_MAP = {
25  0: AlarmControlPanelState.ARMED_HOME,
26  1: AlarmControlPanelState.ARMED_AWAY,
27  2: AlarmControlPanelState.ARMED_NIGHT,
28  3: AlarmControlPanelState.DISARMED,
29  4: AlarmControlPanelState.TRIGGERED,
30 }
31 
32 TARGET_STATE_MAP = {
33  AlarmControlPanelState.ARMED_HOME: 0,
34  AlarmControlPanelState.ARMED_AWAY: 1,
35  AlarmControlPanelState.ARMED_NIGHT: 2,
36  AlarmControlPanelState.DISARMED: 3,
37 }
38 
39 
41  hass: HomeAssistant,
42  config_entry: ConfigEntry,
43  async_add_entities: AddEntitiesCallback,
44 ) -> None:
45  """Set up Homekit alarm control panel."""
46  hkid: str = config_entry.data["AccessoryPairingID"]
47  conn: HKDevice = hass.data[KNOWN_DEVICES][hkid]
48 
49  @callback
50  def async_add_service(service: Service) -> bool:
51  if service.type != ServicesTypes.SECURITY_SYSTEM:
52  return False
53  info = {"aid": service.accessory.aid, "iid": service.iid}
54  entity = HomeKitAlarmControlPanelEntity(conn, info)
55  conn.async_migrate_unique_id(
56  entity.old_unique_id, entity.unique_id, Platform.ALARM_CONTROL_PANEL
57  )
58  async_add_entities([entity])
59  return True
60 
61  conn.add_listener(async_add_service)
62 
63 
65  """Representation of a Homekit Alarm Control Panel."""
66 
67  _attr_supported_features = (
68  AlarmControlPanelEntityFeature.ARM_HOME
69  | AlarmControlPanelEntityFeature.ARM_AWAY
70  | AlarmControlPanelEntityFeature.ARM_NIGHT
71  )
72 
73  def get_characteristic_types(self) -> list[str]:
74  """Define the homekit characteristics the entity cares about."""
75  return [
76  CharacteristicsTypes.SECURITY_SYSTEM_STATE_CURRENT,
77  CharacteristicsTypes.SECURITY_SYSTEM_STATE_TARGET,
78  CharacteristicsTypes.BATTERY_LEVEL,
79  ]
80 
81  @property
82  def alarm_state(self) -> AlarmControlPanelState:
83  """Return the state of the device."""
84  return CURRENT_STATE_MAP[
85  self.serviceservice.value(CharacteristicsTypes.SECURITY_SYSTEM_STATE_CURRENT)
86  ]
87 
88  async def async_alarm_disarm(self, code: str | None = None) -> None:
89  """Send disarm command."""
90  await self.set_alarm_stateset_alarm_state(AlarmControlPanelState.DISARMED, code)
91 
92  async def async_alarm_arm_away(self, code: str | None = None) -> None:
93  """Send arm command."""
94  await self.set_alarm_stateset_alarm_state(AlarmControlPanelState.ARMED_AWAY, code)
95 
96  async def async_alarm_arm_home(self, code: str | None = None) -> None:
97  """Send stay command."""
98  await self.set_alarm_stateset_alarm_state(AlarmControlPanelState.ARMED_HOME, code)
99 
100  async def async_alarm_arm_night(self, code: str | None = None) -> None:
101  """Send night command."""
102  await self.set_alarm_stateset_alarm_state(AlarmControlPanelState.ARMED_NIGHT, code)
103 
104  async def set_alarm_state(
105  self, state: AlarmControlPanelState, code: str | None = None
106  ) -> None:
107  """Send state command."""
108  await self.async_put_characteristicsasync_put_characteristics(
109  {CharacteristicsTypes.SECURITY_SYSTEM_STATE_TARGET: TARGET_STATE_MAP[state]}
110  )
111 
112  @property
113  def extra_state_attributes(self) -> dict[str, Any] | None:
114  """Return the optional state attributes."""
115  battery_level = self.serviceservice.value(CharacteristicsTypes.BATTERY_LEVEL)
116 
117  if not battery_level:
118  return {}
119  return {ATTR_BATTERY_LEVEL: battery_level}
None async_put_characteristics(self, dict[str, Any] characteristics)
Definition: entity.py:125
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)