Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for BlueMaestro sensors."""
2 
3 from __future__ import annotations
4 
5 from bluemaestro_ble import (
6  SensorDeviceClass as BlueMaestroSensorDeviceClass,
7  SensorUpdate,
8  Units,
9 )
10 
12  PassiveBluetoothDataProcessor,
13  PassiveBluetoothDataUpdate,
14  PassiveBluetoothProcessorEntity,
15 )
17  SensorDeviceClass,
18  SensorEntity,
19  SensorEntityDescription,
20  SensorStateClass,
21 )
22 from homeassistant.const import (
23  PERCENTAGE,
24  SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
25  EntityCategory,
26  UnitOfPressure,
27  UnitOfTemperature,
28 )
29 from homeassistant.core import HomeAssistant
30 from homeassistant.helpers.entity_platform import AddEntitiesCallback
31 from homeassistant.helpers.sensor import sensor_device_info_to_hass_device_info
32 
33 from . import BlueMaestroConfigEntry
34 from .device import device_key_to_bluetooth_entity_key
35 
36 SENSOR_DESCRIPTIONS = {
37  (BlueMaestroSensorDeviceClass.BATTERY, Units.PERCENTAGE): SensorEntityDescription(
38  key=f"{BlueMaestroSensorDeviceClass.BATTERY}_{Units.PERCENTAGE}",
39  device_class=SensorDeviceClass.BATTERY,
40  native_unit_of_measurement=PERCENTAGE,
41  state_class=SensorStateClass.MEASUREMENT,
42  entity_category=EntityCategory.DIAGNOSTIC,
43  ),
44  (BlueMaestroSensorDeviceClass.HUMIDITY, Units.PERCENTAGE): SensorEntityDescription(
45  key=f"{BlueMaestroSensorDeviceClass.HUMIDITY}_{Units.PERCENTAGE}",
46  device_class=SensorDeviceClass.HUMIDITY,
47  native_unit_of_measurement=PERCENTAGE,
48  state_class=SensorStateClass.MEASUREMENT,
49  ),
50  (
51  BlueMaestroSensorDeviceClass.SIGNAL_STRENGTH,
52  Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
54  key=f"{BlueMaestroSensorDeviceClass.SIGNAL_STRENGTH}_{Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT}",
55  device_class=SensorDeviceClass.SIGNAL_STRENGTH,
56  native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
57  state_class=SensorStateClass.MEASUREMENT,
58  entity_category=EntityCategory.DIAGNOSTIC,
59  entity_registry_enabled_default=False,
60  ),
61  (
62  BlueMaestroSensorDeviceClass.TEMPERATURE,
63  Units.TEMP_CELSIUS,
65  key=f"{BlueMaestroSensorDeviceClass.TEMPERATURE}_{Units.TEMP_CELSIUS}",
66  device_class=SensorDeviceClass.TEMPERATURE,
67  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
68  state_class=SensorStateClass.MEASUREMENT,
69  ),
70  (
71  BlueMaestroSensorDeviceClass.DEW_POINT,
72  Units.TEMP_CELSIUS,
74  key=f"{BlueMaestroSensorDeviceClass.DEW_POINT}_{Units.TEMP_CELSIUS}",
75  device_class=SensorDeviceClass.TEMPERATURE,
76  translation_key="dew_point",
77  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
78  state_class=SensorStateClass.MEASUREMENT,
79  ),
80  (
81  BlueMaestroSensorDeviceClass.PRESSURE,
82  Units.PRESSURE_MBAR,
84  key=f"{BlueMaestroSensorDeviceClass.PRESSURE}_{Units.PRESSURE_MBAR}",
85  device_class=SensorDeviceClass.PRESSURE,
86  native_unit_of_measurement=UnitOfPressure.MBAR,
87  state_class=SensorStateClass.MEASUREMENT,
88  ),
89 }
90 
91 
93  sensor_update: SensorUpdate,
94 ) -> PassiveBluetoothDataUpdate:
95  """Convert a sensor update to a bluetooth data update."""
97  devices={
98  device_id: sensor_device_info_to_hass_device_info(device_info)
99  for device_id, device_info in sensor_update.devices.items()
100  },
101  entity_descriptions={
102  device_key_to_bluetooth_entity_key(device_key): SENSOR_DESCRIPTIONS[
103  (description.device_class, description.native_unit_of_measurement)
104  ]
105  for device_key, description in sensor_update.entity_descriptions.items()
106  if description.device_class and description.native_unit_of_measurement
107  },
108  entity_data={
109  device_key_to_bluetooth_entity_key(device_key): sensor_values.native_value
110  for device_key, sensor_values in sensor_update.entity_values.items()
111  },
112  entity_names={},
113  )
114 
115 
117  hass: HomeAssistant,
118  entry: BlueMaestroConfigEntry,
119  async_add_entities: AddEntitiesCallback,
120 ) -> None:
121  """Set up the BlueMaestro BLE sensors."""
122  coordinator = entry.runtime_data
123  processor = PassiveBluetoothDataProcessor(sensor_update_to_bluetooth_data_update)
124  entry.async_on_unload(
125  processor.async_add_entities_listener(
126  BlueMaestroBluetoothSensorEntity, async_add_entities
127  )
128  )
129  entry.async_on_unload(coordinator.async_register_processor(processor))
130 
131 
133  PassiveBluetoothProcessorEntity[
134  PassiveBluetoothDataProcessor[float | int | None, SensorUpdate]
135  ],
136  SensorEntity,
137 ):
138  """Representation of a BlueMaestro sensor."""
139 
140  @property
141  def native_value(self) -> int | float | None:
142  """Return the native value."""
143  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:94
None async_setup_entry(HomeAssistant hass, BlueMaestroConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:120
DeviceInfo sensor_device_info_to_hass_device_info(SensorDeviceInfo sensor_device_info)
Definition: sensor.py:20