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