Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Platform for binary sensor integration."""
2 
3 from __future__ import annotations
4 
5 from devolo_home_control_api.devices.zwave import Zwave
6 from devolo_home_control_api.homecontrol import HomeControl
7 
9  BinarySensorDeviceClass,
10  BinarySensorEntity,
11 )
12 from homeassistant.const import EntityCategory
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 
16 from . import DevoloHomeControlConfigEntry
17 from .entity import DevoloDeviceEntity
18 
19 DEVICE_CLASS_MAPPING = {
20  "Water alarm": BinarySensorDeviceClass.MOISTURE,
21  "Home Security": BinarySensorDeviceClass.MOTION,
22  "Smoke Alarm": BinarySensorDeviceClass.SMOKE,
23  "Heat Alarm": BinarySensorDeviceClass.HEAT,
24  "door": BinarySensorDeviceClass.DOOR,
25  "overload": BinarySensorDeviceClass.SAFETY,
26 }
27 
28 
30  hass: HomeAssistant,
31  entry: DevoloHomeControlConfigEntry,
32  async_add_entities: AddEntitiesCallback,
33 ) -> None:
34  """Get all binary sensor and multi level sensor devices and setup them via config entry."""
35  entities: list[BinarySensorEntity] = []
36 
37  for gateway in entry.runtime_data:
38  entities.extend(
40  homecontrol=gateway,
41  device_instance=device,
42  element_uid=binary_sensor,
43  )
44  for device in gateway.binary_sensor_devices
45  for binary_sensor in device.binary_sensor_property
46  )
47  entities.extend(
49  homecontrol=gateway,
50  device_instance=device,
51  element_uid=remote,
52  key=index,
53  )
54  for device in gateway.devices.values()
55  if hasattr(device, "remote_control_property")
56  for remote in device.remote_control_property
57  for index in range(1, device.remote_control_property[remote].key_count + 1)
58  )
59  async_add_entities(entities)
60 
61 
63  """Representation of a binary sensor within devolo Home Control."""
64 
65  def __init__(
66  self, homecontrol: HomeControl, device_instance: Zwave, element_uid: str
67  ) -> None:
68  """Initialize a devolo binary sensor."""
69  self._binary_sensor_property_binary_sensor_property = device_instance.binary_sensor_property[
70  element_uid
71  ]
72 
73  super().__init__(
74  homecontrol=homecontrol,
75  device_instance=device_instance,
76  element_uid=element_uid,
77  )
78 
79  self._attr_device_class_attr_device_class = DEVICE_CLASS_MAPPING.get(
80  self._binary_sensor_property_binary_sensor_property.sub_type
81  or self._binary_sensor_property_binary_sensor_property.sensor_type
82  )
83 
84  if device_instance.binary_sensor_property[element_uid].sub_type != "":
85  self._attr_name_attr_name = device_instance.binary_sensor_property[
86  element_uid
87  ].sub_type.capitalize()
88  else:
89  self._attr_name_attr_name = device_instance.binary_sensor_property[
90  element_uid
91  ].sensor_type.capitalize()
92 
93  self._value_value_value = self._binary_sensor_property_binary_sensor_property.state
94 
95  if self._attr_device_class_attr_device_class == BinarySensorDeviceClass.SAFETY:
96  self._attr_entity_category_attr_entity_category = EntityCategory.DIAGNOSTIC
97 
98  if element_uid.startswith("devolo.WarningBinaryFI:"):
99  self._attr_device_class_attr_device_class = BinarySensorDeviceClass.PROBLEM
100  self._attr_entity_category_attr_entity_category = EntityCategory.DIAGNOSTIC
101  self._attr_entity_registry_enabled_default_attr_entity_registry_enabled_default = False
102 
103  @property
104  def is_on(self) -> bool:
105  """Return the state."""
106  return bool(self._value_value_value)
107 
108 
110  """Representation of a remote control within devolo Home Control."""
111 
112  def __init__(
113  self,
114  homecontrol: HomeControl,
115  device_instance: Zwave,
116  element_uid: str,
117  key: int,
118  ) -> None:
119  """Initialize a devolo remote control."""
120  self._remote_control_property_remote_control_property = device_instance.remote_control_property[
121  element_uid
122  ]
123 
124  super().__init__(
125  homecontrol=homecontrol,
126  device_instance=device_instance,
127  element_uid=f"{element_uid}_{key}",
128  )
129 
130  self._key_key = key
131  self._attr_is_on_attr_is_on = False
132  self._attr_name_attr_name = f"Button {key}"
133 
134  def _sync(self, message: tuple) -> None:
135  """Update the binary sensor state."""
136  if (
137  message[0] == self._remote_control_property_remote_control_property.element_uid
138  and message[1] == self._key_key
139  ):
140  self._attr_is_on_attr_is_on = True
141  elif (
142  message[0] == self._remote_control_property_remote_control_property.element_uid and message[1] == 0
143  ):
144  self._attr_is_on_attr_is_on = False
145  else:
146  self._generic_message_generic_message(message)
147  self.schedule_update_ha_stateschedule_update_ha_state()
None __init__(self, HomeControl homecontrol, Zwave device_instance, str element_uid)
None __init__(self, HomeControl homecontrol, Zwave device_instance, str element_uid, int key)
None schedule_update_ha_state(self, bool force_refresh=False)
Definition: entity.py:1244
None async_setup_entry(HomeAssistant hass, DevoloHomeControlConfigEntry entry, AddEntitiesCallback async_add_entities)