Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Tilt Hydrometers."""
2 
3 from __future__ import annotations
4 
5 from tilt_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 SIGNAL_STRENGTH_DECIBELS_MILLIWATT, UnitOfTemperature
22 from homeassistant.core import HomeAssistant
23 from homeassistant.helpers.entity_platform import AddEntitiesCallback
24 from homeassistant.helpers.sensor import sensor_device_info_to_hass_device_info
25 
26 from .const import DOMAIN
27 
28 SENSOR_DESCRIPTIONS = {
29  (DeviceClass.TEMPERATURE, Units.TEMP_FAHRENHEIT): SensorEntityDescription(
30  key=f"{DeviceClass.TEMPERATURE}_{Units.TEMP_FAHRENHEIT}",
31  device_class=SensorDeviceClass.TEMPERATURE,
32  native_unit_of_measurement=UnitOfTemperature.FAHRENHEIT,
33  state_class=SensorStateClass.MEASUREMENT,
34  ),
35  (DeviceClass.SPECIFIC_GRAVITY, Units.SPECIFIC_GRAVITY): SensorEntityDescription(
36  key=f"{DeviceClass.SPECIFIC_GRAVITY}_{Units.SPECIFIC_GRAVITY}",
37  state_class=SensorStateClass.MEASUREMENT,
38  ),
39  (
40  DeviceClass.SIGNAL_STRENGTH,
41  Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
43  key=f"{DeviceClass.SIGNAL_STRENGTH}_{Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT}",
44  device_class=SensorDeviceClass.SIGNAL_STRENGTH,
45  native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
46  state_class=SensorStateClass.MEASUREMENT,
47  entity_registry_enabled_default=False,
48  ),
49 }
50 
51 
53  device_key: DeviceKey,
54 ) -> PassiveBluetoothEntityKey:
55  """Convert a device key to an entity key."""
56  return PassiveBluetoothEntityKey(device_key.key, device_key.device_id)
57 
58 
60  sensor_update: SensorUpdate,
61 ) -> PassiveBluetoothDataUpdate:
62  """Convert a sensor update to a bluetooth data update."""
64  devices={
65  device_id: sensor_device_info_to_hass_device_info(device_info)
66  for device_id, device_info in sensor_update.devices.items()
67  },
68  entity_descriptions={
69  _device_key_to_bluetooth_entity_key(device_key): SENSOR_DESCRIPTIONS[
70  (description.device_class, description.native_unit_of_measurement)
71  ]
72  for device_key, description in sensor_update.entity_descriptions.items()
73  if description.device_class and description.native_unit_of_measurement
74  },
75  entity_data={
76  _device_key_to_bluetooth_entity_key(device_key): sensor_values.native_value
77  for device_key, sensor_values in sensor_update.entity_values.items()
78  },
79  entity_names={
80  _device_key_to_bluetooth_entity_key(device_key): sensor_values.name
81  for device_key, sensor_values in sensor_update.entity_values.items()
82  },
83  )
84 
85 
87  hass: HomeAssistant,
89  async_add_entities: AddEntitiesCallback,
90 ) -> None:
91  """Set up the Tilt Hydrometer BLE sensors."""
92  coordinator: PassiveBluetoothProcessorCoordinator = hass.data[DOMAIN][
93  entry.entry_id
94  ]
95  processor = PassiveBluetoothDataProcessor(sensor_update_to_bluetooth_data_update)
96  entry.async_on_unload(
97  processor.async_add_entities_listener(
98  TiltBluetoothSensorEntity, async_add_entities
99  )
100  )
101  entry.async_on_unload(coordinator.async_register_processor(processor))
102 
103 
105  PassiveBluetoothProcessorEntity[
106  PassiveBluetoothDataProcessor[float | int | None, SensorUpdate]
107  ],
108  SensorEntity,
109 ):
110  """Representation of a Tilt Hydrometer BLE sensor."""
111 
112  @property
113  def native_value(self) -> int | float | None:
114  """Return the native value."""
115  return self.processor.entity_data.get(self.entity_key)
PassiveBluetoothDataUpdate sensor_update_to_bluetooth_data_update(SensorUpdate sensor_update)
Definition: sensor.py:61
None async_setup_entry(HomeAssistant hass, config_entries.ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:90
PassiveBluetoothEntityKey _device_key_to_bluetooth_entity_key(DeviceKey device_key)
Definition: sensor.py:54
DeviceInfo sensor_device_info_to_hass_device_info(SensorDeviceInfo sensor_device_info)
Definition: sensor.py:20