Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Interfaces with Egardia/Woonveilig alarm control panel."""
2 
3 from __future__ import annotations
4 
6  BinarySensorDeviceClass,
7  BinarySensorEntity,
8 )
9 from homeassistant.const import STATE_OFF, STATE_ON
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
13 
14 from . import ATTR_DISCOVER_DEVICES, EGARDIA_DEVICE
15 
16 EGARDIA_TYPE_TO_DEVICE_CLASS = {
17  "IR Sensor": BinarySensorDeviceClass.MOTION,
18  "Door Contact": BinarySensorDeviceClass.OPENING,
19  "IR": BinarySensorDeviceClass.MOTION,
20 }
21 
22 
24  hass: HomeAssistant,
25  config: ConfigType,
26  async_add_entities: AddEntitiesCallback,
27  discovery_info: DiscoveryInfoType | None = None,
28 ) -> None:
29  """Initialize the platform."""
30  if discovery_info is None or discovery_info[ATTR_DISCOVER_DEVICES] is None:
31  return
32 
33  disc_info = discovery_info[ATTR_DISCOVER_DEVICES]
34 
36  (
38  sensor_id=disc_info[sensor]["id"],
39  name=disc_info[sensor]["name"],
40  egardia_system=hass.data[EGARDIA_DEVICE],
41  device_class=EGARDIA_TYPE_TO_DEVICE_CLASS.get(
42  disc_info[sensor]["type"], None
43  ),
44  )
45  for sensor in disc_info
46  ),
47  True,
48  )
49 
50 
52  """Represents a sensor based on an Egardia sensor (IR, Door Contact)."""
53 
54  def __init__(self, sensor_id, name, egardia_system, device_class):
55  """Initialize the sensor device."""
56  self._id_id = sensor_id
57  self._name_name = name
58  self._state_state = None
59  self._device_class_device_class = device_class
60  self._egardia_system_egardia_system = egardia_system
61 
62  def update(self) -> None:
63  """Update the status."""
64  egardia_input = self._egardia_system_egardia_system.getsensorstate(self._id_id)
65  self._state_state = STATE_ON if egardia_input else STATE_OFF
66 
67  @property
68  def name(self):
69  """Return the name of the device."""
70  return self._name_name
71 
72  @property
73  def is_on(self):
74  """Whether the device is switched on."""
75  return self._state_state == STATE_ON
76 
77  @property
78  def device_class(self):
79  """Return the device class."""
80  return self._device_class_device_class
def __init__(self, sensor_id, name, egardia_system, device_class)
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)