Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """DataUpdateCoordinator for Elgato."""
2 
3 from dataclasses import dataclass
4 
5 from elgato import BatteryInfo, Elgato, ElgatoConnectionError, Info, Settings, State
6 
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.const import CONF_HOST, CONF_PORT
9 from homeassistant.core import HomeAssistant
10 from homeassistant.helpers.aiohttp_client import async_get_clientsession
11 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
12 
13 from .const import DOMAIN, LOGGER, SCAN_INTERVAL
14 
15 
16 @dataclass
17 class ElgatoData:
18  """Elgato data stored in the DataUpdateCoordinator."""
19 
20  battery: BatteryInfo | None
21  info: Info
22  settings: Settings
23  state: State
24 
25 
27  """Class to manage fetching Elgato data."""
28 
29  config_entry: ConfigEntry
30  has_battery: bool | None = None
31 
32  def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None:
33  """Initialize the coordinator."""
34  self.config_entryconfig_entryconfig_entry = entry
35  self.clientclient = Elgato(
36  entry.data[CONF_HOST],
37  port=entry.data[CONF_PORT],
38  session=async_get_clientsession(hass),
39  )
40  super().__init__(
41  hass,
42  LOGGER,
43  name=f"{DOMAIN}_{entry.data[CONF_HOST]}",
44  update_interval=SCAN_INTERVAL,
45  )
46 
47  async def _async_update_data(self) -> ElgatoData:
48  """Fetch data from the Elgato device."""
49  try:
50  if self.has_batteryhas_battery is None:
51  self.has_batteryhas_battery = await self.clientclient.has_battery()
52 
53  return ElgatoData(
54  battery=await self.clientclient.battery() if self.has_batteryhas_battery else None,
55  info=await self.clientclient.info(),
56  settings=await self.clientclient.settings(),
57  state=await self.clientclient.state(),
58  )
59  except ElgatoConnectionError as err:
60  raise UpdateFailed(err) from err
None __init__(self, HomeAssistant hass, ConfigEntry entry)
Definition: coordinator.py:32
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)
bool state(HomeAssistant hass, str|State|None entity, Any req_state, timedelta|None for_period=None, str|None attribute=None, TemplateVarsType variables=None)
Definition: condition.py:551