Home Assistant Unofficial Reference 2024.12.1
air_quality.py
Go to the documentation of this file.
1 """Support for openSenseMap Air Quality data."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 import logging
7 
8 from opensensemap_api import OpenSenseMap
9 from opensensemap_api.exceptions import OpenSenseMapError
10 import voluptuous as vol
11 
13  PLATFORM_SCHEMA as AIR_QUALITY_PLATFORM_SCHEMA,
14  AirQualityEntity,
15 )
16 from homeassistant.const import CONF_NAME
17 from homeassistant.core import HomeAssistant
18 from homeassistant.exceptions import PlatformNotReady
19 from homeassistant.helpers.aiohttp_client import async_get_clientsession
21 from homeassistant.helpers.entity_platform import AddEntitiesCallback
22 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
23 from homeassistant.util import Throttle
24 
25 _LOGGER = logging.getLogger(__name__)
26 
27 
28 CONF_STATION_ID = "station_id"
29 
30 SCAN_INTERVAL = timedelta(minutes=10)
31 
32 PLATFORM_SCHEMA = AIR_QUALITY_PLATFORM_SCHEMA.extend(
33  {vol.Required(CONF_STATION_ID): cv.string, vol.Optional(CONF_NAME): cv.string}
34 )
35 
36 
38  hass: HomeAssistant,
39  config: ConfigType,
40  async_add_entities: AddEntitiesCallback,
41  discovery_info: DiscoveryInfoType | None = None,
42 ) -> None:
43  """Set up the openSenseMap air quality platform."""
44 
45  name = config.get(CONF_NAME)
46  station_id = config[CONF_STATION_ID]
47 
48  session = async_get_clientsession(hass)
49  osm_api = OpenSenseMapData(OpenSenseMap(station_id, session))
50 
51  await osm_api.async_update()
52 
53  if "name" not in osm_api.api.data:
54  _LOGGER.error("Station %s is not available", station_id)
55  raise PlatformNotReady
56 
57  station_name = osm_api.api.data["name"] if name is None else name
58 
59  async_add_entities([OpenSenseMapQuality(station_name, osm_api)], True)
60 
61 
63  """Implementation of an openSenseMap air quality entity."""
64 
65  _attr_attribution = "Data provided by openSenseMap"
66 
67  def __init__(self, name, osm):
68  """Initialize the air quality entity."""
69  self._name_name = name
70  self._osm_osm = osm
71 
72  @property
73  def name(self):
74  """Return the name of the air quality entity."""
75  return self._name_name
76 
77  @property
79  """Return the particulate matter 2.5 level."""
80  return self._osm_osm.api.pm2_5
81 
82  @property
84  """Return the particulate matter 10 level."""
85  return self._osm_osm.api.pm10
86 
87  async def async_update(self):
88  """Get the latest data from the openSenseMap API."""
89  await self._osm_osm.async_update()
90 
91 
93  """Get the latest data and update the states."""
94 
95  def __init__(self, api):
96  """Initialize the data object."""
97  self.apiapi = api
98 
99  @Throttle(SCAN_INTERVAL)
100  async def async_update(self):
101  """Get the latest data from the Pi-hole."""
102 
103  try:
104  await self.apiapi.get_data()
105  except OpenSenseMapError as err:
106  _LOGGER.error("Unable to fetch data: %s", err)
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: air_quality.py:42
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)