From 72419cb60f983a38a8b3ad16973b401b6cbc62b2 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Sat, 5 Aug 2023 15:08:45 -0700 Subject: [PATCH] Add a min_sunrise_time and max_sunset_time (#707) --- README.md | 2 ++ custom_components/adaptive_lighting/const.py | 12 ++++++++++++ .../adaptive_lighting/strings.json | 2 ++ custom_components/adaptive_lighting/switch.py | 18 ++++++++++++++++-- .../adaptive_lighting/translations/en.json | 2 ++ 5 files changed, 34 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 71500862..b83d880f 100644 --- a/README.md +++ b/README.md @@ -115,10 +115,12 @@ The YAML and frontend configuration methods support all of the options listed be | `sleep_transition` | Duration of transition when "sleep mode" is toggled in seconds. 😴 | `1` | `float` 0-6553 | | `transition_until_sleep` | When enabled, Adaptive Lighting will treat sleep settings as the minimum, transitioning to these values after sunset. 🌙 | `False` | `bool` | | `sunrise_time` | Set a fixed time (HH:MM:SS) for sunrise. 🌅 | `None` | `str` | +| `min_sunrise_time` | Set the earliest virtual sunrise time (HH:MM:SS), allowing for later sunrises. 🌅 | `None` | `str` | | `max_sunrise_time` | Set the latest virtual sunrise time (HH:MM:SS), allowing for earlier sunrises. 🌅 | `None` | `str` | | `sunrise_offset` | Adjust sunrise time with a positive or negative offset in seconds. ⏰ | `0` | `int` | | `sunset_time` | Set a fixed time (HH:MM:SS) for sunset. 🌇 | `None` | `str` | | `min_sunset_time` | Set the earliest virtual sunset time (HH:MM:SS), allowing for later sunsets. 🌇 | `None` | `str` | +| `max_sunset_time` | Set the latest virtual sunset time (HH:MM:SS), allowing for earlier sunsets. 🌇 | `None` | `str` | | `sunset_offset` | Adjust sunset time with a positive or negative offset in seconds. ⏰ | `0` | `int` | | `brightness_mode` | Brightness mode to use. Possible values are `default`, `linear`, and `tanh` (uses `brightness_mode_time_dark` and `brightness_mode_time_light`). 📈 | `default` | one of `['default', 'linear', 'tanh']` | | `brightness_mode_time_dark` | (Ignored if `brightness_mode='default'`) The duration in seconds to ramp up/down the brightness before/after sunrise/sunset. 📈📉 | `900` | `int` | diff --git a/custom_components/adaptive_lighting/const.py b/custom_components/adaptive_lighting/const.py index a8b53afd..1d060c56 100644 --- a/custom_components/adaptive_lighting/const.py +++ b/custom_components/adaptive_lighting/const.py @@ -122,6 +122,11 @@ DOCS[ CONF_SUNRISE_TIME = "sunrise_time" DOCS[CONF_SUNRISE_TIME] = "Set a fixed time (HH:MM:SS) for sunrise. 🌅" +CONF_MIN_SUNRISE_TIME = "min_sunrise_time" +DOCS[ + CONF_MIN_SUNRISE_TIME +] = "Set the earliest virtual sunrise time (HH:MM:SS), allowing for later sunrises. 🌅" + CONF_MAX_SUNRISE_TIME = "max_sunrise_time" DOCS[CONF_MAX_SUNRISE_TIME] = ( "Set the latest virtual sunrise time (HH:MM:SS), allowing" @@ -141,6 +146,11 @@ DOCS[ CONF_MIN_SUNSET_TIME ] = "Set the earliest virtual sunset time (HH:MM:SS), allowing for later sunsets. 🌇" +CONF_MAX_SUNSET_TIME = "max_sunset_time" +DOCS[ + CONF_MAX_SUNSET_TIME +] = "Set the latest virtual sunset time (HH:MM:SS), allowing for earlier sunsets. 🌇" + CONF_BRIGHTNESS_MODE, DEFAULT_BRIGHTNESS_MODE = "brightness_mode", "default" DOCS[CONF_BRIGHTNESS_MODE] = ( "Brightness mode to use. Possible values are `default`, `linear`, and `tanh` " @@ -299,10 +309,12 @@ VALIDATION_TUPLES = [ (CONF_SLEEP_TRANSITION, DEFAULT_SLEEP_TRANSITION, VALID_TRANSITION), (CONF_ADAPT_UNTIL_SLEEP, DEFAULT_ADAPT_UNTIL_SLEEP, bool), (CONF_SUNRISE_TIME, NONE_STR, str), + (CONF_MIN_SUNRISE_TIME, NONE_STR, str), (CONF_MAX_SUNRISE_TIME, NONE_STR, str), (CONF_SUNRISE_OFFSET, DEFAULT_SUNRISE_OFFSET, int), (CONF_SUNSET_TIME, NONE_STR, str), (CONF_MIN_SUNSET_TIME, NONE_STR, str), + (CONF_MAX_SUNSET_TIME, NONE_STR, str), (CONF_SUNSET_OFFSET, DEFAULT_SUNSET_OFFSET, int), ( CONF_BRIGHTNESS_MODE, diff --git a/custom_components/adaptive_lighting/strings.json b/custom_components/adaptive_lighting/strings.json index 0751500b..cece3da7 100644 --- a/custom_components/adaptive_lighting/strings.json +++ b/custom_components/adaptive_lighting/strings.json @@ -35,10 +35,12 @@ "sleep_transition": "sleep_transition: Duration of transition when \"sleep mode\" is toggled in seconds. 😴", "transition_until_sleep": "transition_until_sleep: When enabled, Adaptive Lighting will treat sleep settings as the minimum, transitioning to these values after sunset. 🌙", "sunrise_time": "sunrise_time: Set a fixed time (HH:MM:SS) for sunrise. 🌅", + "min_sunrise_time": "min_sunrise_time: Set the earliest virtual sunrise time (HH:MM:SS), allowing for later sunrises. 🌅", "max_sunrise_time": "max_sunrise_time: Set the latest virtual sunrise time (HH:MM:SS), allowing for earlier sunrises. 🌅", "sunrise_offset": "sunrise_offset: Adjust sunrise time with a positive or negative offset in seconds. ⏰", "sunset_time": "sunset_time: Set a fixed time (HH:MM:SS) for sunset. 🌇", "min_sunset_time": "min_sunset_time: Set the earliest virtual sunset time (HH:MM:SS), allowing for later sunsets. 🌇", + "max_sunset_time": "max_sunset_time: Set the latest virtual sunset time (HH:MM:SS), allowing for earlier sunsets. 🌇", "sunset_offset": "sunset_offset: Adjust sunset time with a positive or negative offset in seconds. ⏰", "brightness_mode": "brightness_mode: Brightness mode to use. Possible values are `default`, `linear`, and `tanh` (uses `brightness_mode_time_dark` and `brightness_mode_time_light`). 📈", "brightness_mode_time_dark": "brightness_mode_time_dark: (Ignored if `brightness_mode='default'`) The duration in seconds to ramp up/down the brightness before/after sunrise/sunset. 📈📉", diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index afeb7fa7..b5bf65de 100644 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -117,8 +117,10 @@ from .const import ( CONF_MAX_BRIGHTNESS, CONF_MAX_COLOR_TEMP, CONF_MAX_SUNRISE_TIME, + CONF_MAX_SUNSET_TIME, CONF_MIN_BRIGHTNESS, CONF_MIN_COLOR_TEMP, + CONF_MIN_SUNRISE_TIME, CONF_MIN_SUNSET_TIME, CONF_MULTI_LIGHT_INTERCEPT, CONF_ONLY_ONCE, @@ -903,10 +905,12 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): sleep_rgb_or_color_temp=data[CONF_SLEEP_RGB_OR_COLOR_TEMP], sunrise_offset=data[CONF_SUNRISE_OFFSET], sunrise_time=data[CONF_SUNRISE_TIME], + min_sunrise_time=data[CONF_MIN_SUNRISE_TIME], max_sunrise_time=data[CONF_MAX_SUNRISE_TIME], sunset_offset=data[CONF_SUNSET_OFFSET], sunset_time=data[CONF_SUNSET_TIME], min_sunset_time=data[CONF_MIN_SUNSET_TIME], + max_sunset_time=data[CONF_MAX_SUNSET_TIME], brightness_mode=data[CONF_BRIGHTNESS_MODE], brightness_mode_time_dark=data[CONF_BRIGHTNESS_MODE_TIME_DARK], brightness_mode_time_light=data[CONF_BRIGHTNESS_MODE_TIME_LIGHT], @@ -1571,12 +1575,14 @@ class SunLightSettings: sleep_rgb_or_color_temp: Literal["color_temp", "rgb_color"] sleep_color_temp: int sleep_rgb_color: tuple[int, int, int] - sunrise_offset: datetime.timedelta | None sunrise_time: datetime.time | None + sunrise_offset: datetime.timedelta | None + min_sunrise_time: datetime.time | None max_sunrise_time: datetime.time | None - sunset_offset: datetime.timedelta | None sunset_time: datetime.time | None + sunset_offset: datetime.timedelta | None min_sunset_time: datetime.time | None + max_sunset_time: datetime.time | None brightness_mode: Literal["default", "linear", "tanh"] brightness_mode_time_dark: datetime.timedelta | None brightness_mode_time_light: datetime.timedelta | None @@ -1589,6 +1595,10 @@ class SunLightSettings: if self.sunrise_time is None else self._replace_time(date, "sunrise") ) + self.sunrise_offset + if self.min_sunrise_time is not None: + min_sunrise = self._replace_time(date, "min_sunrise") + if min_sunrise > sunrise: + sunrise = min_sunrise if self.max_sunrise_time is not None: max_sunrise = self._replace_time(date, "max_sunrise") if max_sunrise < sunrise: @@ -1606,6 +1616,10 @@ class SunLightSettings: min_sunset = self._replace_time(date, "min_sunset") if min_sunset > sunset: sunset = min_sunset + if self.max_sunset_time is not None: + max_sunset = self._replace_time(date, "max_sunset") + if max_sunset < sunset: + sunset = max_sunset return sunset def _replace_time(self, date: datetime.datetime, key: str) -> datetime.datetime: diff --git a/custom_components/adaptive_lighting/translations/en.json b/custom_components/adaptive_lighting/translations/en.json index 511a11c8..c8e2c413 100644 --- a/custom_components/adaptive_lighting/translations/en.json +++ b/custom_components/adaptive_lighting/translations/en.json @@ -36,10 +36,12 @@ "sleep_transition": "sleep_transition: Duration of transition when \"sleep mode\" is toggled in seconds. 😴", "transition_until_sleep": "transition_until_sleep: When enabled, Adaptive Lighting will treat sleep settings as the minimum, transitioning to these values after sunset. 🌙", "sunrise_time": "sunrise_time: Set a fixed time (HH:MM:SS) for sunrise. 🌅", + "min_sunrise_time": "min_sunrise_time: Set the earliest virtual sunrise time (HH:MM:SS), allowing for later sunrises. 🌅", "max_sunrise_time": "max_sunrise_time: Set the latest virtual sunrise time (HH:MM:SS), allowing for earlier sunrises. 🌅", "sunrise_offset": "sunrise_offset: Adjust sunrise time with a positive or negative offset in seconds. ⏰", "sunset_time": "sunset_time: Set a fixed time (HH:MM:SS) for sunset. 🌇", "min_sunset_time": "min_sunset_time: Set the earliest virtual sunset time (HH:MM:SS), allowing for later sunsets. 🌇", + "max_sunset_time": "max_sunset_time: Set the latest virtual sunset time (HH:MM:SS), allowing for earlier sunsets. 🌇", "sunset_offset": "sunset_offset: Adjust sunset time with a positive or negative offset in seconds. ⏰", "brightness_mode": "brightness_mode: Brightness mode to use. Possible values are `default`, `linear`, and `tanh` (uses `brightness_mode_time_dark` and `brightness_mode_time_light`). 📈", "brightness_mode_time_dark": "brightness_mode_time_dark: (Ignored if `brightness_mode='default'`) The duration in seconds to ramp up/down the brightness before/after sunrise/sunset. 📈📉",