2019-02-14 04:35:12 +00:00
|
|
|
"""Support for Lutron lights."""
|
2020-04-26 16:49:41 +00:00
|
|
|
from homeassistant.components.light import (
|
|
|
|
ATTR_BRIGHTNESS,
|
|
|
|
SUPPORT_BRIGHTNESS,
|
|
|
|
LightEntity,
|
|
|
|
)
|
2019-03-21 05:56:46 +00:00
|
|
|
|
|
|
|
from . import LUTRON_CONTROLLER, LUTRON_DEVICES, LutronDevice
|
2017-01-25 08:11:37 +00:00
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-06-08 22:21:06 +00:00
|
|
|
"""Set up the Lutron lights."""
|
2017-01-25 08:11:37 +00:00
|
|
|
devs = []
|
2019-07-31 19:25:30 +00:00
|
|
|
for (area_name, device) in hass.data[LUTRON_DEVICES]["light"]:
|
2017-04-12 07:52:01 +00:00
|
|
|
dev = LutronLight(area_name, device, hass.data[LUTRON_CONTROLLER])
|
2017-01-25 08:11:37 +00:00
|
|
|
devs.append(dev)
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(devs, 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."""
|
|
|
|
|
2017-04-12 07:52:01 +00:00
|
|
|
def __init__(self, area_name, lutron_device, controller):
|
2017-01-25 08:11:37 +00:00
|
|
|
"""Initialize the light."""
|
|
|
|
self._prev_brightness = None
|
2018-12-14 07:52:29 +00:00
|
|
|
super().__init__(area_name, lutron_device, controller)
|
2017-01-25 08:11:37 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
|
|
|
return SUPPORT_BRIGHTNESS
|
|
|
|
|
|
|
|
@property
|
|
|
|
def brightness(self):
|
|
|
|
"""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
|
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""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)
|
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Turn the light off."""
|
|
|
|
self._lutron_device.level = 0
|
|
|
|
|
|
|
|
@property
|
2021-03-11 19:11:25 +00:00
|
|
|
def extra_state_attributes(self):
|
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
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if device is on."""
|
|
|
|
return self._lutron_device.last_level() > 0
|
|
|
|
|
|
|
|
def update(self):
|
2017-05-01 03:10:08 +00:00
|
|
|
"""Call when forcing a refresh of the device."""
|
2017-01-25 08:11:37 +00:00
|
|
|
if self._prev_brightness is None:
|
|
|
|
self._prev_brightness = to_hass_level(self._lutron_device.level)
|