Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Light platform for Evil Genius Light."""
2 
3 from __future__ import annotations
4 
5 import asyncio
6 from typing import Any, cast
7 
8 from homeassistant.components import light
9 from homeassistant.components.light import ColorMode, LightEntity, LightEntityFeature
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 
14 from .const import DOMAIN
15 from .coordinator import EvilGeniusUpdateCoordinator
16 from .entity import EvilGeniusEntity
17 from .util import update_when_done
18 
19 HA_NO_EFFECT = "None"
20 FIB_NO_EFFECT = "Solid Color"
21 
22 
24  hass: HomeAssistant,
25  config_entry: ConfigEntry,
26  async_add_entities: AddEntitiesCallback,
27 ) -> None:
28  """Set up the Evil Genius light platform."""
29  coordinator: EvilGeniusUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
30  async_add_entities([EvilGeniusLight(coordinator)])
31 
32 
34  """Evil Genius Labs light."""
35 
36  _attr_name = None
37  _attr_supported_features = LightEntityFeature.EFFECT
38  _attr_supported_color_modes = {ColorMode.RGB}
39  _attr_color_mode = ColorMode.RGB
40 
41  def __init__(self, coordinator: EvilGeniusUpdateCoordinator) -> None:
42  """Initialize the Evil Genius light."""
43  super().__init__(coordinator)
44  self._attr_unique_id_attr_unique_id = self.coordinator.info["wiFiChipId"]
45  self._attr_effect_list_attr_effect_list = [
46  pattern
47  for pattern in self.coordinator.data["pattern"]["options"]
48  if pattern != FIB_NO_EFFECT
49  ]
50  self._attr_effect_list_attr_effect_list.insert(0, HA_NO_EFFECT)
51 
52  @property
53  def is_on(self) -> bool:
54  """Return if light is on."""
55  return cast(int, self.coordinator.data["power"]["value"]) == 1
56 
57  @property
58  def brightness(self) -> int:
59  """Return brightness."""
60  return cast(int, self.coordinator.data["brightness"]["value"])
61 
62  @property
63  def rgb_color(self) -> tuple[int, int, int]:
64  """Return the rgb color value [int, int, int]."""
65  return cast(
66  "tuple[int, int, int]",
67  tuple(
68  int(val)
69  for val in self.coordinator.data["solidColor"]["value"].split(",")
70  ),
71  )
72 
73  @property
74  def effect(self) -> str:
75  """Return current effect."""
76  value = cast(
77  str,
78  self.coordinator.data["pattern"]["options"][
79  self.coordinator.data["pattern"]["value"]
80  ],
81  )
82  if value == FIB_NO_EFFECT:
83  return HA_NO_EFFECT
84  return value
85 
86  @update_when_done
87  async def async_turn_on(
88  self,
89  **kwargs: Any,
90  ) -> None:
91  """Turn light on."""
92  if (brightness := kwargs.get(light.ATTR_BRIGHTNESS)) is not None:
93  async with asyncio.timeout(5):
94  await self.coordinator.client.set_path_value("brightness", brightness)
95 
96  # Setting a color will change the effect to "Solid Color" so skip setting effect
97  if (rgb_color := kwargs.get(light.ATTR_RGB_COLOR)) is not None:
98  async with asyncio.timeout(5):
99  await self.coordinator.client.set_rgb_color(*rgb_color)
100 
101  elif (effect := kwargs.get(light.ATTR_EFFECT)) is not None:
102  if effect == HA_NO_EFFECT:
103  effect = FIB_NO_EFFECT
104  async with asyncio.timeout(5):
105  await self.coordinator.client.set_path_value(
106  "pattern", self.coordinator.data["pattern"]["options"].index(effect)
107  )
108 
109  async with asyncio.timeout(5):
110  await self.coordinator.client.set_path_value("power", 1)
111 
112  @update_when_done
113  async def async_turn_off(self, **kwargs: Any) -> None:
114  """Turn light off."""
115  async with asyncio.timeout(5):
116  await self.coordinator.client.set_path_value("power", 0)
None __init__(self, EvilGeniusUpdateCoordinator coordinator)
Definition: light.py:41
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: light.py:27