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