Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """The aurora component."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 import logging
7 from typing import TYPE_CHECKING
8 
9 from aiohttp import ClientError
10 from auroranoaa import AuroraForecast
11 
12 from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers.aiohttp_client import async_get_clientsession
15 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
16 
17 from .const import CONF_THRESHOLD, DEFAULT_THRESHOLD
18 
19 if TYPE_CHECKING:
20  from . import AuroraConfigEntry
21 
22 _LOGGER = logging.getLogger(__name__)
23 
24 
26  """Class to manage fetching data from the NOAA Aurora API."""
27 
28  config_entry: AuroraConfigEntry
29 
30  def __init__(self, hass: HomeAssistant) -> None:
31  """Initialize the data updater."""
32 
33  super().__init__(
34  hass=hass,
35  logger=_LOGGER,
36  name="Aurora",
37  update_interval=timedelta(minutes=5),
38  )
39 
40  self.apiapi = AuroraForecast(async_get_clientsession(hass))
41  self.latitudelatitude = round(self.config_entryconfig_entry.data[CONF_LATITUDE])
42  self.longitudelongitude = round(self.config_entryconfig_entry.data[CONF_LONGITUDE])
43  self.thresholdthreshold = int(
44  self.config_entryconfig_entry.options.get(CONF_THRESHOLD, DEFAULT_THRESHOLD)
45  )
46 
47  async def _async_update_data(self) -> int:
48  """Fetch the data from the NOAA Aurora Forecast."""
49 
50  try:
51  return await self.apiapi.get_forecast_data(self.longitudelongitude, self.latitudelatitude)
52  except ClientError as error:
53  raise UpdateFailed(f"Error updating from NOAA: {error}") from error
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)