Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for Overkiz lights."""
2 
3 from __future__ import annotations
4 
5 from typing import Any, cast
6 
7 from pyoverkiz.enums import OverkizCommand, OverkizCommandParam, OverkizState
8 
10  ATTR_BRIGHTNESS,
11  ATTR_RGB_COLOR,
12  ColorMode,
13  LightEntity,
14 )
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.const import Platform
17 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 
20 from . import HomeAssistantOverkizData
21 from .const import DOMAIN
22 from .coordinator import OverkizDataUpdateCoordinator
23 from .entity import OverkizEntity
24 
25 
27  hass: HomeAssistant,
28  entry: ConfigEntry,
29  async_add_entities: AddEntitiesCallback,
30 ) -> None:
31  """Set up the Overkiz lights from a config entry."""
32  data: HomeAssistantOverkizData = hass.data[DOMAIN][entry.entry_id]
33 
35  OverkizLight(device.device_url, data.coordinator)
36  for device in data.platforms[Platform.LIGHT]
37  )
38 
39 
41  """Representation of an Overkiz Light."""
42 
43  def __init__(
44  self, device_url: str, coordinator: OverkizDataUpdateCoordinator
45  ) -> None:
46  """Initialize a device."""
47  super().__init__(device_url, coordinator)
48 
49  self._attr_supported_color_modes_attr_supported_color_modes: set[ColorMode] = set()
50 
51  if self.executorexecutor.has_command(OverkizCommand.SET_RGB):
52  self._attr_color_mode_attr_color_mode = ColorMode.RGB
53  elif self.executorexecutor.has_command(OverkizCommand.SET_INTENSITY):
54  self._attr_color_mode_attr_color_mode = ColorMode.BRIGHTNESS
55  else:
56  self._attr_color_mode_attr_color_mode = ColorMode.ONOFF
57  self._attr_supported_color_modes_attr_supported_color_modes = {self._attr_color_mode_attr_color_mode}
58 
59  @property
60  def is_on(self) -> bool:
61  """Return true if light is on."""
62  return (
63  self.executorexecutor.select_state(OverkizState.CORE_ON_OFF)
64  == OverkizCommandParam.ON
65  )
66 
67  @property
68  def rgb_color(self) -> tuple[int, int, int] | None:
69  """Return the rgb color value [int, int, int] (0-255)."""
70  red = self.executorexecutor.select_state(OverkizState.CORE_RED_COLOR_INTENSITY)
71  green = self.executorexecutor.select_state(OverkizState.CORE_GREEN_COLOR_INTENSITY)
72  blue = self.executorexecutor.select_state(OverkizState.CORE_BLUE_COLOR_INTENSITY)
73 
74  if red is None or green is None or blue is None:
75  return None
76 
77  return (cast(int, red), cast(int, green), cast(int, blue))
78 
79  @property
80  def brightness(self) -> int | None:
81  """Return the brightness of this light (0-255)."""
82  value = self.executorexecutor.select_state(OverkizState.CORE_LIGHT_INTENSITY)
83  if value is not None:
84  return round(cast(int, value) * 255 / 100)
85 
86  return None
87 
88  async def async_turn_on(self, **kwargs: Any) -> None:
89  """Turn the light on."""
90  rgb_color = kwargs.get(ATTR_RGB_COLOR)
91  brightness = kwargs.get(ATTR_BRIGHTNESS)
92 
93  if rgb_color is not None:
94  await self.executorexecutor.async_execute_command(
95  OverkizCommand.SET_RGB,
96  *[round(float(c)) for c in kwargs[ATTR_RGB_COLOR]],
97  )
98  return
99 
100  if brightness is not None:
101  await self.executorexecutor.async_execute_command(
102  OverkizCommand.SET_INTENSITY, round(float(brightness) / 255 * 100)
103  )
104  return
105 
106  await self.executorexecutor.async_execute_command(OverkizCommand.ON)
107 
108  async def async_turn_off(self, **kwargs: Any) -> None:
109  """Turn the light off."""
110  await self.executorexecutor.async_execute_command(OverkizCommand.OFF)
tuple[int, int, int]|None rgb_color(self)
Definition: light.py:68
None __init__(self, str device_url, OverkizDataUpdateCoordinator coordinator)
Definition: light.py:45
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: light.py:30