Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for Lutron lights."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any
7 
8 from pylutron import Output
9 
11  ATTR_BRIGHTNESS,
12  ATTR_FLASH,
13  ATTR_TRANSITION,
14  ColorMode,
15  LightEntity,
16  LightEntityFeature,
17 )
18 from homeassistant.config_entries import ConfigEntry
19 from homeassistant.core import HomeAssistant
20 from homeassistant.helpers.entity_platform import AddEntitiesCallback
21 
22 from . import DOMAIN, LutronData
23 from .entity import LutronDevice
24 
25 
27  hass: HomeAssistant,
28  config_entry: ConfigEntry,
29  async_add_entities: AddEntitiesCallback,
30 ) -> None:
31  """Set up the Lutron light platform.
32 
33  Adds dimmers from the Main Repeater associated with the config_entry as
34  light entities.
35  """
36  entry_data: LutronData = hass.data[DOMAIN][config_entry.entry_id]
37 
39  (
40  LutronLight(area_name, device, entry_data.client)
41  for area_name, device in entry_data.lights
42  ),
43  True,
44  )
45 
46 
47 def to_lutron_level(level):
48  """Convert the given Home Assistant light level (0-255) to Lutron (0.0-100.0)."""
49  return float((level * 100) / 255)
50 
51 
52 def to_hass_level(level):
53  """Convert the given Lutron (0.0-100.0) light level to Home Assistant (0-255)."""
54  return int((level * 255) / 100)
55 
56 
58  """Representation of a Lutron Light, including dimmable."""
59 
60  _attr_color_mode = ColorMode.BRIGHTNESS
61  _attr_supported_color_modes = {ColorMode.BRIGHTNESS}
62  _attr_supported_features = LightEntityFeature.TRANSITION | LightEntityFeature.FLASH
63  _lutron_device: Output
64  _prev_brightness: int | None = None
65  _attr_name = None
66 
67  def turn_on(self, **kwargs: Any) -> None:
68  """Turn the light on."""
69  if flash := kwargs.get(ATTR_FLASH):
70  self._lutron_device_lutron_device.flash(0.5 if flash == "short" else 1.5)
71  else:
72  if ATTR_BRIGHTNESS in kwargs and self._lutron_device_lutron_device.is_dimmable:
73  brightness = kwargs[ATTR_BRIGHTNESS]
74  elif self._prev_brightness_prev_brightness == 0:
75  brightness = 255 / 2
76  else:
77  brightness = self._prev_brightness_prev_brightness
78  self._prev_brightness_prev_brightness = brightness
79  args = {"new_level": to_lutron_level(brightness)}
80  if ATTR_TRANSITION in kwargs:
81  args["fade_time_seconds"] = kwargs[ATTR_TRANSITION]
82  self._lutron_device_lutron_device.set_level(**args)
83 
84  def turn_off(self, **kwargs: Any) -> None:
85  """Turn the light off."""
86  args = {"new_level": 0}
87  if ATTR_TRANSITION in kwargs:
88  args["fade_time_seconds"] = kwargs[ATTR_TRANSITION]
89  self._lutron_device_lutron_device.set_level(**args)
90 
91  @property
92  def extra_state_attributes(self) -> Mapping[str, Any] | None:
93  """Return the state attributes."""
94  return {"lutron_integration_id": self._lutron_device_lutron_device.id}
95 
96  def _request_state(self) -> None:
97  """Request the state from the device."""
98  _ = self._lutron_device_lutron_device.level
99 
100  def _update_attrs(self) -> None:
101  """Update the state attributes."""
102  level = self._lutron_device_lutron_device.last_level()
103  self._attr_is_on_attr_is_on = level > 0
104  hass_level = to_hass_level(level)
105  self._attr_brightness_attr_brightness = hass_level
106  if self._prev_brightness_prev_brightness is None or hass_level != 0:
107  self._prev_brightness_prev_brightness = hass_level
Mapping[str, Any]|None extra_state_attributes(self)
Definition: light.py:92
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: light.py:30