Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """Coordinator for SleepIQ."""
2 
3 import asyncio
4 from dataclasses import dataclass
5 from datetime import timedelta
6 import logging
7 
8 from asyncsleepiq import AsyncSleepIQ
9 
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
12 
13 _LOGGER = logging.getLogger(__name__)
14 
15 UPDATE_INTERVAL = timedelta(seconds=60)
16 LONGER_UPDATE_INTERVAL = timedelta(minutes=5)
17 
18 
20  """SleepIQ data update coordinator."""
21 
22  def __init__(
23  self,
24  hass: HomeAssistant,
25  client: AsyncSleepIQ,
26  username: str,
27  ) -> None:
28  """Initialize coordinator."""
29  super().__init__(
30  hass,
31  _LOGGER,
32  name=f"{username}@SleepIQ",
33  update_interval=UPDATE_INTERVAL,
34  )
35  self.clientclient = client
36 
37  async def _async_update_data(self) -> None:
38  tasks = [self.clientclient.fetch_bed_statuses()] + [
39  bed.foundation.update_foundation_status()
40  for bed in self.clientclient.beds.values()
41  ]
42  await asyncio.gather(*tasks)
43 
44 
46  """SleepIQ data update coordinator."""
47 
48  def __init__(
49  self,
50  hass: HomeAssistant,
51  client: AsyncSleepIQ,
52  username: str,
53  ) -> None:
54  """Initialize coordinator."""
55  super().__init__(
56  hass,
57  _LOGGER,
58  name=f"{username}@SleepIQPause",
59  update_interval=LONGER_UPDATE_INTERVAL,
60  )
61  self.clientclient = client
62 
63  async def _async_update_data(self) -> None:
64  await asyncio.gather(
65  *[bed.fetch_pause_mode() for bed in self.clientclient.beds.values()]
66  )
67 
68 
69 @dataclass
71  """Data for the sleepiq integration."""
72 
73  data_coordinator: SleepIQDataUpdateCoordinator
74  pause_coordinator: SleepIQPauseUpdateCoordinator
75  client: AsyncSleepIQ
None __init__(self, HomeAssistant hass, AsyncSleepIQ client, str username)
Definition: coordinator.py:27
None __init__(self, HomeAssistant hass, AsyncSleepIQ client, str username)
Definition: coordinator.py:53