Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for moat ble sensors."""
2 
3 from __future__ import annotations
4 
5 from moat_ble import DeviceClass, DeviceKey, SensorUpdate, Units
6 
7 from homeassistant import config_entries
9  PassiveBluetoothDataProcessor,
10  PassiveBluetoothDataUpdate,
11  PassiveBluetoothEntityKey,
12  PassiveBluetoothProcessorCoordinator,
13  PassiveBluetoothProcessorEntity,
14 )
16  SensorDeviceClass,
17  SensorEntity,
18  SensorEntityDescription,
19  SensorStateClass,
20 )
21 from homeassistant.const import (
22  PERCENTAGE,
23  SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
24  UnitOfElectricPotential,
25  UnitOfTemperature,
26 )
27 from homeassistant.core import HomeAssistant
28 from homeassistant.helpers.entity_platform import AddEntitiesCallback
29 from homeassistant.helpers.sensor import sensor_device_info_to_hass_device_info
30 
31 from .const import DOMAIN
32 
33 SENSOR_DESCRIPTIONS = {
34  (DeviceClass.TEMPERATURE, Units.TEMP_CELSIUS): SensorEntityDescription(
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,
39  ),
40  (DeviceClass.HUMIDITY, Units.PERCENTAGE): SensorEntityDescription(
41  key=f"{DeviceClass.HUMIDITY}_{Units.PERCENTAGE}",
42  device_class=SensorDeviceClass.HUMIDITY,
43  native_unit_of_measurement=PERCENTAGE,
44  state_class=SensorStateClass.MEASUREMENT,
45  ),
46  (DeviceClass.BATTERY, Units.PERCENTAGE): SensorEntityDescription(
47  key=f"{DeviceClass.BATTERY}_{Units.PERCENTAGE}",
48  device_class=SensorDeviceClass.BATTERY,
49  native_unit_of_measurement=PERCENTAGE,
50  state_class=SensorStateClass.MEASUREMENT,
51  ),
52  (DeviceClass.VOLTAGE, Units.ELECTRIC_POTENTIAL_VOLT): SensorEntityDescription(
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,
57  ),
58  (
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,
67  ),
68 }
69 
70 
72  device_key: DeviceKey,
73 ) -> PassiveBluetoothEntityKey:
74  """Convert a device key to an entity key."""
75  return PassiveBluetoothEntityKey(device_key.key, device_key.device_id)
76 
77 
79  sensor_update: SensorUpdate,
80 ) -> PassiveBluetoothDataUpdate:
81  """Convert a sensor update to a bluetooth data update."""
83  devices={
84  device_id: sensor_device_info_to_hass_device_info(device_info)
85  for device_id, device_info in sensor_update.devices.items()
86  },
87  entity_descriptions={
88  _device_key_to_bluetooth_entity_key(device_key): SENSOR_DESCRIPTIONS[
89  (description.device_class, description.native_unit_of_measurement)
90  ]
91  for device_key, description in sensor_update.entity_descriptions.items()
92  if description.device_class and description.native_unit_of_measurement
93  },
94  entity_data={
95  _device_key_to_bluetooth_entity_key(device_key): sensor_values.native_value
96  for device_key, sensor_values in sensor_update.entity_values.items()
97  },
98  entity_names={
99  _device_key_to_bluetooth_entity_key(device_key): sensor_values.name
100  for device_key, sensor_values in sensor_update.entity_values.items()
101  },
102  )
103 
104 
106  hass: HomeAssistant,
108  async_add_entities: AddEntitiesCallback,
109 ) -> None:
110  """Set up the Moat BLE sensors."""
111  coordinator: PassiveBluetoothProcessorCoordinator = hass.data[DOMAIN][
112  entry.entry_id
113  ]
114  processor = PassiveBluetoothDataProcessor(sensor_update_to_bluetooth_data_update)
115  entry.async_on_unload(
116  processor.async_add_entities_listener(
117  MoatBluetoothSensorEntity, async_add_entities
118  )
119  )
120  entry.async_on_unload(coordinator.async_register_processor(processor))
121 
122 
124  PassiveBluetoothProcessorEntity[
125  PassiveBluetoothDataProcessor[float | int | None, SensorUpdate]
126  ],
127  SensorEntity,
128 ):
129  """Representation of a moat ble sensor."""
130 
131  @property
132  def native_value(self) -> int | float | None:
133  """Return the native value."""
134  return self.processor.entity_data.get(self.entity_key)
PassiveBluetoothEntityKey _device_key_to_bluetooth_entity_key(DeviceKey device_key)
Definition: sensor.py:73
None async_setup_entry(HomeAssistant hass, config_entries.ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:109
PassiveBluetoothDataUpdate sensor_update_to_bluetooth_data_update(SensorUpdate sensor_update)
Definition: sensor.py:80
DeviceInfo sensor_device_info_to_hass_device_info(SensorDeviceInfo sensor_device_info)
Definition: sensor.py:20