Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Sensirion sensors."""
2 
3 from __future__ import annotations
4 
5 from sensor_state_data import (
6  DeviceKey,
7  SensorDescription,
8  SensorDeviceClass as SSDSensorDeviceClass,
9  SensorUpdate,
10  Units,
11 )
12 
13 from homeassistant import config_entries
15  PassiveBluetoothDataProcessor,
16  PassiveBluetoothDataUpdate,
17  PassiveBluetoothEntityKey,
18  PassiveBluetoothProcessorCoordinator,
19  PassiveBluetoothProcessorEntity,
20 )
22  SensorDeviceClass,
23  SensorEntity,
24  SensorEntityDescription,
25  SensorStateClass,
26 )
27 from homeassistant.const import (
28  CONCENTRATION_PARTS_PER_MILLION,
29  PERCENTAGE,
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: dict[
39  tuple[SSDSensorDeviceClass, Units | None], SensorEntityDescription
40 ] = {
41  (
42  SSDSensorDeviceClass.CO2,
43  Units.CONCENTRATION_PARTS_PER_MILLION,
45  key=f"{SSDSensorDeviceClass.CO2}_{Units.CONCENTRATION_PARTS_PER_MILLION}",
46  device_class=SensorDeviceClass.CO2,
47  native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
48  state_class=SensorStateClass.MEASUREMENT,
49  ),
50  (SSDSensorDeviceClass.HUMIDITY, Units.PERCENTAGE): SensorEntityDescription(
51  key=f"{SSDSensorDeviceClass.HUMIDITY}_{Units.PERCENTAGE}",
52  device_class=SensorDeviceClass.HUMIDITY,
53  native_unit_of_measurement=PERCENTAGE,
54  state_class=SensorStateClass.MEASUREMENT,
55  ),
56  (SSDSensorDeviceClass.TEMPERATURE, Units.TEMP_CELSIUS): SensorEntityDescription(
57  key=f"{SSDSensorDeviceClass.TEMPERATURE}_{Units.TEMP_CELSIUS}",
58  device_class=SensorDeviceClass.TEMPERATURE,
59  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
60  state_class=SensorStateClass.MEASUREMENT,
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  description: SensorDescription,
74 ) -> tuple[SSDSensorDeviceClass, Units | None]:
75  assert description.device_class is not None
76  return (description.device_class, description.native_unit_of_measurement)
77 
78 
80  sensor_update: SensorUpdate,
81 ) -> PassiveBluetoothDataUpdate:
82  """Convert a sensor update to a bluetooth data update."""
84  devices={
85  device_id: sensor_device_info_to_hass_device_info(device_info)
86  for device_id, device_info in sensor_update.devices.items()
87  },
88  entity_descriptions={
89  _device_key_to_bluetooth_entity_key(device_key): SENSOR_DESCRIPTIONS[
90  _to_sensor_key(description)
91  ]
92  for device_key, description in sensor_update.entity_descriptions.items()
93  if _to_sensor_key(description) in SENSOR_DESCRIPTIONS
94  },
95  entity_data={
96  _device_key_to_bluetooth_entity_key(device_key): sensor_values.native_value
97  for device_key, sensor_values in sensor_update.entity_values.items()
98  },
99  entity_names={
100  _device_key_to_bluetooth_entity_key(device_key): sensor_values.name
101  for device_key, sensor_values in sensor_update.entity_values.items()
102  },
103  )
104 
105 
107  hass: HomeAssistant,
109  async_add_entities: AddEntitiesCallback,
110 ) -> None:
111  """Set up the Sensirion BLE sensors."""
112  coordinator: PassiveBluetoothProcessorCoordinator = hass.data[DOMAIN][
113  entry.entry_id
114  ]
115  processor = PassiveBluetoothDataProcessor(sensor_update_to_bluetooth_data_update)
116  entry.async_on_unload(
117  processor.async_add_entities_listener(
118  SensirionBluetoothSensorEntity, async_add_entities
119  )
120  )
121  entry.async_on_unload(coordinator.async_register_processor(processor))
122 
123 
125  PassiveBluetoothProcessorEntity[
126  PassiveBluetoothDataProcessor[float | int | None, SensorUpdate]
127  ],
128  SensorEntity,
129 ):
130  """Representation of a Sensirion BLE sensor."""
131 
132  @property
133  def native_value(self) -> int | float | None:
134  """Return the native value."""
135  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:110
PassiveBluetoothEntityKey _device_key_to_bluetooth_entity_key(DeviceKey device_key)
Definition: sensor.py:67
tuple[SSDSensorDeviceClass, Units|None] _to_sensor_key(SensorDescription description)
Definition: sensor.py:74
PassiveBluetoothDataUpdate sensor_update_to_bluetooth_data_update(SensorUpdate sensor_update)
Definition: sensor.py:81
DeviceInfo sensor_device_info_to_hass_device_info(SensorDeviceInfo sensor_device_info)
Definition: sensor.py:20