Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Binary Sensor platform for Sensibo integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from typing import TYPE_CHECKING
8 
9 from pysensibo.model import MotionSensor, SensiboDevice
10 
12  BinarySensorDeviceClass,
13  BinarySensorEntity,
14  BinarySensorEntityDescription,
15 )
16 from homeassistant.const import EntityCategory
17 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 
20 from . import SensiboConfigEntry
21 from .coordinator import SensiboDataUpdateCoordinator
22 from .entity import SensiboDeviceBaseEntity, SensiboMotionBaseEntity
23 
24 PARALLEL_UPDATES = 0
25 
26 
27 @dataclass(frozen=True, kw_only=True)
29  """Describes Sensibo Motion sensor entity."""
30 
31  value_fn: Callable[[MotionSensor], bool | None]
32 
33 
34 @dataclass(frozen=True, kw_only=True)
36  """Describes Sensibo Motion sensor entity."""
37 
38  value_fn: Callable[[SensiboDevice], bool | None]
39 
40 
41 FILTER_CLEAN_REQUIRED_DESCRIPTION = SensiboDeviceBinarySensorEntityDescription(
42  key="filter_clean",
43  translation_key="filter_clean",
44  device_class=BinarySensorDeviceClass.PROBLEM,
45  value_fn=lambda data: data.filter_clean,
46 )
47 
48 MOTION_SENSOR_TYPES: tuple[SensiboMotionBinarySensorEntityDescription, ...] = (
50  key="alive",
51  device_class=BinarySensorDeviceClass.CONNECTIVITY,
52  entity_category=EntityCategory.DIAGNOSTIC,
53  value_fn=lambda data: data.alive,
54  ),
56  key="is_main_sensor",
57  translation_key="is_main_sensor",
58  entity_category=EntityCategory.DIAGNOSTIC,
59  value_fn=lambda data: data.is_main_sensor,
60  ),
62  key="motion",
63  device_class=BinarySensorDeviceClass.MOTION,
64  value_fn=lambda data: data.motion,
65  ),
66 )
67 
68 MOTION_DEVICE_SENSOR_TYPES: tuple[SensiboDeviceBinarySensorEntityDescription, ...] = (
70  key="room_occupied",
71  translation_key="room_occupied",
72  device_class=BinarySensorDeviceClass.MOTION,
73  value_fn=lambda data: data.room_occupied,
74  ),
75 )
76 
77 DEVICE_SENSOR_TYPES: tuple[SensiboDeviceBinarySensorEntityDescription, ...] = (
78  FILTER_CLEAN_REQUIRED_DESCRIPTION,
79 )
80 
81 PURE_SENSOR_TYPES: tuple[SensiboDeviceBinarySensorEntityDescription, ...] = (
83  key="pure_ac_integration",
84  translation_key="pure_ac_integration",
85  entity_category=EntityCategory.DIAGNOSTIC,
86  device_class=BinarySensorDeviceClass.CONNECTIVITY,
87  value_fn=lambda data: data.pure_ac_integration,
88  ),
90  key="pure_geo_integration",
91  translation_key="pure_geo_integration",
92  entity_category=EntityCategory.DIAGNOSTIC,
93  device_class=BinarySensorDeviceClass.CONNECTIVITY,
94  value_fn=lambda data: data.pure_geo_integration,
95  ),
97  key="pure_measure_integration",
98  translation_key="pure_measure_integration",
99  entity_category=EntityCategory.DIAGNOSTIC,
100  device_class=BinarySensorDeviceClass.CONNECTIVITY,
101  value_fn=lambda data: data.pure_measure_integration,
102  ),
104  key="pure_prime_integration",
105  translation_key="pure_prime_integration",
106  entity_category=EntityCategory.DIAGNOSTIC,
107  device_class=BinarySensorDeviceClass.CONNECTIVITY,
108  value_fn=lambda data: data.pure_prime_integration,
109  ),
110  FILTER_CLEAN_REQUIRED_DESCRIPTION,
111 )
112 
113 DESCRIPTION_BY_MODELS = {"pure": PURE_SENSOR_TYPES}
114 
115 
117  hass: HomeAssistant,
118  entry: SensiboConfigEntry,
119  async_add_entities: AddEntitiesCallback,
120 ) -> None:
121  """Set up Sensibo binary sensor platform."""
122 
123  coordinator = entry.runtime_data
124 
125  entities: list[SensiboMotionSensor | SensiboDeviceSensor] = []
126 
127  for device_id, device_data in coordinator.data.parsed.items():
128  if device_data.motion_sensors:
129  entities.extend(
131  coordinator, device_id, sensor_id, sensor_data, description
132  )
133  for sensor_id, sensor_data in device_data.motion_sensors.items()
134  for description in MOTION_SENSOR_TYPES
135  )
136  entities.extend(
137  SensiboDeviceSensor(coordinator, device_id, description)
138  for description in MOTION_DEVICE_SENSOR_TYPES
139  for device_id, device_data in coordinator.data.parsed.items()
140  if device_data.motion_sensors
141  )
142  entities.extend(
143  SensiboDeviceSensor(coordinator, device_id, description)
144  for device_id, device_data in coordinator.data.parsed.items()
145  for description in DESCRIPTION_BY_MODELS.get(
146  device_data.model, DEVICE_SENSOR_TYPES
147  )
148  )
149 
150  async_add_entities(entities)
151 
152 
154  """Representation of a Sensibo Motion Binary Sensor."""
155 
156  entity_description: SensiboMotionBinarySensorEntityDescription
157 
158  def __init__(
159  self,
160  coordinator: SensiboDataUpdateCoordinator,
161  device_id: str,
162  sensor_id: str,
163  sensor_data: MotionSensor,
164  entity_description: SensiboMotionBinarySensorEntityDescription,
165  ) -> None:
166  """Initiate Sensibo Motion Binary Sensor."""
167  super().__init__(
168  coordinator,
169  device_id,
170  sensor_id,
171  sensor_data,
172  )
173  self.entity_descriptionentity_description = entity_description
174  self._attr_unique_id_attr_unique_id = f"{sensor_id}-{entity_description.key}"
175 
176  @property
177  def is_on(self) -> bool | None:
178  """Return true if the binary sensor is on."""
179  if TYPE_CHECKING:
180  assert self.sensor_datasensor_data
181  return self.entity_descriptionentity_description.value_fn(self.sensor_datasensor_data)
182 
183 
185  """Representation of a Sensibo Device Binary Sensor."""
186 
187  entity_description: SensiboDeviceBinarySensorEntityDescription
188 
189  def __init__(
190  self,
191  coordinator: SensiboDataUpdateCoordinator,
192  device_id: str,
193  entity_description: SensiboDeviceBinarySensorEntityDescription,
194  ) -> None:
195  """Initiate Sensibo Device Binary Sensor."""
196  super().__init__(
197  coordinator,
198  device_id,
199  )
200  self.entity_descriptionentity_description = entity_description
201  self._attr_unique_id_attr_unique_id = f"{device_id}-{entity_description.key}"
202 
203  @property
204  def is_on(self) -> bool | None:
205  """Return true if the binary sensor is on."""
206  return self.entity_descriptionentity_description.value_fn(self.device_datadevice_data)
None __init__(self, SensiboDataUpdateCoordinator coordinator, str device_id, SensiboDeviceBinarySensorEntityDescription entity_description)
None __init__(self, SensiboDataUpdateCoordinator coordinator, str device_id, str sensor_id, MotionSensor sensor_data, SensiboMotionBinarySensorEntityDescription entity_description)
None async_setup_entry(HomeAssistant hass, SensiboConfigEntry entry, AddEntitiesCallback async_add_entities)