2019-02-13 21:21:14 +01:00
|
|
|
"""Support for Vera lights."""
|
2015-03-02 21:09:00 +11:00
|
|
|
import logging
|
2015-10-27 23:18:46 +00:00
|
|
|
|
2016-09-12 15:52:22 +02:00
|
|
|
from homeassistant.components.light import (
|
2019-07-31 12:25:30 -07:00
|
|
|
ATTR_BRIGHTNESS,
|
|
|
|
ATTR_HS_COLOR,
|
|
|
|
ENTITY_ID_FORMAT,
|
|
|
|
SUPPORT_BRIGHTNESS,
|
|
|
|
SUPPORT_COLOR,
|
|
|
|
Light,
|
|
|
|
)
|
2018-03-18 18:00:29 -04:00
|
|
|
import homeassistant.util.color as color_util
|
2015-12-30 19:44:02 +00:00
|
|
|
|
2019-03-20 22:56:46 -07:00
|
|
|
from . import VERA_CONTROLLER, VERA_DEVICES, VeraDevice
|
|
|
|
|
2015-03-08 23:52:50 +11:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2015-03-02 21:09:00 +11:00
|
|
|
|
2015-03-09 01:58:11 +11:00
|
|
|
|
2018-08-24 16:37:30 +02:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-05-02 18:18:47 +02:00
|
|
|
"""Set up the Vera lights."""
|
2018-08-24 16:37:30 +02:00
|
|
|
add_entities(
|
2019-07-31 12:25:30 -07:00
|
|
|
[
|
|
|
|
VeraLight(device, hass.data[VERA_CONTROLLER])
|
|
|
|
for device in hass.data[VERA_DEVICES]["light"]
|
|
|
|
],
|
|
|
|
True,
|
|
|
|
)
|
2015-10-27 23:18:46 +00:00
|
|
|
|
2015-10-27 23:43:06 +00:00
|
|
|
|
2016-03-15 09:17:09 +00:00
|
|
|
class VeraLight(VeraDevice, Light):
|
2016-03-07 22:08:21 +01:00
|
|
|
"""Representation of a Vera Light, including dimmable."""
|
2015-10-27 23:18:46 +00:00
|
|
|
|
2016-03-15 09:17:09 +00:00
|
|
|
def __init__(self, vera_device, controller):
|
2016-03-07 22:08:21 +01:00
|
|
|
"""Initialize the light."""
|
2016-03-15 09:17:09 +00:00
|
|
|
self._state = False
|
2017-06-15 16:28:24 -06:00
|
|
|
self._color = None
|
|
|
|
self._brightness = None
|
2016-03-15 09:17:09 +00:00
|
|
|
VeraDevice.__init__(self, vera_device, controller)
|
2017-03-11 18:06:46 +00:00
|
|
|
self.entity_id = ENTITY_ID_FORMAT.format(self.vera_id)
|
2016-02-07 21:45:15 +00:00
|
|
|
|
2015-10-27 23:18:46 +00:00
|
|
|
@property
|
2016-02-06 22:28:29 -08:00
|
|
|
def brightness(self):
|
2016-03-07 22:08:21 +01:00
|
|
|
"""Return the brightness of the light."""
|
2017-06-15 16:28:24 -06:00
|
|
|
return self._brightness
|
2015-10-27 23:18:46 +00:00
|
|
|
|
2017-06-08 04:28:03 -06:00
|
|
|
@property
|
2018-03-18 18:00:29 -04:00
|
|
|
def hs_color(self):
|
2017-06-08 04:28:03 -06:00
|
|
|
"""Return the color of the light."""
|
2017-06-15 16:28:24 -06:00
|
|
|
return self._color
|
2017-06-08 04:28:03 -06:00
|
|
|
|
2016-08-16 09:07:07 +03:00
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
2017-06-15 16:28:24 -06:00
|
|
|
if self._color:
|
2018-03-18 18:00:29 -04:00
|
|
|
return SUPPORT_BRIGHTNESS | SUPPORT_COLOR
|
2017-07-05 23:30:01 -07:00
|
|
|
return SUPPORT_BRIGHTNESS
|
2016-08-16 09:07:07 +03:00
|
|
|
|
2015-10-27 23:18:46 +00:00
|
|
|
def turn_on(self, **kwargs):
|
2016-03-07 22:08:21 +01:00
|
|
|
"""Turn the light on."""
|
2018-03-18 18:00:29 -04:00
|
|
|
if ATTR_HS_COLOR in kwargs and self._color:
|
|
|
|
rgb = color_util.color_hs_to_RGB(*kwargs[ATTR_HS_COLOR])
|
|
|
|
self.vera_device.set_color(rgb)
|
2017-06-08 04:28:03 -06:00
|
|
|
elif ATTR_BRIGHTNESS in kwargs and self.vera_device.is_dimmable:
|
2015-10-27 23:18:46 +00:00
|
|
|
self.vera_device.set_brightness(kwargs[ATTR_BRIGHTNESS])
|
|
|
|
else:
|
|
|
|
self.vera_device.switch_on()
|
|
|
|
|
2017-04-04 11:02:09 +01:00
|
|
|
self._state = True
|
2016-11-30 22:33:38 +01:00
|
|
|
self.schedule_update_ha_state(True)
|
2016-02-07 21:45:15 +00:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
2016-03-07 22:08:21 +01:00
|
|
|
"""Turn the light off."""
|
2016-02-07 21:45:15 +00:00
|
|
|
self.vera_device.switch_off()
|
2017-04-04 11:02:09 +01:00
|
|
|
self._state = False
|
2016-11-30 22:33:38 +01:00
|
|
|
self.schedule_update_ha_state()
|
2016-02-07 21:45:15 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-03-07 22:08:21 +01:00
|
|
|
"""Return true if device is on."""
|
2016-03-15 09:17:09 +00:00
|
|
|
return self._state
|
2016-02-07 21:45:15 +00:00
|
|
|
|
|
|
|
def update(self):
|
2017-04-30 23:10:08 -04:00
|
|
|
"""Call to update state."""
|
2016-03-15 09:17:09 +00:00
|
|
|
self._state = self.vera_device.is_switched_on()
|
2017-06-19 10:54:13 +03:00
|
|
|
if self.vera_device.is_dimmable:
|
|
|
|
# If it is dimmable, both functions exist. In case color
|
|
|
|
# is not supported, it will return None
|
|
|
|
self._brightness = self.vera_device.get_brightness()
|
2018-03-18 18:00:29 -04:00
|
|
|
rgb = self.vera_device.get_color()
|
|
|
|
self._color = color_util.color_RGB_to_hs(*rgb) if rgb else None
|