2019-02-14 04:35:12 +00:00
|
|
|
"""Support for Lutron lights."""
|
2022-01-03 14:11:59 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-01-05 15:39:14 +00:00
|
|
|
from collections.abc import Mapping
|
2022-07-31 11:53:22 +00:00
|
|
|
from typing import Any
|
|
|
|
|
2024-01-07 16:48:23 +00:00
|
|
|
from pylutron import Output
|
|
|
|
|
2022-04-23 19:21:57 +00:00
|
|
|
from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity
|
2024-01-05 15:39:14 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-01-03 14:11:59 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2024-01-05 21:04:10 +00:00
|
|
|
from . import DOMAIN, LutronData
|
2024-01-05 19:38:02 +00:00
|
|
|
from .entity import LutronDevice
|
2017-01-25 08:11:37 +00:00
|
|
|
|
|
|
|
|
2024-01-05 15:39:14 +00:00
|
|
|
async def async_setup_entry(
|
2022-01-03 14:11:59 +00:00
|
|
|
hass: HomeAssistant,
|
2024-01-05 15:39:14 +00:00
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
2022-01-03 14:11:59 +00:00
|
|
|
) -> None:
|
2024-01-05 15:39:14 +00:00
|
|
|
"""Set up the Lutron light platform.
|
2017-01-25 08:11:37 +00:00
|
|
|
|
2024-01-05 15:39:14 +00:00
|
|
|
Adds dimmers from the Main Repeater associated with the config_entry as
|
|
|
|
light entities.
|
|
|
|
"""
|
2024-01-05 21:04:10 +00:00
|
|
|
entry_data: LutronData = hass.data[DOMAIN][config_entry.entry_id]
|
|
|
|
async_add_entities(
|
|
|
|
[
|
|
|
|
LutronLight(area_name, device, entry_data.client)
|
|
|
|
for area_name, device in entry_data.lights
|
|
|
|
],
|
|
|
|
True,
|
|
|
|
)
|
2017-01-25 08:11:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
def to_lutron_level(level):
|
2020-01-05 12:09:17 +00:00
|
|
|
"""Convert the given Home Assistant light level (0-255) to Lutron (0.0-100.0)."""
|
2017-01-25 08:11:37 +00:00
|
|
|
return float((level * 100) / 255)
|
|
|
|
|
|
|
|
|
|
|
|
def to_hass_level(level):
|
2020-01-05 12:09:17 +00:00
|
|
|
"""Convert the given Lutron (0.0-100.0) light level to Home Assistant (0-255)."""
|
2017-01-25 08:11:37 +00:00
|
|
|
return int((level * 255) / 100)
|
|
|
|
|
|
|
|
|
2020-04-26 16:49:41 +00:00
|
|
|
class LutronLight(LutronDevice, LightEntity):
|
2017-01-25 08:11:37 +00:00
|
|
|
"""Representation of a Lutron Light, including dimmable."""
|
|
|
|
|
2022-04-23 19:21:57 +00:00
|
|
|
_attr_color_mode = ColorMode.BRIGHTNESS
|
|
|
|
_attr_supported_color_modes = {ColorMode.BRIGHTNESS}
|
2024-01-07 16:48:23 +00:00
|
|
|
_lutron_device: Output
|
|
|
|
_prev_brightness: int | None = None
|
2024-01-22 14:52:59 +00:00
|
|
|
_attr_name = None
|
2017-01-25 08:11:37 +00:00
|
|
|
|
2022-07-31 11:53:22 +00:00
|
|
|
def turn_on(self, **kwargs: Any) -> None:
|
2017-01-25 08:11:37 +00:00
|
|
|
"""Turn the light on."""
|
|
|
|
if ATTR_BRIGHTNESS in kwargs and self._lutron_device.is_dimmable:
|
|
|
|
brightness = kwargs[ATTR_BRIGHTNESS]
|
|
|
|
elif self._prev_brightness == 0:
|
|
|
|
brightness = 255 / 2
|
|
|
|
else:
|
|
|
|
brightness = self._prev_brightness
|
|
|
|
self._prev_brightness = brightness
|
|
|
|
self._lutron_device.level = to_lutron_level(brightness)
|
|
|
|
|
2022-07-31 11:53:22 +00:00
|
|
|
def turn_off(self, **kwargs: Any) -> None:
|
2017-01-25 08:11:37 +00:00
|
|
|
"""Turn the light off."""
|
|
|
|
self._lutron_device.level = 0
|
|
|
|
|
|
|
|
@property
|
2024-01-05 15:39:14 +00:00
|
|
|
def extra_state_attributes(self) -> Mapping[str, Any] | None:
|
2017-01-25 08:11:37 +00:00
|
|
|
"""Return the state attributes."""
|
2020-10-05 10:51:48 +00:00
|
|
|
return {"lutron_integration_id": self._lutron_device.id}
|
2017-01-25 08:11:37 +00:00
|
|
|
|
2024-01-25 11:54:31 +00:00
|
|
|
def _request_state(self) -> None:
|
|
|
|
"""Request the state from the device."""
|
|
|
|
self._lutron_device.level # pylint: disable=pointless-statement
|
|
|
|
|
|
|
|
def _update_attrs(self) -> None:
|
|
|
|
"""Update the state attributes."""
|
|
|
|
level = self._lutron_device.last_level()
|
|
|
|
self._attr_is_on = level > 0
|
|
|
|
hass_level = to_hass_level(level)
|
|
|
|
self._attr_brightness = hass_level
|
|
|
|
if self._prev_brightness is None or hass_level != 0:
|
|
|
|
self._prev_brightness = hass_level
|