Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """Coordinator for the RainMachine integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable, Coroutine
6 from datetime import timedelta
7 from typing import TYPE_CHECKING, Any
8 
9 from homeassistant.core import HomeAssistant, callback
11  async_dispatcher_connect,
12  async_dispatcher_send,
13 )
14 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
15 
16 from .const import LOGGER
17 
18 SIGNAL_REBOOT_COMPLETED = "rainmachine_reboot_completed_{0}"
19 SIGNAL_REBOOT_REQUESTED = "rainmachine_reboot_requested_{0}"
20 
21 if TYPE_CHECKING:
22  from . import RainMachineConfigEntry
23 
24 
26  """Define an extended DataUpdateCoordinator."""
27 
28  config_entry: RainMachineConfigEntry
29 
30  def __init__(
31  self,
32  hass: HomeAssistant,
33  *,
34  entry: RainMachineConfigEntry,
35  name: str,
36  api_category: str,
37  update_interval: timedelta,
38  update_method: Callable[[], Coroutine[Any, Any, dict]],
39  ) -> None:
40  """Initialize."""
41  super().__init__(
42  hass,
43  LOGGER,
44  name=name,
45  update_interval=update_interval,
46  update_method=update_method,
47  always_update=False,
48  )
49 
50  self._rebooting_rebooting = False
51  self._signal_handler_unsubs: list[Callable[[], None]] = []
52  self.config_entryconfig_entryconfig_entry = entry
53  self.signal_reboot_completedsignal_reboot_completed = SIGNAL_REBOOT_COMPLETED.format(
54  self.config_entryconfig_entryconfig_entry.entry_id
55  )
56  self.signal_reboot_requestedsignal_reboot_requested = SIGNAL_REBOOT_REQUESTED.format(
57  self.config_entryconfig_entryconfig_entry.entry_id
58  )
59 
60  @callback
61  def async_initialize(self) -> None:
62  """Initialize the coordinator."""
63 
64  @callback
65  def async_reboot_completed() -> None:
66  """Respond to a reboot completed notification."""
67  LOGGER.debug("%s responding to reboot complete", self.namename)
68  self._rebooting_rebooting = False
69  self.last_update_successlast_update_successlast_update_success = True
70  self.async_update_listenersasync_update_listeners()
71 
72  @callback
73  def async_reboot_requested() -> None:
74  """Respond to a reboot request."""
75  LOGGER.debug("%s responding to reboot request", self.namename)
76  self._rebooting_rebooting = True
77  self.last_update_successlast_update_successlast_update_success = False
78  self.async_update_listenersasync_update_listeners()
79 
80  for signal, func in (
81  (self.signal_reboot_completedsignal_reboot_completed, async_reboot_completed),
82  (self.signal_reboot_requestedsignal_reboot_requested, async_reboot_requested),
83  ):
84  self._signal_handler_unsubs.append(
85  async_dispatcher_connect(self.hasshass, signal, func)
86  )
87 
88  @callback
89  def async_check_reboot_complete() -> None:
90  """Check whether an active reboot has been completed."""
91  if self._rebooting_rebooting and self.last_update_successlast_update_successlast_update_success:
92  LOGGER.debug("%s discovered reboot complete", self.namename)
93  async_dispatcher_send(self.hasshass, self.signal_reboot_completedsignal_reboot_completed)
94 
95  self.async_add_listenerasync_add_listenerasync_add_listener(async_check_reboot_complete)
96 
97  @callback
98  def async_teardown() -> None:
99  """Tear the coordinator down appropriately."""
100  for unsub in self._signal_handler_unsubs:
101  unsub()
102 
103  self.config_entryconfig_entryconfig_entry.async_on_unload(async_teardown)
None __init__(self, HomeAssistant hass, *RainMachineConfigEntry entry, str name, str api_category, timedelta update_interval, Callable[[], Coroutine[Any, Any, dict]] update_method)
Definition: coordinator.py:39
Callable[[], None] async_add_listener(self, CALLBACK_TYPE update_callback, Any context=None)
Callable[[], None] async_add_listener(self, CALLBACK_TYPE update_callback, Any context=None)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)
Definition: dispatcher.py:193