2019-02-14 15:01:46 +00:00
|
|
|
"""Support for Homematic lights."""
|
2017-02-20 16:07:33 +00:00
|
|
|
from homeassistant.components.light import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_BRIGHTNESS,
|
2020-03-23 09:56:44 +00:00
|
|
|
ATTR_COLOR_TEMP,
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_EFFECT,
|
|
|
|
ATTR_HS_COLOR,
|
|
|
|
ATTR_TRANSITION,
|
|
|
|
SUPPORT_BRIGHTNESS,
|
|
|
|
SUPPORT_COLOR,
|
2020-03-23 09:56:44 +00:00
|
|
|
SUPPORT_COLOR_TEMP,
|
2019-07-31 19:25:30 +00:00
|
|
|
SUPPORT_EFFECT,
|
2020-04-26 16:49:41 +00:00
|
|
|
LightEntity,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-06-24 08:06:58 +00:00
|
|
|
|
2020-01-14 10:26:59 +00:00
|
|
|
from .const import ATTR_DISCOVER_DEVICES
|
|
|
|
from .entity import HMDevice
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2016-08-16 06:07:07 +00:00
|
|
|
SUPPORT_HOMEMATIC = SUPPORT_BRIGHTNESS
|
|
|
|
|
2016-06-24 08:06:58 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-05-01 03:10:08 +00:00
|
|
|
"""Set up the Homematic light platform."""
|
2016-06-28 20:53:53 +00:00
|
|
|
if discovery_info is None:
|
|
|
|
return
|
|
|
|
|
2017-02-20 16:07:33 +00:00
|
|
|
devices = []
|
2017-07-06 03:02:16 +00:00
|
|
|
for conf in discovery_info[ATTR_DISCOVER_DEVICES]:
|
2017-08-31 19:16:44 +00:00
|
|
|
new_device = HMLight(conf)
|
2017-02-20 16:07:33 +00:00
|
|
|
devices.append(new_device)
|
|
|
|
|
2020-01-14 10:26:59 +00:00
|
|
|
add_entities(devices, True)
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:49:41 +00:00
|
|
|
class HMLight(HMDevice, LightEntity):
|
2016-06-30 08:33:34 +00:00
|
|
|
"""Representation of a Homematic light."""
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def brightness(self):
|
|
|
|
"""Return the brightness of this light between 0..255."""
|
|
|
|
# Is dimmer?
|
2019-07-31 19:25:30 +00:00
|
|
|
if self._state == "LEVEL":
|
2016-06-24 08:06:58 +00:00
|
|
|
return int(self._hm_get_state() * 255)
|
2017-07-06 03:02:16 +00:00
|
|
|
return None
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-06-30 08:33:34 +00:00
|
|
|
"""Return true if light is on."""
|
2016-06-24 08:06:58 +00:00
|
|
|
try:
|
|
|
|
return self._hm_get_state() > 0
|
|
|
|
except TypeError:
|
|
|
|
return False
|
|
|
|
|
2016-08-16 06:07:07 +00:00
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
2019-10-19 12:40:42 +00:00
|
|
|
features = SUPPORT_BRIGHTNESS
|
2019-07-31 19:25:30 +00:00
|
|
|
if "COLOR" in self._hmdevice.WRITENODE:
|
2019-10-19 12:40:42 +00:00
|
|
|
features |= SUPPORT_COLOR
|
|
|
|
if "PROGRAM" in self._hmdevice.WRITENODE:
|
|
|
|
features |= SUPPORT_EFFECT
|
2020-03-23 09:56:44 +00:00
|
|
|
if hasattr(self._hmdevice, "get_color_temp"):
|
|
|
|
features |= SUPPORT_COLOR_TEMP
|
2019-10-19 12:40:42 +00:00
|
|
|
return features
|
2018-11-19 13:26:08 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def hs_color(self):
|
|
|
|
"""Return the hue and saturation color value [float, float]."""
|
|
|
|
if not self.supported_features & SUPPORT_COLOR:
|
|
|
|
return None
|
2019-10-25 14:06:52 +00:00
|
|
|
hue, sat = self._hmdevice.get_hs_color(self._channel)
|
2019-07-31 19:25:30 +00:00
|
|
|
return hue * 360.0, sat * 100.0
|
2018-11-19 13:26:08 +00:00
|
|
|
|
2020-03-23 09:56:44 +00:00
|
|
|
@property
|
|
|
|
def color_temp(self):
|
|
|
|
"""Return the color temp in mireds [int]."""
|
|
|
|
if not self.supported_features & SUPPORT_COLOR_TEMP:
|
|
|
|
return None
|
|
|
|
hm_color_temp = self._hmdevice.get_color_temp(self._channel)
|
|
|
|
return self.max_mireds - (self.max_mireds - self.min_mireds) * hm_color_temp
|
|
|
|
|
2018-11-19 13:26:08 +00:00
|
|
|
@property
|
|
|
|
def effect_list(self):
|
|
|
|
"""Return the list of supported effects."""
|
|
|
|
if not self.supported_features & SUPPORT_EFFECT:
|
|
|
|
return None
|
|
|
|
return self._hmdevice.get_effect_list()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def effect(self):
|
|
|
|
"""Return the current color change program of the light."""
|
|
|
|
if not self.supported_features & SUPPORT_EFFECT:
|
|
|
|
return None
|
|
|
|
return self._hmdevice.get_effect()
|
2016-08-16 06:07:07 +00:00
|
|
|
|
2016-06-24 08:06:58 +00:00
|
|
|
def turn_on(self, **kwargs):
|
2018-11-19 13:26:08 +00:00
|
|
|
"""Turn the light on and/or change color or color effect settings."""
|
2019-04-17 12:30:54 +00:00
|
|
|
if ATTR_TRANSITION in kwargs:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._hmdevice.setValue("RAMP_TIME", kwargs[ATTR_TRANSITION])
|
2019-04-17 12:30:54 +00:00
|
|
|
|
2017-07-06 03:02:16 +00:00
|
|
|
if ATTR_BRIGHTNESS in kwargs and self._state == "LEVEL":
|
2016-06-24 08:06:58 +00:00
|
|
|
percent_bright = float(kwargs[ATTR_BRIGHTNESS]) / 255
|
|
|
|
self._hmdevice.set_level(percent_bright, self._channel)
|
2020-03-23 09:56:44 +00:00
|
|
|
elif (
|
|
|
|
ATTR_HS_COLOR not in kwargs
|
|
|
|
and ATTR_COLOR_TEMP not in kwargs
|
|
|
|
and ATTR_EFFECT not in kwargs
|
|
|
|
):
|
2016-06-24 08:06:58 +00:00
|
|
|
self._hmdevice.on(self._channel)
|
|
|
|
|
2020-05-21 08:35:04 +00:00
|
|
|
if ATTR_HS_COLOR in kwargs and self.supported_features & SUPPORT_COLOR:
|
2018-11-19 13:26:08 +00:00
|
|
|
self._hmdevice.set_hs_color(
|
2019-07-31 19:25:30 +00:00
|
|
|
hue=kwargs[ATTR_HS_COLOR][0] / 360.0,
|
|
|
|
saturation=kwargs[ATTR_HS_COLOR][1] / 100.0,
|
2019-10-25 14:06:52 +00:00
|
|
|
channel=self._channel,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-03-23 09:56:44 +00:00
|
|
|
if ATTR_COLOR_TEMP in kwargs:
|
|
|
|
hm_temp = (self.max_mireds - kwargs[ATTR_COLOR_TEMP]) / (
|
|
|
|
self.max_mireds - self.min_mireds
|
|
|
|
)
|
|
|
|
self._hmdevice.set_color_temp(hm_temp)
|
2018-11-19 13:26:08 +00:00
|
|
|
if ATTR_EFFECT in kwargs:
|
|
|
|
self._hmdevice.set_effect(kwargs[ATTR_EFFECT])
|
|
|
|
|
2016-06-24 08:06:58 +00:00
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Turn the light off."""
|
2017-02-14 22:19:57 +00:00
|
|
|
self._hmdevice.off(self._channel)
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
def _init_data_struct(self):
|
2016-06-30 08:33:34 +00:00
|
|
|
"""Generate a data dict (self._data) from the Homematic metadata."""
|
2016-06-24 08:06:58 +00:00
|
|
|
# Use LEVEL
|
2016-09-09 17:33:12 +00:00
|
|
|
self._state = "LEVEL"
|
2018-11-19 13:26:08 +00:00
|
|
|
self._data[self._state] = None
|
|
|
|
|
|
|
|
if self.supported_features & SUPPORT_COLOR:
|
2019-10-19 12:40:42 +00:00
|
|
|
self._data.update({"COLOR": None})
|
|
|
|
if self.supported_features & SUPPORT_EFFECT:
|
|
|
|
self._data.update({"PROGRAM": None})
|