Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """Component to embed TP-Link smart home devices."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 import logging
7 
8 from kasa import AuthenticationError, Device, KasaException
9 
10 from homeassistant import config_entries
11 from homeassistant.core import HomeAssistant
12 from homeassistant.exceptions import ConfigEntryAuthFailed
13 from homeassistant.helpers.debounce import Debouncer
14 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
15 
16 _LOGGER = logging.getLogger(__name__)
17 
18 REQUEST_REFRESH_DELAY = 0.35
19 
20 
22  """DataUpdateCoordinator to gather data for a specific TPLink device."""
23 
24  config_entry: config_entries.ConfigEntry
25 
26  def __init__(
27  self,
28  hass: HomeAssistant,
29  device: Device,
30  update_interval: timedelta,
31  ) -> None:
32  """Initialize DataUpdateCoordinator to gather data for specific SmartPlug."""
33  self.devicedevice = device
34  super().__init__(
35  hass,
36  _LOGGER,
37  name=device.host,
38  update_interval=update_interval,
39  # We don't want an immediate refresh since the device
40  # takes a moment to reflect the state change
41  request_refresh_debouncer=Debouncer(
42  hass, _LOGGER, cooldown=REQUEST_REFRESH_DELAY, immediate=False
43  ),
44  )
45 
46  async def _async_update_data(self) -> None:
47  """Fetch all device and sensor data from api."""
48  try:
49  await self.devicedevice.update(update_children=False)
50  except AuthenticationError as ex:
51  raise ConfigEntryAuthFailed from ex
52  except KasaException as ex:
53  raise UpdateFailed(ex) from ex
IssData update(pyiss.ISS iss)
Definition: __init__.py:33