Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """Coordinator for handling data fetching and updates."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import TYPE_CHECKING, Any
7 
8 from madvr.madvr import Madvr
9 
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
12 
13 from .const import DOMAIN
14 
15 _LOGGER = logging.getLogger(__name__)
16 
17 if TYPE_CHECKING:
18  from . import MadVRConfigEntry
19 
20 
21 class MadVRCoordinator(DataUpdateCoordinator[dict[str, Any]]):
22  """Madvr coordinator for Envy (push-based API)."""
23 
24  config_entry: MadVRConfigEntry
25 
26  def __init__(
27  self,
28  hass: HomeAssistant,
29  client: Madvr,
30  ) -> None:
31  """Initialize madvr coordinator."""
32  super().__init__(hass, _LOGGER, name=DOMAIN)
33  assert self.config_entryconfig_entry.unique_id
34  self.macmac = self.config_entryconfig_entry.unique_id
35  self.clientclient = client
36  # this does not use poll/refresh, so we need to set this to not None on init
37  self.datadatadata = {}
38  # this passes a callback to the client to push new data to the coordinator
39  self.clientclient.set_update_callback(self.handle_push_datahandle_push_data)
40  _LOGGER.debug("MadVRCoordinator initialized with mac: %s", self.macmac)
41 
42  def handle_push_data(self, data: dict[str, Any]) -> None:
43  """Handle new data pushed from the API."""
44  _LOGGER.debug("Received push data: %s", data)
45  # inform HA that we have new data
46  self.async_set_updated_dataasync_set_updated_data(data)
47 
48  async def handle_coordinator_load(self) -> None:
49  """Handle operations on integration load."""
50  _LOGGER.debug("Using loop: %s", self.clientclient.loop)
51  # tell the library to start background tasks
52  await self.clientclient.async_add_tasks()
53  _LOGGER.debug("Added %s tasks to client", len(self.clientclient.tasks))
None __init__(self, HomeAssistant hass, Madvr client)
Definition: coordinator.py:30