Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """Coordinator for the IKEA Idasen Desk integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from idasen_ha import Desk
8 
9 from homeassistant.components import bluetooth
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
12 
13 _LOGGER = logging.getLogger(__name__)
14 
15 
17  """Class to manage updates for the Idasen Desk."""
18 
19  def __init__(
20  self,
21  hass: HomeAssistant,
22  logger: logging.Logger,
23  name: str,
24  address: str,
25  ) -> None:
26  """Init IdasenDeskCoordinator."""
27 
28  super().__init__(hass, logger, name=name)
29  self._address_address = address
30  self._expected_connected_expected_connected = False
31 
32  self.deskdesk = Desk(self.async_set_updated_dataasync_set_updated_data)
33 
34  async def async_connect(self) -> bool:
35  """Connect to desk."""
36  _LOGGER.debug("Trying to connect %s", self._address_address)
37  self._expected_connected_expected_connected = True
38  ble_device = bluetooth.async_ble_device_from_address(
39  self.hasshass, self._address_address, connectable=True
40  )
41  if ble_device is None:
42  _LOGGER.debug("No BLEDevice for %s", self._address_address)
43  return False
44  await self.deskdesk.connect(ble_device)
45  return True
46 
47  async def async_disconnect(self) -> None:
48  """Disconnect from desk."""
49  self._expected_connected_expected_connected = False
50  _LOGGER.debug("Disconnecting from %s", self._address_address)
51  await self.deskdesk.disconnect()
52 
53  async def async_connect_if_expected(self) -> None:
54  """Ensure that the desk is connected if that is the expected state."""
55  if self._expected_connected_expected_connected:
56  await self.async_connectasync_connect()
None __init__(self, HomeAssistant hass, logging.Logger logger, str name, str address)
Definition: coordinator.py:25