Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """ScreenlogicDataUpdateCoordinator definition."""
2 
3 from datetime import timedelta
4 import logging
5 from typing import TYPE_CHECKING
6 
7 from screenlogicpy import ScreenLogicGateway
8 from screenlogicpy.const.common import (
9  SL_GATEWAY_IP,
10  SL_GATEWAY_NAME,
11  SL_GATEWAY_PORT,
12  ScreenLogicCommunicationError,
13 )
14 from screenlogicpy.device_const.system import EQUIPMENT_FLAG
15 
16 from homeassistant.config_entries import ConfigEntry
17 from homeassistant.const import CONF_IP_ADDRESS, CONF_PORT, CONF_SCAN_INTERVAL
18 from homeassistant.core import HomeAssistant
19 from homeassistant.helpers.debounce import Debouncer
20 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
21 
22 from .config_flow import async_discover_gateways_by_unique_id, name_for_mac
23 from .const import DEFAULT_SCAN_INTERVAL, DOMAIN
24 
25 _LOGGER = logging.getLogger(__name__)
26 
27 REQUEST_REFRESH_DELAY = 2
28 HEATER_COOLDOWN_DELAY = 6
29 
30 
32  hass: HomeAssistant, entry: ConfigEntry
33 ) -> dict[str, str | int]:
34  """Construct connect_info from configuration entry and returns it to caller."""
35  mac = entry.unique_id
36  # Attempt to rediscover gateway to follow IP changes
37  discovered_gateways = await async_discover_gateways_by_unique_id()
38  if mac in discovered_gateways:
39  return discovered_gateways[mac]
40 
41  _LOGGER.debug("Gateway rediscovery failed for %s", entry.title)
42  if TYPE_CHECKING:
43  assert mac is not None
44  # Static connection defined or fallback from discovery
45  return {
46  SL_GATEWAY_NAME: name_for_mac(mac),
47  SL_GATEWAY_IP: entry.data[CONF_IP_ADDRESS],
48  SL_GATEWAY_PORT: entry.data[CONF_PORT],
49  }
50 
51 
53  """Class to manage the data update for the Screenlogic component."""
54 
55  def __init__(
56  self,
57  hass: HomeAssistant,
58  *,
59  config_entry: ConfigEntry,
60  gateway: ScreenLogicGateway,
61  ) -> None:
62  """Initialize the Screenlogic Data Update Coordinator."""
63  self.config_entryconfig_entryconfig_entry = config_entry
64  self.gatewaygateway = gateway
65 
66  interval = timedelta(
67  seconds=config_entry.options.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
68  )
69  super().__init__(
70  hass,
71  _LOGGER,
72  name=DOMAIN,
73  update_interval=interval,
74  # Debounced option since the device takes
75  # a moment to reflect the knock-on changes
76  request_refresh_debouncer=Debouncer(
77  hass, _LOGGER, cooldown=REQUEST_REFRESH_DELAY, immediate=False
78  ),
79  )
80 
81  async def _async_update_configured_data(self) -> None:
82  """Update data sets based on equipment config."""
83  if not self.gatewaygateway.is_client:
84  await self.gatewaygateway.async_get_status()
85  if EQUIPMENT_FLAG.INTELLICHEM in self.gatewaygateway.equipment_flags:
86  await self.gatewaygateway.async_get_chemistry()
87 
88  await self.gatewaygateway.async_get_pumps()
89  if EQUIPMENT_FLAG.CHLORINATOR in self.gatewaygateway.equipment_flags:
90  await self.gatewaygateway.async_get_scg()
91 
92  async def _async_update_data(self) -> None:
93  """Fetch data from the Screenlogic gateway."""
94  assert self.config_entryconfig_entryconfig_entry is not None
95  try:
96  if not self.gatewaygateway.is_connected:
97  connect_info = await async_get_connect_info(
98  self.hasshass, self.config_entryconfig_entryconfig_entry
99  )
100  await self.gatewaygateway.async_connect(**connect_info)
101 
102  await self._async_update_configured_data_async_update_configured_data()
103  except ScreenLogicCommunicationError as sle:
104  if self.gatewaygateway.is_connected:
105  await self.gatewaygateway.async_disconnect()
106  raise UpdateFailed(sle.msg) from sle
None __init__(self, HomeAssistant hass, *ConfigEntry config_entry, ScreenLogicGateway gateway)
Definition: coordinator.py:61
dict[str, dict[str, Any]] async_discover_gateways_by_unique_id()
Definition: config_flow.py:35
dict[str, str|int] async_get_connect_info(HomeAssistant hass, ConfigEntry entry)
Definition: coordinator.py:33