Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """Data coordinator for the dwd_weather_warnings integration."""
2 
3 from __future__ import annotations
4 
5 from dwdwfsapi import DwdWeatherWarningsAPI
6 
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.core import HomeAssistant
9 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
10 from homeassistant.util import location
11 
12 from .const import (
13  CONF_REGION_DEVICE_TRACKER,
14  CONF_REGION_IDENTIFIER,
15  DEFAULT_SCAN_INTERVAL,
16  DOMAIN,
17  LOGGER,
18 )
19 from .exceptions import EntityNotFoundError
20 from .util import get_position_data
21 
22 type DwdWeatherWarningsConfigEntry = ConfigEntry[DwdWeatherWarningsCoordinator]
23 
24 
26  """Custom coordinator for the dwd_weather_warnings integration."""
27 
28  config_entry: DwdWeatherWarningsConfigEntry
29  api: DwdWeatherWarningsAPI
30 
31  def __init__(self, hass: HomeAssistant) -> None:
32  """Initialize the dwd_weather_warnings coordinator."""
33  super().__init__(
34  hass, LOGGER, name=DOMAIN, update_interval=DEFAULT_SCAN_INTERVAL
35  )
36 
37  self._device_tracker_device_tracker = None
38  self._previous_position_previous_position = None
39 
40  async def _async_setup(self) -> None:
41  """Set up coordinator."""
42  if region_identifier := self.config_entryconfig_entry.data.get(CONF_REGION_IDENTIFIER):
43  self.apiapi = await self.hasshass.async_add_executor_job(
44  DwdWeatherWarningsAPI, region_identifier
45  )
46  else:
47  self._device_tracker_device_tracker = self.config_entryconfig_entry.data.get(
48  CONF_REGION_DEVICE_TRACKER
49  )
50 
51  async def _async_update_data(self) -> None:
52  """Get the latest data from the DWD Weather Warnings API."""
53  if self._device_tracker_device_tracker:
54  try:
55  position = get_position_data(self.hasshass, self._device_tracker_device_tracker)
56  except (EntityNotFoundError, AttributeError) as err:
57  raise UpdateFailed(f"Error fetching position: {err!r}") from err
58 
59  distance = None
60  if self._previous_position_previous_position is not None:
61  distance = location.distance(
62  self._previous_position_previous_position[0],
63  self._previous_position_previous_position[1],
64  position[0],
65  position[1],
66  )
67 
68  if distance is None or distance > 50:
69  # Only create a new object on the first update
70  # or when the distance to the previous position
71  # changes by more than 50 meters (to take GPS
72  # inaccuracy into account).
73  self.apiapi = await self.hasshass.async_add_executor_job(
74  DwdWeatherWarningsAPI, position
75  )
76  else:
77  # Otherwise update the API to check for new warnings.
78  await self.hasshass.async_add_executor_job(self.apiapi.update)
79 
80  self._previous_position_previous_position = position
81  else:
82  await self.hasshass.async_add_executor_job(self.apiapi.update)
tuple[float, float]|None get_position_data(HomeAssistant hass, str registry_id)
Definition: util.py:14