switch.flux: add interval and transition attributes (#9700)

This commit is contained in:
Abílio Costa 2017-10-22 10:27:04 +01:00 committed by Pascal Vizeli
parent bd4304e838
commit 80053ef21b

View File

@ -11,7 +11,8 @@ import logging
import voluptuous as vol import voluptuous as vol
from homeassistant.components.light import is_on, turn_on from homeassistant.components.light import (
is_on, turn_on, VALID_TRANSITION, ATTR_TRANSITION)
from homeassistant.components.switch import DOMAIN, SwitchDevice from homeassistant.components.switch import DOMAIN, SwitchDevice
from homeassistant.const import CONF_NAME, CONF_PLATFORM, CONF_LIGHTS from homeassistant.const import CONF_NAME, CONF_PLATFORM, CONF_LIGHTS
from homeassistant.helpers.event import track_time_change from homeassistant.helpers.event import track_time_change
@ -35,6 +36,7 @@ CONF_STOP_CT = 'stop_colortemp'
CONF_BRIGHTNESS = 'brightness' CONF_BRIGHTNESS = 'brightness'
CONF_DISABLE_BRIGTNESS_ADJUST = 'disable_brightness_adjust' CONF_DISABLE_BRIGTNESS_ADJUST = 'disable_brightness_adjust'
CONF_MODE = 'mode' CONF_MODE = 'mode'
CONF_INTERVAL = 'interval'
MODE_XY = 'xy' MODE_XY = 'xy'
MODE_MIRED = 'mired' MODE_MIRED = 'mired'
@ -57,37 +59,39 @@ PLATFORM_SCHEMA = vol.Schema({
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_DISABLE_BRIGTNESS_ADJUST): cv.boolean, vol.Optional(CONF_DISABLE_BRIGTNESS_ADJUST): cv.boolean,
vol.Optional(CONF_MODE, default=DEFAULT_MODE): vol.Optional(CONF_MODE, default=DEFAULT_MODE):
vol.Any(MODE_XY, MODE_MIRED, MODE_RGB) vol.Any(MODE_XY, MODE_MIRED, MODE_RGB),
vol.Optional(CONF_INTERVAL, default=30): cv.positive_int,
vol.Optional(ATTR_TRANSITION, default=30): VALID_TRANSITION
}) })
def set_lights_xy(hass, lights, x_val, y_val, brightness): def set_lights_xy(hass, lights, x_val, y_val, brightness, transition):
"""Set color of array of lights.""" """Set color of array of lights."""
for light in lights: for light in lights:
if is_on(hass, light): if is_on(hass, light):
turn_on(hass, light, turn_on(hass, light,
xy_color=[x_val, y_val], xy_color=[x_val, y_val],
brightness=brightness, brightness=brightness,
transition=30) transition=transition)
def set_lights_temp(hass, lights, mired, brightness): def set_lights_temp(hass, lights, mired, brightness, transition):
"""Set color of array of lights.""" """Set color of array of lights."""
for light in lights: for light in lights:
if is_on(hass, light): if is_on(hass, light):
turn_on(hass, light, turn_on(hass, light,
color_temp=int(mired), color_temp=int(mired),
brightness=brightness, brightness=brightness,
transition=30) transition=transition)
def set_lights_rgb(hass, lights, rgb): def set_lights_rgb(hass, lights, rgb, transition):
"""Set color of array of lights.""" """Set color of array of lights."""
for light in lights: for light in lights:
if is_on(hass, light): if is_on(hass, light):
turn_on(hass, light, turn_on(hass, light,
rgb_color=rgb, rgb_color=rgb,
transition=30) transition=transition)
# pylint: disable=unused-argument # pylint: disable=unused-argument
@ -103,9 +107,12 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
brightness = config.get(CONF_BRIGHTNESS) brightness = config.get(CONF_BRIGHTNESS)
disable_brightness_adjust = config.get(CONF_DISABLE_BRIGTNESS_ADJUST) disable_brightness_adjust = config.get(CONF_DISABLE_BRIGTNESS_ADJUST)
mode = config.get(CONF_MODE) mode = config.get(CONF_MODE)
flux = FluxSwitch(name, hass, False, lights, start_time, stop_time, interval = config.get(CONF_INTERVAL)
transition = config.get(ATTR_TRANSITION)
flux = FluxSwitch(name, hass, lights, start_time, stop_time,
start_colortemp, sunset_colortemp, stop_colortemp, start_colortemp, sunset_colortemp, stop_colortemp,
brightness, disable_brightness_adjust, mode) brightness, disable_brightness_adjust, mode, interval,
transition)
add_devices([flux]) add_devices([flux])
def update(call=None): def update(call=None):
@ -119,9 +126,10 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
class FluxSwitch(SwitchDevice): class FluxSwitch(SwitchDevice):
"""Representation of a Flux switch.""" """Representation of a Flux switch."""
def __init__(self, name, hass, state, lights, start_time, stop_time, def __init__(self, name, hass, lights, start_time, stop_time,
start_colortemp, sunset_colortemp, stop_colortemp, start_colortemp, sunset_colortemp, stop_colortemp,
brightness, disable_brightness_adjust, mode): brightness, disable_brightness_adjust, mode, interval,
transition):
"""Initialize the Flux switch.""" """Initialize the Flux switch."""
self._name = name self._name = name
self.hass = hass self.hass = hass
@ -134,6 +142,8 @@ class FluxSwitch(SwitchDevice):
self._brightness = brightness self._brightness = brightness
self._disable_brightness_adjust = disable_brightness_adjust self._disable_brightness_adjust = disable_brightness_adjust
self._mode = mode self._mode = mode
self._interval = interval
self._transition = transition
self.unsub_tracker = None self.unsub_tracker = None
@property @property
@ -155,7 +165,7 @@ class FluxSwitch(SwitchDevice):
self.flux_update() self.flux_update()
self.unsub_tracker = track_time_change( self.unsub_tracker = track_time_change(
self.hass, self.flux_update, second=[0, 30]) self.hass, self.flux_update, second=[0, self._interval])
self.schedule_update_ha_state() self.schedule_update_ha_state()
@ -232,20 +242,21 @@ class FluxSwitch(SwitchDevice):
brightness = None brightness = None
if self._mode == MODE_XY: if self._mode == MODE_XY:
set_lights_xy(self.hass, self._lights, x_val, set_lights_xy(self.hass, self._lights, x_val,
y_val, brightness) y_val, brightness, self._transition)
_LOGGER.info("Lights updated to x:%s y:%s brightness:%s, %s%% " _LOGGER.info("Lights updated to x:%s y:%s brightness:%s, %s%% "
"of %s cycle complete at %s", x_val, y_val, "of %s cycle complete at %s", x_val, y_val,
brightness, round( brightness, round(
percentage_complete * 100), time_state, now) percentage_complete * 100), time_state, now)
elif self._mode == MODE_RGB: elif self._mode == MODE_RGB:
set_lights_rgb(self.hass, self._lights, rgb) set_lights_rgb(self.hass, self._lights, rgb, self._transition)
_LOGGER.info("Lights updated to rgb:%s, %s%% " _LOGGER.info("Lights updated to rgb:%s, %s%% "
"of %s cycle complete at %s", rgb, "of %s cycle complete at %s", rgb,
round(percentage_complete * 100), time_state, now) round(percentage_complete * 100), time_state, now)
else: else:
# Convert to mired and clamp to allowed values # Convert to mired and clamp to allowed values
mired = color_temperature_kelvin_to_mired(temp) mired = color_temperature_kelvin_to_mired(temp)
set_lights_temp(self.hass, self._lights, mired, brightness) set_lights_temp(self.hass, self._lights, mired, brightness,
self._transition)
_LOGGER.info("Lights updated to mired:%s brightness:%s, %s%% " _LOGGER.info("Lights updated to mired:%s brightness:%s, %s%% "
"of %s cycle complete at %s", mired, brightness, "of %s cycle complete at %s", mired, brightness,
round(percentage_complete * 100), time_state, now) round(percentage_complete * 100), time_state, now)