Add RGB support to switch.flux (#8417)

pull/8468/head
Dougal Matthews 2017-07-14 04:53:19 +02:00 committed by Paulus Schoutsen
parent af9a0e8fea
commit 21e82bd037
2 changed files with 65 additions and 2 deletions

View File

@ -37,6 +37,7 @@ CONF_MODE = 'mode'
MODE_XY = 'xy'
MODE_MIRED = 'mired'
MODE_RGB = 'rgb'
DEFAULT_MODE = MODE_XY
PLATFORM_SCHEMA = vol.Schema({
@ -55,7 +56,7 @@ PLATFORM_SCHEMA = vol.Schema({
vol.All(vol.Coerce(int), vol.Range(min=0, max=255)),
vol.Optional(CONF_DISABLE_BRIGTNESS_ADJUST): cv.boolean,
vol.Optional(CONF_MODE, default=DEFAULT_MODE):
vol.Any(MODE_XY, MODE_MIRED)
vol.Any(MODE_XY, MODE_MIRED, MODE_RGB)
})
@ -79,6 +80,15 @@ def set_lights_temp(hass, lights, mired, brightness):
transition=30)
def set_lights_rgb(hass, lights, rgb):
"""Set color of array of lights."""
for light in lights:
if is_on(hass, light):
turn_on(hass, light,
rgb_color=rgb,
transition=30)
# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the Flux switches."""
@ -194,7 +204,8 @@ 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))
rgb = color_temperature_to_rgb(temp)
x_val, y_val, b_val = color_RGB_to_xy(*rgb)
brightness = self._brightness if self._brightness else b_val
if self._disable_brightness_adjust:
brightness = None
@ -205,6 +216,11 @@ class FluxSwitch(SwitchDevice):
"of %s cycle complete at %s", x_val, y_val,
brightness, round(
percentage_complete * 100), time_state, now)
elif self._mode == MODE_RGB:
set_lights_rgb(self.hass, self._lights, rgb)
_LOGGER.info("Lights updated to rgb:%s, %s%% "
"of %s cycle complete at %s", rgb,
round(percentage_complete * 100), time_state, now)
else:
# Convert to mired and clamp to allowed values
mired = color_temperature_kelvin_to_mired(temp)

View File

@ -557,3 +557,50 @@ 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_rgb(self):
"""Test the flux switch´s mode rgb."""
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)
def event_date(hass, event, now=None):
if event == 'sunrise':
return sunrise_time
else:
return sunset_time
with patch('homeassistant.util.dt.now', return_value=test_time):
with patch('homeassistant.helpers.sun.get_astral_event_date',
side_effect=event_date):
assert setup_component(self.hass, switch.DOMAIN, {
switch.DOMAIN: {
'platform': 'flux',
'name': 'flux',
'lights': [dev1.entity_id],
'mode': 'rgb'
}
})
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]
rgb = (255, 198, 152)
rounded_call = tuple(map(round, call.data[light.ATTR_RGB_COLOR]))
self.assertEqual(rounded_call, rgb)