Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """DataUpdateCoordinator for the LG ThinQ device."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from thinqconnect import ThinQAPIException
9 from thinqconnect.integration import HABridge
10 
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
13 
14 from .const import DOMAIN
15 
16 _LOGGER = logging.getLogger(__name__)
17 
18 
20  """LG Device's Data Update Coordinator."""
21 
22  def __init__(self, hass: HomeAssistant, ha_bridge: HABridge) -> None:
23  """Initialize data coordinator."""
24  super().__init__(
25  hass,
26  _LOGGER,
27  name=f"{DOMAIN}_{ha_bridge.device.device_id}",
28  )
29 
30  self.datadatadata = {}
31  self.apiapi = ha_bridge
32  self.device_iddevice_id = ha_bridge.device.device_id
33  self.sub_idsub_id = ha_bridge.sub_id
34 
35  alias = ha_bridge.device.alias
36 
37  # The device name is usually set to 'alias'.
38  # But, if the sub_id exists, it will be set to 'alias {sub_id}'.
39  # e.g. alias='MyWashTower', sub_id='dryer' then 'MyWashTower dryer'.
40  self.device_namedevice_name = f"{alias} {self.sub_id}" if self.sub_idsub_id else alias
41 
42  # The unique id is usually set to 'device_id'.
43  # But, if the sub_id exists, it will be set to 'device_id_{sub_id}'.
44  # e.g. device_id='TQSXXXX', sub_id='dryer' then 'TQSXXXX_dryer'.
45  self.unique_idunique_id = (
46  f"{self.device_id}_{self.sub_id}" if self.sub_idsub_id else self.device_iddevice_id
47  )
48 
49  async def _async_update_data(self) -> dict[str, Any]:
50  """Request to the server to update the status from full response data."""
51  try:
52  return await self.apiapi.fetch_data()
53  except ThinQAPIException as e:
54  raise UpdateFailed(e) from e
55 
56  def refresh_status(self) -> None:
57  """Refresh current status."""
58  self.async_set_updated_dataasync_set_updated_data(self.datadatadata)
59 
60  def handle_update_status(self, status: dict[str, Any]) -> None:
61  """Handle the status received from the mqtt connection."""
62  data = self.apiapi.update_status(status)
63  if data is not None:
64  self.async_set_updated_dataasync_set_updated_data(data)
65 
66  def handle_notification_message(self, message: str | None) -> None:
67  """Handle the status received from the mqtt connection."""
68  data = self.apiapi.update_notification(message)
69  if data is not None:
70  self.async_set_updated_dataasync_set_updated_data(data)
71 
72 
74  hass: HomeAssistant, ha_bridge: HABridge
75 ) -> DeviceDataUpdateCoordinator:
76  """Create DeviceDataUpdateCoordinator and device_api per device."""
77  coordinator = DeviceDataUpdateCoordinator(hass, ha_bridge)
78  await coordinator.async_refresh()
79 
80  _LOGGER.debug(
81  "Setup device's coordinator: %s, model:%s",
82  coordinator.device_name,
83  coordinator.api.device.model_name,
84  )
85  return coordinator
None __init__(self, HomeAssistant hass, HABridge ha_bridge)
Definition: coordinator.py:22
DeviceDataUpdateCoordinator async_setup_device_coordinator(HomeAssistant hass, HABridge ha_bridge)
Definition: coordinator.py:75
MetOfficeData fetch_data(datapoint.Manager connection, Site site, str mode)
Definition: helpers.py:32