2016-06-02 06:38:19 +00:00
|
|
|
"""
|
|
|
|
Flux for Home-Assistant.
|
|
|
|
|
|
|
|
The idea was taken from https://github.com/KpaBap/hue-flux/
|
|
|
|
|
|
|
|
For more details about this component, please refer to the documentation at
|
2016-07-13 09:10:31 +00:00
|
|
|
https://home-assistant.io/components/switch.flux/
|
2016-06-02 06:38:19 +00:00
|
|
|
"""
|
2017-09-02 16:02:11 +00:00
|
|
|
import datetime
|
2016-06-02 06:38:19 +00:00
|
|
|
import logging
|
2017-09-02 16:02:11 +00:00
|
|
|
|
2016-06-02 06:38:19 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2017-10-29 11:32:02 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2017-10-22 09:27:04 +00:00
|
|
|
from homeassistant.components.light import (
|
|
|
|
is_on, turn_on, VALID_TRANSITION, ATTR_TRANSITION)
|
2016-06-02 06:38:19 +00:00
|
|
|
from homeassistant.components.switch import DOMAIN, SwitchDevice
|
2017-10-29 11:32:02 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_NAME, CONF_PLATFORM, CONF_LIGHTS, CONF_MODE)
|
2017-01-17 06:55:05 +00:00
|
|
|
from homeassistant.helpers.event import track_time_change
|
2017-05-09 07:03:34 +00:00
|
|
|
from homeassistant.helpers.sun import get_astral_event_date
|
2017-07-24 06:53:03 +00:00
|
|
|
from homeassistant.util import slugify
|
2016-10-02 05:57:15 +00:00
|
|
|
from homeassistant.util.color import (
|
|
|
|
color_temperature_to_rgb, color_RGB_to_xy,
|
2017-04-29 22:04:20 +00:00
|
|
|
color_temperature_kelvin_to_mired)
|
2016-06-02 06:38:19 +00:00
|
|
|
from homeassistant.util.dt import now as dt_now
|
2017-05-09 07:03:34 +00:00
|
|
|
|
2016-06-02 06:38:19 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
CONF_START_TIME = 'start_time'
|
|
|
|
CONF_STOP_TIME = 'stop_time'
|
|
|
|
CONF_START_CT = 'start_colortemp'
|
|
|
|
CONF_SUNSET_CT = 'sunset_colortemp'
|
|
|
|
CONF_STOP_CT = 'stop_colortemp'
|
|
|
|
CONF_BRIGHTNESS = 'brightness'
|
2018-01-29 22:37:19 +00:00
|
|
|
CONF_DISABLE_BRIGHTNESS_ADJUST = 'disable_brightness_adjust'
|
2017-10-22 09:27:04 +00:00
|
|
|
CONF_INTERVAL = 'interval'
|
2016-07-31 23:55:48 +00:00
|
|
|
|
|
|
|
MODE_XY = 'xy'
|
|
|
|
MODE_MIRED = 'mired'
|
2017-07-14 02:53:19 +00:00
|
|
|
MODE_RGB = 'rgb'
|
2016-07-31 23:55:48 +00:00
|
|
|
DEFAULT_MODE = MODE_XY
|
2017-10-29 11:32:02 +00:00
|
|
|
DEPENDENCIES = ['light']
|
|
|
|
|
2016-06-02 06:38:19 +00:00
|
|
|
|
|
|
|
PLATFORM_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(CONF_PLATFORM): 'flux',
|
|
|
|
vol.Required(CONF_LIGHTS): cv.entity_ids,
|
|
|
|
vol.Optional(CONF_NAME, default="Flux"): cv.string,
|
|
|
|
vol.Optional(CONF_START_TIME): cv.time,
|
2017-09-02 16:02:11 +00:00
|
|
|
vol.Optional(CONF_STOP_TIME, default=datetime.time(22, 0)): cv.time,
|
2016-06-02 06:38:19 +00:00
|
|
|
vol.Optional(CONF_START_CT, default=4000):
|
|
|
|
vol.All(vol.Coerce(int), vol.Range(min=1000, max=40000)),
|
|
|
|
vol.Optional(CONF_SUNSET_CT, default=3000):
|
|
|
|
vol.All(vol.Coerce(int), vol.Range(min=1000, max=40000)),
|
|
|
|
vol.Optional(CONF_STOP_CT, default=1900):
|
|
|
|
vol.All(vol.Coerce(int), vol.Range(min=1000, max=40000)),
|
|
|
|
vol.Optional(CONF_BRIGHTNESS):
|
2016-07-31 23:55:48 +00:00
|
|
|
vol.All(vol.Coerce(int), vol.Range(min=0, max=255)),
|
2018-01-29 22:37:19 +00:00
|
|
|
vol.Optional(CONF_DISABLE_BRIGHTNESS_ADJUST): cv.boolean,
|
2016-07-31 23:55:48 +00:00
|
|
|
vol.Optional(CONF_MODE, default=DEFAULT_MODE):
|
2017-10-22 09:27:04 +00:00
|
|
|
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
|
2016-06-02 06:38:19 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
2017-10-22 09:27:04 +00:00
|
|
|
def set_lights_xy(hass, lights, x_val, y_val, brightness, transition):
|
2016-06-02 06:38:19 +00:00
|
|
|
"""Set color of array of lights."""
|
|
|
|
for light in lights:
|
|
|
|
if is_on(hass, light):
|
|
|
|
turn_on(hass, light,
|
|
|
|
xy_color=[x_val, y_val],
|
|
|
|
brightness=brightness,
|
2017-10-22 09:27:04 +00:00
|
|
|
transition=transition)
|
2016-06-02 06:38:19 +00:00
|
|
|
|
|
|
|
|
2017-10-22 09:27:04 +00:00
|
|
|
def set_lights_temp(hass, lights, mired, brightness, transition):
|
2016-07-31 23:55:48 +00:00
|
|
|
"""Set color of array of lights."""
|
|
|
|
for light in lights:
|
|
|
|
if is_on(hass, light):
|
|
|
|
turn_on(hass, light,
|
2016-10-02 05:57:15 +00:00
|
|
|
color_temp=int(mired),
|
|
|
|
brightness=brightness,
|
2017-10-22 09:27:04 +00:00
|
|
|
transition=transition)
|
2016-07-31 23:55:48 +00:00
|
|
|
|
|
|
|
|
2017-10-22 09:27:04 +00:00
|
|
|
def set_lights_rgb(hass, lights, rgb, transition):
|
2017-07-14 02:53:19 +00:00
|
|
|
"""Set color of array of lights."""
|
|
|
|
for light in lights:
|
|
|
|
if is_on(hass, light):
|
|
|
|
turn_on(hass, light,
|
|
|
|
rgb_color=rgb,
|
2017-10-22 09:27:04 +00:00
|
|
|
transition=transition)
|
2017-07-14 02:53:19 +00:00
|
|
|
|
|
|
|
|
2016-06-02 06:38:19 +00:00
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the Flux switches."""
|
2016-06-02 06:38:19 +00:00
|
|
|
name = config.get(CONF_NAME)
|
|
|
|
lights = config.get(CONF_LIGHTS)
|
|
|
|
start_time = config.get(CONF_START_TIME)
|
|
|
|
stop_time = config.get(CONF_STOP_TIME)
|
|
|
|
start_colortemp = config.get(CONF_START_CT)
|
|
|
|
sunset_colortemp = config.get(CONF_SUNSET_CT)
|
|
|
|
stop_colortemp = config.get(CONF_STOP_CT)
|
|
|
|
brightness = config.get(CONF_BRIGHTNESS)
|
2018-01-29 22:37:19 +00:00
|
|
|
disable_brightness_adjust = config.get(CONF_DISABLE_BRIGHTNESS_ADJUST)
|
2016-07-31 23:55:48 +00:00
|
|
|
mode = config.get(CONF_MODE)
|
2017-10-22 09:27:04 +00:00
|
|
|
interval = config.get(CONF_INTERVAL)
|
|
|
|
transition = config.get(ATTR_TRANSITION)
|
|
|
|
flux = FluxSwitch(name, hass, lights, start_time, stop_time,
|
2016-06-02 06:38:19 +00:00
|
|
|
start_colortemp, sunset_colortemp, stop_colortemp,
|
2017-10-22 09:27:04 +00:00
|
|
|
brightness, disable_brightness_adjust, mode, interval,
|
|
|
|
transition)
|
2016-06-02 06:38:19 +00:00
|
|
|
add_devices([flux])
|
|
|
|
|
|
|
|
def update(call=None):
|
|
|
|
"""Update lights."""
|
|
|
|
flux.flux_update()
|
|
|
|
|
2017-07-24 06:53:03 +00:00
|
|
|
service_name = slugify("{} {}".format(name, 'update'))
|
|
|
|
hass.services.register(DOMAIN, service_name, update)
|
2016-06-02 06:38:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FluxSwitch(SwitchDevice):
|
2016-07-13 09:10:31 +00:00
|
|
|
"""Representation of a Flux switch."""
|
2016-06-02 06:38:19 +00:00
|
|
|
|
2017-10-22 09:27:04 +00:00
|
|
|
def __init__(self, name, hass, lights, start_time, stop_time,
|
2016-06-02 06:38:19 +00:00
|
|
|
start_colortemp, sunset_colortemp, stop_colortemp,
|
2017-10-22 09:27:04 +00:00
|
|
|
brightness, disable_brightness_adjust, mode, interval,
|
|
|
|
transition):
|
2016-06-02 06:38:19 +00:00
|
|
|
"""Initialize the Flux switch."""
|
|
|
|
self._name = name
|
|
|
|
self.hass = hass
|
|
|
|
self._lights = lights
|
|
|
|
self._start_time = start_time
|
|
|
|
self._stop_time = stop_time
|
|
|
|
self._start_colortemp = start_colortemp
|
|
|
|
self._sunset_colortemp = sunset_colortemp
|
|
|
|
self._stop_colortemp = stop_colortemp
|
|
|
|
self._brightness = brightness
|
2017-01-27 07:08:08 +00:00
|
|
|
self._disable_brightness_adjust = disable_brightness_adjust
|
2016-07-31 23:55:48 +00:00
|
|
|
self._mode = mode
|
2017-10-22 09:27:04 +00:00
|
|
|
self._interval = interval
|
|
|
|
self._transition = transition
|
2016-09-07 13:59:59 +00:00
|
|
|
self.unsub_tracker = None
|
2016-06-02 06:38:19 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the device if any."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if switch is on."""
|
2017-04-30 07:05:29 +00:00
|
|
|
return self.unsub_tracker is not None
|
2016-06-02 06:38:19 +00:00
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""Turn on flux."""
|
2017-04-30 07:05:29 +00:00
|
|
|
if self.is_on:
|
|
|
|
return
|
|
|
|
|
2017-05-02 16:18:47 +00:00
|
|
|
# Make initial update
|
2017-04-30 07:05:29 +00:00
|
|
|
self.flux_update()
|
|
|
|
|
2017-05-02 16:18:47 +00:00
|
|
|
self.unsub_tracker = track_time_change(
|
2017-10-22 09:27:04 +00:00
|
|
|
self.hass, self.flux_update, second=[0, self._interval])
|
2017-04-30 07:05:29 +00:00
|
|
|
|
2016-11-16 16:26:29 +00:00
|
|
|
self.schedule_update_ha_state()
|
2016-06-02 06:38:19 +00:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Turn off flux."""
|
2016-09-07 13:59:59 +00:00
|
|
|
if self.unsub_tracker is not None:
|
|
|
|
self.unsub_tracker()
|
|
|
|
self.unsub_tracker = None
|
|
|
|
|
2016-11-16 16:26:29 +00:00
|
|
|
self.schedule_update_ha_state()
|
2016-06-02 06:38:19 +00:00
|
|
|
|
2016-08-17 03:53:00 +00:00
|
|
|
def flux_update(self, now=None):
|
2016-06-02 06:38:19 +00:00
|
|
|
"""Update all the lights using flux."""
|
2016-08-17 03:53:00 +00:00
|
|
|
if now is None:
|
|
|
|
now = dt_now()
|
2017-09-02 16:02:11 +00:00
|
|
|
|
2017-05-09 07:03:34 +00:00
|
|
|
sunset = get_astral_event_date(self.hass, 'sunset', now.date())
|
2016-06-02 06:38:19 +00:00
|
|
|
start_time = self.find_start_time(now)
|
2017-05-02 16:18:47 +00:00
|
|
|
stop_time = now.replace(
|
|
|
|
hour=self._stop_time.hour, minute=self._stop_time.minute,
|
|
|
|
second=0)
|
2016-06-02 06:38:19 +00:00
|
|
|
|
2017-09-02 16:02:11 +00:00
|
|
|
if stop_time <= start_time:
|
|
|
|
# stop_time does not happen in the same day as start_time
|
|
|
|
if start_time < now:
|
|
|
|
# stop time is tomorrow
|
|
|
|
stop_time += datetime.timedelta(days=1)
|
|
|
|
elif now < start_time:
|
|
|
|
# stop_time was yesterday since the new start_time is not reached
|
|
|
|
stop_time -= datetime.timedelta(days=1)
|
|
|
|
|
2016-06-02 06:38:19 +00:00
|
|
|
if start_time < now < sunset:
|
|
|
|
# Daytime
|
2016-07-31 23:55:48 +00:00
|
|
|
time_state = 'day'
|
2016-06-02 06:38:19 +00:00
|
|
|
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())
|
2016-07-31 23:55:48 +00:00
|
|
|
percentage_complete = seconds_from_start / day_length
|
|
|
|
temp_offset = temp_range * percentage_complete
|
2016-06-02 06:38:19 +00:00
|
|
|
if self._start_colortemp > self._sunset_colortemp:
|
|
|
|
temp = self._start_colortemp - temp_offset
|
|
|
|
else:
|
|
|
|
temp = self._start_colortemp + temp_offset
|
|
|
|
else:
|
2018-01-27 19:58:27 +00:00
|
|
|
# Night time
|
2016-07-31 23:55:48 +00:00
|
|
|
time_state = 'night'
|
2017-09-02 16:02:11 +00:00
|
|
|
|
|
|
|
if now < stop_time:
|
|
|
|
if stop_time < start_time and stop_time.day == sunset.day:
|
|
|
|
# we need to use yesterday's sunset time
|
|
|
|
sunset_time = sunset - datetime.timedelta(days=1)
|
|
|
|
else:
|
|
|
|
sunset_time = sunset
|
|
|
|
|
|
|
|
# pylint: disable=no-member
|
|
|
|
night_length = int(stop_time.timestamp() -
|
|
|
|
sunset_time.timestamp())
|
|
|
|
seconds_from_sunset = int(now.timestamp() -
|
|
|
|
sunset_time.timestamp())
|
|
|
|
percentage_complete = seconds_from_sunset / night_length
|
2016-06-02 06:38:19 +00:00
|
|
|
else:
|
2017-09-02 16:02:11 +00:00
|
|
|
percentage_complete = 1
|
|
|
|
|
2016-06-02 06:38:19 +00:00
|
|
|
temp_range = abs(self._sunset_colortemp - self._stop_colortemp)
|
2016-07-31 23:55:48 +00:00
|
|
|
temp_offset = temp_range * percentage_complete
|
2016-06-02 06:38:19 +00:00
|
|
|
if self._sunset_colortemp > self._stop_colortemp:
|
|
|
|
temp = self._sunset_colortemp - temp_offset
|
|
|
|
else:
|
|
|
|
temp = self._sunset_colortemp + temp_offset
|
2017-07-14 02:53:19 +00:00
|
|
|
rgb = color_temperature_to_rgb(temp)
|
|
|
|
x_val, y_val, b_val = color_RGB_to_xy(*rgb)
|
2016-10-02 05:57:15 +00:00
|
|
|
brightness = self._brightness if self._brightness else b_val
|
2017-01-27 07:08:08 +00:00
|
|
|
if self._disable_brightness_adjust:
|
|
|
|
brightness = None
|
2016-07-31 23:55:48 +00:00
|
|
|
if self._mode == MODE_XY:
|
2016-06-02 06:38:19 +00:00
|
|
|
set_lights_xy(self.hass, self._lights, x_val,
|
2017-10-22 09:27:04 +00:00
|
|
|
y_val, brightness, self._transition)
|
2017-05-02 16:18:47 +00:00
|
|
|
_LOGGER.info("Lights updated to x:%s y:%s brightness:%s, %s%% "
|
|
|
|
"of %s cycle complete at %s", x_val, y_val,
|
2016-07-31 23:55:48 +00:00
|
|
|
brightness, round(
|
2017-01-17 06:55:05 +00:00
|
|
|
percentage_complete * 100), time_state, now)
|
2017-07-14 02:53:19 +00:00
|
|
|
elif self._mode == MODE_RGB:
|
2017-10-22 09:27:04 +00:00
|
|
|
set_lights_rgb(self.hass, self._lights, rgb, self._transition)
|
2017-07-14 02:53:19 +00:00
|
|
|
_LOGGER.info("Lights updated to rgb:%s, %s%% "
|
|
|
|
"of %s cycle complete at %s", rgb,
|
|
|
|
round(percentage_complete * 100), time_state, now)
|
2016-07-31 23:55:48 +00:00
|
|
|
else:
|
2016-10-02 05:57:15 +00:00
|
|
|
# Convert to mired and clamp to allowed values
|
|
|
|
mired = color_temperature_kelvin_to_mired(temp)
|
2017-10-22 09:27:04 +00:00
|
|
|
set_lights_temp(self.hass, self._lights, mired, brightness,
|
|
|
|
self._transition)
|
2017-05-02 16:18:47 +00:00
|
|
|
_LOGGER.info("Lights updated to mired:%s brightness:%s, %s%% "
|
|
|
|
"of %s cycle complete at %s", mired, brightness,
|
2017-01-17 06:55:05 +00:00
|
|
|
round(percentage_complete * 100), time_state, now)
|
2016-06-02 06:38:19 +00:00
|
|
|
|
|
|
|
def find_start_time(self, now):
|
|
|
|
"""Return sunrise or start_time if given."""
|
|
|
|
if self._start_time:
|
2017-05-02 16:18:47 +00:00
|
|
|
sunrise = now.replace(
|
|
|
|
hour=self._start_time.hour, minute=self._start_time.minute,
|
|
|
|
second=0)
|
2016-06-02 06:38:19 +00:00
|
|
|
else:
|
2017-05-09 07:03:34 +00:00
|
|
|
sunrise = get_astral_event_date(self.hass, 'sunrise', now.date())
|
2016-06-02 06:38:19 +00:00
|
|
|
return sunrise
|