1 """Support for Tilt Hydrometers."""
3 from __future__
import annotations
5 from tilt_ble
import DeviceClass, DeviceKey, SensorUpdate, Units
7 from homeassistant
import config_entries
9 PassiveBluetoothDataProcessor,
10 PassiveBluetoothDataUpdate,
11 PassiveBluetoothEntityKey,
12 PassiveBluetoothProcessorCoordinator,
13 PassiveBluetoothProcessorEntity,
18 SensorEntityDescription,
26 from .const
import DOMAIN
28 SENSOR_DESCRIPTIONS = {
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,
36 key=f
"{DeviceClass.SPECIFIC_GRAVITY}_{Units.SPECIFIC_GRAVITY}",
37 state_class=SensorStateClass.MEASUREMENT,
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,
53 device_key: DeviceKey,
54 ) -> PassiveBluetoothEntityKey:
55 """Convert a device key to an entity key."""
60 sensor_update: SensorUpdate,
61 ) -> PassiveBluetoothDataUpdate:
62 """Convert a sensor update to a bluetooth data update."""
66 for device_id, device_info
in sensor_update.devices.items()
70 (description.device_class, description.native_unit_of_measurement)
72 for device_key, description
in sensor_update.entity_descriptions.items()
73 if description.device_class
and description.native_unit_of_measurement
77 for device_key, sensor_values
in sensor_update.entity_values.items()
81 for device_key, sensor_values
in sensor_update.entity_values.items()
89 async_add_entities: AddEntitiesCallback,
91 """Set up the Tilt Hydrometer BLE sensors."""
92 coordinator: PassiveBluetoothProcessorCoordinator = hass.data[DOMAIN][
96 entry.async_on_unload(
97 processor.async_add_entities_listener(
98 TiltBluetoothSensorEntity, async_add_entities
101 entry.async_on_unload(coordinator.async_register_processor(processor))
105 PassiveBluetoothProcessorEntity[
106 PassiveBluetoothDataProcessor[float | int |
None, SensorUpdate]
110 """Representation of a Tilt Hydrometer BLE sensor."""
114 """Return the native value."""
115 return self.processor.entity_data.get(self.entity_key)
int|float|None native_value(self)
PassiveBluetoothDataUpdate sensor_update_to_bluetooth_data_update(SensorUpdate sensor_update)
None async_setup_entry(HomeAssistant hass, config_entries.ConfigEntry entry, AddEntitiesCallback async_add_entities)
PassiveBluetoothEntityKey _device_key_to_bluetooth_entity_key(DeviceKey device_key)
DeviceInfo sensor_device_info_to_hass_device_info(SensorDeviceInfo sensor_device_info)