Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """Coordinator for the SamsungTV integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable, Coroutine
6 from datetime import timedelta
7 from typing import Any
8 
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
12 
13 from .bridge import SamsungTVBridge
14 from .const import DOMAIN, LOGGER
15 
16 SCAN_INTERVAL = 10
17 
18 
20  """Coordinator for the SamsungTV integration."""
21 
22  config_entry: ConfigEntry
23 
24  def __init__(self, hass: HomeAssistant, bridge: SamsungTVBridge) -> None:
25  """Initialize the coordinator."""
26  super().__init__(
27  hass,
28  LOGGER,
29  name=DOMAIN,
30  update_interval=timedelta(seconds=SCAN_INTERVAL),
31  )
32 
33  self.bridgebridge = bridge
34  self.is_onis_on: bool | None = False
35  self.async_extra_update: Callable[[], Coroutine[Any, Any, None]] | None = None
36 
37  async def _async_update_data(self) -> None:
38  """Fetch data from SamsungTV bridge."""
39  if self.bridgebridge.auth_failed or self.hasshass.is_stopping:
40  return
41  old_state = self.is_onis_on
42  if self.bridgebridge.power_off_in_progress:
43  self.is_onis_on = False
44  else:
45  self.is_onis_on = await self.bridgebridge.async_is_on()
46  if self.is_onis_on != old_state:
47  LOGGER.debug("TV %s state updated to %s", self.bridgebridge.host, self.is_onis_on)
48 
49  if self.async_extra_update:
50  await self.async_extra_update()
None __init__(self, HomeAssistant hass, SamsungTVBridge bridge)
Definition: coordinator.py:24