Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for Plum Lightpad lights."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from plumlightpad import Plum
8 
10  ATTR_BRIGHTNESS,
11  ATTR_HS_COLOR,
12  ColorMode,
13  LightEntity,
14 )
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers.aiohttp_client import async_get_clientsession
18 from homeassistant.helpers.device_registry import DeviceInfo
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 import homeassistant.util.color as color_util
21 
22 from .const import DOMAIN
23 
24 
26  hass: HomeAssistant,
27  entry: ConfigEntry,
28  async_add_entities: AddEntitiesCallback,
29 ) -> None:
30  """Set up Plum Lightpad dimmer lights and glow rings."""
31 
32  plum: Plum = hass.data[DOMAIN][entry.entry_id]
33 
34  def setup_entities(device) -> None:
35  entities: list[LightEntity] = []
36 
37  if "lpid" in device:
38  lightpad = plum.get_lightpad(device["lpid"])
39  entities.append(GlowRing(lightpad=lightpad))
40 
41  if "llid" in device:
42  logical_load = plum.get_load(device["llid"])
43  entities.append(PlumLight(load=logical_load))
44 
45  async_add_entities(entities)
46 
47  async def new_load(device):
48  setup_entities(device)
49 
50  async def new_lightpad(device):
51  setup_entities(device)
52 
53  device_web_session = async_get_clientsession(hass, verify_ssl=False)
54  entry.async_create_background_task(
55  hass,
56  plum.discover(
57  hass.loop,
58  loadListener=new_load,
59  lightpadListener=new_lightpad,
60  websession=device_web_session,
61  ),
62  "plum.light-discover",
63  )
64 
65 
67  """Representation of a Plum Lightpad dimmer."""
68 
69  _attr_should_poll = False
70  _attr_has_entity_name = True
71  _attr_name = None
72 
73  def __init__(self, load):
74  """Initialize the light."""
75  self._load_load = load
76  self._brightness_brightness = load.level
77  unique_id = f"{load.llid}.light"
78  self._attr_unique_id_attr_unique_id = unique_id
79  self._attr_device_info_attr_device_info = DeviceInfo(
80  identifiers={(DOMAIN, unique_id)},
81  manufacturer="Plum",
82  model="Dimmer",
83  name=load.name,
84  )
85 
86  async def async_added_to_hass(self) -> None:
87  """Subscribe to dimmerchange events."""
88  self._load_load.add_event_listener("dimmerchange", self.dimmerchangedimmerchange)
89 
90  def dimmerchange(self, event):
91  """Change event handler updating the brightness."""
92  self._brightness_brightness = event["level"]
93  self.schedule_update_ha_stateschedule_update_ha_state()
94 
95  @property
96  def brightness(self) -> int:
97  """Return the brightness of this switch between 0..255."""
98  return self._brightness_brightness
99 
100  @property
101  def is_on(self) -> bool:
102  """Return true if light is on."""
103  return self._brightness_brightness > 0
104 
105  @property
106  def color_mode(self) -> ColorMode:
107  """Flag supported features."""
108  if self._load_load.dimmable:
109  return ColorMode.BRIGHTNESS
110  return ColorMode.ONOFF
111 
112  @property
113  def supported_color_modes(self) -> set[ColorMode]:
114  """Flag supported color modes."""
115  return {self.color_modecolor_modecolor_mode}
116 
117  async def async_turn_on(self, **kwargs: Any) -> None:
118  """Turn the light on."""
119  if ATTR_BRIGHTNESS in kwargs:
120  await self._load_load.turn_on(kwargs[ATTR_BRIGHTNESS])
121  else:
122  await self._load_load.turn_on()
123 
124  async def async_turn_off(self, **kwargs: Any) -> None:
125  """Turn the light off."""
126  await self._load_load.turn_off()
127 
128 
130  """Representation of a Plum Lightpad dimmer glow ring."""
131 
132  _attr_color_mode = ColorMode.HS
133  _attr_should_poll = False
134  _attr_translation_key = "glow_ring"
135  _attr_supported_color_modes = {ColorMode.HS}
136 
137  def __init__(self, lightpad):
138  """Initialize the light."""
139  self._lightpad_lightpad = lightpad
140  self._attr_name_attr_name = f"{lightpad.friendly_name} Glow Ring"
141 
142  self._attr_is_on_attr_is_on = lightpad.glow_enabled
143  self._glow_intensity_glow_intensity = lightpad.glow_intensity
144  unique_id = f"{self._lightpad.lpid}.glow"
145  self._attr_unique_id_attr_unique_id = unique_id
146 
147  self._red_red = lightpad.glow_color["red"]
148  self._green_green = lightpad.glow_color["green"]
149  self._blue_blue = lightpad.glow_color["blue"]
150  self._attr_device_info_attr_device_info = DeviceInfo(
151  identifiers={(DOMAIN, unique_id)},
152  manufacturer="Plum",
153  model="Glow Ring",
154  name=self._attr_name_attr_name,
155  )
156 
157  async def async_added_to_hass(self) -> None:
158  """Subscribe to configchange events."""
159  self._lightpad_lightpad.add_event_listener("configchange", self.configchange_eventconfigchange_event)
160 
161  def configchange_event(self, event):
162  """Handle Configuration change event."""
163  config = event["changes"]
164 
165  self._attr_is_on_attr_is_on = config["glowEnabled"]
166  self._glow_intensity_glow_intensity = config["glowIntensity"]
167 
168  self._red_red = config["glowColor"]["red"]
169  self._green_green = config["glowColor"]["green"]
170  self._blue_blue = config["glowColor"]["blue"]
171  self.schedule_update_ha_stateschedule_update_ha_state()
172 
173  @property
174  def hs_color(self):
175  """Return the hue and saturation color value [float, float]."""
176  return color_util.color_RGB_to_hs(self._red_red, self._green_green, self._blue_blue)
177 
178  @property
179  def brightness(self) -> int:
180  """Return the brightness of this switch between 0..255."""
181  return min(max(int(round(self._glow_intensity_glow_intensity * 255, 0)), 0), 255)
182 
183  async def async_turn_on(self, **kwargs: Any) -> None:
184  """Turn the light on."""
185  if ATTR_BRIGHTNESS in kwargs:
186  brightness_pct = kwargs[ATTR_BRIGHTNESS] / 255.0
187  await self._lightpad_lightpad.set_config({"glowIntensity": brightness_pct})
188  elif ATTR_HS_COLOR in kwargs:
189  hs_color = kwargs[ATTR_HS_COLOR]
190  red, green, blue = color_util.color_hs_to_RGB(*hs_color)
191  await self._lightpad_lightpad.set_glow_color(red, green, blue, 0)
192  else:
193  await self._lightpad_lightpad.set_config({"glowEnabled": True})
194 
195  async def async_turn_off(self, **kwargs: Any) -> None:
196  """Turn the light off."""
197  if ATTR_BRIGHTNESS in kwargs:
198  brightness_pct = kwargs[ATTR_BRIGHTNESS] / 255.0
199  await self._lightpad_lightpad.set_config({"glowIntensity": brightness_pct})
200  else:
201  await self._lightpad_lightpad.set_config({"glowEnabled": False})
ColorMode|str|None color_mode(self)
Definition: __init__.py:909
None schedule_update_ha_state(self, bool force_refresh=False)
Definition: entity.py:1244
None turn_off(self, **Any kwargs)
Definition: entity.py:1705
None turn_on(self, **Any kwargs)
Definition: entity.py:1697
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: light.py:29
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)