Home Assistant Unofficial Reference 2024.12.1
hub.py
Go to the documentation of this file.
1 """Code to handle a Pulse Hub."""
2 
3 from __future__ import annotations
4 
5 import asyncio
6 from collections.abc import Callable
7 
8 import aiopulse
9 
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers.dispatcher import async_dispatcher_send
13 
14 from .const import ACMEDA_ENTITY_REMOVE, ACMEDA_HUB_UPDATE, LOGGER
15 from .helpers import update_devices
16 
17 
18 class PulseHub:
19  """Manages a single Pulse Hub."""
20 
21  api: aiopulse.Hub
22 
23  def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None:
24  """Initialize the system."""
25  self.config_entryconfig_entry = config_entry
26  self.hasshass = hass
27  self.tasks: list[asyncio.Task[None]] = []
28  self.current_rollers: dict[int, aiopulse.Roller] = {}
29  self.cleanup_callbacks: list[Callable[[], None]] = []
30 
31  @property
32  def title(self) -> str:
33  """Return the title of the hub shown in the integrations list."""
34  return f"{self.api.id} ({self.api.host})"
35 
36  @property
37  def host(self) -> str:
38  """Return the host of this hub."""
39  return self.config_entryconfig_entry.data["host"] # type: ignore[no-any-return]
40 
41  async def async_setup(self, tries: int = 0) -> bool:
42  """Set up a hub based on host parameter."""
43  self.apiapi = hub = aiopulse.Hub(self.hosthost)
44 
45  hub.callback_subscribe(self.async_notify_updateasync_notify_update)
46  self.tasks.append(asyncio.create_task(hub.run()))
47 
48  LOGGER.debug("Hub setup complete")
49  return True
50 
51  async def async_reset(self) -> bool:
52  """Reset this hub to default state."""
53 
54  for cleanup_callback in self.cleanup_callbacks:
55  cleanup_callback()
56 
57  # If not setup
58  if self.apiapi is None:
59  return False
60 
61  self.apiapi.callback_unsubscribe(self.async_notify_updateasync_notify_update)
62  await self.apiapi.stop()
63  del self.apiapi
64  self.apiapi = None
65 
66  # Wait for any running tasks to complete
67  await asyncio.wait(self.tasks)
68 
69  return True
70 
71  async def async_notify_update(self, update_type: aiopulse.UpdateType) -> None:
72  """Evaluate entities when hub reports that update has occurred."""
73  LOGGER.debug("Hub {update_type.name} updated")
74 
75  if update_type == aiopulse.UpdateType.rollers:
76  await update_devices(self.hasshass, self.config_entryconfig_entry, self.apiapi.rollers)
77  self.hasshass.config_entries.async_update_entry(
78  self.config_entryconfig_entry, title=self.titletitle
79  )
80 
82  self.hasshass, ACMEDA_HUB_UPDATE.format(self.config_entryconfig_entry.entry_id)
83  )
84 
85  for unique_id in list(self.current_rollers):
86  if unique_id not in self.apiapi.rollers:
87  LOGGER.debug("Notifying remove of %s", unique_id)
88  self.current_rollers.pop(unique_id)
90  self.hasshass, ACMEDA_ENTITY_REMOVE.format(unique_id)
91  )
None __init__(self, HomeAssistant hass, ConfigEntry config_entry)
Definition: hub.py:23
None async_notify_update(self, aiopulse.UpdateType update_type)
Definition: hub.py:71
bool async_setup(self, int tries=0)
Definition: hub.py:41
None update_devices(HomeAssistant hass, ConfigEntry config_entry, dict[int, Roller] api)
Definition: helpers.py:47
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)
Definition: dispatcher.py:193