Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for inkbird ble sensors."""
2 
3 from __future__ import annotations
4 
5 from inkbird_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  UnitOfTemperature,
25 )
26 from homeassistant.core import HomeAssistant
27 from homeassistant.helpers.entity_platform import AddEntitiesCallback
28 from homeassistant.helpers.sensor import sensor_device_info_to_hass_device_info
29 
30 from .const import DOMAIN
31 
32 SENSOR_DESCRIPTIONS = {
33  (DeviceClass.TEMPERATURE, Units.TEMP_CELSIUS): SensorEntityDescription(
34  key=f"{DeviceClass.TEMPERATURE}_{Units.TEMP_CELSIUS}",
35  device_class=SensorDeviceClass.TEMPERATURE,
36  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
37  state_class=SensorStateClass.MEASUREMENT,
38  ),
39  (DeviceClass.HUMIDITY, Units.PERCENTAGE): SensorEntityDescription(
40  key=f"{DeviceClass.HUMIDITY}_{Units.PERCENTAGE}",
41  device_class=SensorDeviceClass.HUMIDITY,
42  native_unit_of_measurement=PERCENTAGE,
43  state_class=SensorStateClass.MEASUREMENT,
44  ),
45  (DeviceClass.BATTERY, Units.PERCENTAGE): SensorEntityDescription(
46  key=f"{DeviceClass.BATTERY}_{Units.PERCENTAGE}",
47  device_class=SensorDeviceClass.BATTERY,
48  native_unit_of_measurement=PERCENTAGE,
49  state_class=SensorStateClass.MEASUREMENT,
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  ),
61 }
62 
63 
65  device_key: DeviceKey,
66 ) -> PassiveBluetoothEntityKey:
67  """Convert a device key to an entity key."""
68  return PassiveBluetoothEntityKey(device_key.key, device_key.device_id)
69 
70 
72  sensor_update: SensorUpdate,
73 ) -> PassiveBluetoothDataUpdate:
74  """Convert a sensor update to a bluetooth data update."""
76  devices={
77  device_id: sensor_device_info_to_hass_device_info(device_info)
78  for device_id, device_info in sensor_update.devices.items()
79  },
80  entity_descriptions={
81  _device_key_to_bluetooth_entity_key(device_key): SENSOR_DESCRIPTIONS[
82  (description.device_class, description.native_unit_of_measurement)
83  ]
84  for device_key, description in sensor_update.entity_descriptions.items()
85  if description.device_class and description.native_unit_of_measurement
86  },
87  entity_data={
88  _device_key_to_bluetooth_entity_key(device_key): sensor_values.native_value
89  for device_key, sensor_values in sensor_update.entity_values.items()
90  },
91  entity_names={
92  _device_key_to_bluetooth_entity_key(device_key): sensor_values.name
93  for device_key, sensor_values in sensor_update.entity_values.items()
94  },
95  )
96 
97 
99  hass: HomeAssistant,
101  async_add_entities: AddEntitiesCallback,
102 ) -> None:
103  """Set up the INKBIRD BLE sensors."""
104  coordinator: PassiveBluetoothProcessorCoordinator = hass.data[DOMAIN][
105  entry.entry_id
106  ]
107  processor = PassiveBluetoothDataProcessor(sensor_update_to_bluetooth_data_update)
108  entry.async_on_unload(
109  processor.async_add_entities_listener(
110  INKBIRDBluetoothSensorEntity, async_add_entities
111  )
112  )
113  entry.async_on_unload(coordinator.async_register_processor(processor))
114 
115 
117  PassiveBluetoothProcessorEntity[
118  PassiveBluetoothDataProcessor[float | int | None, SensorUpdate]
119  ],
120  SensorEntity,
121 ):
122  """Representation of a inkbird ble sensor."""
123 
124  @property
125  def native_value(self) -> int | float | None:
126  """Return the native value."""
127  return self.processor.entity_data.get(self.entity_key)
PassiveBluetoothEntityKey _device_key_to_bluetooth_entity_key(DeviceKey device_key)
Definition: sensor.py:66
None async_setup_entry(HomeAssistant hass, config_entries.ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:102
PassiveBluetoothDataUpdate sensor_update_to_bluetooth_data_update(SensorUpdate sensor_update)
Definition: sensor.py:73
DeviceInfo sensor_device_info_to_hass_device_info(SensorDeviceInfo sensor_device_info)
Definition: sensor.py:20