1 """Support for Leaone sensors."""
3 from __future__
import annotations
5 from leaone_ble
import DeviceClass
as LeaoneSensorDeviceClass, SensorUpdate, Units
7 from homeassistant
import config_entries
9 PassiveBluetoothDataProcessor,
10 PassiveBluetoothDataUpdate,
11 PassiveBluetoothProcessorCoordinator,
12 PassiveBluetoothProcessorEntity,
17 SensorEntityDescription,
21 SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
29 from .const
import DOMAIN
30 from .device
import device_key_to_bluetooth_entity_key
32 SENSOR_DESCRIPTIONS = {
34 LeaoneSensorDeviceClass.MASS_NON_STABILIZED,
37 key=f
"{LeaoneSensorDeviceClass.MASS_NON_STABILIZED}_{Units.MASS_KILOGRAMS}",
38 device_class=SensorDeviceClass.WEIGHT,
39 native_unit_of_measurement=UnitOfMass.KILOGRAMS,
40 state_class=SensorStateClass.MEASUREMENT,
41 entity_category=EntityCategory.DIAGNOSTIC,
44 key=f
"{LeaoneSensorDeviceClass.MASS}_{Units.MASS_KILOGRAMS}",
45 device_class=SensorDeviceClass.WEIGHT,
46 native_unit_of_measurement=UnitOfMass.KILOGRAMS,
47 state_class=SensorStateClass.MEASUREMENT,
50 key=f
"{LeaoneSensorDeviceClass.IMPEDANCE}_{Units.OHM}",
52 native_unit_of_measurement=Units.OHM,
53 state_class=SensorStateClass.MEASUREMENT,
54 entity_category=EntityCategory.DIAGNOSTIC,
55 entity_registry_enabled_default=
False,
58 LeaoneSensorDeviceClass.SIGNAL_STRENGTH,
59 Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
61 key=f
"{LeaoneSensorDeviceClass.SIGNAL_STRENGTH}_{Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT}",
62 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
63 native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
64 state_class=SensorStateClass.MEASUREMENT,
65 entity_category=EntityCategory.DIAGNOSTIC,
66 entity_registry_enabled_default=
False,
69 LeaoneSensorDeviceClass.PACKET_ID,
72 key=
str(LeaoneSensorDeviceClass.PACKET_ID),
73 entity_category=EntityCategory.DIAGNOSTIC,
74 state_class=SensorStateClass.MEASUREMENT,
75 entity_registry_enabled_default=
False,
81 sensor_update: SensorUpdate,
82 ) -> PassiveBluetoothDataUpdate:
83 """Convert a sensor update to a bluetooth data update."""
87 for device_id, device_info
in sensor_update.devices.items()
91 (description.device_class, description.native_unit_of_measurement)
93 for device_key, description
in sensor_update.entity_descriptions.items()
94 if description.device_class
and description.native_unit_of_measurement
98 for device_key, sensor_values
in sensor_update.entity_values.items()
102 for device_key, sensor_values
in sensor_update.entity_values.items()
110 async_add_entities: AddEntitiesCallback,
112 """Set up the Leaone BLE sensors."""
113 coordinator: PassiveBluetoothProcessorCoordinator = hass.data[DOMAIN][
117 entry.async_on_unload(
118 processor.async_add_entities_listener(
119 LeaoneBluetoothSensorEntity, async_add_entities
122 entry.async_on_unload(
123 coordinator.async_register_processor(processor, SensorEntityDescription)
128 PassiveBluetoothProcessorEntity[
129 PassiveBluetoothDataProcessor[float | int |
None, SensorUpdate]
133 """Representation of a Leaone sensor."""
137 """Return the native value."""
138 return self.processor.entity_data.get(self.entity_key)
142 """Return True if entity is available.
144 The sensor is only created when the device is seen.
146 Since these are sleepy devices which stop broadcasting
147 when not in use, we can't rely on the last update time
148 so once we have seen the device we always return True.
154 """Return True if the device is no longer broadcasting."""
155 return not self.processor.available
int|float|None native_value(self)
PassiveBluetoothEntityKey device_key_to_bluetooth_entity_key(DeviceKey device_key)
PassiveBluetoothDataUpdate sensor_update_to_bluetooth_data_update(SensorUpdate sensor_update)
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)