Home Assistant Unofficial Reference 2024.12.1
alarm_control_panel.py
Go to the documentation of this file.
1 """Module for SIA Alarm Control Panels."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 import logging
7 from typing import TYPE_CHECKING
8 
9 from pysiaalarm import SIAEvent
10 
12  AlarmControlPanelEntity,
13  AlarmControlPanelEntityDescription,
14  AlarmControlPanelState,
15 )
16 from homeassistant.config_entries import ConfigEntry
17 from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN
18 from homeassistant.core import HomeAssistant, State
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 
21 from .const import CONF_ACCOUNT, CONF_ACCOUNTS, CONF_ZONES, KEY_ALARM, PREVIOUS_STATE
22 from .entity import SIABaseEntity, SIAEntityDescription
23 
24 _LOGGER = logging.getLogger(__name__)
25 
26 
27 @dataclass(frozen=True)
29  AlarmControlPanelEntityDescription,
30  SIAEntityDescription,
31 ):
32  """Describes SIA alarm control panel entity."""
33 
34 
35 ENTITY_DESCRIPTION_ALARM = SIAAlarmControlPanelEntityDescription(
36  key=KEY_ALARM,
37  code_consequences={
38  "PA": AlarmControlPanelState.TRIGGERED,
39  "JA": AlarmControlPanelState.TRIGGERED,
40  "TA": AlarmControlPanelState.TRIGGERED,
41  "BA": AlarmControlPanelState.TRIGGERED,
42  "HA": AlarmControlPanelState.TRIGGERED,
43  "CA": AlarmControlPanelState.ARMED_AWAY,
44  "CB": AlarmControlPanelState.ARMED_AWAY,
45  "CG": AlarmControlPanelState.ARMED_AWAY,
46  "CL": AlarmControlPanelState.ARMED_AWAY,
47  "CP": AlarmControlPanelState.ARMED_AWAY,
48  "CQ": AlarmControlPanelState.ARMED_AWAY,
49  "CS": AlarmControlPanelState.ARMED_AWAY,
50  "CF": AlarmControlPanelState.ARMED_CUSTOM_BYPASS,
51  "NP": AlarmControlPanelState.DISARMED,
52  "NO": AlarmControlPanelState.DISARMED,
53  "OA": AlarmControlPanelState.DISARMED,
54  "OB": AlarmControlPanelState.DISARMED,
55  "OG": AlarmControlPanelState.DISARMED,
56  "OP": AlarmControlPanelState.DISARMED,
57  "OQ": AlarmControlPanelState.DISARMED,
58  "OR": AlarmControlPanelState.DISARMED,
59  "OS": AlarmControlPanelState.DISARMED,
60  "NC": AlarmControlPanelState.ARMED_NIGHT,
61  "NL": AlarmControlPanelState.ARMED_NIGHT,
62  "NE": AlarmControlPanelState.ARMED_NIGHT,
63  "NF": AlarmControlPanelState.ARMED_NIGHT,
64  "BR": PREVIOUS_STATE,
65  },
66 )
67 
68 
70  hass: HomeAssistant,
71  entry: ConfigEntry,
72  async_add_entities: AddEntitiesCallback,
73 ) -> None:
74  """Set up SIA alarm_control_panel(s) from a config entry."""
77  entry, account_data[CONF_ACCOUNT], zone, ENTITY_DESCRIPTION_ALARM
78  )
79  for account_data in entry.data[CONF_ACCOUNTS]
80  for zone in range(
81  1,
82  entry.options[CONF_ACCOUNTS][account_data[CONF_ACCOUNT]][CONF_ZONES] + 1,
83  )
84  )
85 
86 
88  """Class for SIA Alarm Control Panels."""
89 
90  entity_description: SIAAlarmControlPanelEntityDescription
91 
92  def __init__(
93  self,
94  entry: ConfigEntry,
95  account: str,
96  zone: int,
97  entity_description: SIAAlarmControlPanelEntityDescription,
98  ) -> None:
99  """Create SIAAlarmControlPanel object."""
100  super().__init__(
101  entry,
102  account,
103  zone,
104  entity_description,
105  )
106 
107  self._attr_alarm_state_attr_alarm_state: AlarmControlPanelState | None = None
108  self._old_state_old_state: AlarmControlPanelState | None = None
109 
110  def handle_last_state(self, last_state: State | None) -> None:
111  """Handle the last state."""
112  self._attr_alarm_state_attr_alarm_state = None
113  if last_state is not None and last_state.state not in (
114  STATE_UNAVAILABLE,
115  STATE_UNKNOWN,
116  ):
117  self._attr_alarm_state_attr_alarm_state = AlarmControlPanelState(last_state.state)
118  if self.statestatestatestate == STATE_UNAVAILABLE:
119  self._attr_available_attr_available_attr_available = False
120 
121  def update_state(self, sia_event: SIAEvent) -> bool:
122  """Update the state of the alarm control panel.
123 
124  Return True if the event was relevant for this entity.
125  """
126  new_state = None
127  if sia_event.code:
128  new_state = self.entity_descriptionentity_description.code_consequences.get(sia_event.code)
129  if new_state is None:
130  return False
131  _LOGGER.debug("New state will be %s", new_state)
132  if new_state == PREVIOUS_STATE:
133  new_state = self._old_state_old_state
134  if TYPE_CHECKING:
135  assert isinstance(new_state, AlarmControlPanelState)
136  self._attr_alarm_state_attr_alarm_state, self._old_state_old_state = new_state, self._attr_alarm_state_attr_alarm_state
137  return True
None __init__(self, ConfigEntry entry, str account, int zone, SIAAlarmControlPanelEntityDescription entity_description)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)