8000 Added mired and kelvin mode to flux by HBDK · Pull Request #2665 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Added mired and kelvin mode to flux #2665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 41 additions & 17 deletions homeassistant/components/switch/flux.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
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',
Expand All @@ -46,7 +52,9 @@
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)
})


Expand All @@ -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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no unit tests around this method

"""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."""
Expand All @@ -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):
Expand All @@ -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
Expand All @@ -102,6 +123,7 @@ def __init__(self, name, hass, state, lights, start_time, stop_time,
self._sunset_colortemp = sunset_colortemp
self._stop_colortemp = stop_colortemp
self._brightness = brightness
self._mode = mode
self.tracker = None

@property
Expand Down Expand Up @@ -141,25 +163,19 @@ def flux_update(self, now=dt_now()):

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:
Expand All @@ -168,20 +184,28 @@ def flux_update(self, now=dt_now()):
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."""
Expand Down
84 changes: 84 additions & 0 deletions tests/components/switch/test_flux.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,3 +480,87 @@ def test_flux_with_multiple_lights(self):
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)
0