Home Assistant Unofficial Reference 2024.12.1
alarm_control_panel.py
Go to the documentation of this file.
1 """Support for Minut Point."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 import logging
7 
9  DOMAIN as ALARM_CONTROL_PANEL_DOMAIN,
10  AlarmControlPanelEntity,
11  AlarmControlPanelEntityFeature,
12  AlarmControlPanelState,
13 )
14 from homeassistant.config_entries import ConfigEntry
15 from homeassistant.core import HomeAssistant, callback
16 from homeassistant.helpers.device_registry import DeviceInfo
17 from homeassistant.helpers.dispatcher import async_dispatcher_connect
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 
20 from . import MinutPointClient
21 from .const import DOMAIN as POINT_DOMAIN, POINT_DISCOVERY_NEW, SIGNAL_WEBHOOK
22 
23 _LOGGER = logging.getLogger(__name__)
24 
25 
26 EVENT_MAP = {
27  "off": AlarmControlPanelState.DISARMED,
28  "alarm_silenced": AlarmControlPanelState.DISARMED,
29  "alarm_grace_period_expired": AlarmControlPanelState.TRIGGERED,
30 }
31 
32 
34  hass: HomeAssistant,
35  config_entry: ConfigEntry,
36  async_add_entities: AddEntitiesCallback,
37 ) -> None:
38  """Set up a Point's alarm_control_panel based on a config entry."""
39 
40  async def async_discover_home(home_id):
41  """Discover and add a discovered home."""
42  client = config_entry.runtime_data.client
43  async_add_entities([MinutPointAlarmControl(client, home_id)], True)
44 
46  hass,
47  POINT_DISCOVERY_NEW.format(ALARM_CONTROL_PANEL_DOMAIN, POINT_DOMAIN),
48  async_discover_home,
49  )
50 
51 
53  """The platform class required by Home Assistant."""
54 
55  _attr_supported_features = AlarmControlPanelEntityFeature.ARM_AWAY
56  _attr_code_arm_required = False
57 
58  def __init__(self, point_client: MinutPointClient, home_id: str) -> None:
59  """Initialize the entity."""
60  self._client_client = point_client
61  self._home_id_home_id = home_id
62  self._async_unsub_hook_dispatcher_connect_async_unsub_hook_dispatcher_connect: Callable[[], None] | None = None
63  self._home_home = point_client.homes[self._home_id_home_id]
64 
65  self._attr_name_attr_name = self._home_home["name"]
66  self._attr_unique_id_attr_unique_id = f"point.{home_id}"
67  self._attr_device_info_attr_device_info = DeviceInfo(
68  identifiers={(POINT_DOMAIN, home_id)},
69  manufacturer="Minut",
70  name=self._attr_name_attr_name,
71  )
72 
73  async def async_added_to_hass(self) -> None:
74  """Call when entity is added to HOme Assistant."""
75  await super().async_added_to_hass()
76  self._async_unsub_hook_dispatcher_connect_async_unsub_hook_dispatcher_connect = async_dispatcher_connect(
77  self.hasshass, SIGNAL_WEBHOOK, self._webhook_event_webhook_event
78  )
79 
80  async def async_will_remove_from_hass(self) -> None:
81  """Disconnect dispatcher listener when removed."""
82  await super().async_will_remove_from_hass()
83  if self._async_unsub_hook_dispatcher_connect_async_unsub_hook_dispatcher_connect:
84  self._async_unsub_hook_dispatcher_connect_async_unsub_hook_dispatcher_connect()
85 
86  @callback
87  def _webhook_event(self, data, webhook):
88  """Process new event from the webhook."""
89  _type = data.get("event", {}).get("type")
90  _device_id = data.get("event", {}).get("device_id")
91  _changed_by = data.get("event", {}).get("user_id")
92  if (
93  _device_id not in self._home_home["devices"] and _type not in EVENT_MAP
94  ) and _type != "alarm_silenced": # alarm_silenced does not have device_id
95  return
96  _LOGGER.debug("Received webhook: %s", _type)
97  self._home_home["alarm_status"] = _type
98  self._attr_changed_by_attr_changed_by = _changed_by
99  self.async_write_ha_stateasync_write_ha_state()
100 
101  @property
102  def alarm_state(self) -> AlarmControlPanelState:
103  """Return state of the device."""
104  return EVENT_MAP.get(
105  self._home_home["alarm_status"], AlarmControlPanelState.ARMED_AWAY
106  )
107 
108  async def async_alarm_disarm(self, code: str | None = None) -> None:
109  """Send disarm command."""
110  status = await self._client_client.async_alarm_disarm(self._home_id_home_id)
111  if status:
112  self._home_home["alarm_status"] = "off"
113 
114  async def async_alarm_arm_away(self, code: str | None = None) -> None:
115  """Send arm away command."""
116  status = await self._client_client.async_alarm_arm(self._home_id_home_id)
117  if status:
118  self._home_home["alarm_status"] = "on"
None __init__(self, MinutPointClient point_client, str home_id)
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103