2019-02-14 05:35:12 +01:00
|
|
|
"""Support for Lutron lights."""
|
2022-01-03 15:11:59 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-01-05 09:39:14 -06:00
|
|
|
from collections.abc import Mapping
|
2022-07-31 13:53:22 +02:00
|
|
|
from typing import Any
|
|
|
|
|
2024-01-07 17:48:23 +01:00
|
|
|
from pylutron import Output
|
|
|
|
|
2022-04-23 21:21:57 +02:00
|
|
|
from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity
|
2024-01-05 09:39:14 -06:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-01-03 15:11:59 +01:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-03-20 22:56:46 -07:00
|
|
|
|
2024-01-05 22:04:10 +01:00
|
|
|
from . import DOMAIN, LutronData
|
2024-01-05 20:38:02 +01:00
|
|
|
from .entity import LutronDevice
|
2017-01-25 00:11:37 -08:00
|
|
|
|
|
|
|
|
2024-01-05 09:39:14 -06:00
|
|
|
async def async_setup_entry(
|
2022-01-03 15:11:59 +01:00
|
|
|
hass: HomeAssistant,
|
2024-01-05 09:39:14 -06:00
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
2022-01-03 15:11:59 +01:00
|
|
|
) -> None:
|
2024-01-05 09:39:14 -06:00
|
|
|
"""Set up the Lutron light platform.
|
2017-01-25 00:11:37 -08:00
|
|
|
|
2024-01-05 09:39:14 -06:00
|
|
|
Adds dimmers from the Main Repeater associated with the config_entry as
|
|
|
|
light entities.
|
|
|
|
"""
|
2024-01-05 22:04:10 +01: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 00:11:37 -08:00
|
|
|
|
|
|
|
|
|
|
|
def to_lutron_level(level):
|
2020-01-05 14:09:17 +02:00
|
|
|
"""Convert the given Home Assistant light level (0-255) to Lutron (0.0-100.0)."""
|
2017-01-25 00:11:37 -08:00
|
|
|
return float((level * 100) / 255)
|
|
|
|
|
|
|
|
|
|
|
|
def to_hass_level(level):
|
2020-01-05 14:09:17 +02:00
|
|
|
"""Convert the given Lutron (0.0-100.0) light level to Home Assistant (0-255)."""
|
2017-01-25 00:11:37 -08:00
|
|
|
return int((level * 255) / 100)
|
|
|
|
|
|
|
|
|
2020-04-26 18:49:41 +02:00
|
|
|
class LutronLight(LutronDevice, LightEntity):
|
2017-01-25 00:11:37 -08:00
|
|
|
"""Representation of a Lutron Light, including dimmable."""
|
|
|
|
|
2022-04-23 21:21:57 +02:00
|
|
|
_attr_color_mode = ColorMode.BRIGHTNESS
|
|
|
|
_attr_supported_color_modes = {ColorMode.BRIGHTNESS}
|
2024-01-07 17:48:23 +01:00
|
|
|
_lutron_device: Output
|
|
|
|
_prev_brightness: int | None = None
|
2017-01-25 00:11:37 -08:00
|
|
|
|
|
|
|
@property
|
2024-01-05 09:39:14 -06:00
|
|
|
def brightness(self) -> int:
|
2017-01-25 00:11:37 -08:00
|
|
|
"""Return the brightness of the light."""
|
|
|
|
new_brightness = to_hass_level(self._lutron_device.last_level())
|
|
|
|
if new_brightness != 0:
|
|
|
|
self._prev_brightness = new_brightness
|
|
|
|
return new_brightness
|
|
|
|
|
2022-07-31 13:53:22 +02:00
|
|
|
def turn_on(self, **kwargs: Any) -> None:
|
2017-01-25 00:11:37 -08: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 13:53:22 +02:00
|
|
|
def turn_off(self, **kwargs: Any) -> None:
|
2017-01-25 00:11:37 -08:00
|
|
|
"""Turn the light off."""
|
|
|
|
self._lutron_device.level = 0
|
|
|
|
|
|
|
|
@property
|
2024-01-05 09:39:14 -06:00
|
|
|
def extra_state_attributes(self) -> Mapping[str, Any] | None:
|
2017-01-25 00:11:37 -08:00
|
|
|
"""Return the state attributes."""
|
2020-10-05 12:51:48 +02:00
|
|
|
return {"lutron_integration_id": self._lutron_device.id}
|
2017-01-25 00:11:37 -08:00
|
|
|
|
|
|
|
@property
|
2024-01-05 09:39:14 -06:00
|
|
|
def is_on(self) -> bool:
|
2017-01-25 00:11:37 -08:00
|
|
|
"""Return true if device is on."""
|
|
|
|
return self._lutron_device.last_level() > 0
|
|
|
|
|
2022-07-31 13:53:22 +02:00
|
|
|
def update(self) -> None:
|
2017-04-30 23:10:08 -04:00
|
|
|
"""Call when forcing a refresh of the device."""
|
2017-01-25 00:11:37 -08:00
|
|
|
if self._prev_brightness is None:
|
|
|
|
self._prev_brightness = to_hass_level(self._lutron_device.level)
|