Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for AirVisual Pro sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from typing import Any
8 
10  SensorDeviceClass,
11  SensorEntity,
12  SensorEntityDescription,
13  SensorStateClass,
14 )
15 from homeassistant.const import (
16  CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
17  CONCENTRATION_PARTS_PER_MILLION,
18  PERCENTAGE,
19  EntityCategory,
20  UnitOfTemperature,
21 )
22 from homeassistant.core import HomeAssistant, callback
23 from homeassistant.helpers.entity_platform import AddEntitiesCallback
24 
25 from . import AirVisualProConfigEntry
26 from .entity import AirVisualProEntity
27 
28 
29 @dataclass(frozen=True, kw_only=True)
31  """Describe an AirVisual Pro sensor."""
32 
33  value_fn: Callable[
34  [dict[str, Any], dict[str, Any], dict[str, Any], dict[str, Any]], float | int
35  ]
36 
37 
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[
44  async_get_aqi_locale(settings)
45  ],
46  ),
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(
52  history.get(
53  f'Outdoor {"AQI(US)" if settings["is_aqi_usa"] else "AQI(CN)"}', -1
54  )
55  ),
56  translation_key="outdoor_air_quality_index",
57  ),
59  key="battery_level",
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"],
65  ),
67  key="carbon_dioxide",
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"],
72  ),
74  key="humidity",
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[
79  "humidity"
80  ],
81  ),
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"],
88  ),
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"],
95  ),
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"],
102  ),
104  key="temperature",
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[
109  "temperature_C"
110  ],
111  ),
113  key="voc",
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"],
118  ),
119 )
120 
121 
122 @callback
123 def async_get_aqi_locale(settings: dict[str, Any]) -> str:
124  """Return the correct AQI locale based on settings data."""
125  if settings["is_aqi_usa"]:
126  return "aqi_us"
127  return "aqi_cn"
128 
129 
131  hass: HomeAssistant,
132  entry: AirVisualProConfigEntry,
133  async_add_entities: AddEntitiesCallback,
134 ) -> None:
135  """Set up AirVisual sensors based on a config entry."""
137  AirVisualProSensor(entry.runtime_data.coordinator, description)
138  for description in SENSOR_DESCRIPTIONS
139  )
140 
141 
143  """Define an AirVisual Pro sensor."""
144 
145  _attr_has_entity_name = True
146 
147  entity_description: AirVisualProMeasurementDescription
148 
149  @property
150  def native_value(self) -> float | int:
151  """Return the sensor value."""
152  return self.entity_descriptionentity_description.value_fn(
153  self.coordinator.data["settings"],
154  self.coordinator.data["status"],
155  self.coordinator.data["measurements"],
156  self.coordinator.data["history"],
157  )
str async_get_aqi_locale(dict[str, Any] settings)
Definition: sensor.py:123
None async_setup_entry(HomeAssistant hass, AirVisualProConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:134