Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Component for handling Air Quality data for your location."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 import logging
7 from typing import Final, final
8 
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import CONCENTRATION_MICROGRAMS_PER_CUBIC_METER
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers import config_validation as cv
13 from homeassistant.helpers.entity import Entity
14 from homeassistant.helpers.entity_component import EntityComponent
15 from homeassistant.helpers.typing import ConfigType, StateType
16 from homeassistant.util.hass_dict import HassKey
17 
18 from .const import DOMAIN
19 
20 _LOGGER: Final = logging.getLogger(__name__)
21 
22 DATA_COMPONENT: HassKey[EntityComponent[AirQualityEntity]] = HassKey(DOMAIN)
23 ENTITY_ID_FORMAT: Final = DOMAIN + ".{}"
24 PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA
25 PLATFORM_SCHEMA_BASE = cv.PLATFORM_SCHEMA_BASE
26 SCAN_INTERVAL: Final = timedelta(seconds=30)
27 
28 ATTR_AQI: Final = "air_quality_index"
29 ATTR_CO2: Final = "carbon_dioxide"
30 ATTR_CO: Final = "carbon_monoxide"
31 ATTR_N2O: Final = "nitrogen_oxide"
32 ATTR_NO: Final = "nitrogen_monoxide"
33 ATTR_NO2: Final = "nitrogen_dioxide"
34 ATTR_OZONE: Final = "ozone"
35 ATTR_PM_0_1: Final = "particulate_matter_0_1"
36 ATTR_PM_10: Final = "particulate_matter_10"
37 ATTR_PM_2_5: Final = "particulate_matter_2_5"
38 ATTR_SO2: Final = "sulphur_dioxide"
39 
40 PROP_TO_ATTR: Final[dict[str, str]] = {
41  "air_quality_index": ATTR_AQI,
42  "carbon_dioxide": ATTR_CO2,
43  "carbon_monoxide": ATTR_CO,
44  "nitrogen_oxide": ATTR_N2O,
45  "nitrogen_monoxide": ATTR_NO,
46  "nitrogen_dioxide": ATTR_NO2,
47  "ozone": ATTR_OZONE,
48  "particulate_matter_0_1": ATTR_PM_0_1,
49  "particulate_matter_10": ATTR_PM_10,
50  "particulate_matter_2_5": ATTR_PM_2_5,
51  "sulphur_dioxide": ATTR_SO2,
52 }
53 
54 # mypy: disallow-any-generics
55 
56 
57 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
58  """Set up the air quality component."""
59  component = hass.data[DATA_COMPONENT] = EntityComponent[AirQualityEntity](
60  _LOGGER, DOMAIN, hass, SCAN_INTERVAL
61  )
62  await component.async_setup(config)
63  return True
64 
65 
66 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
67  """Set up a config entry."""
68  return await hass.data[DATA_COMPONENT].async_setup_entry(entry)
69 
70 
71 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
72  """Unload a config entry."""
73  return await hass.data[DATA_COMPONENT].async_unload_entry(entry)
74 
75 
77  """ABC for air quality data."""
78 
79  @property
80  def particulate_matter_2_5(self) -> StateType:
81  """Return the particulate matter 2.5 level."""
82  raise NotImplementedError
83 
84  @property
85  def particulate_matter_10(self) -> StateType:
86  """Return the particulate matter 10 level."""
87  return None
88 
89  @property
90  def particulate_matter_0_1(self) -> StateType:
91  """Return the particulate matter 0.1 level."""
92  return None
93 
94  @property
95  def air_quality_index(self) -> StateType:
96  """Return the Air Quality Index (AQI)."""
97  return None
98 
99  @property
100  def ozone(self) -> StateType:
101  """Return the O3 (ozone) level."""
102  return None
103 
104  @property
105  def carbon_monoxide(self) -> StateType:
106  """Return the CO (carbon monoxide) level."""
107  return None
108 
109  @property
110  def carbon_dioxide(self) -> StateType:
111  """Return the CO2 (carbon dioxide) level."""
112  return None
113 
114  @property
115  def sulphur_dioxide(self) -> StateType:
116  """Return the SO2 (sulphur dioxide) level."""
117  return None
118 
119  @property
120  def nitrogen_oxide(self) -> StateType:
121  """Return the N2O (nitrogen oxide) level."""
122  return None
123 
124  @property
125  def nitrogen_monoxide(self) -> StateType:
126  """Return the NO (nitrogen monoxide) level."""
127  return None
128 
129  @property
130  def nitrogen_dioxide(self) -> StateType:
131  """Return the NO2 (nitrogen dioxide) level."""
132  return None
133 
134  @final
135  @property
136  def state_attributes(self) -> dict[str, str | int | float]:
137  """Return the state attributes."""
138  data: dict[str, str | int | float] = {}
139 
140  for prop, attr in PROP_TO_ATTR.items():
141  if (value := getattr(self, prop)) is not None:
142  data[attr] = value
143 
144  return data
145 
146  @property
147  def state(self) -> StateType:
148  """Return the current state."""
149  return self.particulate_matter_2_5particulate_matter_2_5
150 
151  @property
152  def unit_of_measurement(self) -> str:
153  """Return the unit of measurement of this entity."""
154  return CONCENTRATION_MICROGRAMS_PER_CUBIC_METER
dict[str, str|int|float] state_attributes(self)
Definition: __init__.py:136
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:71
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:66
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:57