Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for Yeelight Sunflower color bulbs (not Yeelight Blue or WiFi)."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 import voluptuous as vol
9 import yeelightsunflower
10 
12  ATTR_BRIGHTNESS,
13  ATTR_HS_COLOR,
14  PLATFORM_SCHEMA as LIGHT_PLATFORM_SCHEMA,
15  ColorMode,
16  LightEntity,
17 )
18 from homeassistant.const import CONF_HOST
19 from homeassistant.core import HomeAssistant
21 from homeassistant.helpers.entity_platform import AddEntitiesCallback
22 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
23 import homeassistant.util.color as color_util
24 
25 _LOGGER = logging.getLogger(__name__)
26 
27 PLATFORM_SCHEMA = LIGHT_PLATFORM_SCHEMA.extend({vol.Required(CONF_HOST): cv.string})
28 
29 
31  hass: HomeAssistant,
32  config: ConfigType,
33  add_entities: AddEntitiesCallback,
34  discovery_info: DiscoveryInfoType | None = None,
35 ) -> None:
36  """Set up the Yeelight Sunflower Light platform."""
37  host = config.get(CONF_HOST)
38  hub = yeelightsunflower.Hub(host)
39 
40  if not hub.available:
41  _LOGGER.error("Could not connect to Yeelight Sunflower hub")
42  return
43 
44  add_entities(SunflowerBulb(light) for light in hub.get_lights())
45 
46 
48  """Representation of a Yeelight Sunflower Light."""
49 
50  _attr_color_mode = ColorMode.HS
51  _attr_supported_color_modes = {ColorMode.HS}
52 
53  def __init__(self, light: yeelightsunflower.Bulb) -> None:
54  """Initialize a Yeelight Sunflower bulb."""
55  self._light_light = light
56  self._attr_available_attr_available = light.available
57  self._brightness_brightness = light.brightness
58  self._attr_is_on_attr_is_on = light.is_on
59  self._rgb_color_rgb_color = light.rgb_color
60  self._attr_unique_id_attr_unique_id = light.zid
61  self._attr_name_attr_name = f"sunflower_{self._light.zid}"
62 
63  @property
64  def brightness(self) -> int:
65  """Return the brightness is 0-255; Yeelight's brightness is 0-100."""
66  return int(self._brightness_brightness / 100 * 255)
67 
68  @property
69  def hs_color(self) -> tuple[float, float]:
70  """Return the color property."""
71  return color_util.color_RGB_to_hs(*self._rgb_color_rgb_color)
72 
73  def turn_on(self, **kwargs: Any) -> None:
74  """Instruct the light to turn on, optionally set colour/brightness."""
75  # when no arguments, just turn light on (full brightness)
76  if not kwargs:
77  self._light_light.turn_on()
78  elif ATTR_HS_COLOR in kwargs and ATTR_BRIGHTNESS in kwargs:
79  rgb = color_util.color_hs_to_RGB(*kwargs[ATTR_HS_COLOR])
80  bright = int(kwargs[ATTR_BRIGHTNESS] / 255 * 100)
81  self._light_light.set_all(rgb[0], rgb[1], rgb[2], bright)
82  elif ATTR_HS_COLOR in kwargs:
83  rgb = color_util.color_hs_to_RGB(*kwargs[ATTR_HS_COLOR])
84  self._light_light.set_rgb_color(rgb[0], rgb[1], rgb[2])
85  elif ATTR_BRIGHTNESS in kwargs:
86  bright = int(kwargs[ATTR_BRIGHTNESS] / 255 * 100)
87  self._light_light.set_brightness(bright)
88 
89  def turn_off(self, **kwargs: Any) -> None:
90  """Instruct the light to turn off."""
91  self._light_light.turn_off()
92 
93  def update(self) -> None:
94  """Fetch new state data for this light and update local values."""
95  self._light_light.update()
96  self._attr_available_attr_available = self._light_light.available
97  self._brightness_brightness = self._light_light.brightness
98  self._attr_is_on_attr_is_on = self._light_light.is_on
99  self._rgb_color_rgb_color = self._light_light.rgb_color
None __init__(self, yeelightsunflower.Bulb light)
Definition: light.py:53
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:35