Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for myStrom Wifi bulbs."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from pymystrom.exceptions import MyStromConnectionError
9 
11  ATTR_BRIGHTNESS,
12  ATTR_EFFECT,
13  ATTR_HS_COLOR,
14  ColorMode,
15  LightEntity,
16  LightEntityFeature,
17 )
18 from homeassistant.config_entries import ConfigEntry
19 from homeassistant.core import HomeAssistant
20 from homeassistant.helpers.device_registry import DeviceInfo
21 from homeassistant.helpers.entity_platform import AddEntitiesCallback
22 
23 from .const import DOMAIN, MANUFACTURER
24 
25 _LOGGER = logging.getLogger(__name__)
26 
27 DEFAULT_NAME = "myStrom bulb"
28 
29 EFFECT_RAINBOW = "rainbow"
30 EFFECT_SUNRISE = "sunrise"
31 
32 
34  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
35 ) -> None:
36  """Set up the myStrom entities."""
37  info = hass.data[DOMAIN][entry.entry_id].info
38  device = hass.data[DOMAIN][entry.entry_id].device
39  async_add_entities([MyStromLight(device, entry.title, info["mac"])])
40 
41 
43  """Representation of the myStrom WiFi bulb."""
44 
45  _attr_has_entity_name = True
46  _attr_name = None
47  _attr_color_mode = ColorMode.HS
48  _attr_supported_color_modes = {ColorMode.HS}
49  _attr_supported_features = LightEntityFeature.EFFECT | LightEntityFeature.FLASH
50  _attr_effect_list = [EFFECT_RAINBOW, EFFECT_SUNRISE]
51 
52  def __init__(self, bulb, name, mac):
53  """Initialize the light."""
54  self._bulb_bulb = bulb
55  self._attr_available_attr_available = False
56  self._attr_unique_id_attr_unique_id = mac
57  self._attr_hs_color_attr_hs_color = 0, 0
58  self._attr_device_info_attr_device_info = DeviceInfo(
59  identifiers={(DOMAIN, mac)},
60  name=name,
61  manufacturer=MANUFACTURER,
62  sw_version=self._bulb_bulb.firmware,
63  )
64 
65  async def async_turn_on(self, **kwargs: Any) -> None:
66  """Turn on the light."""
67  brightness = kwargs.get(ATTR_BRIGHTNESS, 255)
68  effect = kwargs.get(ATTR_EFFECT)
69 
70  if ATTR_HS_COLOR in kwargs:
71  color_h, color_s = kwargs[ATTR_HS_COLOR]
72  elif ATTR_BRIGHTNESS in kwargs:
73  # Brightness update, keep color
74  if self.hs_colorhs_color is not None:
75  color_h, color_s = self.hs_colorhs_color
76  else:
77  color_h, color_s = 0, 0 # Back to white
78  else:
79  color_h, color_s = 0, 0 # Back to white
80 
81  try:
82  if not self.is_onis_on:
83  await self._bulb_bulb.set_on()
84  if brightness is not None:
85  await self._bulb_bulb.set_color_hsv(
86  int(color_h), int(color_s), round(brightness * 100 / 255)
87  )
88  if effect == EFFECT_SUNRISE:
89  await self._bulb_bulb.set_sunrise(30)
90  if effect == EFFECT_RAINBOW:
91  await self._bulb_bulb.set_rainbow(30)
92  except MyStromConnectionError:
93  _LOGGER.warning("No route to myStrom bulb")
94 
95  async def async_turn_off(self, **kwargs: Any) -> None:
96  """Turn off the bulb."""
97  try:
98  await self._bulb_bulb.set_off()
99  except MyStromConnectionError:
100  _LOGGER.warning("The myStrom bulb not online")
101 
102  async def async_update(self) -> None:
103  """Fetch new state data for this light."""
104  try:
105  await self._bulb_bulb.get_state()
106  self._attr_is_on_attr_is_on = self._bulb_bulb.state
107 
108  colors = self._bulb_bulb.color
109  try:
110  color_h, color_s, color_v = colors.split(";")
111  except ValueError:
112  color_s, color_v = colors.split(";")
113  color_h = 0
114 
115  self._attr_hs_color_attr_hs_color = int(color_h), int(color_s)
116  self._attr_brightness_attr_brightness = int(int(color_v) * 255 / 100)
117 
118  self._attr_available_attr_available = True
119  except MyStromConnectionError:
120  _LOGGER.warning("No route to myStrom bulb")
121  self._attr_available_attr_available = False
tuple[float, float]|None hs_color(self)
Definition: __init__.py:947
str|float get_state(dict[str, float] data, str key)
Definition: sensor.py:26
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: light.py:35