2019-02-13 20:21:14 +00:00
|
|
|
"""Support for deCONZ lights."""
|
2018-01-01 16:08:13 +00:00
|
|
|
from homeassistant.components.light import (
|
2019-07-31 19:25:30 +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
|
|
|
|
2019-07-25 16:39:38 +00:00
|
|
|
from .const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
COVER_TYPES,
|
|
|
|
DOMAIN as DECONZ_DOMAIN,
|
|
|
|
NEW_GROUP,
|
|
|
|
NEW_LIGHT,
|
|
|
|
SWITCH_TYPES,
|
|
|
|
)
|
2019-01-16 07:33:04 +00:00
|
|
|
from .deconz_device import DeconzDevice
|
2019-09-05 23:38:00 +00:00
|
|
|
from .gateway import get_gateway_from_config_entry, DeconzEntityHandler
|
2019-01-15 18:29:56 +00:00
|
|
|
|
2018-01-01 16:08:13 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2019-05-27 04:56:00 +00:00
|
|
|
"""Old way of setting up deCONZ platforms."""
|
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
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
2018-05-05 14:11:00 +00:00
|
|
|
"""Set up the deCONZ lights and groups from a config entry."""
|
2019-04-05 00:48:24 +00:00
|
|
|
gateway = get_gateway_from_config_entry(hass, config_entry)
|
2018-11-05 15:21:44 +00:00
|
|
|
|
2019-09-05 23:38:00 +00:00
|
|
|
entity_handler = DeconzEntityHandler(gateway)
|
|
|
|
|
2018-05-05 14:11:00 +00:00
|
|
|
@callback
|
|
|
|
def async_add_light(lights):
|
|
|
|
"""Add light from deCONZ."""
|
|
|
|
entities = []
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2018-05-05 14:11:00 +00:00
|
|
|
for light in lights:
|
2018-09-21 17:59:20 +00:00
|
|
|
if light.type not in COVER_TYPES + SWITCH_TYPES:
|
2018-11-05 15:21:44 +00:00
|
|
|
entities.append(DeconzLight(light, gateway))
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities(entities, True)
|
2018-06-15 18:31:22 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
gateway.listeners.append(
|
|
|
|
async_dispatcher_connect(
|
2019-09-05 23:38:00 +00:00
|
|
|
hass, gateway.async_signal_new_device(NEW_LIGHT), async_add_light
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
)
|
2018-05-05 14:11:00 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_add_group(groups):
|
|
|
|
"""Add group from deCONZ."""
|
|
|
|
entities = []
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2018-05-05 14:11:00 +00:00
|
|
|
for group in groups:
|
2019-09-05 23:38:00 +00:00
|
|
|
if group.lights:
|
|
|
|
new_group = DeconzGroup(group, gateway)
|
|
|
|
entity_handler.add_entity(new_group)
|
|
|
|
entities.append(new_group)
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities(entities, True)
|
2018-06-15 18:31:22 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
gateway.listeners.append(
|
|
|
|
async_dispatcher_connect(
|
2019-09-05 23:38:00 +00:00
|
|
|
hass, gateway.async_signal_new_device(NEW_GROUP), async_add_group
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
)
|
2018-05-05 14:11:00 +00:00
|
|
|
|
2018-11-05 15:21:44 +00:00
|
|
|
async_add_light(gateway.api.lights.values())
|
|
|
|
async_add_group(gateway.api.groups.values())
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
|
2019-01-16 07:33:04 +00:00
|
|
|
class DeconzLight(DeconzDevice, Light):
|
2018-01-01 16:08:13 +00:00
|
|
|
"""Representation of a deCONZ light."""
|
|
|
|
|
2019-01-16 07:33:04 +00:00
|
|
|
def __init__(self, device, gateway):
|
2019-07-25 16:39:38 +00:00
|
|
|
"""Set up light."""
|
2019-01-16 07:33:04 +00:00
|
|
|
super().__init__(device, gateway)
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
self._features = SUPPORT_BRIGHTNESS
|
|
|
|
self._features |= SUPPORT_FLASH
|
|
|
|
self._features |= SUPPORT_TRANSITION
|
|
|
|
|
2019-01-16 07:33:04 +00:00
|
|
|
if self._device.ct is not None:
|
2018-01-01 16:08:13 +00:00
|
|
|
self._features |= SUPPORT_COLOR_TEMP
|
|
|
|
|
2019-01-16 07:33:04 +00:00
|
|
|
if self._device.xy is not None:
|
2018-03-18 22:00:29 +00:00
|
|
|
self._features |= SUPPORT_COLOR
|
2018-01-01 16:08:13 +00:00
|
|
|
|
2019-01-16 07:33:04 +00:00
|
|
|
if self._device.effect is not None:
|
2018-01-01 16:08:13 +00:00
|
|
|
self._features |= SUPPORT_EFFECT
|
|
|
|
|
|
|
|
@property
|
|
|
|
def brightness(self):
|
|
|
|
"""Return the brightness of this light between 0..255."""
|
2019-01-16 07:33:04 +00:00
|
|
|
return self._device.brightness
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def effect_list(self):
|
|
|
|
"""Return the list of supported effects."""
|
|
|
|
return [EFFECT_COLORLOOP]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def color_temp(self):
|
|
|
|
"""Return the CT color value."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if self._device.colormode != "ct":
|
2018-08-03 11:56:54 +00:00
|
|
|
return None
|
|
|
|
|
2019-01-16 07:33:04 +00:00
|
|
|
return self._device.ct
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
@property
|
2018-06-24 21:48:59 +00:00
|
|
|
def hs_color(self):
|
|
|
|
"""Return the hs color value."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if self._device.colormode in ("xy", "hs") and self._device.xy:
|
2019-01-16 07:33:04 +00:00
|
|
|
return color_util.color_xy_to_hs(*self._device.xy)
|
2018-06-24 21:48:59 +00:00
|
|
|
return None
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if light is on."""
|
2019-01-16 07:33:04 +00:00
|
|
|
return self._device.state
|
2018-01-30 22:42:24 +00:00
|
|
|
|
2018-01-01 16:08:13 +00:00
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
|
|
|
return self._features
|
|
|
|
|
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."""
|
2019-07-31 19:25:30 +00:00
|
|
|
data = {"on": True}
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
if ATTR_COLOR_TEMP in kwargs:
|
2019-07-31 19:25:30 +00:00
|
|
|
data["ct"] = kwargs[ATTR_COLOR_TEMP]
|
2018-01-01 16:08:13 +00:00
|
|
|
|
2018-03-18 22:00:29 +00:00
|
|
|
if ATTR_HS_COLOR in kwargs:
|
2019-07-31 19:25:30 +00:00
|
|
|
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:
|
2019-07-31 19:25:30 +00:00
|
|
|
data["bri"] = kwargs[ATTR_BRIGHTNESS]
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
if ATTR_TRANSITION in kwargs:
|
2019-07-31 19:25:30 +00:00
|
|
|
data["transitiontime"] = int(kwargs[ATTR_TRANSITION] * 10)
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
if ATTR_FLASH in kwargs:
|
|
|
|
if kwargs[ATTR_FLASH] == FLASH_SHORT:
|
2019-07-31 19:25:30 +00:00
|
|
|
data["alert"] = "select"
|
|
|
|
del data["on"]
|
2018-01-01 16:08:13 +00:00
|
|
|
elif kwargs[ATTR_FLASH] == FLASH_LONG:
|
2019-07-31 19:25:30 +00:00
|
|
|
data["alert"] = "lselect"
|
|
|
|
del data["on"]
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
if ATTR_EFFECT in kwargs:
|
|
|
|
if kwargs[ATTR_EFFECT] == EFFECT_COLORLOOP:
|
2019-07-31 19:25:30 +00:00
|
|
|
data["effect"] = "colorloop"
|
2018-01-01 16:08:13 +00:00
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
data["effect"] = "none"
|
2018-01-01 16:08:13 +00:00
|
|
|
|
2019-01-16 07:33:04 +00:00
|
|
|
await self._device.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."""
|
2019-07-31 19:25:30 +00:00
|
|
|
data = {"on": False}
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
if ATTR_TRANSITION in kwargs:
|
2019-07-31 19:25:30 +00:00
|
|
|
data["bri"] = 0
|
|
|
|
data["transitiontime"] = int(kwargs[ATTR_TRANSITION] * 10)
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
if ATTR_FLASH in kwargs:
|
|
|
|
if kwargs[ATTR_FLASH] == FLASH_SHORT:
|
2019-07-31 19:25:30 +00:00
|
|
|
data["alert"] = "select"
|
|
|
|
del data["on"]
|
2018-01-01 16:08:13 +00:00
|
|
|
elif kwargs[ATTR_FLASH] == FLASH_LONG:
|
2019-07-31 19:25:30 +00:00
|
|
|
data["alert"] = "lselect"
|
|
|
|
del data["on"]
|
2018-01-01 16:08:13 +00:00
|
|
|
|
2019-01-16 07:33:04 +00:00
|
|
|
await self._device.async_set_state(data)
|
2018-08-01 09:03:08 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the device state attributes."""
|
|
|
|
attributes = {}
|
2019-07-31 19:25:30 +00:00
|
|
|
attributes["is_deconz_group"] = self._device.type == "LightGroup"
|
2019-05-03 15:55:42 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if self._device.type == "LightGroup":
|
|
|
|
attributes["all_on"] = self._device.all_on
|
2019-05-03 15:55:42 +00:00
|
|
|
|
2018-08-01 09:03:08 +00:00
|
|
|
return attributes
|
2019-07-25 16:39:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DeconzGroup(DeconzLight):
|
|
|
|
"""Representation of a deCONZ group."""
|
|
|
|
|
|
|
|
def __init__(self, device, gateway):
|
|
|
|
"""Set up group and create an unique id."""
|
|
|
|
super().__init__(device, gateway)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
self._unique_id = "{}-{}".format(
|
|
|
|
self.gateway.api.config.bridgeid, self._device.deconz_id
|
|
|
|
)
|
2019-07-25 16:39:38 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique identifier for this device."""
|
|
|
|
return self._unique_id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return a device description for device registry."""
|
|
|
|
bridgeid = self.gateway.api.config.bridgeid
|
|
|
|
|
|
|
|
return {
|
2019-07-31 19:25:30 +00:00
|
|
|
"identifiers": {(DECONZ_DOMAIN, self.unique_id)},
|
|
|
|
"manufacturer": "Dresden Elektronik",
|
|
|
|
"model": "deCONZ group",
|
|
|
|
"name": self._device.name,
|
|
|
|
"via_device": (DECONZ_DOMAIN, bridgeid),
|
2019-07-25 16:39:38 +00:00
|
|
|
}
|