Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for Qingping binary sensors."""
2 
3 from __future__ import annotations
4 
5 from qingping_ble import (
6  BinarySensorDeviceClass as QingpingBinarySensorDeviceClass,
7  SensorUpdate,
8 )
9 
11  BinarySensorDeviceClass,
12  BinarySensorEntity,
13  BinarySensorEntityDescription,
14 )
16  PassiveBluetoothDataProcessor,
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 . import QingpingConfigEntry
25 from .device import device_key_to_bluetooth_entity_key
26 
27 BINARY_SENSOR_DESCRIPTIONS = {
28  QingpingBinarySensorDeviceClass.MOTION: BinarySensorEntityDescription(
29  key=QingpingBinarySensorDeviceClass.MOTION,
30  device_class=BinarySensorDeviceClass.MOTION,
31  ),
32  QingpingBinarySensorDeviceClass.LIGHT: BinarySensorEntityDescription(
33  key=QingpingBinarySensorDeviceClass.LIGHT,
34  device_class=BinarySensorDeviceClass.LIGHT,
35  ),
36  QingpingBinarySensorDeviceClass.DOOR: BinarySensorEntityDescription(
37  key=QingpingBinarySensorDeviceClass.DOOR,
38  device_class=BinarySensorDeviceClass.DOOR,
39  ),
40  QingpingBinarySensorDeviceClass.PROBLEM: BinarySensorEntityDescription(
41  key=QingpingBinarySensorDeviceClass.PROBLEM,
42  device_class=BinarySensorDeviceClass.PROBLEM,
43  ),
44 }
45 
46 
48  sensor_update: SensorUpdate,
49 ) -> PassiveBluetoothDataUpdate:
50  """Convert a sensor update to a bluetooth data update."""
52  devices={
53  device_id: sensor_device_info_to_hass_device_info(device_info)
54  for device_id, device_info in sensor_update.devices.items()
55  },
56  entity_descriptions={
57  device_key_to_bluetooth_entity_key(device_key): BINARY_SENSOR_DESCRIPTIONS[
58  description.device_class
59  ]
60  for device_key, description in sensor_update.binary_entity_descriptions.items()
61  if description.device_class
62  },
63  entity_data={
64  device_key_to_bluetooth_entity_key(device_key): sensor_values.native_value
65  for device_key, sensor_values in sensor_update.binary_entity_values.items()
66  },
67  entity_names={
68  device_key_to_bluetooth_entity_key(device_key): sensor_values.name
69  for device_key, sensor_values in sensor_update.binary_entity_values.items()
70  },
71  )
72 
73 
75  hass: HomeAssistant,
76  entry: QingpingConfigEntry,
77  async_add_entities: AddEntitiesCallback,
78 ) -> None:
79  """Set up the Qingping BLE sensors."""
80  coordinator = entry.runtime_data
81  processor = PassiveBluetoothDataProcessor(sensor_update_to_bluetooth_data_update)
82  entry.async_on_unload(
83  processor.async_add_entities_listener(
84  QingpingBluetoothSensorEntity, async_add_entities
85  )
86  )
87  entry.async_on_unload(
88  coordinator.async_register_processor(processor, BinarySensorEntityDescription)
89  )
90 
91 
93  PassiveBluetoothProcessorEntity[
94  PassiveBluetoothDataProcessor[bool | None, SensorUpdate]
95  ],
96  BinarySensorEntity,
97 ):
98  """Representation of a Qingping binary sensor."""
99 
100  @property
101  def is_on(self) -> bool | None:
102  """Return the native value."""
103  return self.processor.entity_data.get(self.entity_key)
PassiveBluetoothEntityKey device_key_to_bluetooth_entity_key(DeviceKey device_key)
Definition: device.py:14
None async_setup_entry(HomeAssistant hass, QingpingConfigEntry entry, AddEntitiesCallback async_add_entities)
PassiveBluetoothDataUpdate sensor_update_to_bluetooth_data_update(SensorUpdate sensor_update)
DeviceInfo sensor_device_info_to_hass_device_info(SensorDeviceInfo sensor_device_info)
Definition: sensor.py:20