Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """Data update coordinator for the jvc_projector integration."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 import logging
7 from typing import Any
8 
9 from jvcprojector import (
10  JvcProjector,
11  JvcProjectorAuthError,
12  JvcProjectorConnectError,
13  const,
14 )
15 
16 from homeassistant.core import HomeAssistant
17 from homeassistant.exceptions import ConfigEntryAuthFailed
18 from homeassistant.helpers.device_registry import format_mac
19 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
20 
21 from .const import NAME
22 
23 _LOGGER = logging.getLogger(__name__)
24 
25 INTERVAL_SLOW = timedelta(seconds=10)
26 INTERVAL_FAST = timedelta(seconds=5)
27 
28 
30  """Data update coordinator for the JVC Projector integration."""
31 
32  def __init__(self, hass: HomeAssistant, device: JvcProjector) -> None:
33  """Initialize the coordinator."""
34  super().__init__(
35  hass=hass,
36  logger=_LOGGER,
37  name=NAME,
38  update_interval=INTERVAL_SLOW,
39  )
40 
41  self.devicedevice = device
42  self.unique_idunique_id = format_mac(device.mac)
43 
44  async def _async_update_data(self) -> dict[str, Any]:
45  """Get the latest state data."""
46  try:
47  state = await self.devicedevice.get_state()
48  except JvcProjectorConnectError as err:
49  raise UpdateFailed(f"Unable to connect to {self.device.host}") from err
50  except JvcProjectorAuthError as err:
51  raise ConfigEntryAuthFailed("Password authentication failed") from err
52 
53  old_interval = self.update_intervalupdate_intervalupdate_intervalupdate_intervalupdate_interval
54 
55  if state[const.POWER] != const.STANDBY:
57  else:
58  self.update_intervalupdate_intervalupdate_intervalupdate_intervalupdate_interval = INTERVAL_SLOW
59 
60  if self.update_intervalupdate_intervalupdate_intervalupdate_intervalupdate_interval != old_interval:
61  _LOGGER.debug("Changed update interval to %s", self.update_intervalupdate_intervalupdate_intervalupdate_intervalupdate_interval)
62 
63  return state
str|float get_state(dict[str, float] data, str key)
Definition: sensor.py:26