Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for Minut Point binary sensors."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from pypoint import EVENTS
8 
10  DOMAIN as BINARY_SENSOR_DOMAIN,
11  BinarySensorDeviceClass,
12  BinarySensorEntity,
13 )
14 from homeassistant.config_entries import ConfigEntry
15 from homeassistant.core import HomeAssistant, callback
16 from homeassistant.helpers.dispatcher import async_dispatcher_connect
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 
19 from .const import DOMAIN as POINT_DOMAIN, POINT_DISCOVERY_NEW, SIGNAL_WEBHOOK
20 from .entity import MinutPointEntity
21 
22 _LOGGER = logging.getLogger(__name__)
23 
24 
25 DEVICES = {
26  "alarm": {"icon": "mdi:alarm-bell"},
27  "battery": {"device_class": BinarySensorDeviceClass.BATTERY},
28  "button_press": {"icon": "mdi:gesture-tap-button"},
29  "cold": {"device_class": BinarySensorDeviceClass.COLD},
30  "connectivity": {"device_class": BinarySensorDeviceClass.CONNECTIVITY},
31  "dry": {"icon": "mdi:water"},
32  "glass": {"icon": "mdi:window-closed-variant"},
33  "heat": {"device_class": BinarySensorDeviceClass.HEAT},
34  "moisture": {"device_class": BinarySensorDeviceClass.MOISTURE},
35  "motion": {"device_class": BinarySensorDeviceClass.MOTION},
36  "noise": {"icon": "mdi:volume-high"},
37  "sound": {"device_class": BinarySensorDeviceClass.SOUND},
38  "tamper_old": {"icon": "mdi:shield-alert"},
39  "tamper": {"icon": "mdi:shield-alert"},
40 }
41 
42 
44  hass: HomeAssistant,
45  config_entry: ConfigEntry,
46  async_add_entities: AddEntitiesCallback,
47 ) -> None:
48  """Set up a Point's binary sensors based on a config entry."""
49 
50  async def async_discover_sensor(device_id):
51  """Discover and add a discovered sensor."""
52  client = config_entry.runtime_data.client
54  (
55  MinutPointBinarySensor(client, device_id, device_name)
56  for device_name in DEVICES
57  if device_name in EVENTS
58  ),
59  True,
60  )
61 
63  hass,
64  POINT_DISCOVERY_NEW.format(BINARY_SENSOR_DOMAIN, POINT_DOMAIN),
65  async_discover_sensor,
66  )
67 
68 
70  """The platform class required by Home Assistant."""
71 
72  def __init__(self, point_client, device_id, device_name):
73  """Initialize the binary sensor."""
74  super().__init__(
75  point_client,
76  device_id,
77  DEVICES[device_name].get("device_class", device_name),
78  )
79  self._device_name_device_name = device_name
80  self._async_unsub_hook_dispatcher_connect_async_unsub_hook_dispatcher_connect = None
81  self._events_events = EVENTS[device_name]
82  self._attr_unique_id_attr_unique_id_attr_unique_id = f"point.{device_id}-{device_name}"
83  self._attr_icon_attr_icon = DEVICES[self._device_name_device_name].get("icon")
84 
85  async def async_added_to_hass(self) -> None:
86  """Call when entity is added to HOme Assistant."""
87  await super().async_added_to_hass()
88  self._async_unsub_hook_dispatcher_connect_async_unsub_hook_dispatcher_connect = async_dispatcher_connect(
89  self.hasshass, SIGNAL_WEBHOOK, self._webhook_event_webhook_event
90  )
91 
92  async def async_will_remove_from_hass(self) -> None:
93  """Disconnect dispatcher listener when removed."""
94  await super().async_will_remove_from_hass()
95  if self._async_unsub_hook_dispatcher_connect_async_unsub_hook_dispatcher_connect:
96  self._async_unsub_hook_dispatcher_connect_async_unsub_hook_dispatcher_connect()
97 
98  async def _update_callback(self):
99  """Update the value of the sensor."""
100  if not self.is_updatedis_updated:
101  return
102  if self.device_classdevice_classdevice_class == BinarySensorDeviceClass.CONNECTIVITY:
103  # connectivity is the other way around.
104  self._attr_is_on_attr_is_on = self._events_events[0] not in self.devicedevice.ongoing_events
105  else:
106  self._attr_is_on_attr_is_on = self._events_events[0] in self.devicedevice.ongoing_events
107  self.async_write_ha_stateasync_write_ha_state()
108 
109  @callback
110  def _webhook_event(self, data, webhook):
111  """Process new event from the webhook."""
112  if self.devicedevice.webhook != webhook:
113  return
114  _type = data.get("event", {}).get("type")
115  _device_id = data.get("event", {}).get("device_id")
116  if _type not in self._events_events or _device_id != self.devicedevice.device_id:
117  return
118  _LOGGER.debug("Received webhook: %s", _type)
119  if _type == self._events_events[0]:
120  _is_on = True
121  elif _type == self._events_events[1]:
122  _is_on = False
123  else:
124  return
125 
126  if self.device_classdevice_classdevice_class == BinarySensorDeviceClass.CONNECTIVITY:
127  # connectivity is the other way around.
128  self._attr_is_on_attr_is_on = not _is_on
129  else:
130  self._attr_is_on_attr_is_on = _is_on
131  self.async_write_ha_stateasync_write_ha_state()
def __init__(self, point_client, device_id, device_name)
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