Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for ThermoBeacon sensors."""
2 
3 from __future__ import annotations
4 
5 from thermobeacon_ble import (
6  SensorDeviceClass as ThermoBeaconSensorDeviceClass,
7  SensorUpdate,
8  Units,
9 )
10 
11 from homeassistant import config_entries
13  PassiveBluetoothDataProcessor,
14  PassiveBluetoothDataUpdate,
15  PassiveBluetoothProcessorCoordinator,
16  PassiveBluetoothProcessorEntity,
17 )
19  SensorDeviceClass,
20  SensorEntity,
21  SensorEntityDescription,
22  SensorStateClass,
23 )
24 from homeassistant.const import (
25  PERCENTAGE,
26  SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
27  EntityCategory,
28  UnitOfElectricPotential,
29  UnitOfTemperature,
30 )
31 from homeassistant.core import HomeAssistant
32 from homeassistant.helpers.entity_platform import AddEntitiesCallback
33 from homeassistant.helpers.sensor import sensor_device_info_to_hass_device_info
34 
35 from .const import DOMAIN
36 from .device import device_key_to_bluetooth_entity_key
37 
38 SENSOR_DESCRIPTIONS = {
39  (ThermoBeaconSensorDeviceClass.BATTERY, Units.PERCENTAGE): SensorEntityDescription(
40  key=f"{ThermoBeaconSensorDeviceClass.BATTERY}_{Units.PERCENTAGE}",
41  device_class=SensorDeviceClass.BATTERY,
42  native_unit_of_measurement=PERCENTAGE,
43  state_class=SensorStateClass.MEASUREMENT,
44  entity_category=EntityCategory.DIAGNOSTIC,
45  ),
46  (ThermoBeaconSensorDeviceClass.HUMIDITY, Units.PERCENTAGE): SensorEntityDescription(
47  key=f"{ThermoBeaconSensorDeviceClass.HUMIDITY}_{Units.PERCENTAGE}",
48  device_class=SensorDeviceClass.HUMIDITY,
49  native_unit_of_measurement=PERCENTAGE,
50  state_class=SensorStateClass.MEASUREMENT,
51  ),
52  (
53  ThermoBeaconSensorDeviceClass.SIGNAL_STRENGTH,
54  Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
56  key=f"{ThermoBeaconSensorDeviceClass.SIGNAL_STRENGTH}_{Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT}",
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  (
64  ThermoBeaconSensorDeviceClass.TEMPERATURE,
65  Units.TEMP_CELSIUS,
67  key=f"{ThermoBeaconSensorDeviceClass.TEMPERATURE}_{Units.TEMP_CELSIUS}",
68  device_class=SensorDeviceClass.TEMPERATURE,
69  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
70  state_class=SensorStateClass.MEASUREMENT,
71  ),
72  (
73  ThermoBeaconSensorDeviceClass.VOLTAGE,
74  Units.ELECTRIC_POTENTIAL_VOLT,
76  key=f"{ThermoBeaconSensorDeviceClass.VOLTAGE}_{Units.ELECTRIC_POTENTIAL_VOLT}",
77  device_class=SensorDeviceClass.VOLTAGE,
78  native_unit_of_measurement=UnitOfElectricPotential.VOLT,
79  state_class=SensorStateClass.MEASUREMENT,
80  entity_category=EntityCategory.DIAGNOSTIC,
81  entity_registry_enabled_default=False,
82  ),
83 }
84 
85 
87  sensor_update: SensorUpdate,
88 ) -> PassiveBluetoothDataUpdate:
89  """Convert a sensor update to a bluetooth data update."""
91  devices={
92  device_id: sensor_device_info_to_hass_device_info(device_info)
93  for device_id, device_info in sensor_update.devices.items()
94  },
95  entity_descriptions={
96  device_key_to_bluetooth_entity_key(device_key): SENSOR_DESCRIPTIONS[
97  (description.device_class, description.native_unit_of_measurement)
98  ]
99  for device_key, description in sensor_update.entity_descriptions.items()
100  if description.device_class and description.native_unit_of_measurement
101  },
102  entity_data={
103  device_key_to_bluetooth_entity_key(device_key): sensor_values.native_value
104  for device_key, sensor_values in sensor_update.entity_values.items()
105  },
106  entity_names={
107  device_key_to_bluetooth_entity_key(device_key): sensor_values.name
108  for device_key, sensor_values in sensor_update.entity_values.items()
109  },
110  )
111 
112 
114  hass: HomeAssistant,
116  async_add_entities: AddEntitiesCallback,
117 ) -> None:
118  """Set up the ThermoBeacon BLE sensors."""
119  coordinator: PassiveBluetoothProcessorCoordinator = hass.data[DOMAIN][
120  entry.entry_id
121  ]
122  processor = PassiveBluetoothDataProcessor(sensor_update_to_bluetooth_data_update)
123  entry.async_on_unload(
124  processor.async_add_entities_listener(
125  ThermoBeaconBluetoothSensorEntity, 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 ThermoBeacon 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:88
None async_setup_entry(HomeAssistant hass, config_entries.ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:117
DeviceInfo sensor_device_info_to_hass_device_info(SensorDeviceInfo sensor_device_info)
Definition: sensor.py:20