2019-02-14 04:35:12 +00:00
|
|
|
"""Support for Lutron Caseta lights."""
|
2020-10-03 20:06:23 +00:00
|
|
|
from datetime import timedelta
|
2017-03-23 00:18:14 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from homeassistant.components.light import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_BRIGHTNESS,
|
2020-10-03 20:06:23 +00:00
|
|
|
ATTR_TRANSITION,
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN,
|
|
|
|
SUPPORT_BRIGHTNESS,
|
2020-10-03 20:06:23 +00:00
|
|
|
SUPPORT_TRANSITION,
|
2020-04-26 16:49:41 +00:00
|
|
|
LightEntity,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2020-05-11 09:05:13 +00:00
|
|
|
from . import DOMAIN as CASETA_DOMAIN, LutronCasetaDevice
|
2017-03-23 00:18:14 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2020-03-20 09:42:34 +00:00
|
|
|
def to_lutron_level(level):
|
|
|
|
"""Convert the given Home Assistant light level (0-255) to Lutron (0-100)."""
|
2020-04-13 22:07:32 +00:00
|
|
|
return int(round((level * 100) / 255))
|
2020-03-20 09:42:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def to_hass_level(level):
|
|
|
|
"""Convert the given Lutron (0-100) light level to Home Assistant (0-255)."""
|
|
|
|
return int((level * 255) // 100)
|
|
|
|
|
|
|
|
|
2020-05-11 09:05:13 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up the Lutron Caseta light platform.
|
|
|
|
|
|
|
|
Adds dimmers from the Caseta bridge associated with the config_entry as
|
|
|
|
light entities.
|
|
|
|
"""
|
|
|
|
|
2020-03-22 16:35:01 +00:00
|
|
|
entities = []
|
2020-05-11 09:05:13 +00:00
|
|
|
bridge = hass.data[CASETA_DOMAIN][config_entry.entry_id]
|
2017-09-05 09:30:36 +00:00
|
|
|
light_devices = bridge.get_devices_by_domain(DOMAIN)
|
2020-05-11 09:05:13 +00:00
|
|
|
|
2017-03-23 00:18:14 +00:00
|
|
|
for light_device in light_devices:
|
2020-03-22 16:35:01 +00:00
|
|
|
entity = LutronCasetaLight(light_device, bridge)
|
|
|
|
entities.append(entity)
|
2017-03-23 00:18:14 +00:00
|
|
|
|
2020-03-22 16:35:01 +00:00
|
|
|
async_add_entities(entities, True)
|
2017-03-23 00:18:14 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:49:41 +00:00
|
|
|
class LutronCasetaLight(LutronCasetaDevice, LightEntity):
|
2017-03-23 00:18:14 +00:00
|
|
|
"""Representation of a Lutron Light, including dimmable."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
2020-10-03 20:06:23 +00:00
|
|
|
return SUPPORT_BRIGHTNESS | SUPPORT_TRANSITION
|
2017-03-23 00:18:14 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def brightness(self):
|
|
|
|
"""Return the brightness of the light."""
|
2019-11-26 17:06:14 +00:00
|
|
|
return to_hass_level(self._device["current_state"])
|
2017-03-23 00:18:14 +00:00
|
|
|
|
2020-10-03 20:06:23 +00:00
|
|
|
async def _set_brightness(self, brightness, **kwargs):
|
|
|
|
args = {}
|
|
|
|
if ATTR_TRANSITION in kwargs:
|
|
|
|
args["fade_time"] = timedelta(seconds=kwargs[ATTR_TRANSITION])
|
|
|
|
|
|
|
|
await self._smartbridge.set_value(
|
|
|
|
self.device_id, to_lutron_level(brightness), **args
|
|
|
|
)
|
|
|
|
|
2018-10-01 06:56:50 +00:00
|
|
|
async def async_turn_on(self, **kwargs):
|
2017-03-23 00:18:14 +00:00
|
|
|
"""Turn the light on."""
|
2020-10-03 20:06:23 +00:00
|
|
|
brightness = kwargs.pop(ATTR_BRIGHTNESS, 255)
|
|
|
|
|
|
|
|
await self._set_brightness(brightness, **kwargs)
|
2017-03-23 00:18:14 +00:00
|
|
|
|
2018-10-01 06:56:50 +00:00
|
|
|
async def async_turn_off(self, **kwargs):
|
2017-03-23 00:18:14 +00:00
|
|
|
"""Turn the light off."""
|
2020-10-03 20:06:23 +00:00
|
|
|
await self._set_brightness(0, **kwargs)
|
2017-03-23 00:18:14 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if device is on."""
|
2019-11-26 17:06:14 +00:00
|
|
|
return self._device["current_state"] > 0
|
2017-03-23 00:18:14 +00:00
|
|
|
|
2018-10-01 06:56:50 +00:00
|
|
|
async def async_update(self):
|
2017-05-01 03:10:08 +00:00
|
|
|
"""Call when forcing a refresh of the device."""
|
2019-11-26 17:06:14 +00:00
|
|
|
self._device = self._smartbridge.get_device_by_id(self.device_id)
|
|
|
|
_LOGGER.debug(self._device)
|