Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for lights under the iGlo brand."""
2 
3 from __future__ import annotations
4 
5 import math
6 from typing import Any
7 
8 from iglo import Lamp
9 from iglo.lamp import MODE_WHITE
10 import voluptuous as vol
11 
13  ATTR_BRIGHTNESS,
14  ATTR_COLOR_TEMP,
15  ATTR_EFFECT,
16  ATTR_HS_COLOR,
17  PLATFORM_SCHEMA as LIGHT_PLATFORM_SCHEMA,
18  ColorMode,
19  LightEntity,
20  LightEntityFeature,
21 )
22 from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT
23 from homeassistant.core import HomeAssistant
25 from homeassistant.helpers.entity_platform import AddEntitiesCallback
26 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
27 import homeassistant.util.color as color_util
28 
29 DEFAULT_NAME = "iGlo Light"
30 DEFAULT_PORT = 8080
31 
32 PLATFORM_SCHEMA = LIGHT_PLATFORM_SCHEMA.extend(
33  {
34  vol.Required(CONF_HOST): cv.string,
35  vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
36  vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
37  }
38 )
39 
40 
42  hass: HomeAssistant,
43  config: ConfigType,
44  add_entities: AddEntitiesCallback,
45  discovery_info: DiscoveryInfoType | None = None,
46 ) -> None:
47  """Set up the iGlo lights."""
48  host = config.get(CONF_HOST)
49  name = config.get(CONF_NAME)
50  port = config.get(CONF_PORT)
51  add_entities([IGloLamp(name, host, port)], True)
52 
53 
55  """Representation of an iGlo light."""
56 
57  _attr_supported_color_modes = {ColorMode.COLOR_TEMP, ColorMode.HS}
58  _attr_supported_features = LightEntityFeature.EFFECT
59 
60  def __init__(self, name, host, port):
61  """Initialize the light."""
62 
63  self._name_name = name
64  self._lamp_lamp = Lamp(0, host, port)
65 
66  @property
67  def name(self):
68  """Return the name of the light."""
69  return self._name_name
70 
71  @property
72  def brightness(self):
73  """Return the brightness of this light between 0..255."""
74  return int((self._lamp_lamp.state()["brightness"] / 200.0) * 255)
75 
76  @property
77  def color_mode(self) -> ColorMode:
78  """Return the color mode of the light."""
79  if self._lamp_lamp.state()["mode"] == MODE_WHITE:
80  return ColorMode.COLOR_TEMP
81  # The iglo library reports MODE_WHITE when an effect is active, this is not
82  # supported by Home Assistant, just report ColorMode.HS
83  return ColorMode.HS
84 
85  @property
86  def color_temp(self):
87  """Return the color temperature."""
88  return color_util.color_temperature_kelvin_to_mired(self._lamp_lamp.state()["white"])
89 
90  @property
91  def min_mireds(self) -> int:
92  """Return the coldest color_temp that this light supports."""
93  return math.ceil(
94  color_util.color_temperature_kelvin_to_mired(self._lamp_lamp.max_kelvin)
95  )
96 
97  @property
98  def max_mireds(self) -> int:
99  """Return the warmest color_temp that this light supports."""
100  return math.ceil(
101  color_util.color_temperature_kelvin_to_mired(self._lamp_lamp.min_kelvin)
102  )
103 
104  @property
105  def hs_color(self):
106  """Return the hs value."""
107  return color_util.color_RGB_to_hs(*self._lamp_lamp.state()["rgb"])
108 
109  @property
110  def effect(self):
111  """Return the current effect."""
112  return self._lamp_lamp.state()["effect"]
113 
114  @property
115  def effect_list(self):
116  """Return the list of supported effects."""
117  return self._lamp_lamp.effect_list()
118 
119  @property
120  def is_on(self):
121  """Return true if light is on."""
122  return self._lamp_lamp.state()["on"]
123 
124  def turn_on(self, **kwargs: Any) -> None:
125  """Turn the light on."""
126  if not self.is_onis_onis_on:
127  self._lamp_lamp.switch(True)
128  if ATTR_BRIGHTNESS in kwargs:
129  brightness = int((kwargs[ATTR_BRIGHTNESS] / 255.0) * 200.0)
130  self._lamp_lamp.brightness(brightness)
131  return
132 
133  if ATTR_HS_COLOR in kwargs:
134  rgb = color_util.color_hs_to_RGB(*kwargs[ATTR_HS_COLOR])
135  self._lamp_lamp.rgb(*rgb)
136  return
137 
138  if ATTR_COLOR_TEMP in kwargs:
139  kelvin = int(
140  color_util.color_temperature_mired_to_kelvin(kwargs[ATTR_COLOR_TEMP])
141  )
142  self._lamp_lamp.white(kelvin)
143  return
144 
145  if ATTR_EFFECT in kwargs:
146  effect = kwargs[ATTR_EFFECT]
147  self._lamp_lamp.effect(effect)
148  return
149 
150  def turn_off(self, **kwargs: Any) -> None:
151  """Turn the light off."""
152  self._lamp_lamp.switch(False)
def __init__(self, name, host, port)
Definition: light.py:60
None turn_on(self, **Any kwargs)
Definition: light.py:124
None turn_off(self, **Any kwargs)
Definition: light.py:150
Literal["on", "off"]|None state(self)
Definition: entity.py:1686
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: light.py:46