Home Assistant Unofficial Reference 2024.12.1
air_quality.py
Go to the documentation of this file.
1 """Support for Ampio Air Quality data."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Final
7 
8 from asmog import AmpioSmog
9 import voluptuous as vol
10 
12  PLATFORM_SCHEMA as AIR_QUALITY_PLATFORM_SCHEMA,
13  AirQualityEntity,
14 )
15 from homeassistant.const import CONF_NAME
16 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers.aiohttp_client import async_get_clientsession
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
21 from homeassistant.util import Throttle
22 
23 from .const import CONF_STATION_ID, SCAN_INTERVAL
24 
25 _LOGGER: Final = logging.getLogger(__name__)
26 
27 PLATFORM_SCHEMA: Final = AIR_QUALITY_PLATFORM_SCHEMA.extend(
28  {vol.Required(CONF_STATION_ID): cv.string, vol.Optional(CONF_NAME): cv.string}
29 )
30 
31 
33  hass: HomeAssistant,
34  config: ConfigType,
35  async_add_entities: AddEntitiesCallback,
36  discovery_info: DiscoveryInfoType | None = None,
37 ) -> None:
38  """Set up the Ampio Smog air quality platform."""
39 
40  name = config.get(CONF_NAME)
41  station_id = config[CONF_STATION_ID]
42 
43  session = async_get_clientsession(hass)
44  api = AmpioSmogMapData(AmpioSmog(station_id, hass.loop, session))
45 
46  await api.async_update()
47 
48  if not api.api.data:
49  _LOGGER.error("Station %s is not available", station_id)
50  return
51 
52  async_add_entities([AmpioSmogQuality(api, station_id, name)], True)
53 
54 
56  """Implementation of an Ampio Smog air quality entity."""
57 
58  _attr_attribution = "Data provided by Ampio"
59 
60  def __init__(
61  self, api: AmpioSmogMapData, station_id: str, name: str | None
62  ) -> None:
63  """Initialize the air quality entity."""
64  self._ampio_ampio = api
65  self._station_id_station_id = station_id
66  self._name_name = name or api.api.name
67 
68  @property
69  def name(self) -> str:
70  """Return the name of the air quality entity."""
71  return self._name_name
72 
73  @property
74  def unique_id(self) -> str:
75  """Return unique_name."""
76  return f"ampio_smog_{self._station_id}"
77 
78  @property
79  def particulate_matter_2_5(self) -> str | None:
80  """Return the particulate matter 2.5 level."""
81  return self._ampio_ampio.api.pm2_5 # type: ignore[no-any-return]
82 
83  @property
84  def particulate_matter_10(self) -> str | None:
85  """Return the particulate matter 10 level."""
86  return self._ampio_ampio.api.pm10 # type: ignore[no-any-return]
87 
88  async def async_update(self) -> None:
89  """Get the latest data from the AmpioMap API."""
90  await self._ampio_ampio.async_update()
91 
92 
94  """Get the latest data and update the states."""
95 
96  def __init__(self, api: AmpioSmog) -> None:
97  """Initialize the data object."""
98  self.apiapi = api
99 
100  @Throttle(SCAN_INTERVAL)
101  async def async_update(self) -> None:
102  """Get the latest data from AmpioMap."""
103  await self.apiapi.get_data()
None __init__(self, AmpioSmogMapData api, str station_id, str|None name)
Definition: air_quality.py:62
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: air_quality.py:37
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)