Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """DataUpdateCoordinator for Motionblinds integration."""
2 
3 import asyncio
4 from datetime import timedelta
5 import logging
6 from typing import Any
7 
8 from motionblinds import DEVICE_TYPES_WIFI, ParseException
9 
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
12 
13 from .const import (
14  ATTR_AVAILABLE,
15  CONF_WAIT_FOR_PUSH,
16  KEY_API_LOCK,
17  KEY_GATEWAY,
18  UPDATE_INTERVAL,
19  UPDATE_INTERVAL_FAST,
20 )
21 
22 _LOGGER = logging.getLogger(__name__)
23 
24 
26  """Class to manage fetching data from single endpoint."""
27 
28  def __init__(
29  self,
30  hass: HomeAssistant,
31  logger: logging.Logger,
32  coordinator_info: dict[str, Any],
33  *,
34  name: str,
35  update_interval: timedelta,
36  ) -> None:
37  """Initialize global data updater."""
38  super().__init__(
39  hass,
40  logger,
41  name=name,
42  update_interval=update_interval,
43  )
44 
45  self.api_lockapi_lock = coordinator_info[KEY_API_LOCK]
46  self._gateway_gateway = coordinator_info[KEY_GATEWAY]
47  self._wait_for_push_wait_for_push = coordinator_info[CONF_WAIT_FOR_PUSH]
48 
49  def update_gateway(self):
50  """Fetch data from gateway."""
51  try:
52  self._gateway_gateway.Update()
53  except (TimeoutError, ParseException):
54  # let the error be logged and handled by the motionblinds library
55  return {ATTR_AVAILABLE: False}
56 
57  return {ATTR_AVAILABLE: True}
58 
59  def update_blind(self, blind):
60  """Fetch data from a blind."""
61  try:
62  if blind.device_type in DEVICE_TYPES_WIFI:
63  blind.Update_from_cache()
64  elif self._wait_for_push_wait_for_push:
65  blind.Update()
66  else:
67  blind.Update_trigger()
68  except (TimeoutError, ParseException):
69  # let the error be logged and handled by the motionblinds library
70  return {ATTR_AVAILABLE: False}
71 
72  return {ATTR_AVAILABLE: True}
73 
74  async def _async_update_data(self):
75  """Fetch the latest data from the gateway and blinds."""
76  data = {}
77 
78  async with self.api_lockapi_lock:
79  data[KEY_GATEWAY] = await self.hasshass.async_add_executor_job(
80  self.update_gatewayupdate_gateway
81  )
82 
83  for blind in self._gateway_gateway.device_list.values():
84  await asyncio.sleep(1.5)
85  async with self.api_lockapi_lock:
86  data[blind.mac] = await self.hasshass.async_add_executor_job(
87  self.update_blindupdate_blind, blind
88  )
89 
90  all_available = all(device[ATTR_AVAILABLE] for device in data.values())
91  if all_available:
92  self.update_intervalupdate_intervalupdate_intervalupdate_intervalupdate_interval = timedelta(seconds=UPDATE_INTERVAL)
93  else:
94  self.update_intervalupdate_intervalupdate_intervalupdate_intervalupdate_interval = timedelta(seconds=UPDATE_INTERVAL_FAST)
95 
96  return data
None __init__(self, HomeAssistant hass, logging.Logger logger, dict[str, Any] coordinator_info, *str name, timedelta update_interval)
Definition: coordinator.py:36