1 """Support for RAPT Pill hydrometers."""
3 from __future__
import annotations
5 from rapt_ble
import DeviceClass, DeviceKey, SensorUpdate, Units
7 from homeassistant
import config_entries
9 PassiveBluetoothDataProcessor,
10 PassiveBluetoothDataUpdate,
11 PassiveBluetoothEntityKey,
12 PassiveBluetoothProcessorCoordinator,
13 PassiveBluetoothProcessorEntity,
18 SensorEntityDescription,
23 SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
31 from .const
import DOMAIN
33 SENSOR_DESCRIPTIONS = {
35 key=f
"{DeviceClass.TEMPERATURE}_{Units.TEMP_CELSIUS}",
36 device_class=SensorDeviceClass.TEMPERATURE,
37 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
38 state_class=SensorStateClass.MEASUREMENT,
41 key=f
"{DeviceClass.SPECIFIC_GRAVITY}_{Units.SPECIFIC_GRAVITY}",
42 state_class=SensorStateClass.MEASUREMENT,
45 key=f
"{DeviceClass.BATTERY}_{Units.PERCENTAGE}",
46 device_class=SensorDeviceClass.BATTERY,
47 native_unit_of_measurement=PERCENTAGE,
48 state_class=SensorStateClass.MEASUREMENT,
49 entity_category=EntityCategory.DIAGNOSTIC,
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 entity_category=EntityCategory.DIAGNOSTIC,
66 device_key: DeviceKey,
67 ) -> PassiveBluetoothEntityKey:
68 """Convert a device key to an entity key."""
73 sensor_update: SensorUpdate,
74 ) -> PassiveBluetoothDataUpdate:
75 """Convert a sensor update to a bluetooth data update."""
79 for device_id, device_info
in sensor_update.devices.items()
83 (description.device_class, description.native_unit_of_measurement)
85 for device_key, description
in sensor_update.entity_descriptions.items()
86 if description.device_class
and description.native_unit_of_measurement
90 for device_key, sensor_values
in sensor_update.entity_values.items()
94 for device_key, sensor_values
in sensor_update.entity_values.items()
102 async_add_entities: AddEntitiesCallback,
104 """Set up the RAPT Pill BLE sensors."""
105 coordinator: PassiveBluetoothProcessorCoordinator = hass.data[DOMAIN][
109 entry.async_on_unload(
110 processor.async_add_entities_listener(
111 RAPTPillBluetoothSensorEntity, async_add_entities
114 entry.async_on_unload(coordinator.async_register_processor(processor))
118 PassiveBluetoothProcessorEntity[
119 PassiveBluetoothDataProcessor[float | int |
None, SensorUpdate]
123 """Representation of a RAPT Pill BLE sensor."""
127 """Return the native value."""
128 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)
PassiveBluetoothEntityKey _device_key_to_bluetooth_entity_key(DeviceKey device_key)
None async_setup_entry(HomeAssistant hass, config_entries.ConfigEntry entry, AddEntitiesCallback async_add_entities)
DeviceInfo sensor_device_info_to_hass_device_info(SensorDeviceInfo sensor_device_info)