Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for PurpleAir sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 
8 from aiopurpleair.models.sensors import SensorModel
9 
11  SensorDeviceClass,
12  SensorEntity,
13  SensorEntityDescription,
14  SensorStateClass,
15 )
16 from homeassistant.config_entries import ConfigEntry
17 from homeassistant.const import (
18  CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
19  PERCENTAGE,
20  SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
21  EntityCategory,
22  UnitOfPressure,
23  UnitOfTemperature,
24  UnitOfTime,
25  UnitOfVolume,
26 )
27 from homeassistant.core import HomeAssistant
28 from homeassistant.helpers.entity_platform import AddEntitiesCallback
29 
30 from .const import CONF_SENSOR_INDICES, DOMAIN
31 from .coordinator import PurpleAirDataUpdateCoordinator
32 from .entity import PurpleAirEntity
33 
34 CONCENTRATION_PARTICLES_PER_100_MILLILITERS = f"particles/100{UnitOfVolume.MILLILITERS}"
35 
36 
37 @dataclass(frozen=True, kw_only=True)
39  """Define an object to describe PurpleAir sensor entities."""
40 
41  value_fn: Callable[[SensorModel], float | str | None]
42 
43 
44 SENSOR_DESCRIPTIONS = [
46  key="humidity",
47  device_class=SensorDeviceClass.HUMIDITY,
48  native_unit_of_measurement=PERCENTAGE,
49  state_class=SensorStateClass.MEASUREMENT,
50  value_fn=lambda sensor: sensor.humidity,
51  ),
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,
59  ),
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,
67  ),
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,
75  ),
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,
82  ),
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,
90  ),
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,
97  ),
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,
105  ),
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,
112  ),
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,
120  ),
122  key="pressure",
123  device_class=SensorDeviceClass.PRESSURE,
124  native_unit_of_measurement=UnitOfPressure.MBAR,
125  state_class=SensorStateClass.MEASUREMENT,
126  value_fn=lambda sensor: sensor.pressure,
127  ),
129  key="rssi",
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,
137  ),
139  key="temperature",
140  device_class=SensorDeviceClass.TEMPERATURE,
141  native_unit_of_measurement=UnitOfTemperature.FAHRENHEIT,
142  state_class=SensorStateClass.MEASUREMENT,
143  value_fn=lambda sensor: sensor.temperature,
144  ),
146  key="uptime",
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,
154  ),
156  # This sensor is an air quality index for VOCs. More info at https://github.com/home-assistant/core/pull/84896
157  key="voc",
158  translation_key="voc_aqi",
159  device_class=SensorDeviceClass.AQI,
160  state_class=SensorStateClass.MEASUREMENT,
161  value_fn=lambda sensor: sensor.voc,
162  ),
163 ]
164 
165 
167  hass: HomeAssistant,
168  entry: ConfigEntry,
169  async_add_entities: AddEntitiesCallback,
170 ) -> None:
171  """Set up PurpleAir sensors based on a config entry."""
172  coordinator: PurpleAirDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
174  PurpleAirSensorEntity(coordinator, entry, sensor_index, description)
175  for sensor_index in entry.options[CONF_SENSOR_INDICES]
176  for description in SENSOR_DESCRIPTIONS
177  )
178 
179 
181  """Define a representation of a PurpleAir sensor."""
182 
183  entity_description: PurpleAirSensorEntityDescription
184 
185  def __init__(
186  self,
187  coordinator: PurpleAirDataUpdateCoordinator,
188  entry: ConfigEntry,
189  sensor_index: int,
190  description: PurpleAirSensorEntityDescription,
191  ) -> None:
192  """Initialize."""
193  super().__init__(coordinator, entry, sensor_index)
194 
195  self._attr_unique_id_attr_unique_id = f"{self._sensor_index}-{description.key}"
196  self.entity_descriptionentity_description = description
197 
198  @property
199  def native_value(self) -> float | str | None:
200  """Return the sensor value."""
201  return self.entity_descriptionentity_description.value_fn(self.sensor_datasensor_data)
None __init__(self, PurpleAirDataUpdateCoordinator coordinator, ConfigEntry entry, int sensor_index, PurpleAirSensorEntityDescription description)
Definition: sensor.py:191
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:170