1 """Support for govee ble sensors."""
3 from __future__
import annotations
5 from datetime
import date, datetime
6 from decimal
import Decimal
8 from govee_ble
import DeviceClass, SensorUpdate, Units
9 from govee_ble.parser
import ERROR
12 PassiveBluetoothDataProcessor,
13 PassiveBluetoothDataUpdate,
14 PassiveBluetoothProcessorEntity,
19 SensorEntityDescription,
23 CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
25 SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
32 from .coordinator
import GoveeBLEConfigEntry, GoveeBLEPassiveBluetoothDataProcessor
33 from .device
import device_key_to_bluetooth_entity_key
35 type _SensorValueType = str | int | float | date | datetime | Decimal |
None
37 SENSOR_DESCRIPTIONS = {
39 key=f
"{DeviceClass.TEMPERATURE}_{Units.TEMP_CELSIUS}",
40 device_class=SensorDeviceClass.TEMPERATURE,
41 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
42 state_class=SensorStateClass.MEASUREMENT,
45 key=f
"{DeviceClass.HUMIDITY}_{Units.PERCENTAGE}",
46 device_class=SensorDeviceClass.HUMIDITY,
47 native_unit_of_measurement=PERCENTAGE,
48 state_class=SensorStateClass.MEASUREMENT,
51 key=f
"{DeviceClass.BATTERY}_{Units.PERCENTAGE}",
52 device_class=SensorDeviceClass.BATTERY,
53 native_unit_of_measurement=PERCENTAGE,
54 state_class=SensorStateClass.MEASUREMENT,
57 DeviceClass.SIGNAL_STRENGTH,
58 Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
60 key=f
"{DeviceClass.SIGNAL_STRENGTH}_{Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT}",
61 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
62 native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
63 state_class=SensorStateClass.MEASUREMENT,
64 entity_registry_enabled_default=
False,
68 Units.CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
70 key=f
"{DeviceClass.PM25}_{Units.CONCENTRATION_MICROGRAMS_PER_CUBIC_METER}",
71 device_class=SensorDeviceClass.PM25,
72 native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
73 state_class=SensorStateClass.MEASUREMENT,
79 sensor_update: SensorUpdate,
80 ) -> PassiveBluetoothDataUpdate[_SensorValueType]:
81 """Convert a sensor update to a bluetooth data update."""
85 for device_id, device_info
in sensor_update.devices.items()
89 (description.device_class, description.native_unit_of_measurement)
91 for device_key, description
in sensor_update.entity_descriptions.items()
92 if description.device_class
and description.native_unit_of_measurement
96 for device_key, sensor_values
in sensor_update.entity_values.items()
100 for device_key, sensor_values
in sensor_update.entity_values.items()
107 entry: GoveeBLEConfigEntry,
108 async_add_entities: AddEntitiesCallback,
110 """Set up the Govee BLE sensors."""
111 coordinator = entry.runtime_data
113 entry.async_on_unload(
114 processor.async_add_entities_listener(
115 GoveeBluetoothSensorEntity, async_add_entities
118 entry.async_on_unload(
119 coordinator.async_register_processor(processor, SensorEntityDescription)
124 PassiveBluetoothProcessorEntity[
125 PassiveBluetoothDataProcessor[_SensorValueType, SensorUpdate]
129 """Representation of a govee ble sensor."""
131 processor: GoveeBLEPassiveBluetoothDataProcessor[_SensorValueType]
135 """Return False if sensor is in error."""
136 coordinator = self.processor.coordinator
137 return self.processor.entity_data.get(self.entity_key) != ERROR
and (
138 ((model_info := coordinator.model_info)
and model_info.sleepy)
144 """Return the native value."""
145 return self.processor.entity_data.get(self.entity_key)
_SensorValueType native_value(self)
PassiveBluetoothEntityKey device_key_to_bluetooth_entity_key(DeviceKey device_key)
PassiveBluetoothDataUpdate[_SensorValueType] sensor_update_to_bluetooth_data_update(SensorUpdate sensor_update)
None async_setup_entry(HomeAssistant hass, GoveeBLEConfigEntry entry, AddEntitiesCallback async_add_entities)
DeviceInfo sensor_device_info_to_hass_device_info(SensorDeviceInfo sensor_device_info)