mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Support knx tunable white and color temperature lights (#19699)
* KNX: Bumped version to 0.9.4 and added support for tunable white and color temperature for lights. * Updated to the latest changes * return None when ct value is unknown - return None instead of default value when ct is unknown - remove DEFAULT_COLOR_TEMPERATURE * use Kelvin as base for relative color temperature use Kelvin as base for relative color temperature instead of Mireds * moved fallback value tests for clarity * Update request from oliverblaha Co-Authored-By: marvin-w <marvin@fam-wichmann.de> * Address suggested changes * Update homeassistant/components/knx/light.py Co-Authored-By: marvin-w <marvin@fam-wichmann.de> * Update homeassistant/components/knx/light.py Co-Authored-By: marvin-w <marvin@fam-wichmann.de>
This commit is contained in:
parent
706810bbce
commit
1e95719436
@ -17,7 +17,7 @@ import homeassistant.helpers.config_validation as cv
|
|||||||
from homeassistant.helpers.event import async_track_state_change
|
from homeassistant.helpers.event import async_track_state_change
|
||||||
from homeassistant.helpers.script import Script
|
from homeassistant.helpers.script import Script
|
||||||
|
|
||||||
REQUIREMENTS = ['xknx==0.9.3']
|
REQUIREMENTS = ['xknx==0.9.4']
|
||||||
|
|
||||||
DOMAIN = "knx"
|
DOMAIN = "knx"
|
||||||
DATA_KNX = "data_knx"
|
DATA_KNX = "data_knx"
|
||||||
|
@ -4,28 +4,50 @@ Support for KNX/IP lights.
|
|||||||
For more details about this platform, please refer to the documentation at
|
For more details about this platform, please refer to the documentation at
|
||||||
https://home-assistant.io/components/light.knx/
|
https://home-assistant.io/components/light.knx/
|
||||||
"""
|
"""
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.knx import ATTR_DISCOVER_DEVICES, DATA_KNX
|
|
||||||
from homeassistant.components.light import (
|
from homeassistant.components.light import (
|
||||||
ATTR_BRIGHTNESS, ATTR_HS_COLOR, PLATFORM_SCHEMA, SUPPORT_BRIGHTNESS,
|
ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_HS_COLOR, PLATFORM_SCHEMA,
|
||||||
SUPPORT_COLOR, Light)
|
SUPPORT_BRIGHTNESS, SUPPORT_COLOR, SUPPORT_COLOR_TEMP,
|
||||||
|
Light)
|
||||||
from homeassistant.const import CONF_NAME
|
from homeassistant.const import CONF_NAME
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
import homeassistant.util.color as color_util
|
import homeassistant.util.color as color_util
|
||||||
|
|
||||||
|
from homeassistant.components.knx import ATTR_DISCOVER_DEVICES, DATA_KNX
|
||||||
|
|
||||||
|
|
||||||
CONF_ADDRESS = 'address'
|
CONF_ADDRESS = 'address'
|
||||||
CONF_STATE_ADDRESS = 'state_address'
|
CONF_STATE_ADDRESS = 'state_address'
|
||||||
CONF_BRIGHTNESS_ADDRESS = 'brightness_address'
|
CONF_BRIGHTNESS_ADDRESS = 'brightness_address'
|
||||||
CONF_BRIGHTNESS_STATE_ADDRESS = 'brightness_state_address'
|
CONF_BRIGHTNESS_STATE_ADDRESS = 'brightness_state_address'
|
||||||
CONF_COLOR_ADDRESS = 'color_address'
|
CONF_COLOR_ADDRESS = 'color_address'
|
||||||
CONF_COLOR_STATE_ADDRESS = 'color_state_address'
|
CONF_COLOR_STATE_ADDRESS = 'color_state_address'
|
||||||
|
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'
|
||||||
|
|
||||||
DEFAULT_NAME = 'KNX Light'
|
DEFAULT_NAME = 'KNX Light'
|
||||||
|
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
|
||||||
DEPENDENCIES = ['knx']
|
DEPENDENCIES = ['knx']
|
||||||
|
|
||||||
|
|
||||||
|
class ColorTempModes(Enum):
|
||||||
|
"""Color temperature modes for config validation."""
|
||||||
|
|
||||||
|
absolute = "DPT-7.600"
|
||||||
|
relative = "DPT-5.001"
|
||||||
|
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
vol.Required(CONF_ADDRESS): cv.string,
|
vol.Required(CONF_ADDRESS): cv.string,
|
||||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||||
@ -34,6 +56,14 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|||||||
vol.Optional(CONF_BRIGHTNESS_STATE_ADDRESS): cv.string,
|
vol.Optional(CONF_BRIGHTNESS_STATE_ADDRESS): cv.string,
|
||||||
vol.Optional(CONF_COLOR_ADDRESS): cv.string,
|
vol.Optional(CONF_COLOR_ADDRESS): cv.string,
|
||||||
vol.Optional(CONF_COLOR_STATE_ADDRESS): cv.string,
|
vol.Optional(CONF_COLOR_STATE_ADDRESS): cv.string,
|
||||||
|
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)),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
@ -60,16 +90,36 @@ def async_add_entities_discovery(hass, discovery_info, async_add_entities):
|
|||||||
def async_add_entities_config(hass, config, async_add_entities):
|
def async_add_entities_config(hass, config, async_add_entities):
|
||||||
"""Set up light for KNX platform configured within platform."""
|
"""Set up light for KNX platform configured within platform."""
|
||||||
import xknx
|
import xknx
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
light = xknx.devices.Light(
|
light = xknx.devices.Light(
|
||||||
hass.data[DATA_KNX].xknx,
|
hass.data[DATA_KNX].xknx,
|
||||||
name=config.get(CONF_NAME),
|
name=config[CONF_NAME],
|
||||||
group_address_switch=config.get(CONF_ADDRESS),
|
group_address_switch=config[CONF_ADDRESS],
|
||||||
group_address_switch_state=config.get(CONF_STATE_ADDRESS),
|
group_address_switch_state=config.get(CONF_STATE_ADDRESS),
|
||||||
group_address_brightness=config.get(CONF_BRIGHTNESS_ADDRESS),
|
group_address_brightness=config.get(CONF_BRIGHTNESS_ADDRESS),
|
||||||
group_address_brightness_state=config.get(
|
group_address_brightness_state=config.get(
|
||||||
CONF_BRIGHTNESS_STATE_ADDRESS),
|
CONF_BRIGHTNESS_STATE_ADDRESS),
|
||||||
group_address_color=config.get(CONF_COLOR_ADDRESS),
|
group_address_color=config.get(CONF_COLOR_ADDRESS),
|
||||||
group_address_color_state=config.get(CONF_COLOR_STATE_ADDRESS))
|
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])
|
||||||
hass.data[DATA_KNX].xknx.devices.add(light)
|
hass.data[DATA_KNX].xknx.devices.add(light)
|
||||||
async_add_entities([KNXLight(light)])
|
async_add_entities([KNXLight(light)])
|
||||||
|
|
||||||
@ -81,6 +131,13 @@ class KNXLight(Light):
|
|||||||
"""Initialize of KNX light."""
|
"""Initialize of KNX light."""
|
||||||
self.device = device
|
self.device = device
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_register_callbacks(self):
|
def async_register_callbacks(self):
|
||||||
"""Register callbacks to update hass after device was changed."""
|
"""Register callbacks to update hass after device was changed."""
|
||||||
@ -111,26 +168,51 @@ class KNXLight(Light):
|
|||||||
@property
|
@property
|
||||||
def brightness(self):
|
def brightness(self):
|
||||||
"""Return the brightness of this light between 0..255."""
|
"""Return the brightness of this light between 0..255."""
|
||||||
return self.device.current_brightness \
|
if self.device.supports_color:
|
||||||
if self.device.supports_brightness else \
|
if self.device.current_color is None:
|
||||||
None
|
return None
|
||||||
|
return max(self.device.current_color)
|
||||||
|
if self.device.supports_brightness:
|
||||||
|
return self.device.current_brightness
|
||||||
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def hs_color(self):
|
def hs_color(self):
|
||||||
"""Return the HS color value."""
|
"""Return the HS color value."""
|
||||||
if self.device.supports_color:
|
if self.device.supports_color:
|
||||||
return color_util.color_RGB_to_hs(*self.device.current_color)
|
rgb = self.device.current_color
|
||||||
|
if rgb is None:
|
||||||
|
return None
|
||||||
|
return color_util.color_RGB_to_hs(*rgb)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def color_temp(self):
|
def color_temp(self):
|
||||||
"""Return the CT color temperature."""
|
"""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)))
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def white_value(self):
|
def min_mireds(self):
|
||||||
"""Return the white value of this light between 0..255."""
|
"""Return the coldest color temp this light supports in mireds."""
|
||||||
return None
|
return self._min_mireds
|
||||||
|
|
||||||
|
@property
|
||||||
|
def max_mireds(self):
|
||||||
|
"""Return the warmest color temp this light supports in mireds."""
|
||||||
|
return self._max_mireds
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def effect_list(self):
|
def effect_list(self):
|
||||||
@ -154,19 +236,59 @@ class KNXLight(Light):
|
|||||||
if self.device.supports_brightness:
|
if self.device.supports_brightness:
|
||||||
flags |= SUPPORT_BRIGHTNESS
|
flags |= SUPPORT_BRIGHTNESS
|
||||||
if self.device.supports_color:
|
if self.device.supports_color:
|
||||||
flags |= SUPPORT_COLOR
|
flags |= SUPPORT_COLOR | SUPPORT_BRIGHTNESS
|
||||||
|
if self.device.supports_color_temperature or \
|
||||||
|
self.device.supports_tunable_white:
|
||||||
|
flags |= SUPPORT_COLOR_TEMP
|
||||||
return flags
|
return flags
|
||||||
|
|
||||||
async def async_turn_on(self, **kwargs):
|
async def async_turn_on(self, **kwargs):
|
||||||
"""Turn the light on."""
|
"""Turn the light on."""
|
||||||
if ATTR_BRIGHTNESS in kwargs:
|
brightness = kwargs.get(ATTR_BRIGHTNESS, self.brightness)
|
||||||
if self.device.supports_brightness:
|
hs_color = kwargs.get(ATTR_HS_COLOR, self.hs_color)
|
||||||
await self.device.set_brightness(int(kwargs[ATTR_BRIGHTNESS]))
|
mireds = kwargs.get(ATTR_COLOR_TEMP, self.color_temp)
|
||||||
elif ATTR_HS_COLOR in kwargs:
|
|
||||||
if self.device.supports_color:
|
update_brightness = ATTR_BRIGHTNESS in kwargs
|
||||||
await self.device.set_color(color_util.color_hs_to_RGB(
|
update_color = ATTR_HS_COLOR in kwargs
|
||||||
*kwargs[ATTR_HS_COLOR]))
|
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)
|
||||||
else:
|
else:
|
||||||
|
# no color/brightness change requested, so just turn it on
|
||||||
await self.device.set_on()
|
await self.device.set_on()
|
||||||
|
|
||||||
async def async_turn_off(self, **kwargs):
|
async def async_turn_off(self, **kwargs):
|
||||||
|
@ -1745,7 +1745,7 @@ xbee-helper==0.0.7
|
|||||||
xboxapi==0.1.1
|
xboxapi==0.1.1
|
||||||
|
|
||||||
# homeassistant.components.knx
|
# homeassistant.components.knx
|
||||||
xknx==0.9.3
|
xknx==0.9.4
|
||||||
|
|
||||||
# homeassistant.components.media_player.bluesound
|
# homeassistant.components.media_player.bluesound
|
||||||
# homeassistant.components.sensor.startca
|
# homeassistant.components.sensor.startca
|
||||||
|
Loading…
x
Reference in New Issue
Block a user