1 """Support for Aranet sensors."""
3 from __future__
import annotations
5 from dataclasses
import dataclass
8 from aranet4.client
import Aranet4Advertisement
9 from bleak.backends.device
import BLEDevice
12 PassiveBluetoothDataProcessor,
13 PassiveBluetoothDataUpdate,
14 PassiveBluetoothEntityKey,
15 PassiveBluetoothProcessorEntity,
20 SensorEntityDescription,
27 CONCENTRATION_PARTS_PER_MILLION,
39 from .
import AranetConfigEntry
40 from .const
import ARANET_MANUFACTURER_NAME
43 @dataclass(frozen=True)
45 """Class to describe an Aranet sensor entity."""
50 name: str |
None =
None
51 scale: float | int = 1
54 SENSOR_DESCRIPTIONS = {
58 device_class=SensorDeviceClass.TEMPERATURE,
59 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
60 state_class=SensorStateClass.MEASUREMENT,
65 device_class=SensorDeviceClass.HUMIDITY,
66 native_unit_of_measurement=PERCENTAGE,
67 state_class=SensorStateClass.MEASUREMENT,
72 device_class=SensorDeviceClass.PRESSURE,
73 native_unit_of_measurement=UnitOfPressure.HPA,
74 state_class=SensorStateClass.MEASUREMENT,
78 name=
"Carbon Dioxide",
79 device_class=SensorDeviceClass.CO2,
80 native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
81 state_class=SensorStateClass.MEASUREMENT,
85 translation_key=
"radiation_rate",
86 name=
"Radiation Dose Rate",
87 native_unit_of_measurement=
"μSv/h",
88 state_class=SensorStateClass.MEASUREMENT,
89 suggested_display_precision=2,
93 key=
"radiation_total",
94 translation_key=
"radiation_total",
95 name=
"Radiation Total Dose",
96 native_unit_of_measurement=
"mSv",
97 state_class=SensorStateClass.MEASUREMENT,
98 suggested_display_precision=4,
102 key=
"radon_concentration",
103 translation_key=
"radon_concentration",
104 name=
"Radon Concentration",
105 native_unit_of_measurement=
"Bq/m³",
106 state_class=SensorStateClass.MEASUREMENT,
111 device_class=SensorDeviceClass.BATTERY,
112 native_unit_of_measurement=PERCENTAGE,
113 state_class=SensorStateClass.MEASUREMENT,
114 entity_category=EntityCategory.DIAGNOSTIC,
117 key=
"update_interval",
118 name=
"Update Interval",
119 device_class=SensorDeviceClass.DURATION,
120 native_unit_of_measurement=UnitOfTime.SECONDS,
121 state_class=SensorStateClass.MEASUREMENT,
123 entity_registry_enabled_default=
False,
124 entity_category=EntityCategory.DIAGNOSTIC,
132 ) -> PassiveBluetoothEntityKey:
133 """Convert a device key to an entity key."""
138 adv: Aranet4Advertisement,
140 """Convert a sensor device info to hass device info."""
142 if adv.readings
and adv.readings.name:
143 hass_device_info[ATTR_NAME] = adv.readings.name
144 hass_device_info[ATTR_MANUFACTURER] = ARANET_MANUFACTURER_NAME
145 if adv.manufacturer_data:
146 hass_device_info[ATTR_SW_VERSION] =
str(adv.manufacturer_data.version)
147 return hass_device_info
151 adv: Aranet4Advertisement,
152 ) -> PassiveBluetoothDataUpdate[Any]:
153 """Convert a sensor update to a Bluetooth data update."""
154 data: dict[PassiveBluetoothEntityKey, Any] = {}
155 names: dict[PassiveBluetoothEntityKey, str |
None] = {}
156 descs: dict[PassiveBluetoothEntityKey, EntityDescription] = {}
157 for key, desc
in SENSOR_DESCRIPTIONS.items():
159 val = getattr(adv.readings, key)
164 names[tag] = desc.name
168 entity_descriptions=descs,
176 entry: AranetConfigEntry,
177 async_add_entities: AddEntitiesCallback,
179 """Set up the Aranet sensors."""
181 entry.async_on_unload(
182 processor.async_add_entities_listener(
183 Aranet4BluetoothSensorEntity, async_add_entities
186 entry.async_on_unload(entry.runtime_data.async_register_processor(processor))
190 PassiveBluetoothProcessorEntity[
191 PassiveBluetoothDataProcessor[float | int |
None, Aranet4Advertisement],
195 """Representation of an Aranet sensor."""
199 """Return whether the entity was available in the last update."""
207 and self.processor.entity_data.get(self.entity_key)
is not None
212 """Return the native value."""
213 return self.processor.entity_data.get(self.entity_key)
int|float|None native_value(self)
PassiveBluetoothEntityKey _device_key_to_bluetooth_entity_key(BLEDevice device, str key)
None async_setup_entry(HomeAssistant hass, AranetConfigEntry entry, AddEntitiesCallback async_add_entities)
PassiveBluetoothDataUpdate[Any] sensor_update_to_bluetooth_data_update(Aranet4Advertisement adv)
DeviceInfo _sensor_device_info_to_hass(Aranet4Advertisement adv)