Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for EufyHome lights."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 import lakeside
8 
10  ATTR_BRIGHTNESS,
11  ATTR_COLOR_TEMP,
12  ATTR_HS_COLOR,
13  ColorMode,
14  LightEntity,
15 )
16 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
19 import homeassistant.util.color as color_util
20 from homeassistant.util.color import (
21  color_temperature_kelvin_to_mired as kelvin_to_mired,
22  color_temperature_mired_to_kelvin as mired_to_kelvin,
23 )
24 
25 EUFYHOME_MAX_KELVIN = 6500
26 EUFYHOME_MIN_KELVIN = 2700
27 
28 
30  hass: HomeAssistant,
31  config: ConfigType,
32  add_entities: AddEntitiesCallback,
33  discovery_info: DiscoveryInfoType | None = None,
34 ) -> None:
35  """Set up EufyHome bulbs."""
36  if discovery_info is None:
37  return
38  add_entities([EufyHomeLight(discovery_info)], True)
39 
40 
42  """Representation of a EufyHome light."""
43 
44  def __init__(self, device):
45  """Initialize the light."""
46 
47  self._temp_temp = None
48  self._brightness_brightness = None
49  self._hs_hs = None
50  self._state_state = None
51  self._name_name = device["name"]
52  self._address_address = device["address"]
53  self._code_code = device["code"]
54  self._type_type = device["type"]
55  self._bulb_bulb = lakeside.bulb(self._address_address, self._code_code, self._type_type)
56  self._colormode_colormode = False
57  if self._type_type == "T1011":
58  self._attr_supported_color_modes_attr_supported_color_modes = {ColorMode.BRIGHTNESS}
59  elif self._type_type == "T1012":
60  self._attr_supported_color_modes_attr_supported_color_modes = {ColorMode.COLOR_TEMP}
61  else: # T1013
62  self._attr_supported_color_modes_attr_supported_color_modes = {ColorMode.COLOR_TEMP, ColorMode.HS}
63  self._bulb_bulb.connect()
64 
65  def update(self) -> None:
66  """Synchronise state from the bulb."""
67  self._bulb_bulb.update()
68  if self._bulb_bulb.power:
69  self._brightness_brightness = self._bulb_bulb.brightness
70  self._temp_temp = self._bulb_bulb.temperature
71  if self._bulb_bulb.colors:
72  self._colormode_colormode = True
73  self._hs_hs = color_util.color_RGB_to_hs(*self._bulb_bulb.colors)
74  else:
75  self._colormode_colormode = False
76  self._state_state = self._bulb_bulb.power
77 
78  @property
79  def unique_id(self):
80  """Return the ID of this light."""
81  return self._address_address
82 
83  @property
84  def name(self):
85  """Return the name of the device if any."""
86  return self._name_name
87 
88  @property
89  def is_on(self):
90  """Return true if device is on."""
91  return self._state_state
92 
93  @property
94  def brightness(self):
95  """Return the brightness of this light between 0..255."""
96  return int(self._brightness_brightness * 255 / 100)
97 
98  @property
99  def min_mireds(self) -> int:
100  """Return minimum supported color temperature."""
101  return kelvin_to_mired(EUFYHOME_MAX_KELVIN)
102 
103  @property
104  def max_mireds(self) -> int:
105  """Return maximum supported color temperature."""
106  return kelvin_to_mired(EUFYHOME_MIN_KELVIN)
107 
108  @property
109  def color_temp(self):
110  """Return the color temperature of this light."""
111  temp_in_k = int(
112  EUFYHOME_MIN_KELVIN
113  + (self._temp_temp * (EUFYHOME_MAX_KELVIN - EUFYHOME_MIN_KELVIN) / 100)
114  )
115  return kelvin_to_mired(temp_in_k)
116 
117  @property
118  def hs_color(self):
119  """Return the color of this light."""
120  return self._hs_hs
121 
122  @property
123  def color_mode(self) -> ColorMode:
124  """Return the color mode of the light."""
125  if self._type_type == "T1011":
126  return ColorMode.BRIGHTNESS
127  if self._type_type == "T1012":
128  return ColorMode.COLOR_TEMP
129  # T1013
130  if not self._colormode_colormode:
131  return ColorMode.COLOR_TEMP
132  return ColorMode.HS
133 
134  def turn_on(self, **kwargs: Any) -> None:
135  """Turn the specified light on."""
136  brightness = kwargs.get(ATTR_BRIGHTNESS)
137  colortemp = kwargs.get(ATTR_COLOR_TEMP)
138  hs = kwargs.get(ATTR_HS_COLOR)
139 
140  if brightness is not None:
141  brightness = int(brightness * 100 / 255)
142  else:
143  if self._brightness_brightness is None:
144  self._brightness_brightness = 100
145  brightness = self._brightness_brightness
146 
147  if colortemp is not None:
148  self._colormode_colormode = False
149  temp_in_k = mired_to_kelvin(colortemp)
150  relative_temp = temp_in_k - EUFYHOME_MIN_KELVIN
151  temp = int(
152  relative_temp * 100 / (EUFYHOME_MAX_KELVIN - EUFYHOME_MIN_KELVIN)
153  )
154  else:
155  temp = None
156 
157  if hs is not None:
158  rgb = color_util.color_hsv_to_RGB(hs[0], hs[1], brightness / 255 * 100)
159  self._colormode_colormode = True
160  elif self._colormode_colormode:
161  rgb = color_util.color_hsv_to_RGB(
162  self._hs_hs[0], self._hs_hs[1], brightness / 255 * 100
163  )
164  else:
165  rgb = None
166 
167  try:
168  self._bulb_bulb.set_state(
169  power=True, brightness=brightness, temperature=temp, colors=rgb
170  )
171  except BrokenPipeError:
172  self._bulb_bulb.connect()
173  self._bulb_bulb.set_state(
174  power=True, brightness=brightness, temperature=temp, colors=rgb
175  )
176 
177  def turn_off(self, **kwargs: Any) -> None:
178  """Turn the specified light off."""
179  try:
180  self._bulb_bulb.set_state(power=False)
181  except BrokenPipeError:
182  self._bulb_bulb.connect()
183  self._bulb_bulb.set_state(power=False)
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:34