1 """Support for Qingping sensors."""
3 from __future__
import annotations
5 from qingping_ble
import (
6 SensorDeviceClass
as QingpingSensorDeviceClass,
12 PassiveBluetoothDataProcessor,
13 PassiveBluetoothDataUpdate,
14 PassiveBluetoothProcessorEntity,
19 SensorEntityDescription,
23 CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
24 CONCENTRATION_PARTS_PER_MILLION,
27 SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
36 from .
import QingpingConfigEntry
37 from .device
import device_key_to_bluetooth_entity_key
39 SENSOR_DESCRIPTIONS = {
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,
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,
57 key=f
"{QingpingSensorDeviceClass.HUMIDITY}_{Units.PERCENTAGE}",
58 device_class=SensorDeviceClass.HUMIDITY,
59 native_unit_of_measurement=PERCENTAGE,
60 state_class=SensorStateClass.MEASUREMENT,
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,
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,
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,
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,
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,
104 QingpingSensorDeviceClass.TEMPERATURE,
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,
116 sensor_update: SensorUpdate,
117 ) -> PassiveBluetoothDataUpdate:
118 """Convert a sensor update to a bluetooth data update."""
122 for device_id, device_info
in sensor_update.devices.items()
124 entity_descriptions={
126 (description.device_class, description.native_unit_of_measurement)
128 for device_key, description
in sensor_update.entity_descriptions.items()
129 if description.device_class
and description.native_unit_of_measurement
133 for device_key, sensor_values
in sensor_update.entity_values.items()
137 for device_key, sensor_values
in sensor_update.entity_values.items()
144 entry: QingpingConfigEntry,
145 async_add_entities: AddEntitiesCallback,
147 """Set up the Qingping BLE sensors."""
148 coordinator = entry.runtime_data
150 entry.async_on_unload(
151 processor.async_add_entities_listener(
152 QingpingBluetoothSensorEntity, async_add_entities
155 entry.async_on_unload(
156 coordinator.async_register_processor(processor, SensorEntityDescription)
161 PassiveBluetoothProcessorEntity[
162 PassiveBluetoothDataProcessor[float | int |
None, SensorUpdate]
166 """Representation of a Qingping sensor."""
170 """Return the native value."""
171 return self.processor.entity_data.get(self.entity_key)
int|float|None native_value(self)
PassiveBluetoothEntityKey device_key_to_bluetooth_entity_key(DeviceKey device_key)
PassiveBluetoothDataUpdate sensor_update_to_bluetooth_data_update(SensorUpdate sensor_update)
None async_setup_entry(HomeAssistant hass, QingpingConfigEntry entry, AddEntitiesCallback async_add_entities)
DeviceInfo sensor_device_info_to_hass_device_info(SensorDeviceInfo sensor_device_info)