Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Representation of an RGB light."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from zwave_me_ws import ZWaveMeData
8 
10  ATTR_BRIGHTNESS,
11  ATTR_RGB_COLOR,
12  ColorMode,
13  LightEntity,
14 )
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.core import HomeAssistant, callback
17 from homeassistant.helpers.dispatcher import async_dispatcher_connect
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 
20 from . import ZWaveMeController
21 from .const import DOMAIN, ZWaveMePlatform
22 from .entity import ZWaveMeEntity
23 
24 
26  hass: HomeAssistant,
27  config_entry: ConfigEntry,
28  async_add_entities: AddEntitiesCallback,
29 ) -> None:
30  """Set up the rgb platform."""
31 
32  @callback
33  def add_new_device(new_device: ZWaveMeData) -> None:
34  """Add a new device."""
35  controller = hass.data[DOMAIN][config_entry.entry_id]
36  rgb = ZWaveMeRGB(controller, new_device)
37 
39  [
40  rgb,
41  ]
42  )
43 
45  hass, f"ZWAVE_ME_NEW_{ZWaveMePlatform.RGB_LIGHT.upper()}", add_new_device
46  )
48  hass, f"ZWAVE_ME_NEW_{ZWaveMePlatform.RGBW_LIGHT.upper()}", add_new_device
49  )
51  hass, f"ZWAVE_ME_NEW_{ZWaveMePlatform.BRIGHTNESS_LIGHT.upper()}", add_new_device
52  )
53 
54 
56  """Representation of a ZWaveMe light."""
57 
58  def __init__(
59  self,
60  controller: ZWaveMeController,
61  device: ZWaveMeData,
62  ) -> None:
63  """Initialize the device."""
64  super().__init__(controller=controller, device=device)
65  if device.deviceType in [ZWaveMePlatform.RGB_LIGHT, ZWaveMePlatform.RGBW_LIGHT]:
66  self._attr_color_mode_attr_color_mode = ColorMode.RGB
67  else:
68  self._attr_color_mode_attr_color_mode = ColorMode.BRIGHTNESS
69  self._attr_supported_color_modes: set[ColorMode] = {self._attr_color_mode_attr_color_mode}
70 
71  def turn_off(self, **kwargs: Any) -> None:
72  """Turn the device on."""
73  self.controllercontroller.zwave_api.send_command(self.devicedevice.id, "off")
74 
75  def turn_on(self, **kwargs: Any) -> None:
76  """Turn the device on."""
77  color = kwargs.get(ATTR_RGB_COLOR)
78 
79  if color is None:
80  brightness = kwargs.get(ATTR_BRIGHTNESS)
81  if brightness is None:
82  self.controllercontroller.zwave_api.send_command(self.devicedevice.id, "on")
83  else:
84  self.controllercontroller.zwave_api.send_command(
85  self.devicedevice.id, f"exact?level={round(brightness / 2.55)}"
86  )
87  return
88  red, green, blue = color if any(color) else (255, 255, 255)
89  cmd = f"exact?red={red}&green={green}&blue={blue}"
90  self.controllercontroller.zwave_api.send_command(self.devicedevice.id, cmd)
91 
92  @property
93  def is_on(self) -> bool:
94  """Return true if the light is on."""
95  return self.devicedevice.level == "on"
96 
97  @property
98  def brightness(self) -> int:
99  """Return the brightness of a device."""
100  return max(self.devicedevice.color.values())
101 
102  @property
103  def rgb_color(self) -> tuple[int, int, int]:
104  """Return the rgb color value [int, int, int]."""
105  rgb = self.devicedevice.color
106  return rgb["r"], rgb["g"], rgb["b"]
None __init__(self, ZWaveMeController controller, ZWaveMeData device)
Definition: light.py:62
tuple[int, int, int] rgb_color(self)
Definition: light.py:103
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: light.py:29
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103