Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """Update coordinator for IronOS Integration."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 import logging
7 from typing import TYPE_CHECKING
8 
9 from aiogithubapi import GitHubAPI, GitHubException, GitHubReleaseModel
10 from pynecil import CommunicationError, DeviceInfoResponse, LiveDataResponse, Pynecil
11 
12 from homeassistant.config_entries import ConfigEntry
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
15 
16 from .const import DOMAIN
17 
18 _LOGGER = logging.getLogger(__name__)
19 
20 SCAN_INTERVAL = timedelta(seconds=5)
21 SCAN_INTERVAL_GITHUB = timedelta(hours=3)
22 
23 
25  """IronOS live data coordinator."""
26 
27  device_info: DeviceInfoResponse
28  config_entry: ConfigEntry
29 
30  def __init__(self, hass: HomeAssistant, device: Pynecil) -> None:
31  """Initialize IronOS coordinator."""
32  super().__init__(
33  hass,
34  _LOGGER,
35  name=DOMAIN,
36  update_interval=SCAN_INTERVAL,
37  )
38  self.devicedevice = device
39 
40  async def _async_update_data(self) -> LiveDataResponse:
41  """Fetch data from Device."""
42 
43  try:
44  # device info is cached and won't be refetched on every
45  # coordinator refresh, only after the device has disconnected
46  # the device info is refetched
47  self.device_infodevice_info = await self.devicedevice.get_device_info()
48  return await self.devicedevice.get_live_data()
49 
50  except CommunicationError as e:
51  raise UpdateFailed("Cannot connect to device") from e
52 
53 
55  """IronOS coordinator for retrieving update information from github."""
56 
57  def __init__(self, hass: HomeAssistant, github: GitHubAPI) -> None:
58  """Initialize IronOS coordinator."""
59  super().__init__(
60  hass,
61  _LOGGER,
62  config_entry=None,
63  name=DOMAIN,
64  update_interval=SCAN_INTERVAL_GITHUB,
65  )
66  self.githubgithub = github
67 
68  async def _async_update_data(self) -> GitHubReleaseModel:
69  """Fetch data from Github."""
70 
71  try:
72  release = await self.githubgithub.repos.releases.latest("Ralim/IronOS")
73 
74  except GitHubException as e:
75  raise UpdateFailed(
76  "Failed to retrieve latest release data from Github"
77  ) from e
78 
79  if TYPE_CHECKING:
80  assert release.data
81 
82  return release.data
None __init__(self, HomeAssistant hass, GitHubAPI github)
Definition: coordinator.py:57
None __init__(self, HomeAssistant hass, Pynecil device)
Definition: coordinator.py:30
DeviceInfo get_device_info(str coordinates, str name)
Definition: __init__.py:156