Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """Coordinator for the Evil Genius Labs integration."""
2 
3 from __future__ import annotations
4 
5 import asyncio
6 from datetime import timedelta
7 import logging
8 from typing import cast
9 
10 from aiohttp import ContentTypeError
11 import pyevilgenius
12 
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
15 
16 UPDATE_INTERVAL = 10
17 
18 
20  """Update coordinator for Evil Genius data."""
21 
22  info: dict
23 
24  product: dict | None
25 
26  def __init__(
27  self, hass: HomeAssistant, name: str, client: pyevilgenius.EvilGeniusDevice
28  ) -> None:
29  """Initialize the data update coordinator."""
30  self.clientclient = client
31  super().__init__(
32  hass,
33  logging.getLogger(__name__),
34  name=name,
35  update_interval=timedelta(seconds=UPDATE_INTERVAL),
36  )
37 
38  @property
39  def device_name(self) -> str:
40  """Return the device name."""
41  return cast(str, self.datadata["name"]["value"])
42 
43  @property
44  def product_name(self) -> str | None:
45  """Return the product name."""
46  if self.productproduct is None:
47  return None
48 
49  return cast(str, self.productproduct["productName"])
50 
51  async def _async_update_data(self) -> dict:
52  """Update Evil Genius data."""
53  if not hasattr(self, "info"):
54  async with asyncio.timeout(5):
55  self.infoinfo = await self.clientclient.get_info()
56 
57  if not hasattr(self, "product"):
58  async with asyncio.timeout(5):
59  try:
60  self.productproduct = await self.clientclient.get_product()
61  except ContentTypeError:
62  # Older versions of the API don't support this
63  self.productproduct = None
64 
65  async with asyncio.timeout(5):
66  return cast(dict, await self.clientclient.get_all())
None __init__(self, HomeAssistant hass, str name, pyevilgenius.EvilGeniusDevice client)
Definition: coordinator.py:28
dict[str, Any]|None get_info(HomeAssistant hass)
Definition: coordinator.py:69