2019-02-14 04:35:12 +00:00
|
|
|
"""Support for KNX/IP lights."""
|
2019-02-08 07:28:52 +00:00
|
|
|
from enum import Enum
|
2018-01-21 06:35:38 +00:00
|
|
|
|
2017-04-30 04:20:46 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-02-19 13:09:06 +00:00
|
|
|
from homeassistant.components.knx import ATTR_DISCOVER_DEVICES, DATA_KNX
|
2018-01-21 06:35:38 +00:00
|
|
|
from homeassistant.components.light import (
|
2019-02-08 07:28:52 +00:00
|
|
|
ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_HS_COLOR, PLATFORM_SCHEMA,
|
2019-02-19 13:09:06 +00:00
|
|
|
SUPPORT_BRIGHTNESS, SUPPORT_COLOR, SUPPORT_COLOR_TEMP, Light)
|
|
|
|
from homeassistant.const import CONF_ADDRESS, CONF_NAME
|
2017-09-07 07:11:55 +00:00
|
|
|
from homeassistant.core import callback
|
2017-04-30 04:20:46 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2018-03-18 22:00:29 +00:00
|
|
|
import homeassistant.util.color as color_util
|
2017-04-30 04:20:46 +00:00
|
|
|
|
|
|
|
CONF_STATE_ADDRESS = 'state_address'
|
2017-07-07 05:24:25 +00:00
|
|
|
CONF_BRIGHTNESS_ADDRESS = 'brightness_address'
|
|
|
|
CONF_BRIGHTNESS_STATE_ADDRESS = 'brightness_state_address'
|
2018-02-15 06:06:36 +00:00
|
|
|
CONF_COLOR_ADDRESS = 'color_address'
|
|
|
|
CONF_COLOR_STATE_ADDRESS = 'color_state_address'
|
2019-02-08 07:28:52 +00:00
|
|
|
CONF_COLOR_TEMP_ADDRESS = 'color_temperature_address'
|
|
|
|
CONF_COLOR_TEMP_STATE_ADDRESS = 'color_temperature_state_address'
|
|
|
|
CONF_COLOR_TEMP_MODE = 'color_temperature_mode'
|
|
|
|
CONF_MIN_KELVIN = 'min_kelvin'
|
|
|
|
CONF_MAX_KELVIN = 'max_kelvin'
|
2017-07-07 05:24:25 +00:00
|
|
|
|
2017-04-30 04:20:46 +00:00
|
|
|
DEFAULT_NAME = 'KNX Light'
|
2019-02-08 07:28:52 +00:00
|
|
|
DEFAULT_COLOR = [255, 255, 255]
|
|
|
|
DEFAULT_BRIGHTNESS = 255
|
|
|
|
DEFAULT_COLOR_TEMP_MODE = 'absolute'
|
|
|
|
DEFAULT_MIN_KELVIN = 2700 # 370 mireds
|
|
|
|
DEFAULT_MAX_KELVIN = 6000 # 166 mireds
|
2017-04-30 04:20:46 +00:00
|
|
|
DEPENDENCIES = ['knx']
|
|
|
|
|
2019-02-08 07:28:52 +00:00
|
|
|
|
|
|
|
class ColorTempModes(Enum):
|
|
|
|
"""Color temperature modes for config validation."""
|
|
|
|
|
|
|
|
absolute = "DPT-7.600"
|
|
|
|
relative = "DPT-5.001"
|
|
|
|
|
|
|
|
|
2017-04-30 04:20:46 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_ADDRESS): cv.string,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_STATE_ADDRESS): cv.string,
|
2017-07-07 05:24:25 +00:00
|
|
|
vol.Optional(CONF_BRIGHTNESS_ADDRESS): cv.string,
|
|
|
|
vol.Optional(CONF_BRIGHTNESS_STATE_ADDRESS): cv.string,
|
2018-02-15 06:06:36 +00:00
|
|
|
vol.Optional(CONF_COLOR_ADDRESS): cv.string,
|
|
|
|
vol.Optional(CONF_COLOR_STATE_ADDRESS): cv.string,
|
2019-02-08 07:28:52 +00:00
|
|
|
vol.Optional(CONF_COLOR_TEMP_ADDRESS): cv.string,
|
|
|
|
vol.Optional(CONF_COLOR_TEMP_STATE_ADDRESS): cv.string,
|
|
|
|
vol.Optional(CONF_COLOR_TEMP_MODE, default=DEFAULT_COLOR_TEMP_MODE):
|
|
|
|
cv.enum(ColorTempModes),
|
|
|
|
vol.Optional(CONF_MIN_KELVIN, default=DEFAULT_MIN_KELVIN):
|
|
|
|
vol.All(vol.Coerce(int), vol.Range(min=1)),
|
|
|
|
vol.Optional(CONF_MAX_KELVIN, default=DEFAULT_MAX_KELVIN):
|
|
|
|
vol.All(vol.Coerce(int), vol.Range(min=1)),
|
2017-04-30 04:20:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities,
|
2018-02-24 18:24:33 +00:00
|
|
|
discovery_info=None):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Set up lights for KNX platform."""
|
2017-09-07 07:11:55 +00:00
|
|
|
if discovery_info is not None:
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities_discovery(hass, discovery_info, async_add_entities)
|
2017-09-07 07:11:55 +00:00
|
|
|
else:
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities_config(hass, config, async_add_entities)
|
2017-09-07 07:11:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
@callback
|
2018-08-24 14:37:30 +00:00
|
|
|
def async_add_entities_discovery(hass, discovery_info, async_add_entities):
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Set up lights for KNX platform configured via xknx.yaml."""
|
|
|
|
entities = []
|
|
|
|
for device_name in discovery_info[ATTR_DISCOVER_DEVICES]:
|
|
|
|
device = hass.data[DATA_KNX].xknx.devices[device_name]
|
2018-10-15 01:29:36 +00:00
|
|
|
entities.append(KNXLight(device))
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities(entities)
|
2017-09-07 07:11:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
@callback
|
2018-08-24 14:37:30 +00:00
|
|
|
def async_add_entities_config(hass, config, async_add_entities):
|
2018-01-29 22:37:19 +00:00
|
|
|
"""Set up light for KNX platform configured within platform."""
|
2017-09-07 07:11:55 +00:00
|
|
|
import xknx
|
2019-02-08 07:28:52 +00:00
|
|
|
|
|
|
|
group_address_tunable_white = None
|
|
|
|
group_address_tunable_white_state = None
|
|
|
|
group_address_color_temp = None
|
|
|
|
group_address_color_temp_state = None
|
|
|
|
if config[CONF_COLOR_TEMP_MODE] == ColorTempModes.absolute:
|
|
|
|
group_address_color_temp = config.get(CONF_COLOR_TEMP_ADDRESS)
|
|
|
|
group_address_color_temp_state = \
|
|
|
|
config.get(CONF_COLOR_TEMP_STATE_ADDRESS)
|
|
|
|
elif config[CONF_COLOR_TEMP_MODE] == ColorTempModes.relative:
|
|
|
|
group_address_tunable_white = config.get(CONF_COLOR_TEMP_ADDRESS)
|
|
|
|
group_address_tunable_white_state = \
|
|
|
|
config.get(CONF_COLOR_TEMP_STATE_ADDRESS)
|
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
light = xknx.devices.Light(
|
|
|
|
hass.data[DATA_KNX].xknx,
|
2019-02-08 07:28:52 +00:00
|
|
|
name=config[CONF_NAME],
|
|
|
|
group_address_switch=config[CONF_ADDRESS],
|
2017-09-07 07:11:55 +00:00
|
|
|
group_address_switch_state=config.get(CONF_STATE_ADDRESS),
|
|
|
|
group_address_brightness=config.get(CONF_BRIGHTNESS_ADDRESS),
|
|
|
|
group_address_brightness_state=config.get(
|
2018-02-15 06:06:36 +00:00
|
|
|
CONF_BRIGHTNESS_STATE_ADDRESS),
|
|
|
|
group_address_color=config.get(CONF_COLOR_ADDRESS),
|
2019-02-08 07:28:52 +00:00
|
|
|
group_address_color_state=config.get(CONF_COLOR_STATE_ADDRESS),
|
|
|
|
group_address_tunable_white=group_address_tunable_white,
|
|
|
|
group_address_tunable_white_state=group_address_tunable_white_state,
|
|
|
|
group_address_color_temperature=group_address_color_temp,
|
|
|
|
group_address_color_temperature_state=group_address_color_temp_state,
|
|
|
|
min_kelvin=config[CONF_MIN_KELVIN],
|
|
|
|
max_kelvin=config[CONF_MAX_KELVIN])
|
2017-09-07 07:11:55 +00:00
|
|
|
hass.data[DATA_KNX].xknx.devices.add(light)
|
2018-10-15 01:29:36 +00:00
|
|
|
async_add_entities([KNXLight(light)])
|
2017-09-07 07:11:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
class KNXLight(Light):
|
|
|
|
"""Representation of a KNX light."""
|
|
|
|
|
2018-10-15 01:29:36 +00:00
|
|
|
def __init__(self, device):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Initialize of KNX light."""
|
2018-08-26 19:25:39 +00:00
|
|
|
self.device = device
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2019-02-08 07:28:52 +00:00
|
|
|
self._min_kelvin = device.min_kelvin
|
|
|
|
self._max_kelvin = device.max_kelvin
|
|
|
|
self._min_mireds = \
|
|
|
|
color_util.color_temperature_kelvin_to_mired(self._max_kelvin)
|
|
|
|
self._max_mireds = \
|
|
|
|
color_util.color_temperature_kelvin_to_mired(self._min_kelvin)
|
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
@callback
|
|
|
|
def async_register_callbacks(self):
|
|
|
|
"""Register callbacks to update hass after device was changed."""
|
2018-02-24 18:24:33 +00:00
|
|
|
async def after_update_callback(device):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Call after device was updated."""
|
2018-02-24 18:24:33 +00:00
|
|
|
await self.async_update_ha_state()
|
2018-08-26 19:25:39 +00:00
|
|
|
self.device.register_device_updated_cb(after_update_callback)
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2018-10-15 01:29:36 +00:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Store register state change callback."""
|
|
|
|
self.async_register_callbacks()
|
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the KNX device."""
|
2018-08-26 19:25:39 +00:00
|
|
|
return self.device.name
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2018-01-07 21:39:14 +00:00
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Return True if entity is available."""
|
|
|
|
return self.hass.data[DATA_KNX].connected
|
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""No polling needed within KNX."""
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def brightness(self):
|
|
|
|
"""Return the brightness of this light between 0..255."""
|
2019-02-08 07:28:52 +00:00
|
|
|
if self.device.supports_color:
|
|
|
|
if self.device.current_color is None:
|
|
|
|
return None
|
|
|
|
return max(self.device.current_color)
|
|
|
|
if self.device.supports_brightness:
|
|
|
|
return self.device.current_brightness
|
|
|
|
return None
|
2017-09-07 07:11:55 +00:00
|
|
|
|
|
|
|
@property
|
2018-03-18 22:00:29 +00:00
|
|
|
def hs_color(self):
|
|
|
|
"""Return the HS color value."""
|
2018-08-26 19:25:39 +00:00
|
|
|
if self.device.supports_color:
|
2019-02-08 07:28:52 +00:00
|
|
|
rgb = self.device.current_color
|
|
|
|
if rgb is None:
|
|
|
|
return None
|
|
|
|
return color_util.color_RGB_to_hs(*rgb)
|
2017-09-07 07:11:55 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def color_temp(self):
|
2019-02-08 07:28:52 +00:00
|
|
|
"""Return the color temperature in mireds."""
|
|
|
|
if self.device.supports_color_temperature:
|
|
|
|
kelvin = self.device.current_color_temperature
|
|
|
|
if kelvin is not None:
|
|
|
|
return color_util.color_temperature_kelvin_to_mired(kelvin)
|
|
|
|
if self.device.supports_tunable_white:
|
|
|
|
relative_ct = self.device.current_tunable_white
|
|
|
|
if relative_ct is not None:
|
|
|
|
# as KNX devices typically use Kelvin we use it as base for
|
|
|
|
# calculating ct from percent
|
|
|
|
return color_util.color_temperature_kelvin_to_mired(
|
|
|
|
self._min_kelvin + (
|
|
|
|
(relative_ct / 255) *
|
|
|
|
(self._max_kelvin - self._min_kelvin)))
|
2017-09-07 07:11:55 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
2019-02-08 07:28:52 +00:00
|
|
|
def min_mireds(self):
|
|
|
|
"""Return the coldest color temp this light supports in mireds."""
|
|
|
|
return self._min_mireds
|
|
|
|
|
|
|
|
@property
|
|
|
|
def max_mireds(self):
|
|
|
|
"""Return the warmest color temp this light supports in mireds."""
|
|
|
|
return self._max_mireds
|
2017-09-07 07:11:55 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def effect_list(self):
|
|
|
|
"""Return the list of supported effects."""
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def effect(self):
|
|
|
|
"""Return the current effect."""
|
|
|
|
return None
|
2017-07-07 05:24:25 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Return true if light is on."""
|
2018-08-26 19:25:39 +00:00
|
|
|
return self.device.state
|
2017-07-07 05:24:25 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
2017-09-07 07:11:55 +00:00
|
|
|
flags = 0
|
2018-08-26 19:25:39 +00:00
|
|
|
if self.device.supports_brightness:
|
2017-09-07 07:11:55 +00:00
|
|
|
flags |= SUPPORT_BRIGHTNESS
|
2018-08-26 19:25:39 +00:00
|
|
|
if self.device.supports_color:
|
2019-02-08 07:28:52 +00:00
|
|
|
flags |= SUPPORT_COLOR | SUPPORT_BRIGHTNESS
|
|
|
|
if self.device.supports_color_temperature or \
|
|
|
|
self.device.supports_tunable_white:
|
|
|
|
flags |= SUPPORT_COLOR_TEMP
|
2017-09-07 07:11:55 +00:00
|
|
|
return flags
|
|
|
|
|
2018-02-24 18:24:33 +00:00
|
|
|
async def async_turn_on(self, **kwargs):
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Turn the light on."""
|
2019-02-08 07:28:52 +00:00
|
|
|
brightness = kwargs.get(ATTR_BRIGHTNESS, self.brightness)
|
|
|
|
hs_color = kwargs.get(ATTR_HS_COLOR, self.hs_color)
|
|
|
|
mireds = kwargs.get(ATTR_COLOR_TEMP, self.color_temp)
|
|
|
|
|
|
|
|
update_brightness = ATTR_BRIGHTNESS in kwargs
|
|
|
|
update_color = ATTR_HS_COLOR in kwargs
|
|
|
|
update_color_temp = ATTR_COLOR_TEMP in kwargs
|
|
|
|
|
|
|
|
# always only go one path for turning on (avoid conflicting changes
|
|
|
|
# and weird effects)
|
|
|
|
if self.device.supports_brightness and \
|
|
|
|
(update_brightness and not update_color):
|
|
|
|
# if we don't need to update the color, try updating brightness
|
|
|
|
# directly if supported; don't do it if color also has to be
|
|
|
|
# changed, as RGB color implicitly sets the brightness as well
|
|
|
|
await self.device.set_brightness(brightness)
|
|
|
|
elif self.device.supports_color and \
|
|
|
|
(update_brightness or update_color):
|
|
|
|
# change RGB color (includes brightness)
|
|
|
|
# if brightness or hs_color was not yet set use the default value
|
|
|
|
# to calculate RGB from as a fallback
|
|
|
|
if brightness is None:
|
|
|
|
brightness = DEFAULT_BRIGHTNESS
|
|
|
|
if hs_color is None:
|
|
|
|
hs_color = DEFAULT_COLOR
|
|
|
|
await self.device.set_color(
|
|
|
|
color_util.color_hsv_to_RGB(*hs_color, brightness * 100 / 255))
|
|
|
|
elif self.device.supports_color_temperature and \
|
|
|
|
update_color_temp:
|
|
|
|
# change color temperature without ON telegram
|
|
|
|
kelvin = int(color_util.color_temperature_mired_to_kelvin(mireds))
|
|
|
|
if kelvin > self._max_kelvin:
|
|
|
|
kelvin = self._max_kelvin
|
|
|
|
elif kelvin < self._min_kelvin:
|
|
|
|
kelvin = self._min_kelvin
|
|
|
|
await self.device.set_color_temperature(kelvin)
|
|
|
|
elif self.device.supports_tunable_white and \
|
|
|
|
update_color_temp:
|
|
|
|
# calculate relative_ct from Kelvin to fit typical KNX devices
|
|
|
|
kelvin = int(color_util.color_temperature_mired_to_kelvin(mireds))
|
|
|
|
relative_ct = int(255 * (kelvin - self._min_kelvin) /
|
|
|
|
(self._max_kelvin - self._min_kelvin))
|
|
|
|
await self.device.set_tunable_white(relative_ct)
|
2017-09-07 07:11:55 +00:00
|
|
|
else:
|
2019-02-08 07:28:52 +00:00
|
|
|
# no color/brightness change requested, so just turn it on
|
2018-08-26 19:25:39 +00:00
|
|
|
await self.device.set_on()
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2018-02-24 18:24:33 +00:00
|
|
|
async def async_turn_off(self, **kwargs):
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Turn the light off."""
|
2018-08-26 19:25:39 +00:00
|
|
|
await self.device.set_off()
|