Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for sensorpush ble sensors."""
2 
3 from __future__ import annotations
4 
5 from sensorpush_ble import DeviceClass, DeviceKey, SensorUpdate, Units
6 
8  PassiveBluetoothDataProcessor,
9  PassiveBluetoothDataUpdate,
10  PassiveBluetoothEntityKey,
11  PassiveBluetoothProcessorEntity,
12 )
14  SensorDeviceClass,
15  SensorEntity,
16  SensorEntityDescription,
17  SensorStateClass,
18 )
19 from homeassistant.const import (
20  PERCENTAGE,
21  SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
22  UnitOfPressure,
23  UnitOfTemperature,
24 )
25 from homeassistant.core import HomeAssistant
26 from homeassistant.helpers.entity_platform import AddEntitiesCallback
27 from homeassistant.helpers.sensor import sensor_device_info_to_hass_device_info
28 
29 from . import SensorPushConfigEntry
30 
31 SENSOR_DESCRIPTIONS = {
32  (DeviceClass.TEMPERATURE, Units.TEMP_CELSIUS): SensorEntityDescription(
33  key=f"{DeviceClass.TEMPERATURE}_{Units.TEMP_CELSIUS}",
34  device_class=SensorDeviceClass.TEMPERATURE,
35  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
36  state_class=SensorStateClass.MEASUREMENT,
37  ),
38  (DeviceClass.HUMIDITY, Units.PERCENTAGE): SensorEntityDescription(
39  key=f"{DeviceClass.HUMIDITY}_{Units.PERCENTAGE}",
40  device_class=SensorDeviceClass.HUMIDITY,
41  native_unit_of_measurement=PERCENTAGE,
42  state_class=SensorStateClass.MEASUREMENT,
43  ),
44  (DeviceClass.PRESSURE, Units.PRESSURE_MBAR): SensorEntityDescription(
45  key=f"{DeviceClass.PRESSURE}_{Units.PRESSURE_MBAR}",
46  device_class=SensorDeviceClass.PRESSURE,
47  native_unit_of_measurement=UnitOfPressure.MBAR,
48  state_class=SensorStateClass.MEASUREMENT,
49  ),
50  (
51  DeviceClass.SIGNAL_STRENGTH,
52  Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
54  key=f"{DeviceClass.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_registry_enabled_default=False,
59  ),
60 }
61 
62 
64  device_key: DeviceKey,
65 ) -> PassiveBluetoothEntityKey:
66  """Convert a device key to an entity key."""
67  return PassiveBluetoothEntityKey(device_key.key, device_key.device_id)
68 
69 
71  sensor_update: SensorUpdate,
72 ) -> PassiveBluetoothDataUpdate:
73  """Convert a sensor update to a bluetooth data update."""
75  devices={
76  device_id: sensor_device_info_to_hass_device_info(device_info)
77  for device_id, device_info in sensor_update.devices.items()
78  },
79  entity_descriptions={
80  _device_key_to_bluetooth_entity_key(device_key): SENSOR_DESCRIPTIONS[
81  (description.device_class, description.native_unit_of_measurement)
82  ]
83  for device_key, description in sensor_update.entity_descriptions.items()
84  if description.device_class and description.native_unit_of_measurement
85  },
86  entity_data={
87  _device_key_to_bluetooth_entity_key(device_key): sensor_values.native_value
88  for device_key, sensor_values in sensor_update.entity_values.items()
89  },
90  entity_names={
91  _device_key_to_bluetooth_entity_key(device_key): sensor_values.name
92  for device_key, sensor_values in sensor_update.entity_values.items()
93  },
94  )
95 
96 
98  hass: HomeAssistant,
99  entry: SensorPushConfigEntry,
100  async_add_entities: AddEntitiesCallback,
101 ) -> None:
102  """Set up the SensorPush BLE sensors."""
103  coordinator = entry.runtime_data
104  processor = PassiveBluetoothDataProcessor(sensor_update_to_bluetooth_data_update)
105  entry.async_on_unload(
106  processor.async_add_entities_listener(
107  SensorPushBluetoothSensorEntity, async_add_entities
108  )
109  )
110  entry.async_on_unload(
111  coordinator.async_register_processor(processor, SensorEntityDescription)
112  )
113 
114 
116  PassiveBluetoothProcessorEntity[
117  PassiveBluetoothDataProcessor[float | int | None, SensorUpdate]
118  ],
119  SensorEntity,
120 ):
121  """Representation of a sensorpush ble sensor."""
122 
123  @property
124  def native_value(self) -> int | float | None:
125  """Return the native value."""
126  return self.processor.entity_data.get(self.entity_key)
PassiveBluetoothDataUpdate sensor_update_to_bluetooth_data_update(SensorUpdate sensor_update)
Definition: sensor.py:72
None async_setup_entry(HomeAssistant hass, SensorPushConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:101
PassiveBluetoothEntityKey _device_key_to_bluetooth_entity_key(DeviceKey device_key)
Definition: sensor.py:65
DeviceInfo sensor_device_info_to_hass_device_info(SensorDeviceInfo sensor_device_info)
Definition: sensor.py:20