Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """Update coordinators for Yardian."""
2 
3 from __future__ import annotations
4 
5 import asyncio
6 import datetime
7 import logging
8 
9 from pyyardian import (
10  AsyncYardianClient,
11  NetworkException,
12  NotAuthorizedException,
13  YardianDeviceState,
14 )
15 
16 from homeassistant.config_entries import ConfigEntry
17 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.device_registry import DeviceInfo
19 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
20 
21 from .const import DOMAIN, MANUFACTURER
22 
23 _LOGGER = logging.getLogger(__name__)
24 
25 SCAN_INTERVAL = datetime.timedelta(seconds=30)
26 
27 
29  """Coordinator for Yardian API calls."""
30 
31  def __init__(
32  self,
33  hass: HomeAssistant,
34  entry: ConfigEntry,
35  controller: AsyncYardianClient,
36  ) -> None:
37  """Initialize Yardian API communication."""
38  super().__init__(
39  hass,
40  _LOGGER,
41  name=entry.title,
42  update_interval=SCAN_INTERVAL,
43  always_update=False,
44  )
45 
46  self.controllercontroller = controller
47  self.yidyid = entry.data["yid"]
48  self._name_name = entry.title
49  self._model_model = entry.data["model"]
50 
51  @property
52  def device_info(self) -> DeviceInfo:
53  """Return information about the device."""
54  return DeviceInfo(
55  name=self._name_name,
56  identifiers={(DOMAIN, self.yidyid)},
57  manufacturer=MANUFACTURER,
58  model=self._model_model,
59  )
60 
61  async def _async_update_data(self) -> YardianDeviceState:
62  """Fetch data from Yardian device."""
63  try:
64  async with asyncio.timeout(10):
65  return await self.controllercontroller.fetch_device_state()
66 
67  except TimeoutError as e:
68  raise UpdateFailed("Communication with Device was time out") from e
69  except NotAuthorizedException as e:
70  raise UpdateFailed("Invalid access token") from e
71  except NetworkException as e:
72  raise UpdateFailed("Failed to communicate with Device") from e
None __init__(self, HomeAssistant hass, ConfigEntry entry, AsyncYardianClient controller)
Definition: coordinator.py:36