Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """Coordinator for the Switcher integration."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 import logging
7 
8 from aioswitcher.device import SwitcherBase
9 
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import CONF_TOKEN
12 from homeassistant.core import HomeAssistant, callback
13 from homeassistant.helpers import device_registry as dr, update_coordinator
14 from homeassistant.helpers.dispatcher import async_dispatcher_send
15 
16 from .const import DOMAIN, MAX_UPDATE_INTERVAL_SEC, SIGNAL_DEVICE_ADD
17 
18 _LOGGER = logging.getLogger(__name__)
19 
20 
22  update_coordinator.DataUpdateCoordinator[SwitcherBase]
23 ):
24  """Switcher device data update coordinator."""
25 
26  config_entry: ConfigEntry
27 
28  def __init__(
29  self,
30  hass: HomeAssistant,
31  entry: ConfigEntry,
32  device: SwitcherBase,
33  ) -> None:
34  """Initialize the Switcher device coordinator."""
35  super().__init__(
36  hass,
37  _LOGGER,
38  config_entry=entry,
39  name=device.name,
40  update_interval=timedelta(seconds=MAX_UPDATE_INTERVAL_SEC),
41  )
42  self.datadata = device
43  self.tokentoken = entry.data.get(CONF_TOKEN)
44 
45  async def _async_update_data(self) -> SwitcherBase:
46  """Mark device offline if no data."""
47  raise update_coordinator.UpdateFailed(
48  f"Device {self.name} did not send update for"
49  f" {MAX_UPDATE_INTERVAL_SEC} seconds"
50  )
51 
52  @property
53  def model(self) -> str:
54  """Switcher device model."""
55  return self.datadata.device_type.value
56 
57  @property
58  def device_id(self) -> str:
59  """Switcher device id."""
60  return self.datadata.device_id
61 
62  @property
63  def mac_address(self) -> str:
64  """Switcher device mac address."""
65  return self.datadata.mac_address
66 
67  @callback
68  def async_setup(self) -> None:
69  """Set up the coordinator."""
70  dev_reg = dr.async_get(self.hass)
71  dev_reg.async_get_or_create(
72  config_entry_id=self.config_entry.entry_id,
73  connections={(dr.CONNECTION_NETWORK_MAC, self.mac_addressmac_address)},
74  identifiers={(DOMAIN, self.device_iddevice_id)},
75  manufacturer="Switcher",
76  name=self.name,
77  model=self.modelmodel,
78  )
79  async_dispatcher_send(self.hass, SIGNAL_DEVICE_ADD, self)
None __init__(self, HomeAssistant hass, ConfigEntry entry, SwitcherBase device)
Definition: coordinator.py:33
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)
Definition: dispatcher.py:193