1 """Support for PurpleAir sensors."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
8 from aiopurpleair.models.sensors
import SensorModel
13 SensorEntityDescription,
18 CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
20 SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
30 from .const
import CONF_SENSOR_INDICES, DOMAIN
31 from .coordinator
import PurpleAirDataUpdateCoordinator
32 from .entity
import PurpleAirEntity
34 CONCENTRATION_PARTICLES_PER_100_MILLILITERS = f
"particles/100{UnitOfVolume.MILLILITERS}"
37 @dataclass(frozen=True, kw_only=True)
39 """Define an object to describe PurpleAir sensor entities."""
41 value_fn: Callable[[SensorModel], float | str |
None]
44 SENSOR_DESCRIPTIONS = [
47 device_class=SensorDeviceClass.HUMIDITY,
48 native_unit_of_measurement=PERCENTAGE,
49 state_class=SensorStateClass.MEASUREMENT,
50 value_fn=
lambda sensor: sensor.humidity,
53 key=
"pm0.3_count_concentration",
54 translation_key=
"pm0_3_count_concentration",
55 entity_registry_enabled_default=
False,
56 native_unit_of_measurement=CONCENTRATION_PARTICLES_PER_100_MILLILITERS,
57 state_class=SensorStateClass.MEASUREMENT,
58 value_fn=
lambda sensor: sensor.pm0_3_um_count,
61 key=
"pm0.5_count_concentration",
62 translation_key=
"pm0_5_count_concentration",
63 entity_registry_enabled_default=
False,
64 native_unit_of_measurement=CONCENTRATION_PARTICLES_PER_100_MILLILITERS,
65 state_class=SensorStateClass.MEASUREMENT,
66 value_fn=
lambda sensor: sensor.pm0_5_um_count,
69 key=
"pm1.0_count_concentration",
70 translation_key=
"pm1_0_count_concentration",
71 entity_registry_enabled_default=
False,
72 native_unit_of_measurement=CONCENTRATION_PARTICLES_PER_100_MILLILITERS,
73 state_class=SensorStateClass.MEASUREMENT,
74 value_fn=
lambda sensor: sensor.pm1_0_um_count,
77 key=
"pm1.0_mass_concentration",
78 device_class=SensorDeviceClass.PM1,
79 native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
80 state_class=SensorStateClass.MEASUREMENT,
81 value_fn=
lambda sensor: sensor.pm1_0,
84 key=
"pm10.0_count_concentration",
85 translation_key=
"pm10_0_count_concentration",
86 entity_registry_enabled_default=
False,
87 native_unit_of_measurement=CONCENTRATION_PARTICLES_PER_100_MILLILITERS,
88 state_class=SensorStateClass.MEASUREMENT,
89 value_fn=
lambda sensor: sensor.pm10_0_um_count,
92 key=
"pm10.0_mass_concentration",
93 device_class=SensorDeviceClass.PM10,
94 native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
95 state_class=SensorStateClass.MEASUREMENT,
96 value_fn=
lambda sensor: sensor.pm10_0,
99 key=
"pm2.5_count_concentration",
100 translation_key=
"pm2_5_count_concentration",
101 entity_registry_enabled_default=
False,
102 native_unit_of_measurement=CONCENTRATION_PARTICLES_PER_100_MILLILITERS,
103 state_class=SensorStateClass.MEASUREMENT,
104 value_fn=
lambda sensor: sensor.pm2_5_um_count,
107 key=
"pm2.5_mass_concentration",
108 device_class=SensorDeviceClass.PM25,
109 native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
110 state_class=SensorStateClass.MEASUREMENT,
111 value_fn=
lambda sensor: sensor.pm2_5,
114 key=
"pm5.0_count_concentration",
115 translation_key=
"pm5_0_count_concentration",
116 entity_registry_enabled_default=
False,
117 native_unit_of_measurement=CONCENTRATION_PARTICLES_PER_100_MILLILITERS,
118 state_class=SensorStateClass.MEASUREMENT,
119 value_fn=
lambda sensor: sensor.pm5_0_um_count,
123 device_class=SensorDeviceClass.PRESSURE,
124 native_unit_of_measurement=UnitOfPressure.MBAR,
125 state_class=SensorStateClass.MEASUREMENT,
126 value_fn=
lambda sensor: sensor.pressure,
130 translation_key=
"rssi",
131 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
132 entity_category=EntityCategory.DIAGNOSTIC,
133 entity_registry_enabled_default=
False,
134 native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
135 state_class=SensorStateClass.MEASUREMENT,
136 value_fn=
lambda sensor: sensor.pressure,
140 device_class=SensorDeviceClass.TEMPERATURE,
141 native_unit_of_measurement=UnitOfTemperature.FAHRENHEIT,
142 state_class=SensorStateClass.MEASUREMENT,
143 value_fn=
lambda sensor: sensor.temperature,
147 translation_key=
"uptime",
148 entity_category=EntityCategory.DIAGNOSTIC,
149 entity_registry_enabled_default=
False,
150 device_class=SensorDeviceClass.DURATION,
151 native_unit_of_measurement=UnitOfTime.MINUTES,
152 state_class=SensorStateClass.TOTAL_INCREASING,
153 value_fn=
lambda sensor: sensor.uptime,
158 translation_key=
"voc_aqi",
159 device_class=SensorDeviceClass.AQI,
160 state_class=SensorStateClass.MEASUREMENT,
161 value_fn=
lambda sensor: sensor.voc,
169 async_add_entities: AddEntitiesCallback,
171 """Set up PurpleAir sensors based on a config entry."""
172 coordinator: PurpleAirDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
175 for sensor_index
in entry.options[CONF_SENSOR_INDICES]
176 for description
in SENSOR_DESCRIPTIONS
181 """Define a representation of a PurpleAir sensor."""
183 entity_description: PurpleAirSensorEntityDescription
187 coordinator: PurpleAirDataUpdateCoordinator,
190 description: PurpleAirSensorEntityDescription,
193 super().
__init__(coordinator, entry, sensor_index)
200 """Return the sensor value."""
SensorModel sensor_data(self)
float|str|None native_value(self)
None __init__(self, PurpleAirDataUpdateCoordinator coordinator, ConfigEntry entry, int sensor_index, PurpleAirSensorEntityDescription description)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)