1 """Support for moat ble sensors."""
3 from __future__
import annotations
5 from moat_ble
import DeviceClass, DeviceKey, SensorUpdate, Units
7 from homeassistant
import config_entries
9 PassiveBluetoothDataProcessor,
10 PassiveBluetoothDataUpdate,
11 PassiveBluetoothEntityKey,
12 PassiveBluetoothProcessorCoordinator,
13 PassiveBluetoothProcessorEntity,
18 SensorEntityDescription,
23 SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
24 UnitOfElectricPotential,
31 from .const
import DOMAIN
33 SENSOR_DESCRIPTIONS = {
35 key=f
"{DeviceClass.TEMPERATURE}_{Units.TEMP_CELSIUS}",
36 device_class=SensorDeviceClass.TEMPERATURE,
37 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
38 state_class=SensorStateClass.MEASUREMENT,
41 key=f
"{DeviceClass.HUMIDITY}_{Units.PERCENTAGE}",
42 device_class=SensorDeviceClass.HUMIDITY,
43 native_unit_of_measurement=PERCENTAGE,
44 state_class=SensorStateClass.MEASUREMENT,
47 key=f
"{DeviceClass.BATTERY}_{Units.PERCENTAGE}",
48 device_class=SensorDeviceClass.BATTERY,
49 native_unit_of_measurement=PERCENTAGE,
50 state_class=SensorStateClass.MEASUREMENT,
53 key=f
"{DeviceClass.VOLTAGE}_{Units.ELECTRIC_POTENTIAL_VOLT}",
54 device_class=SensorDeviceClass.VOLTAGE,
55 native_unit_of_measurement=UnitOfElectricPotential.VOLT,
56 state_class=SensorStateClass.MEASUREMENT,
59 DeviceClass.SIGNAL_STRENGTH,
60 Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
62 key=f
"{DeviceClass.SIGNAL_STRENGTH}_{Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT}",
63 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
64 native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
65 state_class=SensorStateClass.MEASUREMENT,
66 entity_registry_enabled_default=
False,
72 device_key: DeviceKey,
73 ) -> PassiveBluetoothEntityKey:
74 """Convert a device key to an entity key."""
79 sensor_update: SensorUpdate,
80 ) -> PassiveBluetoothDataUpdate:
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()
108 async_add_entities: AddEntitiesCallback,
110 """Set up the Moat BLE sensors."""
111 coordinator: PassiveBluetoothProcessorCoordinator = hass.data[DOMAIN][
115 entry.async_on_unload(
116 processor.async_add_entities_listener(
117 MoatBluetoothSensorEntity, async_add_entities
120 entry.async_on_unload(coordinator.async_register_processor(processor))
124 PassiveBluetoothProcessorEntity[
125 PassiveBluetoothDataProcessor[float | int |
None, SensorUpdate]
129 """Representation of a moat ble sensor."""
133 """Return the native value."""
134 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)
None async_setup_entry(HomeAssistant hass, config_entries.ConfigEntry 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)