flux: fix for when stop_time is after midnight (#8932)

* flux: fix for when stop_time is after midnight

* flux: fix imports

* flux: add missing check when now is after midnight

* flux: one more try; should fix all use cases now

* flux switch: fix lint

* flux switch: add new tests

* flux switch: fix tests lint

* flux switch: fix tests docstrings
This commit is contained in:
Abílio Costa authored and Pascal Vizeli committed 2017-09-02 18:02:11 +02:00
1 parent f51163f803
commit 0889e38cb1
2 files changed
+284 -9

No files matched your search

+29 -9
View File
@@ -6,8 +6,9 @@ The idea was taken from https://github.com/KpaBap/hue-flux/
For more details about this component, please refer to the documentation at
https://home-assistant.io/components/switch.flux/
"""
from datetime import time
import datetime
import logging
import voluptuous as vol
from homeassistant.components.light import is_on, turn_on
@@ -46,7 +47,7 @@ PLATFORM_SCHEMA = vol.Schema({
vol.Required(CONF_LIGHTS): cv.entity_ids,
vol.Optional(CONF_NAME, default="Flux"): cv.string,
vol.Optional(CONF_START_TIME): cv.time,
vol.Optional(CONF_STOP_TIME, default=time(22, 0)): cv.time,
vol.Optional(CONF_STOP_TIME, default=datetime.time(22, 0)): cv.time,
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):
@@ -171,12 +172,22 @@ class FluxSwitch(SwitchDevice):
"""Update all the lights using flux."""
if now is None:
now = dt_now()
sunset = get_astral_event_date(self.hass, 'sunset', now.date())
start_time = self.find_start_time(now)
stop_time = now.replace(
hour=self._stop_time.hour, minute=self._stop_time.minute,
second=0)
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)
if start_time < now < sunset:
# Daytime
time_state = 'day'
@@ -192,15 +203,24 @@ class FluxSwitch(SwitchDevice):
else:
# Nightime
time_state = 'night'
if now < stop_time and now > start_time:
now_time = now
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
else:
now_time = stop_time
percentage_complete = 1
temp_range = abs(self._sunset_colortemp - self._stop_colortemp)
night_length = int(stop_time.timestamp() - sunset.timestamp())
seconds_from_sunset = int(now_time.timestamp() -
sunset.timestamp())
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