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
This commit is contained in:
HBDK 2016-08-01 01:55:48 +02:00 committed by Paulus Schoutsen
parent c39c10a088
commit a73c2e57a8
2 changed files with 125 additions and 17 deletions

View file

@ -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."""