Home Assistant Unofficial Reference 2024.12.1
air_quality.py
Go to the documentation of this file.
1 """Support for Kaiterra Air Quality Sensors."""
2 
3 from __future__ import annotations
4 
5 from homeassistant.components.air_quality import AirQualityEntity
6 from homeassistant.const import CONF_DEVICE_ID, CONF_NAME
7 from homeassistant.core import HomeAssistant
8 from homeassistant.helpers.dispatcher import async_dispatcher_connect
9 from homeassistant.helpers.entity_platform import AddEntitiesCallback
10 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
11 
12 from .const import (
13  ATTR_AQI_LEVEL,
14  ATTR_AQI_POLLUTANT,
15  ATTR_VOC,
16  DISPATCHER_KAITERRA,
17  DOMAIN,
18 )
19 
20 
22  hass: HomeAssistant,
23  config: ConfigType,
24  async_add_entities: AddEntitiesCallback,
25  discovery_info: DiscoveryInfoType | None = None,
26 ) -> None:
27  """Set up the air_quality kaiterra sensor."""
28  if discovery_info is None:
29  return
30 
31  api = hass.data[DOMAIN]
32  name = discovery_info[CONF_NAME]
33  device_id = discovery_info[CONF_DEVICE_ID]
34 
35  async_add_entities([KaiterraAirQuality(api, name, device_id)])
36 
37 
39  """Implementation of a Kaittera air quality sensor."""
40 
41  _attr_should_poll = False
42 
43  def __init__(self, api, name, device_id):
44  """Initialize the sensor."""
45  self._api_api = api
46  self._name_name = f"{name} Air Quality"
47  self._device_id_device_id = device_id
48 
49  def _data(self, key):
50  return self._device_device.get(key, {}).get("value")
51 
52  @property
53  def _device(self):
54  return self._api_api.data.get(self._device_id_device_id, {})
55 
56  @property
57  def available(self):
58  """Return the availability of the sensor."""
59  return self._api_api.data.get(self._device_id_device_id) is not None
60 
61  @property
62  def name(self):
63  """Return the name of the sensor."""
64  return self._name_name
65 
66  @property
67  def air_quality_index(self):
68  """Return the Air Quality Index (AQI)."""
69  return self._data_data("aqi")
70 
71  @property
73  """Return the Air Quality Index level."""
74  return self._data_data("aqi_level")
75 
76  @property
78  """Return the Air Quality Index level."""
79  return self._data_data("aqi_pollutant")
80 
81  @property
83  """Return the particulate matter 2.5 level."""
84  return self._data_data("rpm25c")
85 
86  @property
88  """Return the particulate matter 10 level."""
89  return self._data_data("rpm10c")
90 
91  @property
92  def carbon_dioxide(self):
93  """Return the CO2 (carbon dioxide) level."""
94  return self._data_data("rco2")
95 
96  @property
98  """Return the VOC (Volatile Organic Compounds) level."""
99  return self._data_data("rtvoc")
100 
101  @property
102  def unique_id(self):
103  """Return the sensor's unique id."""
104  return f"{self._device_id}_air_quality"
105 
106  @property
108  """Return the device state attributes."""
109  return {
110  attr: value
111  for attr, value in (
112  (ATTR_VOC, self.volatile_organic_compoundsvolatile_organic_compounds),
113  (ATTR_AQI_LEVEL, self.air_quality_index_levelair_quality_index_level),
114  (ATTR_AQI_POLLUTANT, self.air_quality_index_pollutantair_quality_index_pollutant),
115  )
116  if value is not None
117  }
118 
119  async def async_added_to_hass(self):
120  """Register callback."""
121  self.async_on_removeasync_on_remove(
123  self.hasshass, DISPATCHER_KAITERRA, self.async_write_ha_stateasync_write_ha_state
124  )
125  )
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: air_quality.py:26
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103