1 """Support for xiaomi ble sensors."""
3 from __future__
import annotations
5 from typing
import cast
7 from xiaomi_ble
import DeviceClass, SensorUpdate, Units
8 from xiaomi_ble.parser
import ExtendedSensorDeviceClass
11 PassiveBluetoothDataUpdate,
12 PassiveBluetoothProcessorEntity,
17 SensorEntityDescription,
21 CONCENTRATION_MILLIGRAMS_PER_CUBIC_METER,
24 SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
27 UnitOfElectricPotential,
37 from .coordinator
import XiaomiPassiveBluetoothDataProcessor
38 from .device
import device_key_to_bluetooth_entity_key
39 from .types
import XiaomiBLEConfigEntry
41 SENSOR_DESCRIPTIONS = {
43 key=f
"{DeviceClass.BATTERY}_{Units.PERCENTAGE}",
44 device_class=SensorDeviceClass.BATTERY,
45 native_unit_of_measurement=PERCENTAGE,
46 state_class=SensorStateClass.MEASUREMENT,
47 entity_category=EntityCategory.DIAGNOSTIC,
50 key=
str(Units.CONDUCTIVITY),
51 device_class=SensorDeviceClass.CONDUCTIVITY,
52 native_unit_of_measurement=UnitOfConductivity.MICROSIEMENS_PER_CM,
53 state_class=SensorStateClass.MEASUREMENT,
56 DeviceClass.FORMALDEHYDE,
57 Units.CONCENTRATION_MILLIGRAMS_PER_CUBIC_METER,
59 key=f
"{DeviceClass.FORMALDEHYDE}_{Units.CONCENTRATION_MILLIGRAMS_PER_CUBIC_METER}",
60 native_unit_of_measurement=CONCENTRATION_MILLIGRAMS_PER_CUBIC_METER,
61 state_class=SensorStateClass.MEASUREMENT,
64 key=f
"{DeviceClass.HUMIDITY}_{Units.PERCENTAGE}",
65 device_class=SensorDeviceClass.HUMIDITY,
66 native_unit_of_measurement=PERCENTAGE,
67 state_class=SensorStateClass.MEASUREMENT,
70 key=f
"{DeviceClass.ILLUMINANCE}_{Units.LIGHT_LUX}",
71 device_class=SensorDeviceClass.ILLUMINANCE,
72 native_unit_of_measurement=LIGHT_LUX,
73 state_class=SensorStateClass.MEASUREMENT,
77 key=f
"{DeviceClass.IMPEDANCE}_{Units.OHM}",
79 native_unit_of_measurement=Units.OHM,
80 state_class=SensorStateClass.MEASUREMENT,
84 key=f
"{DeviceClass.MASS}_{Units.MASS_KILOGRAMS}",
85 device_class=SensorDeviceClass.WEIGHT,
86 native_unit_of_measurement=UnitOfMass.KILOGRAMS,
87 state_class=SensorStateClass.MEASUREMENT,
91 key=f
"{DeviceClass.MASS_NON_STABILIZED}_{Units.MASS_KILOGRAMS}",
92 device_class=SensorDeviceClass.WEIGHT,
93 native_unit_of_measurement=UnitOfMass.KILOGRAMS,
94 state_class=SensorStateClass.MEASUREMENT,
95 entity_category=EntityCategory.DIAGNOSTIC,
98 key=f
"{DeviceClass.MOISTURE}_{Units.PERCENTAGE}",
99 device_class=SensorDeviceClass.MOISTURE,
100 native_unit_of_measurement=PERCENTAGE,
101 state_class=SensorStateClass.MEASUREMENT,
104 key=f
"{DeviceClass.PRESSURE}_{Units.PRESSURE_MBAR}",
105 device_class=SensorDeviceClass.PRESSURE,
106 native_unit_of_measurement=UnitOfPressure.MBAR,
107 state_class=SensorStateClass.MEASUREMENT,
110 DeviceClass.SIGNAL_STRENGTH,
111 Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
113 key=f
"{DeviceClass.SIGNAL_STRENGTH}_{Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT}",
114 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
115 native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
116 state_class=SensorStateClass.MEASUREMENT,
117 entity_registry_enabled_default=
False,
118 entity_category=EntityCategory.DIAGNOSTIC,
121 key=f
"{DeviceClass.TEMPERATURE}_{Units.TEMP_CELSIUS}",
122 device_class=SensorDeviceClass.TEMPERATURE,
123 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
124 state_class=SensorStateClass.MEASUREMENT,
127 key=f
"{DeviceClass.VOLTAGE}_{Units.ELECTRIC_POTENTIAL_VOLT}",
128 device_class=SensorDeviceClass.VOLTAGE,
129 native_unit_of_measurement=UnitOfElectricPotential.VOLT,
130 state_class=SensorStateClass.MEASUREMENT,
131 entity_category=EntityCategory.DIAGNOSTIC,
135 key=
str(ExtendedSensorDeviceClass.CONSUMABLE),
136 native_unit_of_measurement=PERCENTAGE,
137 state_class=SensorStateClass.MEASUREMENT,
141 key=
str(ExtendedSensorDeviceClass.SCORE),
142 state_class=SensorStateClass.MEASUREMENT,
146 key=
str(ExtendedSensorDeviceClass.COUNTER),
147 native_unit_of_measurement=UnitOfTime.SECONDS,
148 state_class=SensorStateClass.MEASUREMENT,
152 key=
str(ExtendedSensorDeviceClass.KEY_ID), icon=
"mdi:identifier"
156 key=
str(ExtendedSensorDeviceClass.LOCK_METHOD), icon=
"mdi:key-variant"
160 ExtendedSensorDeviceClass.DURATION_DETECTED,
163 key=
str(ExtendedSensorDeviceClass.DURATION_DETECTED),
164 native_unit_of_measurement=UnitOfTime.MINUTES,
165 state_class=SensorStateClass.MEASUREMENT,
169 ExtendedSensorDeviceClass.DURATION_CLEARED,
172 key=
str(ExtendedSensorDeviceClass.DURATION_CLEARED),
173 native_unit_of_measurement=UnitOfTime.MINUTES,
174 state_class=SensorStateClass.MEASUREMENT,
180 sensor_update: SensorUpdate,
181 ) -> PassiveBluetoothDataUpdate[float |
None]:
182 """Convert a sensor update to a bluetooth data update."""
186 for device_id, device_info
in sensor_update.devices.items()
188 entity_descriptions={
190 (description.device_class, description.native_unit_of_measurement)
192 for device_key, description
in sensor_update.entity_descriptions.items()
193 if description.device_class
197 float |
None, sensor_values.native_value
199 for device_key, sensor_values
in sensor_update.entity_values.items()
203 for device_key, sensor_values
in sensor_update.entity_values.items()
210 entry: XiaomiBLEConfigEntry,
211 async_add_entities: AddEntitiesCallback,
213 """Set up the Xiaomi BLE sensors."""
214 coordinator = entry.runtime_data
216 sensor_update_to_bluetooth_data_update
218 entry.async_on_unload(
219 processor.async_add_entities_listener(
220 XiaomiBluetoothSensorEntity, async_add_entities
223 entry.async_on_unload(
224 coordinator.async_register_processor(processor, SensorEntityDescription)
229 PassiveBluetoothProcessorEntity[XiaomiPassiveBluetoothDataProcessor[float |
None]],
232 """Representation of a xiaomi ble sensor."""
236 """Return the native value."""
237 return self.processor.entity_data.get(self.entity_key)
241 """Return True if entity is available."""
242 return self.processor.coordinator.sleepy_device
or super().available
int|float|None native_value(self)
PassiveBluetoothEntityKey device_key_to_bluetooth_entity_key(DeviceKey device_key)
PassiveBluetoothDataUpdate[float|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)