From a73c2e57a8a465b158a0a68be36146ca761604b8 Mon Sep 17 00:00:00 2001 From: HBDK Date: Mon, 1 Aug 2016 01:55:48 +0200 Subject: [PATCH] Added mired and kelvin mode to flux (#2665) * Added mired and kelvin mode to flux * changed as requested * Renamed varible * attempt to add test for new method in flux.py * removed line to fix lint error --- homeassistant/components/switch/flux.py | 58 ++++++++++++----- tests/components/switch/test_flux.py | 84 +++++++++++++++++++++++++ 2 files changed, 125 insertions(+), 17 deletions(-) diff --git a/homeassistant/components/switch/flux.py b/homeassistant/components/switch/flux.py index d222ad1764b..a66b45bc82e 100644 --- a/homeassistant/components/switch/flux.py +++ b/homeassistant/components/switch/flux.py @@ -32,6 +32,12 @@ CONF_START_CT = 'start_colortemp' CONF_SUNSET_CT = 'sunset_colortemp' CONF_STOP_CT = 'stop_colortemp' CONF_BRIGHTNESS = 'brightness' +CONF_MODE = 'mode' + +MODE_XY = 'xy' +MODE_MIRED = 'mired' +MODE_KELVIN = 'kelvin' +DEFAULT_MODE = MODE_XY PLATFORM_SCHEMA = vol.Schema({ vol.Required(CONF_PLATFORM): 'flux', @@ -46,7 +52,9 @@ PLATFORM_SCHEMA = vol.Schema({ vol.Optional(CONF_STOP_CT, default=1900): vol.All(vol.Coerce(int), vol.Range(min=1000, max=40000)), vol.Optional(CONF_BRIGHTNESS): - vol.All(vol.Coerce(int), vol.Range(min=0, max=255)) + 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) }) @@ -60,6 +68,18 @@ def set_lights_xy(hass, lights, x_val, y_val, brightness): transition=30) +def set_lights_temp(hass, lights, kelvin, mode): + """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), + transition=30) + + # pylint: disable=unused-argument def setup_platform(hass, config, add_devices, discovery_info=None): """Setup the Flux switches.""" @@ -71,9 +91,10 @@ def setup_platform(hass, config, add_devices, discovery_info=None): sunset_colortemp = config.get(CONF_SUNSET_CT) stop_colortemp = config.get(CONF_STOP_CT) brightness = config.get(CONF_BRIGHTNESS) + mode = config.get(CONF_MODE) flux = FluxSwitch(name, hass, False, lights, start_time, stop_time, start_colortemp, sunset_colortemp, stop_colortemp, - brightness) + brightness, mode) add_devices([flux]) def update(call=None): @@ -90,7 +111,7 @@ class FluxSwitch(SwitchDevice): # pylint: disable=too-many-arguments def __init__(self, name, hass, state, lights, start_time, stop_time, start_colortemp, sunset_colortemp, stop_colortemp, - brightness): + brightness, mode): """Initialize the Flux switch.""" self._name = name self.hass = hass @@ -102,6 +123,7 @@ class FluxSwitch(SwitchDevice): self._sunset_colortemp = sunset_colortemp self._stop_colortemp = stop_colortemp self._brightness = brightness + self._mode = mode self.tracker = None @property @@ -141,25 +163,19 @@ class FluxSwitch(SwitchDevice): if start_time < now < sunset: # Daytime + time_state = 'day' temp_range = abs(self._start_colortemp - self._sunset_colortemp) day_length = int(sunset.timestamp() - start_time.timestamp()) seconds_from_start = int(now.timestamp() - start_time.timestamp()) - percentage_of_day_complete = seconds_from_start / day_length - temp_offset = temp_range * percentage_of_day_complete + percentage_complete = seconds_from_start / day_length + temp_offset = temp_range * percentage_complete if self._start_colortemp > self._sunset_colortemp: temp = self._start_colortemp - temp_offset else: temp = self._start_colortemp + temp_offset - 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%%" - " of day cycle complete at %s", x_val, y_val, - brightness, round(percentage_of_day_complete*100), - as_local(now)) else: # Nightime + time_state = 'night' if now < stop_time and now > start_time: now_time = now else: @@ -168,20 +184,28 @@ class FluxSwitch(SwitchDevice): night_length = int(stop_time.timestamp() - sunset.timestamp()) seconds_from_sunset = int(now_time.timestamp() - sunset.timestamp()) - percentage_of_night_complete = seconds_from_sunset / night_length - temp_offset = temp_range * percentage_of_night_complete + percentage_complete = seconds_from_sunset / night_length + temp_offset = temp_range * percentage_complete if self._sunset_colortemp > self._stop_colortemp: temp = self._sunset_colortemp - temp_offset else: temp = self._sunset_colortemp + temp_offset + 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%%" - " of night cycle complete at %s", x_val, y_val, - brightness, round(percentage_of_night_complete*100), + " of %s cycle complete at %s", x_val, y_val, + brightness, round( + 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, + round(percentage_complete * 100), + time_state, as_local(now)) def find_start_time(self, now): """Return sunrise or start_time if given.""" diff --git a/tests/components/switch/test_flux.py b/tests/components/switch/test_flux.py index 78d1f5190d6..99815250794 100644 --- a/tests/components/switch/test_flux.py +++ b/tests/components/switch/test_flux.py @@ -480,3 +480,87 @@ class TestSwitchFlux(unittest.TestCase): call = turn_on_calls[-3] self.assertEqual(call.data[light.ATTR_BRIGHTNESS], 171) self.assertEqual(call.data[light.ATTR_XY_COLOR], [0.452, 0.386]) + + def test_flux_with_mired(self): + """Test the flux switch´s mode mired""" + platform = loader.get_component('light.test') + platform.init() + self.assertTrue( + light.setup(self.hass, {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': 'mired' + } + }) + turn_on_calls = mock_service( + self.hass, light.DOMAIN, SERVICE_TURN_ON) + switch.turn_on(self.hass, 'switch.flux') + self.hass.pool.block_till_done() + fire_time_changed(self.hass, test_time) + self.hass.pool.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( + light.setup(self.hass, {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.pool.block_till_done() + fire_time_changed(self.hass, test_time) + self.hass.pool.block_till_done() + call = turn_on_calls[-1] + self.assertEqual(call.data[light.ATTR_COLOR_TEMP], 3708)