Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """The ukraine_alarm component."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 import logging
7 from typing import Any
8 
9 import aiohttp
10 from aiohttp import ClientSession
11 from uasiren.client import Client
12 
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
15 
16 from .const import ALERT_TYPES, DOMAIN
17 
18 _LOGGER = logging.getLogger(__name__)
19 
20 UPDATE_INTERVAL = timedelta(seconds=10)
21 
22 
24  """Class to manage fetching Ukraine Alarm API."""
25 
26  def __init__(
27  self,
28  hass: HomeAssistant,
29  session: ClientSession,
30  region_id: str,
31  ) -> None:
32  """Initialize."""
33  self.region_idregion_id = region_id
34  self.uasirenuasiren = Client(session)
35 
36  super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=UPDATE_INTERVAL)
37 
38  async def _async_update_data(self) -> dict[str, Any]:
39  """Update data via library."""
40  try:
41  res = await self.uasirenuasiren.get_alerts(self.region_idregion_id)
42  except aiohttp.ClientError as error:
43  raise UpdateFailed(f"Error fetching alerts from API: {error}") from error
44 
45  current = {alert_type: False for alert_type in ALERT_TYPES}
46  for alert in res[0]["activeAlerts"]:
47  current[alert["type"]] = True
48 
49  return current
None __init__(self, HomeAssistant hass, ClientSession session, str region_id)
Definition: coordinator.py:31