Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for Xiaomi binary sensors."""
2 
3 from __future__ import annotations
4 
5 from xiaomi_ble.parser import (
6  BinarySensorDeviceClass as XiaomiBinarySensorDeviceClass,
7  ExtendedBinarySensorDeviceClass,
8  SensorUpdate,
9 )
10 
12  BinarySensorDeviceClass,
13  BinarySensorEntity,
14  BinarySensorEntityDescription,
15 )
17  PassiveBluetoothDataUpdate,
18  PassiveBluetoothProcessorEntity,
19 )
20 from homeassistant.core import HomeAssistant
21 from homeassistant.helpers.entity_platform import AddEntitiesCallback
22 from homeassistant.helpers.sensor import sensor_device_info_to_hass_device_info
23 
24 from .coordinator import XiaomiPassiveBluetoothDataProcessor
25 from .device import device_key_to_bluetooth_entity_key
26 from .types import XiaomiBLEConfigEntry
27 
28 BINARY_SENSOR_DESCRIPTIONS = {
29  XiaomiBinarySensorDeviceClass.BATTERY: BinarySensorEntityDescription(
30  key=XiaomiBinarySensorDeviceClass.BATTERY,
31  device_class=BinarySensorDeviceClass.BATTERY,
32  ),
33  XiaomiBinarySensorDeviceClass.DOOR: BinarySensorEntityDescription(
34  key=XiaomiBinarySensorDeviceClass.DOOR,
35  device_class=BinarySensorDeviceClass.DOOR,
36  ),
37  XiaomiBinarySensorDeviceClass.LIGHT: BinarySensorEntityDescription(
38  key=XiaomiBinarySensorDeviceClass.LIGHT,
39  device_class=BinarySensorDeviceClass.LIGHT,
40  ),
41  XiaomiBinarySensorDeviceClass.LOCK: BinarySensorEntityDescription(
42  key=XiaomiBinarySensorDeviceClass.LOCK,
43  device_class=BinarySensorDeviceClass.LOCK,
44  ),
45  XiaomiBinarySensorDeviceClass.MOISTURE: BinarySensorEntityDescription(
46  key=XiaomiBinarySensorDeviceClass.MOISTURE,
47  device_class=BinarySensorDeviceClass.MOISTURE,
48  ),
49  XiaomiBinarySensorDeviceClass.MOTION: BinarySensorEntityDescription(
50  key=XiaomiBinarySensorDeviceClass.MOTION,
51  device_class=BinarySensorDeviceClass.MOTION,
52  ),
53  XiaomiBinarySensorDeviceClass.OCCUPANCY: BinarySensorEntityDescription(
54  key=XiaomiBinarySensorDeviceClass.OCCUPANCY,
55  device_class=BinarySensorDeviceClass.OCCUPANCY,
56  ),
57  XiaomiBinarySensorDeviceClass.OPENING: BinarySensorEntityDescription(
58  key=XiaomiBinarySensorDeviceClass.OPENING,
59  device_class=BinarySensorDeviceClass.OPENING,
60  ),
61  XiaomiBinarySensorDeviceClass.POWER: BinarySensorEntityDescription(
62  key=XiaomiBinarySensorDeviceClass.POWER,
63  device_class=BinarySensorDeviceClass.POWER,
64  ),
65  XiaomiBinarySensorDeviceClass.SMOKE: BinarySensorEntityDescription(
66  key=XiaomiBinarySensorDeviceClass.SMOKE,
67  device_class=BinarySensorDeviceClass.SMOKE,
68  ),
69  ExtendedBinarySensorDeviceClass.ANTILOCK: BinarySensorEntityDescription(
70  key=ExtendedBinarySensorDeviceClass.ANTILOCK,
71  ),
72  ExtendedBinarySensorDeviceClass.ARMED: BinarySensorEntityDescription(
73  key=ExtendedBinarySensorDeviceClass.ARMED,
74  icon="mdi:shield-check",
75  ),
76  ExtendedBinarySensorDeviceClass.CHILDLOCK: BinarySensorEntityDescription(
77  key=ExtendedBinarySensorDeviceClass.CHILDLOCK,
78  ),
79  ExtendedBinarySensorDeviceClass.DEVICE_FORCIBLY_REMOVED: BinarySensorEntityDescription(
80  key=ExtendedBinarySensorDeviceClass.DEVICE_FORCIBLY_REMOVED,
81  device_class=BinarySensorDeviceClass.PROBLEM,
82  ),
83  ExtendedBinarySensorDeviceClass.DOOR_LEFT_OPEN: BinarySensorEntityDescription(
84  key=ExtendedBinarySensorDeviceClass.DOOR_LEFT_OPEN,
85  device_class=BinarySensorDeviceClass.PROBLEM,
86  ),
87  ExtendedBinarySensorDeviceClass.DOOR_STUCK: BinarySensorEntityDescription(
88  key=ExtendedBinarySensorDeviceClass.DOOR_STUCK,
89  device_class=BinarySensorDeviceClass.PROBLEM,
90  ),
91  ExtendedBinarySensorDeviceClass.FINGERPRINT: BinarySensorEntityDescription(
92  key=ExtendedBinarySensorDeviceClass.FINGERPRINT,
93  icon="mdi:fingerprint",
94  ),
95  ExtendedBinarySensorDeviceClass.KNOCK_ON_THE_DOOR: BinarySensorEntityDescription(
96  key=ExtendedBinarySensorDeviceClass.KNOCK_ON_THE_DOOR,
97  ),
98  ExtendedBinarySensorDeviceClass.PRY_THE_DOOR: BinarySensorEntityDescription(
99  key=ExtendedBinarySensorDeviceClass.PRY_THE_DOOR,
100  device_class=BinarySensorDeviceClass.TAMPER,
101  ),
102  ExtendedBinarySensorDeviceClass.TOOTHBRUSH: BinarySensorEntityDescription(
103  key=ExtendedBinarySensorDeviceClass.TOOTHBRUSH,
104  ),
105 }
106 
107 
109  sensor_update: SensorUpdate,
110 ) -> PassiveBluetoothDataUpdate[bool | None]:
111  """Convert a sensor update to a bluetooth data update."""
113  devices={
114  device_id: sensor_device_info_to_hass_device_info(device_info)
115  for device_id, device_info in sensor_update.devices.items()
116  },
117  entity_descriptions={
118  device_key_to_bluetooth_entity_key(device_key): BINARY_SENSOR_DESCRIPTIONS[
119  description.device_class
120  ]
121  for device_key, description in sensor_update.binary_entity_descriptions.items()
122  if description.device_class
123  },
124  entity_data={
125  device_key_to_bluetooth_entity_key(device_key): sensor_values.native_value
126  for device_key, sensor_values in sensor_update.binary_entity_values.items()
127  },
128  entity_names={
129  device_key_to_bluetooth_entity_key(device_key): sensor_values.name
130  for device_key, sensor_values in sensor_update.binary_entity_values.items()
131  },
132  )
133 
134 
136  hass: HomeAssistant,
137  entry: XiaomiBLEConfigEntry,
138  async_add_entities: AddEntitiesCallback,
139 ) -> None:
140  """Set up the Xiaomi BLE sensors."""
141  coordinator = entry.runtime_data
143  sensor_update_to_bluetooth_data_update
144  )
145  entry.async_on_unload(
146  processor.async_add_entities_listener(
147  XiaomiBluetoothSensorEntity, async_add_entities
148  )
149  )
150  entry.async_on_unload(
151  coordinator.async_register_processor(processor, BinarySensorEntityDescription)
152  )
153 
154 
156  PassiveBluetoothProcessorEntity[XiaomiPassiveBluetoothDataProcessor[bool | None]],
157  BinarySensorEntity,
158 ):
159  """Representation of a Xiaomi binary sensor."""
160 
161  @property
162  def is_on(self) -> bool | None:
163  """Return the native value."""
164  return self.processor.entity_data.get(self.entity_key)
165 
166  @property
167  def available(self) -> bool:
168  """Return True if entity is available."""
169  return self.processor.coordinator.sleepy_device or super().available
PassiveBluetoothEntityKey device_key_to_bluetooth_entity_key(DeviceKey device_key)
Definition: device.py:14
PassiveBluetoothDataUpdate[bool|None] sensor_update_to_bluetooth_data_update(SensorUpdate sensor_update)
None async_setup_entry(HomeAssistant hass, XiaomiBLEConfigEntry entry, AddEntitiesCallback async_add_entities)
DeviceInfo sensor_device_info_to_hass_device_info(SensorDeviceInfo sensor_device_info)
Definition: sensor.py:20