2019-02-13 20:21:14 +00:00
|
|
|
"""Support for IKEA Tradfri lights."""
|
2017-04-18 15:26:59 +00:00
|
|
|
import logging
|
2017-04-13 17:04:42 +00:00
|
|
|
|
2019-09-22 21:01:32 +00:00
|
|
|
from pytradfri.error import PytradfriError
|
|
|
|
|
|
|
|
import homeassistant.util.color as color_util
|
2017-04-16 18:35:52 +00:00
|
|
|
from homeassistant.components.light import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_BRIGHTNESS,
|
|
|
|
ATTR_COLOR_TEMP,
|
|
|
|
ATTR_HS_COLOR,
|
|
|
|
ATTR_TRANSITION,
|
2019-10-09 19:56:16 +00:00
|
|
|
Light,
|
2019-07-31 19:25:30 +00:00
|
|
|
SUPPORT_BRIGHTNESS,
|
|
|
|
SUPPORT_COLOR,
|
|
|
|
SUPPORT_COLOR_TEMP,
|
|
|
|
)
|
2019-03-21 05:56:46 +00:00
|
|
|
from homeassistant.core import callback
|
2019-10-09 19:56:16 +00:00
|
|
|
from .base_class import TradfriBaseDevice
|
|
|
|
from .const import (
|
|
|
|
ATTR_DIMMER,
|
|
|
|
ATTR_HUE,
|
|
|
|
ATTR_SAT,
|
|
|
|
ATTR_TRANSITION_TIME,
|
|
|
|
SUPPORTED_LIGHT_FEATURES,
|
|
|
|
SUPPORTED_GROUP_FEATURES,
|
|
|
|
CONF_GATEWAY_ID,
|
|
|
|
CONF_IMPORT_GROUPS,
|
|
|
|
KEY_GATEWAY,
|
|
|
|
KEY_API,
|
|
|
|
)
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2017-04-18 15:26:59 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2017-04-13 17:04:42 +00:00
|
|
|
|
|
|
|
|
2018-09-19 19:21:43 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Load Tradfri lights based on a config entry."""
|
|
|
|
gateway_id = config_entry.data[CONF_GATEWAY_ID]
|
|
|
|
api = hass.data[KEY_API][config_entry.entry_id]
|
|
|
|
gateway = hass.data[KEY_GATEWAY][config_entry.entry_id]
|
2017-04-13 17:04:42 +00:00
|
|
|
|
2018-09-19 19:21:43 +00:00
|
|
|
devices_commands = await api(gateway.get_devices())
|
2018-03-28 22:50:09 +00:00
|
|
|
devices = await api(devices_commands)
|
2017-10-05 16:05:38 +00:00
|
|
|
lights = [dev for dev in devices if dev.has_light_control]
|
|
|
|
if lights:
|
2019-07-31 19:25:30 +00:00
|
|
|
async_add_entities(TradfriLight(light, api, gateway_id) for light in lights)
|
2017-04-13 17:04:42 +00:00
|
|
|
|
2018-09-19 19:21:43 +00:00
|
|
|
if config_entry.data[CONF_IMPORT_GROUPS]:
|
|
|
|
groups_commands = await api(gateway.get_groups())
|
2018-03-28 22:50:09 +00:00
|
|
|
groups = await api(groups_commands)
|
2017-10-05 16:05:38 +00:00
|
|
|
if groups:
|
2019-07-31 19:25:30 +00:00
|
|
|
async_add_entities(TradfriGroup(group, api, gateway_id) for group in groups)
|
2017-04-25 04:57:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TradfriGroup(Light):
|
|
|
|
"""The platform class required by hass."""
|
|
|
|
|
2018-03-29 19:39:56 +00:00
|
|
|
def __init__(self, group, api, gateway_id):
|
2017-04-25 04:57:38 +00:00
|
|
|
"""Initialize a Group."""
|
2017-08-31 04:19:06 +00:00
|
|
|
self._api = api
|
2019-09-03 19:12:51 +00:00
|
|
|
self._unique_id = f"group-{gateway_id}-{group.id}"
|
2018-03-29 19:39:56 +00:00
|
|
|
self._group = group
|
|
|
|
self._name = group.name
|
2017-10-05 16:05:38 +00:00
|
|
|
|
2018-03-29 19:39:56 +00:00
|
|
|
self._refresh(group)
|
2017-10-05 16:05:38 +00:00
|
|
|
|
2018-03-28 22:50:09 +00:00
|
|
|
async def async_added_to_hass(self):
|
2017-10-05 16:05:38 +00:00
|
|
|
"""Start thread when added to hass."""
|
|
|
|
self._async_start_observe()
|
|
|
|
|
2018-03-29 19:39:56 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return unique ID for this group."""
|
|
|
|
return self._unique_id
|
|
|
|
|
2017-04-25 04:57:38 +00:00
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
2018-06-13 05:17:52 +00:00
|
|
|
return SUPPORTED_GROUP_FEATURES
|
2017-04-25 04:57:38 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the display name of this group."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if group lights are on."""
|
|
|
|
return self._group.state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def brightness(self):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Return the brightness of the group lights."""
|
2017-04-25 04:57:38 +00:00
|
|
|
return self._group.dimmer
|
|
|
|
|
2018-03-28 22:50:09 +00:00
|
|
|
async def async_turn_off(self, **kwargs):
|
2017-04-25 04:57:38 +00:00
|
|
|
"""Instruct the group lights to turn off."""
|
2018-03-28 22:50:09 +00:00
|
|
|
await self._api(self._group.set_state(0))
|
2017-04-25 04:57:38 +00:00
|
|
|
|
2018-03-28 22:50:09 +00:00
|
|
|
async def async_turn_on(self, **kwargs):
|
2017-04-25 04:57:38 +00:00
|
|
|
"""Instruct the group lights to turn on, or dim."""
|
2017-10-05 16:05:38 +00:00
|
|
|
keys = {}
|
|
|
|
if ATTR_TRANSITION in kwargs:
|
2019-07-31 19:25:30 +00:00
|
|
|
keys["transition_time"] = int(kwargs[ATTR_TRANSITION]) * 10
|
2017-10-05 16:05:38 +00:00
|
|
|
|
2017-04-25 04:57:38 +00:00
|
|
|
if ATTR_BRIGHTNESS in kwargs:
|
2017-11-05 16:43:45 +00:00
|
|
|
if kwargs[ATTR_BRIGHTNESS] == 255:
|
|
|
|
kwargs[ATTR_BRIGHTNESS] = 254
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await self._api(self._group.set_dimmer(kwargs[ATTR_BRIGHTNESS], **keys))
|
2017-04-25 04:57:38 +00:00
|
|
|
else:
|
2018-03-28 22:50:09 +00:00
|
|
|
await self._api(self._group.set_state(1))
|
2017-10-05 16:05:38 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def _async_start_observe(self, exc=None):
|
|
|
|
"""Start observation of light."""
|
|
|
|
if exc:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.warning("Observation failed for %s", self._name, exc_info=exc)
|
2017-04-25 04:57:38 +00:00
|
|
|
|
2017-07-31 15:58:47 +00:00
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
cmd = self._group.observe(
|
|
|
|
callback=self._observe_update,
|
|
|
|
err_callback=self._async_start_observe,
|
|
|
|
duration=0,
|
|
|
|
)
|
2018-10-02 09:03:09 +00:00
|
|
|
self.hass.async_create_task(self._api(cmd))
|
2018-03-28 22:50:09 +00:00
|
|
|
except PytradfriError as err:
|
2017-10-05 16:05:38 +00:00
|
|
|
_LOGGER.warning("Observation failed, trying again", exc_info=err)
|
|
|
|
self._async_start_observe()
|
|
|
|
|
|
|
|
def _refresh(self, group):
|
|
|
|
"""Refresh the light data."""
|
|
|
|
self._group = group
|
|
|
|
self._name = group.name
|
|
|
|
|
2017-12-06 11:44:41 +00:00
|
|
|
@callback
|
2017-10-05 16:05:38 +00:00
|
|
|
def _observe_update(self, tradfri_device):
|
|
|
|
"""Receive new state data for this light."""
|
|
|
|
self._refresh(tradfri_device)
|
2017-12-06 11:44:41 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
2017-04-25 04:57:38 +00:00
|
|
|
|
2018-04-01 14:50:48 +00:00
|
|
|
async def async_update(self):
|
|
|
|
"""Fetch new state data for the group."""
|
2018-04-01 17:42:47 +00:00
|
|
|
await self._api(self._group.update())
|
2018-04-01 14:50:48 +00:00
|
|
|
|
2017-04-13 17:04:42 +00:00
|
|
|
|
2019-10-07 17:43:47 +00:00
|
|
|
class TradfriLight(TradfriBaseDevice, Light):
|
2017-10-05 16:05:38 +00:00
|
|
|
"""The platform class required by Home Assistant."""
|
2017-04-13 17:04:42 +00:00
|
|
|
|
2019-10-07 17:43:47 +00:00
|
|
|
def __init__(self, device, api, gateway_id):
|
2017-04-13 17:04:42 +00:00
|
|
|
"""Initialize a Light."""
|
2019-10-07 17:43:47 +00:00
|
|
|
super().__init__(device, api, gateway_id)
|
|
|
|
self._unique_id = f"light-{gateway_id}-{device.id}"
|
2018-03-18 22:00:29 +00:00
|
|
|
self._hs_color = None
|
2019-10-09 19:56:16 +00:00
|
|
|
|
|
|
|
# Calculate supported features
|
|
|
|
_features = SUPPORTED_LIGHT_FEATURES
|
|
|
|
if device.light_control.can_set_dimmer:
|
|
|
|
_features |= SUPPORT_BRIGHTNESS
|
|
|
|
if device.light_control.can_set_color:
|
|
|
|
_features |= SUPPORT_COLOR
|
|
|
|
if device.light_control.can_set_temp:
|
|
|
|
_features |= SUPPORT_COLOR_TEMP
|
|
|
|
self._features = _features
|
2017-08-31 04:19:06 +00:00
|
|
|
|
2019-10-07 17:43:47 +00:00
|
|
|
self._refresh(device)
|
2018-09-21 12:47:52 +00:00
|
|
|
|
|
|
|
@property
|
2017-08-31 04:19:06 +00:00
|
|
|
def min_mireds(self):
|
|
|
|
"""Return the coldest color_temp that this light supports."""
|
2019-10-07 17:43:47 +00:00
|
|
|
return self._device_control.min_mireds
|
2017-08-31 04:19:06 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def max_mireds(self):
|
|
|
|
"""Return the warmest color_temp that this light supports."""
|
2019-10-07 17:43:47 +00:00
|
|
|
return self._device_control.max_mireds
|
2017-10-05 16:05:38 +00:00
|
|
|
|
2017-04-13 17:04:42 +00:00
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
2017-04-19 16:15:39 +00:00
|
|
|
return self._features
|
2017-04-13 17:04:42 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if light is on."""
|
2019-10-07 17:43:47 +00:00
|
|
|
return self._device_data.state
|
2017-04-13 17:04:42 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def brightness(self):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Return the brightness of the light."""
|
2019-10-07 17:43:47 +00:00
|
|
|
return self._device_data.dimmer
|
2017-04-13 17:04:42 +00:00
|
|
|
|
2017-04-18 15:26:59 +00:00
|
|
|
@property
|
|
|
|
def color_temp(self):
|
2018-03-28 22:50:09 +00:00
|
|
|
"""Return the color temp value in mireds."""
|
2019-10-07 17:43:47 +00:00
|
|
|
return self._device_data.color_temp
|
2017-04-18 15:26:59 +00:00
|
|
|
|
2017-04-16 18:35:52 +00:00
|
|
|
@property
|
2018-03-18 22:00:29 +00:00
|
|
|
def hs_color(self):
|
|
|
|
"""HS color of the light."""
|
2019-10-07 17:43:47 +00:00
|
|
|
if self._device_control.can_set_color:
|
|
|
|
hsbxy = self._device_data.hsb_xy_color
|
|
|
|
hue = hsbxy[0] / (self._device_control.max_hue / 360)
|
|
|
|
sat = hsbxy[1] / (self._device_control.max_saturation / 100)
|
2018-03-28 22:50:09 +00:00
|
|
|
if hue is not None and sat is not None:
|
|
|
|
return hue, sat
|
|
|
|
|
|
|
|
async def async_turn_off(self, **kwargs):
|
2017-04-13 17:04:42 +00:00
|
|
|
"""Instruct the light to turn off."""
|
2018-06-13 05:17:52 +00:00
|
|
|
# This allows transitioning to off, but resets the brightness
|
|
|
|
# to 1 for the next set_state(True) command
|
|
|
|
transition_time = None
|
|
|
|
if ATTR_TRANSITION in kwargs:
|
|
|
|
transition_time = int(kwargs[ATTR_TRANSITION]) * 10
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
dimmer_data = {ATTR_DIMMER: 0, ATTR_TRANSITION_TIME: transition_time}
|
2019-10-07 17:43:47 +00:00
|
|
|
await self._api(self._device_control.set_dimmer(**dimmer_data))
|
2018-06-13 05:17:52 +00:00
|
|
|
else:
|
2019-10-07 17:43:47 +00:00
|
|
|
await self._api(self._device_control.set_state(False))
|
2017-10-05 16:05:38 +00:00
|
|
|
|
2018-03-28 22:50:09 +00:00
|
|
|
async def async_turn_on(self, **kwargs):
|
|
|
|
"""Instruct the light to turn on."""
|
|
|
|
transition_time = None
|
2017-10-05 16:05:38 +00:00
|
|
|
if ATTR_TRANSITION in kwargs:
|
2018-03-28 22:50:09 +00:00
|
|
|
transition_time = int(kwargs[ATTR_TRANSITION]) * 10
|
|
|
|
|
2018-06-13 05:17:52 +00:00
|
|
|
dimmer_command = None
|
|
|
|
if ATTR_BRIGHTNESS in kwargs:
|
|
|
|
brightness = kwargs[ATTR_BRIGHTNESS]
|
2018-03-28 22:50:09 +00:00
|
|
|
if brightness > 254:
|
|
|
|
brightness = 254
|
2019-07-31 19:25:30 +00:00
|
|
|
dimmer_data = {
|
|
|
|
ATTR_DIMMER: brightness,
|
|
|
|
ATTR_TRANSITION_TIME: transition_time,
|
|
|
|
}
|
2019-10-07 17:43:47 +00:00
|
|
|
dimmer_command = self._device_control.set_dimmer(**dimmer_data)
|
2018-06-13 05:17:52 +00:00
|
|
|
transition_time = None
|
|
|
|
else:
|
2019-10-07 17:43:47 +00:00
|
|
|
dimmer_command = self._device_control.set_state(True)
|
2018-03-28 22:50:09 +00:00
|
|
|
|
2018-06-13 05:17:52 +00:00
|
|
|
color_command = None
|
2019-10-07 17:43:47 +00:00
|
|
|
if ATTR_HS_COLOR in kwargs and self._device_control.can_set_color:
|
|
|
|
hue = int(kwargs[ATTR_HS_COLOR][0] * (self._device_control.max_hue / 360))
|
2019-07-31 19:25:30 +00:00
|
|
|
sat = int(
|
2019-10-07 17:43:47 +00:00
|
|
|
kwargs[ATTR_HS_COLOR][1] * (self._device_control.max_saturation / 100)
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
color_data = {
|
|
|
|
ATTR_HUE: hue,
|
|
|
|
ATTR_SAT: sat,
|
|
|
|
ATTR_TRANSITION_TIME: transition_time,
|
|
|
|
}
|
2019-10-07 17:43:47 +00:00
|
|
|
color_command = self._device_control.set_hsb(**color_data)
|
2018-06-13 05:17:52 +00:00
|
|
|
transition_time = None
|
|
|
|
|
|
|
|
temp_command = None
|
2019-07-31 19:25:30 +00:00
|
|
|
if ATTR_COLOR_TEMP in kwargs and (
|
2019-10-07 17:43:47 +00:00
|
|
|
self._device_control.can_set_temp or self._device_control.can_set_color
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
2018-03-28 22:50:09 +00:00
|
|
|
temp = kwargs[ATTR_COLOR_TEMP]
|
2018-03-29 19:39:56 +00:00
|
|
|
# White Spectrum bulb
|
2019-10-07 17:43:47 +00:00
|
|
|
if self._device_control.can_set_temp:
|
2018-06-13 05:17:52 +00:00
|
|
|
if temp > self.max_mireds:
|
|
|
|
temp = self.max_mireds
|
|
|
|
elif temp < self.min_mireds:
|
|
|
|
temp = self.min_mireds
|
2019-07-31 19:25:30 +00:00
|
|
|
temp_data = {
|
|
|
|
ATTR_COLOR_TEMP: temp,
|
|
|
|
ATTR_TRANSITION_TIME: transition_time,
|
|
|
|
}
|
2019-10-07 17:43:47 +00:00
|
|
|
temp_command = self._device_control.set_color_temp(**temp_data)
|
2018-06-13 05:17:52 +00:00
|
|
|
transition_time = None
|
2018-03-29 19:39:56 +00:00
|
|
|
# Color bulb (CWS)
|
|
|
|
# color_temp needs to be set with hue/saturation
|
2019-10-07 17:43:47 +00:00
|
|
|
elif self._device_control.can_set_color:
|
2018-03-29 19:39:56 +00:00
|
|
|
temp_k = color_util.color_temperature_mired_to_kelvin(temp)
|
|
|
|
hs_color = color_util.color_temperature_to_hs(temp_k)
|
2019-10-07 17:43:47 +00:00
|
|
|
hue = int(hs_color[0] * (self._device_control.max_hue / 360))
|
|
|
|
sat = int(hs_color[1] * (self._device_control.max_saturation / 100))
|
2019-07-31 19:25:30 +00:00
|
|
|
color_data = {
|
|
|
|
ATTR_HUE: hue,
|
|
|
|
ATTR_SAT: sat,
|
|
|
|
ATTR_TRANSITION_TIME: transition_time,
|
|
|
|
}
|
2019-10-07 17:43:47 +00:00
|
|
|
color_command = self._device_control.set_hsb(**color_data)
|
2018-06-13 05:17:52 +00:00
|
|
|
transition_time = None
|
|
|
|
|
|
|
|
# HSB can always be set, but color temp + brightness is bulb dependant
|
|
|
|
command = dimmer_command
|
|
|
|
if command is not None:
|
|
|
|
command += color_command
|
2017-10-05 16:05:38 +00:00
|
|
|
else:
|
2018-06-13 05:17:52 +00:00
|
|
|
command = color_command
|
|
|
|
|
2019-10-07 17:43:47 +00:00
|
|
|
if self._device_control.can_combine_commands:
|
2018-06-13 05:17:52 +00:00
|
|
|
await self._api(command + temp_command)
|
|
|
|
else:
|
|
|
|
if temp_command is not None:
|
|
|
|
await self._api(temp_command)
|
|
|
|
if command is not None:
|
|
|
|
await self._api(command)
|
2017-10-05 16:05:38 +00:00
|
|
|
|
2019-10-07 17:43:47 +00:00
|
|
|
def _refresh(self, device):
|
2017-10-05 16:05:38 +00:00
|
|
|
"""Refresh the light data."""
|
2019-10-07 17:43:47 +00:00
|
|
|
super()._refresh(device)
|
2017-10-05 16:05:38 +00:00
|
|
|
|
|
|
|
# Caching of LightControl and light object
|
2019-10-07 17:43:47 +00:00
|
|
|
self._device_control = device.light_control
|
|
|
|
self._device_data = device.light_control.lights[0]
|