Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for Abode Security System lights."""
2 
3 from __future__ import annotations
4 
5 from math import ceil
6 from typing import Any
7 
8 from jaraco.abode.devices.light import Light
9 
11  ATTR_BRIGHTNESS,
12  ATTR_COLOR_TEMP,
13  ATTR_HS_COLOR,
14  ColorMode,
15  LightEntity,
16 )
17 from homeassistant.config_entries import ConfigEntry
18 from homeassistant.core import HomeAssistant
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 from homeassistant.util.color import (
21  color_temperature_kelvin_to_mired,
22  color_temperature_mired_to_kelvin,
23 )
24 
25 from . import AbodeSystem
26 from .const import DOMAIN
27 from .entity import AbodeDevice
28 
29 
31  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
32 ) -> None:
33  """Set up Abode light devices."""
34  data: AbodeSystem = hass.data[DOMAIN]
35 
37  AbodeLight(data, device)
38  for device in data.abode.get_devices(generic_type="light")
39  )
40 
41 
43  """Representation of an Abode light."""
44 
45  _device: Light
46  _attr_name = None
47 
48  def turn_on(self, **kwargs: Any) -> None:
49  """Turn on the light."""
50  if ATTR_COLOR_TEMP in kwargs and self._device_device.is_color_capable:
51  self._device_device.set_color_temp(
52  int(color_temperature_mired_to_kelvin(kwargs[ATTR_COLOR_TEMP]))
53  )
54  return
55 
56  if ATTR_HS_COLOR in kwargs and self._device_device.is_color_capable:
57  self._device_device.set_color(kwargs[ATTR_HS_COLOR])
58  return
59 
60  if ATTR_BRIGHTNESS in kwargs and self._device_device.is_dimmable:
61  # Convert Home Assistant brightness (0-255) to Abode brightness (0-99)
62  # If 100 is sent to Abode, response is 99 causing an error
63  self._device_device.set_level(ceil(kwargs[ATTR_BRIGHTNESS] * 99 / 255.0))
64  return
65 
66  self._device_device.switch_on()
67 
68  def turn_off(self, **kwargs: Any) -> None:
69  """Turn off the light."""
70  self._device_device.switch_off()
71 
72  @property
73  def is_on(self) -> bool:
74  """Return true if device is on."""
75  return bool(self._device_device.is_on)
76 
77  @property
78  def brightness(self) -> int | None:
79  """Return the brightness of the light."""
80  if self._device_device.is_dimmable and self._device_device.has_brightness:
81  brightness = int(self._device_device.brightness)
82  # Abode returns 100 during device initialization and device refresh
83  # Convert Abode brightness (0-99) to Home Assistant brightness (0-255)
84  return 255 if brightness == 100 else ceil(brightness * 255 / 99.0)
85  return None
86 
87  @property
88  def color_temp(self) -> int | None:
89  """Return the color temp of the light."""
90  if self._device_device.has_color:
91  return color_temperature_kelvin_to_mired(self._device_device.color_temp)
92  return None
93 
94  @property
95  def hs_color(self) -> tuple[float, float] | None:
96  """Return the color of the light."""
97  _hs = None
98  if self._device_device.has_color:
99  _hs = self._device_device.color
100  return _hs
101 
102  @property
103  def color_mode(self) -> str | None:
104  """Return the color mode of the light."""
105  if self._device_device.is_dimmable and self._device_device.is_color_capable:
106  if self.hs_colorhs_colorhs_color is not None:
107  return ColorMode.HS
108  return ColorMode.COLOR_TEMP
109  if self._device_device.is_dimmable:
110  return ColorMode.BRIGHTNESS
111  return ColorMode.ONOFF
112 
113  @property
114  def supported_color_modes(self) -> set[str] | None:
115  """Flag supported color modes."""
116  if self._device_device.is_dimmable and self._device_device.is_color_capable:
117  return {ColorMode.COLOR_TEMP, ColorMode.HS}
118  if self._device_device.is_dimmable:
119  return {ColorMode.BRIGHTNESS}
120  return {ColorMode.ONOFF}
tuple[float, float]|None hs_color(self)
Definition: light.py:95
None turn_off(self, **Any kwargs)
Definition: light.py:68
None turn_on(self, **Any kwargs)
Definition: light.py:48
tuple[float, float]|None hs_color(self)
Definition: __init__.py:947
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: light.py:32
int color_temperature_mired_to_kelvin(float mired_temperature)
Definition: color.py:631
int color_temperature_kelvin_to_mired(float kelvin_temperature)
Definition: color.py:636