1 """Support for Mopeka sensors."""
3 from __future__
import annotations
5 from mopeka_iot_ble
import SensorUpdate
8 PassiveBluetoothDataProcessor,
9 PassiveBluetoothDataUpdate,
10 PassiveBluetoothProcessorEntity,
15 SensorEntityDescription,
20 SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
22 UnitOfElectricPotential,
30 from .
import MopekaConfigEntry
31 from .device
import device_key_to_bluetooth_entity_key
33 SENSOR_DESCRIPTIONS = {
36 device_class=SensorDeviceClass.BATTERY,
37 native_unit_of_measurement=PERCENTAGE,
38 state_class=SensorStateClass.MEASUREMENT,
39 entity_category=EntityCategory.DIAGNOSTIC,
42 key=
"battery_voltage",
43 device_class=SensorDeviceClass.VOLTAGE,
44 native_unit_of_measurement=UnitOfElectricPotential.VOLT,
45 state_class=SensorStateClass.MEASUREMENT,
46 entity_category=EntityCategory.DIAGNOSTIC,
47 entity_registry_enabled_default=
False,
51 device_class=SensorDeviceClass.DISTANCE,
52 native_unit_of_measurement=UnitOfLength.MILLIMETERS,
53 state_class=SensorStateClass.MEASUREMENT,
56 key=
"signal_strength",
57 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
58 native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
59 state_class=SensorStateClass.MEASUREMENT,
60 entity_category=EntityCategory.DIAGNOSTIC,
61 entity_registry_enabled_default=
False,
64 key=
"reading_quality",
65 entity_category=EntityCategory.DIAGNOSTIC,
66 native_unit_of_measurement=PERCENTAGE,
67 state_class=SensorStateClass.MEASUREMENT,
71 device_class=SensorDeviceClass.TEMPERATURE,
72 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
73 state_class=SensorStateClass.MEASUREMENT,
76 key=
"accelerometer_x",
77 entity_category=EntityCategory.DIAGNOSTIC,
78 entity_registry_enabled_default=
False,
81 key=
"accelerometer_y",
82 entity_category=EntityCategory.DIAGNOSTIC,
83 entity_registry_enabled_default=
False,
89 sensor_update: SensorUpdate,
90 ) -> PassiveBluetoothDataUpdate:
91 """Convert a sensor update to a bluetooth data update."""
95 for device_id, device_info
in sensor_update.devices.items()
101 for device_key
in sensor_update.entity_descriptions
102 if device_key.key
in SENSOR_DESCRIPTIONS
106 for device_key, sensor_values
in sensor_update.entity_values.items()
110 for device_key, sensor_values
in sensor_update.entity_values.items()
117 entry: MopekaConfigEntry,
118 async_add_entities: AddEntitiesCallback,
120 """Set up the Mopeka BLE sensors."""
121 coordinator = entry.runtime_data
123 entry.async_on_unload(
124 processor.async_add_entities_listener(
125 MopekaBluetoothSensorEntity, async_add_entities
128 entry.async_on_unload(coordinator.async_register_processor(processor))
132 PassiveBluetoothProcessorEntity[
133 PassiveBluetoothDataProcessor[float | int |
None, SensorUpdate]
137 """Representation of a Mopeka sensor."""
141 """Return the native value."""
142 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, MopekaConfigEntry entry, AddEntitiesCallback async_add_entities)
DeviceInfo sensor_device_info_to_hass_device_info(SensorDeviceInfo sensor_device_info)