Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for lights connected with WMS WebControl pro."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 from typing import Any
7 
8 from wmspro.const import WMS_WebControl_pro_API_actionDescription
9 
10 from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 from homeassistant.util.color import brightness_to_value, value_to_brightness
14 
15 from . import WebControlProConfigEntry
16 from .const import BRIGHTNESS_SCALE
17 from .entity import WebControlProGenericEntity
18 
19 SCAN_INTERVAL = timedelta(seconds=5)
20 PARALLEL_UPDATES = 1
21 
22 
24  hass: HomeAssistant,
25  config_entry: WebControlProConfigEntry,
26  async_add_entities: AddEntitiesCallback,
27 ) -> None:
28  """Set up the WMS based lights from a config entry."""
29  hub = config_entry.runtime_data
30 
31  entities: list[WebControlProGenericEntity] = []
32  for dest in hub.dests.values():
33  if dest.action(WMS_WebControl_pro_API_actionDescription.LightDimming):
34  entities.append(WebControlProDimmer(config_entry.entry_id, dest))
35  elif dest.action(WMS_WebControl_pro_API_actionDescription.LightSwitch):
36  entities.append(WebControlProLight(config_entry.entry_id, dest))
37 
38  async_add_entities(entities)
39 
40 
42  """Representation of a WMS based light."""
43 
44  _attr_color_mode = ColorMode.ONOFF
45  _attr_supported_color_modes = {ColorMode.ONOFF}
46 
47  @property
48  def is_on(self) -> bool:
49  """Return true if light is on."""
50  action = self._dest_dest.action(WMS_WebControl_pro_API_actionDescription.LightSwitch)
51  return action["onOffState"]
52 
53  async def async_turn_on(self, **kwargs: Any) -> None:
54  """Turn the light on."""
55  action = self._dest_dest.action(WMS_WebControl_pro_API_actionDescription.LightSwitch)
56  await action(onOffState=True)
57 
58  async def async_turn_off(self, **kwargs: Any) -> None:
59  """Turn the light off."""
60  action = self._dest_dest.action(WMS_WebControl_pro_API_actionDescription.LightSwitch)
61  await action(onOffState=False)
62 
63 
65  """Representation of a WMS-based dimmable light."""
66 
67  _attr_color_mode = ColorMode.BRIGHTNESS
68  _attr_supported_color_modes = {ColorMode.BRIGHTNESS}
69 
70  @property
71  def brightness(self) -> int:
72  """Return the brightness of this light between 1..255."""
73  action = self._dest_dest.action(
74  WMS_WebControl_pro_API_actionDescription.LightDimming
75  )
76  return value_to_brightness(BRIGHTNESS_SCALE, action["percentage"])
77 
78  async def async_turn_on(self, **kwargs: Any) -> None:
79  """Turn the dimmer on."""
80  if ATTR_BRIGHTNESS not in kwargs:
81  await super().async_turn_on(**kwargs)
82  return
83 
84  action = self._dest_dest.action(
85  WMS_WebControl_pro_API_actionDescription.LightDimming
86  )
87  await action(
88  percentage=brightness_to_value(BRIGHTNESS_SCALE, kwargs[ATTR_BRIGHTNESS])
89  )
None async_setup_entry(HomeAssistant hass, WebControlProConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: light.py:27
float brightness_to_value(tuple[float, float] low_high_range, int brightness)
Definition: color.py:752
int value_to_brightness(tuple[float, float] low_high_range, float value)
Definition: color.py:767