1 """Support for AirVisual Pro sensors."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
12 SensorEntityDescription,
16 CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
17 CONCENTRATION_PARTS_PER_MILLION,
25 from .
import AirVisualProConfigEntry
26 from .entity
import AirVisualProEntity
29 @dataclass(frozen=True, kw_only=True)
31 """Describe an AirVisual Pro sensor."""
34 [dict[str, Any], dict[str, Any], dict[str, Any], dict[str, Any]], float | int
38 SENSOR_DESCRIPTIONS = (
40 key=
"air_quality_index",
41 device_class=SensorDeviceClass.AQI,
42 state_class=SensorStateClass.MEASUREMENT,
43 value_fn=
lambda settings, status, measurements, history: measurements[
48 key=
"outdoor_air_quality_index",
49 device_class=SensorDeviceClass.AQI,
50 state_class=SensorStateClass.MEASUREMENT,
51 value_fn=
lambda settings, status, measurements, history:
int(
53 f
'Outdoor {"AQI(US)" if settings["is_aqi_usa"] else "AQI(CN)"}', -1
56 translation_key=
"outdoor_air_quality_index",
60 device_class=SensorDeviceClass.BATTERY,
61 entity_category=EntityCategory.DIAGNOSTIC,
62 native_unit_of_measurement=PERCENTAGE,
63 state_class=SensorStateClass.MEASUREMENT,
64 value_fn=
lambda settings, status, measurements, history: status[
"battery"],
68 device_class=SensorDeviceClass.CO2,
69 native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
70 state_class=SensorStateClass.MEASUREMENT,
71 value_fn=
lambda settings, status, measurements, history: measurements[
"co2"],
75 device_class=SensorDeviceClass.HUMIDITY,
76 native_unit_of_measurement=PERCENTAGE,
77 state_class=SensorStateClass.MEASUREMENT,
78 value_fn=
lambda settings, status, measurements, history: measurements[
83 key=
"particulate_matter_0_1",
84 translation_key=
"pm01",
85 native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
86 state_class=SensorStateClass.MEASUREMENT,
87 value_fn=
lambda settings, status, measurements, history: measurements[
"pm0_1"],
90 key=
"particulate_matter_1_0",
91 device_class=SensorDeviceClass.PM1,
92 native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
93 state_class=SensorStateClass.MEASUREMENT,
94 value_fn=
lambda settings, status, measurements, history: measurements[
"pm1_0"],
97 key=
"particulate_matter_2_5",
98 device_class=SensorDeviceClass.PM25,
99 native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
100 state_class=SensorStateClass.MEASUREMENT,
101 value_fn=
lambda settings, status, measurements, history: measurements[
"pm2_5"],
105 device_class=SensorDeviceClass.TEMPERATURE,
106 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
107 state_class=SensorStateClass.MEASUREMENT,
108 value_fn=
lambda settings, status, measurements, history: measurements[
114 device_class=SensorDeviceClass.VOLATILE_ORGANIC_COMPOUNDS,
115 native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
116 state_class=SensorStateClass.MEASUREMENT,
117 value_fn=
lambda settings, status, measurements, history: measurements[
"voc"],
124 """Return the correct AQI locale based on settings data."""
125 if settings[
"is_aqi_usa"]:
132 entry: AirVisualProConfigEntry,
133 async_add_entities: AddEntitiesCallback,
135 """Set up AirVisual sensors based on a config entry."""
138 for description
in SENSOR_DESCRIPTIONS
143 """Define an AirVisual Pro sensor."""
145 _attr_has_entity_name =
True
147 entity_description: AirVisualProMeasurementDescription
151 """Return the sensor value."""
153 self.coordinator.data[
"settings"],
154 self.coordinator.data[
"status"],
155 self.coordinator.data[
"measurements"],
156 self.coordinator.data[
"history"],
float|int native_value(self)
str async_get_aqi_locale(dict[str, Any] settings)
None async_setup_entry(HomeAssistant hass, AirVisualProConfigEntry entry, AddEntitiesCallback async_add_entities)