Flux switch improvements (#3615)
* Make flux always adjust brightness of light (even when not in XY mode) * Remove kelvin mode from flux switch The light/turn_on service only works with mired values, kelvin values are out of range per the schema. * Use already defined min/max values for light/turn_on schema * Clamp temp value to light/turn_on allowed valuespull/3543/merge
parent
fcbfcf0aa7
commit
d55ed7a3a2
|
@ -93,7 +93,8 @@ LIGHT_TURN_ON_SCHEMA = vol.Schema({
|
|||
vol.Coerce(tuple)),
|
||||
ATTR_XY_COLOR: vol.All(vol.ExactSequence((cv.small_float, cv.small_float)),
|
||||
vol.Coerce(tuple)),
|
||||
ATTR_COLOR_TEMP: vol.All(int, vol.Range(min=154, max=500)),
|
||||
ATTR_COLOR_TEMP: vol.All(int, vol.Range(min=color_util.HASS_COLOR_MIN,
|
||||
max=color_util.HASS_COLOR_MAX)),
|
||||
ATTR_WHITE_VALUE: vol.All(int, vol.Range(min=0, max=255)),
|
||||
ATTR_FLASH: vol.In([FLASH_SHORT, FLASH_LONG]),
|
||||
ATTR_EFFECT: vol.In([EFFECT_COLORLOOP, EFFECT_RANDOM, EFFECT_WHITE]),
|
||||
|
|
|
@ -15,8 +15,9 @@ from homeassistant.components.sun import next_setting, next_rising
|
|||
from homeassistant.components.switch import DOMAIN, SwitchDevice
|
||||
from homeassistant.const import CONF_NAME, CONF_PLATFORM
|
||||
from homeassistant.helpers.event import track_utc_time_change
|
||||
from homeassistant.util.color import color_temperature_to_rgb as temp_to_rgb
|
||||
from homeassistant.util.color import color_RGB_to_xy
|
||||
from homeassistant.util.color import (
|
||||
color_temperature_to_rgb, color_RGB_to_xy,
|
||||
color_temperature_kelvin_to_mired, HASS_COLOR_MIN, HASS_COLOR_MAX)
|
||||
from homeassistant.util.dt import now as dt_now
|
||||
from homeassistant.util.dt import as_local
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
@ -36,7 +37,6 @@ CONF_MODE = 'mode'
|
|||
|
||||
MODE_XY = 'xy'
|
||||
MODE_MIRED = 'mired'
|
||||
MODE_KELVIN = 'kelvin'
|
||||
DEFAULT_MODE = MODE_XY
|
||||
|
||||
PLATFORM_SCHEMA = vol.Schema({
|
||||
|
@ -54,7 +54,7 @@ PLATFORM_SCHEMA = vol.Schema({
|
|||
vol.Optional(CONF_BRIGHTNESS):
|
||||
vol.All(vol.Coerce(int), vol.Range(min=0, max=255)),
|
||||
vol.Optional(CONF_MODE, default=DEFAULT_MODE):
|
||||
vol.Any(MODE_XY, MODE_MIRED, MODE_KELVIN)
|
||||
vol.Any(MODE_XY, MODE_MIRED)
|
||||
})
|
||||
|
||||
|
||||
|
@ -68,15 +68,13 @@ def set_lights_xy(hass, lights, x_val, y_val, brightness):
|
|||
transition=30)
|
||||
|
||||
|
||||
def set_lights_temp(hass, lights, kelvin, mode):
|
||||
def set_lights_temp(hass, lights, mired, brightness):
|
||||
"""Set color of array of lights."""
|
||||
temp = kelvin
|
||||
if mode == MODE_MIRED:
|
||||
temp = 1000000 / kelvin
|
||||
for light in lights:
|
||||
if is_on(hass, light):
|
||||
turn_on(hass, light,
|
||||
color_temp=int(temp),
|
||||
color_temp=int(mired),
|
||||
brightness=brightness,
|
||||
transition=30)
|
||||
|
||||
|
||||
|
@ -194,9 +192,9 @@ class FluxSwitch(SwitchDevice):
|
|||
temp = self._sunset_colortemp - temp_offset
|
||||
else:
|
||||
temp = self._sunset_colortemp + temp_offset
|
||||
x_val, y_val, b_val = color_RGB_to_xy(*color_temperature_to_rgb(temp))
|
||||
brightness = self._brightness if self._brightness else b_val
|
||||
if self._mode == MODE_XY:
|
||||
x_val, y_val, b_val = color_RGB_to_xy(*temp_to_rgb(temp))
|
||||
brightness = self._brightness if self._brightness else b_val
|
||||
set_lights_xy(self.hass, self._lights, x_val,
|
||||
y_val, brightness)
|
||||
_LOGGER.info("Lights updated to x:%s y:%s brightness:%s, %s%%"
|
||||
|
@ -205,9 +203,12 @@ class FluxSwitch(SwitchDevice):
|
|||
percentage_complete * 100), time_state,
|
||||
as_local(now))
|
||||
else:
|
||||
set_lights_temp(self.hass, self._lights, temp, self._mode)
|
||||
_LOGGER.info("Lights updated to temp:%s, %s%%"
|
||||
" of %s cycle complete at %s", temp,
|
||||
# Convert to mired and clamp to allowed values
|
||||
mired = color_temperature_kelvin_to_mired(temp)
|
||||
mired = max(HASS_COLOR_MIN, min(mired, HASS_COLOR_MAX))
|
||||
set_lights_temp(self.hass, self._lights, mired, brightness)
|
||||
_LOGGER.info("Lights updated to mired:%s brightness:%s, %s%%"
|
||||
" of %s cycle complete at %s", mired, brightness,
|
||||
round(percentage_complete * 100),
|
||||
time_state, as_local(now))
|
||||
|
||||
|
|
|
@ -532,46 +532,3 @@ class TestSwitchFlux(unittest.TestCase):
|
|||
self.hass.block_till_done()
|
||||
call = turn_on_calls[-1]
|
||||
self.assertEqual(call.data[light.ATTR_COLOR_TEMP], 269)
|
||||
|
||||
def test_flux_with_kelvin(self):
|
||||
"""Test the flux switch´s mode kelvin."""
|
||||
platform = loader.get_component('light.test')
|
||||
platform.init()
|
||||
self.assertTrue(
|
||||
setup_component(self.hass, light.DOMAIN,
|
||||
{light.DOMAIN: {CONF_PLATFORM: 'test'}}))
|
||||
|
||||
dev1 = platform.DEVICES[0]
|
||||
|
||||
# Verify initial state of light
|
||||
state = self.hass.states.get(dev1.entity_id)
|
||||
self.assertEqual(STATE_ON, state.state)
|
||||
self.assertIsNone(state.attributes.get('color_temp'))
|
||||
|
||||
test_time = dt_util.now().replace(hour=8, minute=30, second=0)
|
||||
sunset_time = test_time.replace(hour=17, minute=0, second=0)
|
||||
sunrise_time = test_time.replace(hour=5,
|
||||
minute=0,
|
||||
second=0) + timedelta(days=1)
|
||||
|
||||
with patch('homeassistant.util.dt.now', return_value=test_time):
|
||||
with patch('homeassistant.components.sun.next_rising',
|
||||
return_value=sunrise_time):
|
||||
with patch('homeassistant.components.sun.next_setting',
|
||||
return_value=sunset_time):
|
||||
assert setup_component(self.hass, switch.DOMAIN, {
|
||||
switch.DOMAIN: {
|
||||
'platform': 'flux',
|
||||
'name': 'flux',
|
||||
'lights': [dev1.entity_id],
|
||||
'mode': 'kelvin'
|
||||
}
|
||||
})
|
||||
turn_on_calls = mock_service(
|
||||
self.hass, light.DOMAIN, SERVICE_TURN_ON)
|
||||
switch.turn_on(self.hass, 'switch.flux')
|
||||
self.hass.block_till_done()
|
||||
fire_time_changed(self.hass, test_time)
|
||||
self.hass.block_till_done()
|
||||
call = turn_on_calls[-1]
|
||||
self.assertEqual(call.data[light.ATTR_COLOR_TEMP], 3708)
|
||||
|
|
Loading…
Reference in New Issue