Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Mopeka sensors."""
2 
3 from __future__ import annotations
4 
5 from mopeka_iot_ble import SensorUpdate
6 
8  PassiveBluetoothDataProcessor,
9  PassiveBluetoothDataUpdate,
10  PassiveBluetoothProcessorEntity,
11 )
13  SensorDeviceClass,
14  SensorEntity,
15  SensorEntityDescription,
16  SensorStateClass,
17 )
18 from homeassistant.const import (
19  PERCENTAGE,
20  SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
21  EntityCategory,
22  UnitOfElectricPotential,
23  UnitOfLength,
24  UnitOfTemperature,
25 )
26 from homeassistant.core import HomeAssistant
27 from homeassistant.helpers.entity_platform import AddEntitiesCallback
28 from homeassistant.helpers.sensor import sensor_device_info_to_hass_device_info
29 
30 from . import MopekaConfigEntry
31 from .device import device_key_to_bluetooth_entity_key
32 
33 SENSOR_DESCRIPTIONS = {
34  "battery": SensorEntityDescription(
35  key="battery",
36  device_class=SensorDeviceClass.BATTERY,
37  native_unit_of_measurement=PERCENTAGE,
38  state_class=SensorStateClass.MEASUREMENT,
39  entity_category=EntityCategory.DIAGNOSTIC,
40  ),
41  "battery_voltage": SensorEntityDescription(
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,
48  ),
49  "tank_level": SensorEntityDescription(
50  key="tank_level",
51  device_class=SensorDeviceClass.DISTANCE,
52  native_unit_of_measurement=UnitOfLength.MILLIMETERS,
53  state_class=SensorStateClass.MEASUREMENT,
54  ),
55  "signal_strength": SensorEntityDescription(
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,
62  ),
63  "reading_quality": SensorEntityDescription(
64  key="reading_quality",
65  entity_category=EntityCategory.DIAGNOSTIC,
66  native_unit_of_measurement=PERCENTAGE,
67  state_class=SensorStateClass.MEASUREMENT,
68  ),
69  "temperature": SensorEntityDescription(
70  key="temperature",
71  device_class=SensorDeviceClass.TEMPERATURE,
72  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
73  state_class=SensorStateClass.MEASUREMENT,
74  ),
75  "accelerometer_x": SensorEntityDescription(
76  key="accelerometer_x",
77  entity_category=EntityCategory.DIAGNOSTIC,
78  entity_registry_enabled_default=False,
79  ),
80  "accelerometer_y": SensorEntityDescription(
81  key="accelerometer_y",
82  entity_category=EntityCategory.DIAGNOSTIC,
83  entity_registry_enabled_default=False,
84  ),
85 }
86 
87 
89  sensor_update: SensorUpdate,
90 ) -> PassiveBluetoothDataUpdate:
91  """Convert a sensor update to a bluetooth data update."""
93  devices={
94  device_id: sensor_device_info_to_hass_device_info(device_info)
95  for device_id, device_info in sensor_update.devices.items()
96  },
97  entity_descriptions={
98  device_key_to_bluetooth_entity_key(device_key): SENSOR_DESCRIPTIONS[
99  device_key.key
100  ]
101  for device_key in sensor_update.entity_descriptions
102  if device_key.key in SENSOR_DESCRIPTIONS
103  },
104  entity_data={
105  device_key_to_bluetooth_entity_key(device_key): sensor_values.native_value
106  for device_key, sensor_values in sensor_update.entity_values.items()
107  },
108  entity_names={
109  device_key_to_bluetooth_entity_key(device_key): sensor_values.name
110  for device_key, sensor_values in sensor_update.entity_values.items()
111  },
112  )
113 
114 
116  hass: HomeAssistant,
117  entry: MopekaConfigEntry,
118  async_add_entities: AddEntitiesCallback,
119 ) -> None:
120  """Set up the Mopeka BLE sensors."""
121  coordinator = entry.runtime_data
122  processor = PassiveBluetoothDataProcessor(sensor_update_to_bluetooth_data_update)
123  entry.async_on_unload(
124  processor.async_add_entities_listener(
125  MopekaBluetoothSensorEntity, async_add_entities
126  )
127  )
128  entry.async_on_unload(coordinator.async_register_processor(processor))
129 
130 
132  PassiveBluetoothProcessorEntity[
133  PassiveBluetoothDataProcessor[float | int | None, SensorUpdate]
134  ],
135  SensorEntity,
136 ):
137  """Representation of a Mopeka sensor."""
138 
139  @property
140  def native_value(self) -> int | float | None:
141  """Return the native value."""
142  return self.processor.entity_data.get(self.entity_key)
PassiveBluetoothEntityKey device_key_to_bluetooth_entity_key(DeviceKey device_key)
Definition: device.py:14
PassiveBluetoothDataUpdate sensor_update_to_bluetooth_data_update(SensorUpdate sensor_update)
Definition: sensor.py:90
None async_setup_entry(HomeAssistant hass, MopekaConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:119
DeviceInfo sensor_device_info_to_hass_device_info(SensorDeviceInfo sensor_device_info)
Definition: sensor.py:20