Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for Zengge lights."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 import voluptuous as vol
9 from zengge import zengge
10 
12  ATTR_BRIGHTNESS,
13  ATTR_HS_COLOR,
14  ATTR_WHITE,
15  PLATFORM_SCHEMA as LIGHT_PLATFORM_SCHEMA,
16  ColorMode,
17  LightEntity,
18 )
19 from homeassistant.const import CONF_DEVICES, CONF_NAME
20 from homeassistant.core import HomeAssistant
22 from homeassistant.helpers.entity_platform import AddEntitiesCallback
23 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
24 import homeassistant.util.color as color_util
25 
26 _LOGGER = logging.getLogger(__name__)
27 
28 DEVICE_SCHEMA = vol.Schema({vol.Optional(CONF_NAME): cv.string})
29 
30 PLATFORM_SCHEMA = LIGHT_PLATFORM_SCHEMA.extend(
31  {vol.Optional(CONF_DEVICES, default={}): {cv.string: DEVICE_SCHEMA}}
32 )
33 
34 
36  hass: HomeAssistant,
37  config: ConfigType,
38  add_entities: AddEntitiesCallback,
39  discovery_info: DiscoveryInfoType | None = None,
40 ) -> None:
41  """Set up the Zengge platform."""
42  lights = []
43  for address, device_config in config[CONF_DEVICES].items():
44  light = ZenggeLight(device_config[CONF_NAME], address)
45  if light.is_valid:
46  lights.append(light)
47 
48  add_entities(lights, True)
49 
50 
52  """Representation of a Zengge light."""
53 
54  _attr_supported_color_modes = {ColorMode.HS, ColorMode.WHITE}
55 
56  def __init__(self, name: str, address: str) -> None:
57  """Initialize the light."""
58 
59  self._attr_name_attr_name = name
60  self._attr_unique_id_attr_unique_id = address
61  self.is_validis_valid = True
62  self._bulb_bulb = zengge(address)
63  self._white_white = 0
64  self._attr_brightness_attr_brightness = 0
65  self._attr_hs_color_attr_hs_color = (0, 0)
66  self._attr_is_on_attr_is_on = False
67  if self._bulb_bulb.connect() is False:
68  self.is_validis_valid = False
69  _LOGGER.error("Failed to connect to bulb %s, %s", address, name)
70  return
71 
72  @property
73  def white_value(self) -> int:
74  """Return the white property."""
75  return self._white_white
76 
77  @property
78  def color_mode(self) -> ColorMode:
79  """Return the current color mode."""
80  if self._white_white != 0:
81  return ColorMode.WHITE
82  return ColorMode.HS
83 
84  def _set_rgb(self, red: int, green: int, blue: int) -> None:
85  """Set the rgb state."""
86  self._bulb_bulb.set_rgb(red, green, blue)
87 
88  def _set_white(self, white):
89  """Set the white state."""
90  return self._bulb_bulb.set_white(white)
91 
92  def turn_on(self, **kwargs: Any) -> None:
93  """Turn the specified light on."""
94  self._attr_is_on_attr_is_on = True
95  self._bulb_bulb.on()
96 
97  hs_color = kwargs.get(ATTR_HS_COLOR)
98  white = kwargs.get(ATTR_WHITE)
99  brightness = kwargs.get(ATTR_BRIGHTNESS)
100 
101  if white is not None:
102  # Change the bulb to white
103  self._attr_brightness_attr_brightness = white
104  self._white_white = white
105  self._attr_hs_color_attr_hs_color = (0, 0)
106 
107  if hs_color is not None:
108  # Change the bulb to hs
109  self._white_white = 0
110  self._attr_hs_color_attr_hs_color = hs_color
111 
112  if brightness is not None:
113  self._attr_brightness_attr_brightness = brightness
114 
115  if self._white_white != 0:
116  self._set_white_set_white(self.brightnessbrightness)
117  else:
118  assert self.hs_colorhs_color is not None
119  assert self.brightnessbrightness is not None
120  rgb = color_util.color_hsv_to_RGB(
121  self.hs_colorhs_color[0], self.hs_colorhs_color[1], self.brightnessbrightness / 255 * 100
122  )
123  self._set_rgb_set_rgb(*rgb)
124 
125  def turn_off(self, **kwargs: Any) -> None:
126  """Turn the specified light off."""
127  self._attr_is_on_attr_is_on = False
128  self._bulb_bulb.off()
129 
130  def update(self) -> None:
131  """Synchronise internal state with the actual light state."""
132  rgb = self._bulb_bulb.get_colour()
133  hsv = color_util.color_RGB_to_hsv(*rgb)
134  self._attr_hs_color_attr_hs_color = hsv[:2]
135  self._attr_brightness_attr_brightness = int((hsv[2] / 100) * 255)
136  self._white_white = self._bulb_bulb.get_white()
137  if self._white_white:
138  self._attr_brightness_attr_brightness = self._white_white
139  self._attr_is_on_attr_is_on = self._bulb_bulb.get_on()
tuple[float, float]|None hs_color(self)
Definition: __init__.py:947
None _set_rgb(self, int red, int green, int blue)
Definition: light.py:84
None __init__(self, str name, str address)
Definition: light.py:56
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:40