Home Assistant Unofficial Reference 2024.12.1
alarm_control_panel.py
Go to the documentation of this file.
1 """Support for Xiomi Gateway alarm control panels."""
2 
3 from __future__ import annotations
4 
5 from functools import partial
6 import logging
7 
8 from miio import DeviceException
9 
11  AlarmControlPanelEntity,
12  AlarmControlPanelEntityFeature,
13  AlarmControlPanelState,
14 )
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers.device_registry import DeviceInfo
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 
20 from .const import CONF_GATEWAY, DOMAIN
21 
22 _LOGGER = logging.getLogger(__name__)
23 
24 XIAOMI_STATE_ARMED_VALUE = "on"
25 XIAOMI_STATE_DISARMED_VALUE = "off"
26 XIAOMI_STATE_ARMING_VALUE = "oning"
27 
28 
30  hass: HomeAssistant,
31  config_entry: ConfigEntry,
32  async_add_entities: AddEntitiesCallback,
33 ) -> None:
34  """Set up the Xiaomi Gateway Alarm from a config entry."""
35  entities = []
36  gateway = hass.data[DOMAIN][config_entry.entry_id][CONF_GATEWAY]
37  entity = XiaomiGatewayAlarm(
38  gateway,
39  f"{config_entry.title} Alarm",
40  config_entry.data["model"],
41  config_entry.data["mac"],
42  config_entry.unique_id,
43  )
44  entities.append(entity)
45  async_add_entities(entities, update_before_add=True)
46 
47 
49  """Representation of the XiaomiGatewayAlarm."""
50 
51  _attr_icon = "mdi:shield-home"
52  _attr_supported_features = AlarmControlPanelEntityFeature.ARM_AWAY
53  _attr_code_arm_required = False
54 
55  def __init__(
56  self, gateway_device, gateway_name, model, mac_address, gateway_device_id
57  ):
58  """Initialize the entity."""
59  self._gateway_gateway = gateway_device
60  self._attr_name_attr_name = gateway_name
61  self._attr_unique_id_attr_unique_id = f"{model}-{mac_address}"
62  self._attr_available_attr_available = False
63  self._attr_device_info_attr_device_info = DeviceInfo(
64  identifiers={(DOMAIN, gateway_device_id)},
65  )
66 
67  async def _try_command(self, mask_error, func, *args, **kwargs):
68  """Call a device command handling error messages."""
69  try:
70  result = await self.hasshass.async_add_executor_job(
71  partial(func, *args, **kwargs)
72  )
73  _LOGGER.debug("Response received from miio device: %s", result)
74  except DeviceException as exc:
75  _LOGGER.error(mask_error, exc)
76 
77  async def async_alarm_arm_away(self, code: str | None = None) -> None:
78  """Turn on."""
79  await self._try_command_try_command(
80  "Turning the alarm on failed: %s", self._gateway_gateway.alarm.on
81  )
82 
83  async def async_alarm_disarm(self, code: str | None = None) -> None:
84  """Turn off."""
85  await self._try_command_try_command(
86  "Turning the alarm off failed: %s", self._gateway_gateway.alarm.off
87  )
88 
89  async def async_update(self) -> None:
90  """Fetch state from the device."""
91  try:
92  state = await self.hasshass.async_add_executor_job(self._gateway_gateway.alarm.status)
93  except DeviceException as ex:
94  if self._attr_available_attr_available:
95  self._attr_available_attr_available = False
96  _LOGGER.error("Got exception while fetching the state: %s", ex)
97 
98  return
99 
100  _LOGGER.debug("Got new state: %s", state)
101 
102  self._attr_available_attr_available = True
103 
104  if state == XIAOMI_STATE_ARMED_VALUE:
105  self._attr_alarm_state_attr_alarm_state = AlarmControlPanelState.ARMED_AWAY
106  elif state == XIAOMI_STATE_DISARMED_VALUE:
107  self._attr_alarm_state_attr_alarm_state = AlarmControlPanelState.DISARMED
108  elif state == XIAOMI_STATE_ARMING_VALUE:
109  self._attr_alarm_state_attr_alarm_state = AlarmControlPanelState.ARMING
110  else:
111  _LOGGER.warning(
112  "New state (%s) doesn't match expected values: %s/%s/%s",
113  state,
114  XIAOMI_STATE_ARMED_VALUE,
115  XIAOMI_STATE_DISARMED_VALUE,
116  XIAOMI_STATE_ARMING_VALUE,
117  )
118  self._attr_alarm_state_attr_alarm_state = None
119 
120  _LOGGER.debug("State value: %s", self._attr_alarm_state_attr_alarm_state)
def __init__(self, gateway_device, gateway_name, model, mac_address, gateway_device_id)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)