|
| 1 | +"""``air_quality`` domain implementation (read-only).""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +from haclient.core.plugins import DomainSpec, register_domain |
| 8 | +from haclient.entity.base import Entity |
| 9 | + |
| 10 | + |
| 11 | +def _coerce_numeric(value: Any) -> float | int | None: |
| 12 | + """Coerce an air-quality attribute value to a number. |
| 13 | +
|
| 14 | + Parameters |
| 15 | + ---------- |
| 16 | + value : Any |
| 17 | + Raw attribute value from Home Assistant. |
| 18 | +
|
| 19 | + Returns |
| 20 | + ------- |
| 21 | + float, int, or None |
| 22 | + ``int`` when the value is an integer literal, ``float`` for any |
| 23 | + other numeric value (including numeric strings), and ``None`` |
| 24 | + when the value is missing, non-numeric, or one of HA's sentinel |
| 25 | + unavailability strings. |
| 26 | + """ |
| 27 | + if value is None: |
| 28 | + return None |
| 29 | + if isinstance(value, bool): |
| 30 | + # bool is a subclass of int; treat it as not a real measurement. |
| 31 | + return None |
| 32 | + if isinstance(value, int): |
| 33 | + return value |
| 34 | + if isinstance(value, float): |
| 35 | + return value |
| 36 | + if isinstance(value, str): |
| 37 | + if value in ("unknown", "unavailable", ""): |
| 38 | + return None |
| 39 | + try: |
| 40 | + return float(value) |
| 41 | + except ValueError: |
| 42 | + return None |
| 43 | + return None |
| 44 | + |
| 45 | + |
| 46 | +class AirQuality(Entity): |
| 47 | + """A read-only Home Assistant air quality entity. |
| 48 | +
|
| 49 | + Exposes typed properties for the pollutant metrics the underlying |
| 50 | + integration chooses to report. Every metric returns ``None`` when |
| 51 | + the sensor does not provide that reading, so unsupported metrics |
| 52 | + degrade silently rather than raising. The entity is read-only; the |
| 53 | + HA ``air_quality`` domain exposes no service actions. |
| 54 | +
|
| 55 | + The Home Assistant ``air_quality`` platform reports its overall Air |
| 56 | + Quality Index (AQI) as the entity *state* and individual pollutants |
| 57 | + as attributes. `air_quality_index` therefore prefers an explicit |
| 58 | + ``air_quality_index`` attribute when present and falls back to the |
| 59 | + state string. All other metrics are read directly from attributes. |
| 60 | + """ |
| 61 | + |
| 62 | + domain = "air_quality" |
| 63 | + |
| 64 | + # -- Listener decorators ------------------------------------------ |
| 65 | + |
| 66 | + def on_aqi_change(self, func: Any) -> Any: |
| 67 | + """Register a listener for Air Quality Index changes. |
| 68 | +
|
| 69 | + Fires whenever the entity *state* string changes, which mirrors |
| 70 | + the HA convention of reporting the AQI as the entity state. |
| 71 | +
|
| 72 | + Parameters |
| 73 | + ---------- |
| 74 | + func : callable |
| 75 | + Sync or async callable receiving ``(old_state, new_state)``. |
| 76 | +
|
| 77 | + Returns |
| 78 | + ------- |
| 79 | + callable |
| 80 | + The same *func*, returned for decorator use. |
| 81 | + """ |
| 82 | + return self._register_state_value_listener(func) |
| 83 | + |
| 84 | + def on_pm25_change(self, func: Any) -> Any: |
| 85 | + """Register a listener for PM2.5 attribute changes. |
| 86 | +
|
| 87 | + Parameters |
| 88 | + ---------- |
| 89 | + func : callable |
| 90 | + Callable receiving ``(old_value, new_value)`` for the |
| 91 | + ``particulate_matter_2_5`` attribute. |
| 92 | +
|
| 93 | + Returns |
| 94 | + ------- |
| 95 | + callable |
| 96 | + The same *func*, returned for decorator use. |
| 97 | + """ |
| 98 | + return self._register_attr_listener("particulate_matter_2_5", func) |
| 99 | + |
| 100 | + def on_co2_change(self, func: Any) -> Any: |
| 101 | + """Register a listener for CO2 attribute changes. |
| 102 | +
|
| 103 | + Parameters |
| 104 | + ---------- |
| 105 | + func : callable |
| 106 | + Callable receiving ``(old_value, new_value)`` for the |
| 107 | + ``carbon_dioxide`` attribute. |
| 108 | +
|
| 109 | + Returns |
| 110 | + ------- |
| 111 | + callable |
| 112 | + The same *func*, returned for decorator use. |
| 113 | + """ |
| 114 | + return self._register_attr_listener("carbon_dioxide", func) |
| 115 | + |
| 116 | + # -- State properties --------------------------------------------- |
| 117 | + |
| 118 | + @property |
| 119 | + def air_quality_index(self) -> float | int | None: |
| 120 | + """Overall Air Quality Index, if reported. |
| 121 | +
|
| 122 | + Returns |
| 123 | + ------- |
| 124 | + float, int, or None |
| 125 | + The explicit ``air_quality_index`` attribute when present, |
| 126 | + otherwise the numeric coercion of the entity state, or |
| 127 | + ``None`` when neither yields a number. |
| 128 | + """ |
| 129 | + explicit = _coerce_numeric(self.attributes.get("air_quality_index")) |
| 130 | + if explicit is not None: |
| 131 | + return explicit |
| 132 | + return _coerce_numeric(self.state) |
| 133 | + |
| 134 | + @property |
| 135 | + def particulate_matter_2_5(self) -> float | int | None: |
| 136 | + """PM2.5 reading, typically in µg/m³.""" |
| 137 | + return _coerce_numeric(self.attributes.get("particulate_matter_2_5")) |
| 138 | + |
| 139 | + @property |
| 140 | + def particulate_matter_10(self) -> float | int | None: |
| 141 | + """PM10 reading, typically in µg/m³.""" |
| 142 | + return _coerce_numeric(self.attributes.get("particulate_matter_10")) |
| 143 | + |
| 144 | + @property |
| 145 | + def carbon_dioxide(self) -> float | int | None: |
| 146 | + """CO2 concentration, typically in ppm.""" |
| 147 | + return _coerce_numeric(self.attributes.get("carbon_dioxide")) |
| 148 | + |
| 149 | + @property |
| 150 | + def carbon_monoxide(self) -> float | int | None: |
| 151 | + """CO concentration, typically in ppm.""" |
| 152 | + return _coerce_numeric(self.attributes.get("carbon_monoxide")) |
| 153 | + |
| 154 | + @property |
| 155 | + def volatile_organic_compounds(self) -> float | int | None: |
| 156 | + """Volatile organic compound concentration, if reported.""" |
| 157 | + return _coerce_numeric(self.attributes.get("volatile_organic_compounds")) |
| 158 | + |
| 159 | + @property |
| 160 | + def nitrogen_dioxide(self) -> float | int | None: |
| 161 | + """NO2 concentration, if reported.""" |
| 162 | + return _coerce_numeric(self.attributes.get("nitrogen_dioxide")) |
| 163 | + |
| 164 | + @property |
| 165 | + def ozone(self) -> float | int | None: |
| 166 | + """Ozone concentration, if reported.""" |
| 167 | + return _coerce_numeric(self.attributes.get("ozone")) |
| 168 | + |
| 169 | + |
| 170 | +SPEC: DomainSpec[AirQuality] = register_domain( |
| 171 | + DomainSpec(name="air_quality", entity_cls=AirQuality) |
| 172 | +) |
| 173 | +"""The `DomainSpec` registered with the shared `DomainRegistry`.""" |
0 commit comments