2018-01-01 16:08:13 +00:00
|
|
|
"""
|
|
|
|
Support for deCONZ light.
|
|
|
|
|
|
|
|
For more details about this component, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/light.deconz/
|
|
|
|
"""
|
2018-08-01 09:03:08 +00:00
|
|
|
from homeassistant.components.deconz.const import (
|
|
|
|
CONF_ALLOW_DECONZ_GROUPS, DOMAIN as DATA_DECONZ,
|
|
|
|
DATA_DECONZ_ID, DATA_DECONZ_UNSUB, SWITCH_TYPES)
|
2018-01-01 16:08:13 +00:00
|
|
|
from homeassistant.components.light import (
|
2018-03-18 22:00:29 +00:00
|
|
|
ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_EFFECT, ATTR_FLASH, ATTR_HS_COLOR,
|
|
|
|
ATTR_TRANSITION, EFFECT_COLORLOOP, FLASH_LONG, FLASH_SHORT,
|
|
|
|
SUPPORT_BRIGHTNESS, SUPPORT_COLOR, SUPPORT_COLOR_TEMP, SUPPORT_EFFECT,
|
|
|
|
SUPPORT_FLASH, SUPPORT_TRANSITION, Light)
|
2018-01-01 16:08:13 +00:00
|
|
|
from homeassistant.core import callback
|
2018-05-05 14:11:00 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2018-03-18 22:00:29 +00:00
|
|
|
import homeassistant.util.color as color_util
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
DEPENDENCIES = ['deconz']
|
|
|
|
|
|
|
|
|
2018-03-13 07:47:45 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_devices,
|
|
|
|
discovery_info=None):
|
2018-05-05 14:11:00 +00:00
|
|
|
"""Old way of setting up deCONZ lights and group."""
|
2018-04-23 16:00:16 +00:00
|
|
|
pass
|
2018-01-01 16:08:13 +00:00
|
|
|
|
2018-04-23 16:00:16 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_devices):
|
2018-05-05 14:11:00 +00:00
|
|
|
"""Set up the deCONZ lights and groups from a config entry."""
|
|
|
|
@callback
|
|
|
|
def async_add_light(lights):
|
|
|
|
"""Add light from deCONZ."""
|
|
|
|
entities = []
|
|
|
|
for light in lights:
|
2018-08-01 09:03:08 +00:00
|
|
|
if light.type not in SWITCH_TYPES:
|
|
|
|
entities.append(DeconzLight(light))
|
2018-05-05 14:11:00 +00:00
|
|
|
async_add_devices(entities, True)
|
2018-06-15 18:31:22 +00:00
|
|
|
|
2018-05-05 14:11:00 +00:00
|
|
|
hass.data[DATA_DECONZ_UNSUB].append(
|
|
|
|
async_dispatcher_connect(hass, 'deconz_new_light', async_add_light))
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_add_group(groups):
|
|
|
|
"""Add group from deCONZ."""
|
|
|
|
entities = []
|
2018-06-15 18:31:22 +00:00
|
|
|
allow_group = config_entry.data.get(CONF_ALLOW_DECONZ_GROUPS, True)
|
2018-05-05 14:11:00 +00:00
|
|
|
for group in groups:
|
2018-06-15 18:31:22 +00:00
|
|
|
if group.lights and allow_group:
|
2018-05-05 14:11:00 +00:00
|
|
|
entities.append(DeconzLight(group))
|
|
|
|
async_add_devices(entities, True)
|
2018-06-15 18:31:22 +00:00
|
|
|
|
2018-05-05 14:11:00 +00:00
|
|
|
hass.data[DATA_DECONZ_UNSUB].append(
|
|
|
|
async_dispatcher_connect(hass, 'deconz_new_group', async_add_group))
|
|
|
|
|
|
|
|
async_add_light(hass.data[DATA_DECONZ].lights.values())
|
|
|
|
async_add_group(hass.data[DATA_DECONZ].groups.values())
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DeconzLight(Light):
|
|
|
|
"""Representation of a deCONZ light."""
|
|
|
|
|
|
|
|
def __init__(self, light):
|
2018-01-19 06:36:29 +00:00
|
|
|
"""Set up light and add update callback to get data from websocket."""
|
2018-01-01 16:08:13 +00:00
|
|
|
self._light = light
|
|
|
|
|
|
|
|
self._features = SUPPORT_BRIGHTNESS
|
|
|
|
self._features |= SUPPORT_FLASH
|
|
|
|
self._features |= SUPPORT_TRANSITION
|
|
|
|
|
|
|
|
if self._light.ct is not None:
|
|
|
|
self._features |= SUPPORT_COLOR_TEMP
|
|
|
|
|
|
|
|
if self._light.xy is not None:
|
2018-03-18 22:00:29 +00:00
|
|
|
self._features |= SUPPORT_COLOR
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
if self._light.effect is not None:
|
|
|
|
self._features |= SUPPORT_EFFECT
|
|
|
|
|
2018-03-13 07:47:45 +00:00
|
|
|
async def async_added_to_hass(self):
|
2018-01-01 16:08:13 +00:00
|
|
|
"""Subscribe to lights events."""
|
|
|
|
self._light.register_async_callback(self.async_update_callback)
|
2018-02-14 00:23:04 +00:00
|
|
|
self.hass.data[DATA_DECONZ_ID][self.entity_id] = self._light.deconz_id
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_update_callback(self, reason):
|
|
|
|
"""Update the light's state."""
|
|
|
|
self.async_schedule_update_ha_state()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def brightness(self):
|
|
|
|
"""Return the brightness of this light between 0..255."""
|
|
|
|
return self._light.brightness
|
|
|
|
|
|
|
|
@property
|
|
|
|
def effect_list(self):
|
|
|
|
"""Return the list of supported effects."""
|
|
|
|
return [EFFECT_COLORLOOP]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def color_temp(self):
|
|
|
|
"""Return the CT color value."""
|
2018-08-03 11:56:54 +00:00
|
|
|
if self._light.colormode != 'ct':
|
|
|
|
return None
|
|
|
|
|
2018-01-01 16:08:13 +00:00
|
|
|
return self._light.ct
|
|
|
|
|
|
|
|
@property
|
2018-06-24 21:48:59 +00:00
|
|
|
def hs_color(self):
|
|
|
|
"""Return the hs color value."""
|
|
|
|
if self._light.colormode in ('xy', 'hs') and self._light.xy:
|
|
|
|
return color_util.color_xy_to_hs(*self._light.xy)
|
|
|
|
return None
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if light is on."""
|
|
|
|
return self._light.state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the light."""
|
|
|
|
return self._light.name
|
|
|
|
|
2018-01-30 22:42:24 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique identifier for this light."""
|
|
|
|
return self._light.uniqueid
|
|
|
|
|
2018-01-01 16:08:13 +00:00
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
|
|
|
return self._features
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Return True if light is available."""
|
|
|
|
return self._light.reachable
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""No polling needed."""
|
|
|
|
return False
|
|
|
|
|
2018-03-13 07:47:45 +00:00
|
|
|
async def async_turn_on(self, **kwargs):
|
2018-01-01 16:08:13 +00:00
|
|
|
"""Turn on light."""
|
|
|
|
data = {'on': True}
|
|
|
|
|
|
|
|
if ATTR_COLOR_TEMP in kwargs:
|
|
|
|
data['ct'] = kwargs[ATTR_COLOR_TEMP]
|
|
|
|
|
2018-03-18 22:00:29 +00:00
|
|
|
if ATTR_HS_COLOR in kwargs:
|
|
|
|
data['xy'] = color_util.color_hs_to_xy(*kwargs[ATTR_HS_COLOR])
|
2018-02-03 16:08:00 +00:00
|
|
|
|
2018-01-01 16:08:13 +00:00
|
|
|
if ATTR_BRIGHTNESS in kwargs:
|
|
|
|
data['bri'] = kwargs[ATTR_BRIGHTNESS]
|
|
|
|
|
|
|
|
if ATTR_TRANSITION in kwargs:
|
|
|
|
data['transitiontime'] = int(kwargs[ATTR_TRANSITION]) * 10
|
|
|
|
|
|
|
|
if ATTR_FLASH in kwargs:
|
|
|
|
if kwargs[ATTR_FLASH] == FLASH_SHORT:
|
|
|
|
data['alert'] = 'select'
|
|
|
|
del data['on']
|
|
|
|
elif kwargs[ATTR_FLASH] == FLASH_LONG:
|
|
|
|
data['alert'] = 'lselect'
|
|
|
|
del data['on']
|
|
|
|
|
|
|
|
if ATTR_EFFECT in kwargs:
|
|
|
|
if kwargs[ATTR_EFFECT] == EFFECT_COLORLOOP:
|
|
|
|
data['effect'] = 'colorloop'
|
|
|
|
else:
|
|
|
|
data['effect'] = 'none'
|
|
|
|
|
2018-03-13 07:47:45 +00:00
|
|
|
await self._light.async_set_state(data)
|
2018-01-01 16:08:13 +00:00
|
|
|
|
2018-03-13 07:47:45 +00:00
|
|
|
async def async_turn_off(self, **kwargs):
|
2018-01-01 16:08:13 +00:00
|
|
|
"""Turn off light."""
|
|
|
|
data = {'on': False}
|
|
|
|
|
|
|
|
if ATTR_TRANSITION in kwargs:
|
2018-07-01 10:32:48 +00:00
|
|
|
data['bri'] = 0
|
2018-01-01 16:08:13 +00:00
|
|
|
data['transitiontime'] = int(kwargs[ATTR_TRANSITION]) * 10
|
|
|
|
|
|
|
|
if ATTR_FLASH in kwargs:
|
|
|
|
if kwargs[ATTR_FLASH] == FLASH_SHORT:
|
|
|
|
data['alert'] = 'select'
|
|
|
|
del data['on']
|
|
|
|
elif kwargs[ATTR_FLASH] == FLASH_LONG:
|
|
|
|
data['alert'] = 'lselect'
|
|
|
|
del data['on']
|
|
|
|
|
2018-03-13 07:47:45 +00:00
|
|
|
await self._light.async_set_state(data)
|
2018-08-01 09:03:08 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the device state attributes."""
|
|
|
|
attributes = {}
|
|
|
|
attributes['is_deconz_group'] = self._light.type == 'LightGroup'
|
|
|
|
if self._light.type == 'LightGroup':
|
|
|
|
attributes['all_on'] = self._light.all_on
|
|
|
|
return attributes
|