Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Qingping sensors."""
2 
3 from __future__ import annotations
4 
5 from qingping_ble import (
6  SensorDeviceClass as QingpingSensorDeviceClass,
7  SensorUpdate,
8  Units,
9 )
10 
12  PassiveBluetoothDataProcessor,
13  PassiveBluetoothDataUpdate,
14  PassiveBluetoothProcessorEntity,
15 )
17  SensorDeviceClass,
18  SensorEntity,
19  SensorEntityDescription,
20  SensorStateClass,
21 )
22 from homeassistant.const import (
23  CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
24  CONCENTRATION_PARTS_PER_MILLION,
25  LIGHT_LUX,
26  PERCENTAGE,
27  SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
28  EntityCategory,
29  UnitOfPressure,
30  UnitOfTemperature,
31 )
32 from homeassistant.core import HomeAssistant
33 from homeassistant.helpers.entity_platform import AddEntitiesCallback
34 from homeassistant.helpers.sensor import sensor_device_info_to_hass_device_info
35 
36 from . import QingpingConfigEntry
37 from .device import device_key_to_bluetooth_entity_key
38 
39 SENSOR_DESCRIPTIONS = {
40  (QingpingSensorDeviceClass.BATTERY, Units.PERCENTAGE): SensorEntityDescription(
41  key=f"{QingpingSensorDeviceClass.BATTERY}_{Units.PERCENTAGE}",
42  device_class=SensorDeviceClass.BATTERY,
43  native_unit_of_measurement=PERCENTAGE,
44  state_class=SensorStateClass.MEASUREMENT,
45  entity_category=EntityCategory.DIAGNOSTIC,
46  ),
47  (
48  QingpingSensorDeviceClass.CO2,
49  Units.CONCENTRATION_PARTS_PER_MILLION,
51  key=f"{QingpingSensorDeviceClass.CO2}_{Units.CONCENTRATION_PARTS_PER_MILLION}",
52  device_class=SensorDeviceClass.CO2,
53  native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
54  state_class=SensorStateClass.MEASUREMENT,
55  ),
56  (QingpingSensorDeviceClass.HUMIDITY, Units.PERCENTAGE): SensorEntityDescription(
57  key=f"{QingpingSensorDeviceClass.HUMIDITY}_{Units.PERCENTAGE}",
58  device_class=SensorDeviceClass.HUMIDITY,
59  native_unit_of_measurement=PERCENTAGE,
60  state_class=SensorStateClass.MEASUREMENT,
61  ),
62  (QingpingSensorDeviceClass.ILLUMINANCE, Units.LIGHT_LUX): SensorEntityDescription(
63  key=f"{QingpingSensorDeviceClass.ILLUMINANCE}_{Units.LIGHT_LUX}",
64  device_class=SensorDeviceClass.ILLUMINANCE,
65  native_unit_of_measurement=LIGHT_LUX,
66  state_class=SensorStateClass.MEASUREMENT,
67  ),
68  (
69  QingpingSensorDeviceClass.PM10,
70  Units.CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
72  key=f"{QingpingSensorDeviceClass.PM10}_{Units.CONCENTRATION_MICROGRAMS_PER_CUBIC_METER}",
73  device_class=SensorDeviceClass.PM10,
74  native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
75  state_class=SensorStateClass.MEASUREMENT,
76  ),
77  (
78  QingpingSensorDeviceClass.PM25,
79  Units.CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
81  key=f"{QingpingSensorDeviceClass.PM25}_{Units.CONCENTRATION_MICROGRAMS_PER_CUBIC_METER}",
82  device_class=SensorDeviceClass.PM25,
83  native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
84  state_class=SensorStateClass.MEASUREMENT,
85  ),
86  (QingpingSensorDeviceClass.PRESSURE, Units.PRESSURE_MBAR): SensorEntityDescription(
87  key=f"{QingpingSensorDeviceClass.PRESSURE}_{Units.PRESSURE_MBAR}",
88  device_class=SensorDeviceClass.PRESSURE,
89  native_unit_of_measurement=UnitOfPressure.MBAR,
90  state_class=SensorStateClass.MEASUREMENT,
91  ),
92  (
93  QingpingSensorDeviceClass.SIGNAL_STRENGTH,
94  Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
96  key=f"{QingpingSensorDeviceClass.SIGNAL_STRENGTH}_{Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT}",
97  device_class=SensorDeviceClass.SIGNAL_STRENGTH,
98  native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
99  state_class=SensorStateClass.MEASUREMENT,
100  entity_category=EntityCategory.DIAGNOSTIC,
101  entity_registry_enabled_default=False,
102  ),
103  (
104  QingpingSensorDeviceClass.TEMPERATURE,
105  Units.TEMP_CELSIUS,
107  key=f"{QingpingSensorDeviceClass.TEMPERATURE}_{Units.TEMP_CELSIUS}",
108  device_class=SensorDeviceClass.TEMPERATURE,
109  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
110  state_class=SensorStateClass.MEASUREMENT,
111  ),
112 }
113 
114 
116  sensor_update: SensorUpdate,
117 ) -> PassiveBluetoothDataUpdate:
118  """Convert a sensor update to a bluetooth data update."""
120  devices={
121  device_id: sensor_device_info_to_hass_device_info(device_info)
122  for device_id, device_info in sensor_update.devices.items()
123  },
124  entity_descriptions={
125  device_key_to_bluetooth_entity_key(device_key): SENSOR_DESCRIPTIONS[
126  (description.device_class, description.native_unit_of_measurement)
127  ]
128  for device_key, description in sensor_update.entity_descriptions.items()
129  if description.device_class and description.native_unit_of_measurement
130  },
131  entity_data={
132  device_key_to_bluetooth_entity_key(device_key): sensor_values.native_value
133  for device_key, sensor_values in sensor_update.entity_values.items()
134  },
135  entity_names={
136  device_key_to_bluetooth_entity_key(device_key): sensor_values.name
137  for device_key, sensor_values in sensor_update.entity_values.items()
138  },
139  )
140 
141 
143  hass: HomeAssistant,
144  entry: QingpingConfigEntry,
145  async_add_entities: AddEntitiesCallback,
146 ) -> None:
147  """Set up the Qingping BLE sensors."""
148  coordinator = entry.runtime_data
149  processor = PassiveBluetoothDataProcessor(sensor_update_to_bluetooth_data_update)
150  entry.async_on_unload(
151  processor.async_add_entities_listener(
152  QingpingBluetoothSensorEntity, async_add_entities
153  )
154  )
155  entry.async_on_unload(
156  coordinator.async_register_processor(processor, SensorEntityDescription)
157  )
158 
159 
161  PassiveBluetoothProcessorEntity[
162  PassiveBluetoothDataProcessor[float | int | None, SensorUpdate]
163  ],
164  SensorEntity,
165 ):
166  """Representation of a Qingping sensor."""
167 
168  @property
169  def native_value(self) -> int | float | None:
170  """Return the native value."""
171  return self.processor.entity_data.get(self.entity_key)
PassiveBluetoothEntityKey device_key_to_bluetooth_entity_key(DeviceKey device_key)
Definition: device.py:14
PassiveBluetoothDataUpdate sensor_update_to_bluetooth_data_update(SensorUpdate sensor_update)
Definition: sensor.py:117
None async_setup_entry(HomeAssistant hass, QingpingConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:146
DeviceInfo sensor_device_info_to_hass_device_info(SensorDeviceInfo sensor_device_info)
Definition: sensor.py:20