Home Assistant Unofficial Reference 2024.12.1
coordinator.py
Go to the documentation of this file.
1 """Coordinator for Govee light local."""
2 
3 import asyncio
4 from collections.abc import Callable
5 import logging
6 
7 from govee_local_api import GoveeController, GoveeDevice
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 .const import (
14  CONF_DISCOVERY_INTERVAL_DEFAULT,
15  CONF_LISTENING_PORT_DEFAULT,
16  CONF_MULTICAST_ADDRESS_DEFAULT,
17  CONF_TARGET_PORT_DEFAULT,
18  SCAN_INTERVAL,
19 )
20 
21 _LOGGER = logging.getLogger(__name__)
22 
23 type GoveeLocalConfigEntry = ConfigEntry[GoveeLocalApiCoordinator]
24 
25 
27  """Govee light local coordinator."""
28 
29  def __init__(self, hass: HomeAssistant) -> None:
30  """Initialize my coordinator."""
31  super().__init__(
32  hass=hass,
33  logger=_LOGGER,
34  name="GoveeLightLocalApi",
35  update_interval=SCAN_INTERVAL,
36  )
37 
38  self._controller_controller = GoveeController(
39  loop=hass.loop,
40  logger=_LOGGER,
41  broadcast_address=CONF_MULTICAST_ADDRESS_DEFAULT,
42  broadcast_port=CONF_TARGET_PORT_DEFAULT,
43  listening_port=CONF_LISTENING_PORT_DEFAULT,
44  discovery_enabled=True,
45  discovery_interval=CONF_DISCOVERY_INTERVAL_DEFAULT,
46  discovered_callback=None,
47  update_enabled=False,
48  )
49 
50  async def start(self) -> None:
51  """Start the Govee coordinator."""
52  await self._controller_controller.start()
53  self._controller_controller.send_update_message()
54 
56  self, callback: Callable[[GoveeDevice, bool], bool]
57  ) -> None:
58  """Set discovery callback for automatic Govee light discovery."""
59  self._controller_controller.set_device_discovered_callback(callback)
60 
61  def cleanup(self) -> asyncio.Event:
62  """Stop and cleanup the cooridinator."""
63  return self._controller_controller.cleanup()
64 
65  async def turn_on(self, device: GoveeDevice) -> None:
66  """Turn on the light."""
67  await device.turn_on()
68 
69  async def turn_off(self, device: GoveeDevice) -> None:
70  """Turn off the light."""
71  await device.turn_off()
72 
73  async def set_brightness(self, device: GoveeDevice, brightness: int) -> None:
74  """Set light brightness."""
75  await device.set_brightness(brightness)
76 
77  async def set_rgb_color(
78  self, device: GoveeDevice, red: int, green: int, blue: int
79  ) -> None:
80  """Set light RGB color."""
81  await device.set_rgb_color(red, green, blue)
82 
83  async def set_temperature(self, device: GoveeDevice, temperature: int) -> None:
84  """Set light color in kelvin."""
85  await device.set_temperature(temperature)
86 
87  @property
88  def devices(self) -> list[GoveeDevice]:
89  """Return a list of discovered Govee devices."""
90  return self._controller_controller.devices
91 
92  async def _async_update_data(self) -> list[GoveeDevice]:
93  self._controller_controller.send_update_message()
94  return self._controller_controller.devices
None set_discovery_callback(self, Callable[[GoveeDevice, bool], bool] callback)
Definition: coordinator.py:57
None set_temperature(self, GoveeDevice device, int temperature)
Definition: coordinator.py:83
None set_rgb_color(self, GoveeDevice device, int red, int green, int blue)
Definition: coordinator.py:79