Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for RAPT Pill hydrometers."""
2 
3 from __future__ import annotations
4 
5 from rapt_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  EntityCategory,
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.SPECIFIC_GRAVITY, Units.SPECIFIC_GRAVITY): SensorEntityDescription(
41  key=f"{DeviceClass.SPECIFIC_GRAVITY}_{Units.SPECIFIC_GRAVITY}",
42  state_class=SensorStateClass.MEASUREMENT,
43  ),
44  (DeviceClass.BATTERY, Units.PERCENTAGE): SensorEntityDescription(
45  key=f"{DeviceClass.BATTERY}_{Units.PERCENTAGE}",
46  device_class=SensorDeviceClass.BATTERY,
47  native_unit_of_measurement=PERCENTAGE,
48  state_class=SensorStateClass.MEASUREMENT,
49  entity_category=EntityCategory.DIAGNOSTIC,
50  ),
51  (
52  DeviceClass.SIGNAL_STRENGTH,
53  Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
55  key=f"{DeviceClass.SIGNAL_STRENGTH}_{Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT}",
56  device_class=SensorDeviceClass.SIGNAL_STRENGTH,
57  native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
58  state_class=SensorStateClass.MEASUREMENT,
59  entity_registry_enabled_default=False,
60  entity_category=EntityCategory.DIAGNOSTIC,
61  ),
62 }
63 
64 
66  device_key: DeviceKey,
67 ) -> PassiveBluetoothEntityKey:
68  """Convert a device key to an entity key."""
69  return PassiveBluetoothEntityKey(device_key.key, device_key.device_id)
70 
71 
73  sensor_update: SensorUpdate,
74 ) -> PassiveBluetoothDataUpdate:
75  """Convert a sensor update to a bluetooth data update."""
77  devices={
78  device_id: sensor_device_info_to_hass_device_info(device_info)
79  for device_id, device_info in sensor_update.devices.items()
80  },
81  entity_descriptions={
82  _device_key_to_bluetooth_entity_key(device_key): SENSOR_DESCRIPTIONS[
83  (description.device_class, description.native_unit_of_measurement)
84  ]
85  for device_key, description in sensor_update.entity_descriptions.items()
86  if description.device_class and description.native_unit_of_measurement
87  },
88  entity_data={
89  _device_key_to_bluetooth_entity_key(device_key): sensor_values.native_value
90  for device_key, sensor_values in sensor_update.entity_values.items()
91  },
92  entity_names={
93  _device_key_to_bluetooth_entity_key(device_key): sensor_values.name
94  for device_key, sensor_values in sensor_update.entity_values.items()
95  },
96  )
97 
98 
100  hass: HomeAssistant,
102  async_add_entities: AddEntitiesCallback,
103 ) -> None:
104  """Set up the RAPT Pill BLE sensors."""
105  coordinator: PassiveBluetoothProcessorCoordinator = hass.data[DOMAIN][
106  entry.entry_id
107  ]
108  processor = PassiveBluetoothDataProcessor(sensor_update_to_bluetooth_data_update)
109  entry.async_on_unload(
110  processor.async_add_entities_listener(
111  RAPTPillBluetoothSensorEntity, async_add_entities
112  )
113  )
114  entry.async_on_unload(coordinator.async_register_processor(processor))
115 
116 
118  PassiveBluetoothProcessorEntity[
119  PassiveBluetoothDataProcessor[float | int | None, SensorUpdate]
120  ],
121  SensorEntity,
122 ):
123  """Representation of a RAPT Pill BLE sensor."""
124 
125  @property
126  def native_value(self) -> int | float | None:
127  """Return the native value."""
128  return self.processor.entity_data.get(self.entity_key)
PassiveBluetoothDataUpdate sensor_update_to_bluetooth_data_update(SensorUpdate sensor_update)
Definition: sensor.py:74
PassiveBluetoothEntityKey _device_key_to_bluetooth_entity_key(DeviceKey device_key)
Definition: sensor.py:67
None async_setup_entry(HomeAssistant hass, config_entries.ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:103
DeviceInfo sensor_device_info_to_hass_device_info(SensorDeviceInfo sensor_device_info)
Definition: sensor.py:20