From d0010740ca24c5935f8fa936e4c85c360caa3b09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Beye?= Date: Tue, 8 Nov 2022 17:37:57 +0100 Subject: [PATCH 1/2] Add max_sunrise_time and min_sunset_time overrides --- README.md | 4 +++- custom_components/adaptive_lighting/const.py | 6 ++++++ .../adaptive_lighting/strings.json | 2 ++ custom_components/adaptive_lighting/switch.py | 21 ++++++++++++++++++- .../adaptive_lighting/translations/en.json | 2 ++ 5 files changed, 33 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index aa955ee2..5c03341b 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ adaptive_lighting: ### Options | option | description | required | default | type | -| ------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | -------------- | ------- | +|---------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -------- | -------------- | ------- | | name | The name to use when displaying this switch. | False | default | string | | lights | List of light entities for Adaptive Lighting to control (may be empty). | False | list | [] | | prefer_rgb_color | Whether to use RGB color adjustment instead of native light color temperature. | False | False | boolean | @@ -65,8 +65,10 @@ adaptive_lighting: | sleep_rgb_color | List of three numbers between 0-255, indicating the RGB color in sleep mode (only used when sleep_rgb_or_color_temp is 'rgb_color'). | False | `[255, 56, 0]` | list | | sleep_color_temp | Color temperature of lights while the sleep mode is enabled (only used when sleep_rgb_or_color_temp is 'color_temp'). | False | 1000 | integer | | sunrise_time | Override the sunrise time with a fixed time. | False | time | | +| max_sunrise_time | Make the virtual sun always rise at at most a specific time while still allowing for even earlier times based on the real sun | False | time | | | sunrise_offset | Change the sunrise time with a positive or negative offset. | False | 0 | time | | sunset_time | Override the sunset time with a fixed time. | False | time | | +| min_sunset_time | Make the virtual sun always set at at least a specific time while still allowing for even later times based on the real sun | False | time | | | sunset_offset | Change the sunset time with a positive or negative offset. | False | 0 | time | | only_once | Whether to keep adapting the lights (false) or to only adapt the lights as soon as they are turned on (true). | False | False | boolean | | take_over_control | If another source calls `light.turn_on` while the lights are on and being adapted, disable Adaptive Lighting. | False | True | boolean | diff --git a/custom_components/adaptive_lighting/const.py b/custom_components/adaptive_lighting/const.py index b800015b..36c84a2d 100644 --- a/custom_components/adaptive_lighting/const.py +++ b/custom_components/adaptive_lighting/const.py @@ -39,8 +39,10 @@ CONF_SLEEP_RGB_OR_COLOR_TEMP, DEFAULT_SLEEP_RGB_OR_COLOR_TEMP = ( ) CONF_SUNRISE_OFFSET, DEFAULT_SUNRISE_OFFSET = "sunrise_offset", 0 CONF_SUNRISE_TIME = "sunrise_time" +CONF_MAX_SUNRISE_TIME = "max_sunrise_time" CONF_SUNSET_OFFSET, DEFAULT_SUNSET_OFFSET = "sunset_offset", 0 CONF_SUNSET_TIME = "sunset_time" +CONF_MIN_SUNSET_TIME = "min_sunset_time" CONF_TAKE_OVER_CONTROL, DEFAULT_TAKE_OVER_CONTROL = "take_over_control", True CONF_TRANSITION, DEFAULT_TRANSITION = "transition", 45 @@ -97,8 +99,10 @@ VALIDATION_TUPLES = [ selector.ColorRGBSelector(selector.ColorRGBSelectorConfig()), ), (CONF_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_SUNSET_OFFSET, DEFAULT_SUNSET_OFFSET, int), (CONF_ONLY_ONCE, DEFAULT_ONLY_ONCE, bool), (CONF_TAKE_OVER_CONTROL, DEFAULT_TAKE_OVER_CONTROL, bool), @@ -122,8 +126,10 @@ EXTRA_VALIDATION = { CONF_INTERVAL: (cv.time_period, timedelta_as_int), CONF_SUNRISE_OFFSET: (cv.time_period, timedelta_as_int), CONF_SUNRISE_TIME: (cv.time, str), + CONF_MAX_SUNRISE_TIME: (cv.time, str), CONF_SUNSET_OFFSET: (cv.time_period, timedelta_as_int), CONF_SUNSET_TIME: (cv.time, str), + CONF_MIN_SUNSET_TIME: (cv.time, str), } diff --git a/custom_components/adaptive_lighting/strings.json b/custom_components/adaptive_lighting/strings.json index b82f875e..8d4cb44c 100644 --- a/custom_components/adaptive_lighting/strings.json +++ b/custom_components/adaptive_lighting/strings.json @@ -36,8 +36,10 @@ "sleep_color_temp": "sleep_color_temp: Color temperature setting for Sleep Mode. (Kelvin)", "sunrise_offset": "sunrise_offset: How long before(-) or after(+) to define the sunrise point of the cycle (+/- seconds)", "sunrise_time": "sunrise_time: Manual override of the sunrise time, if 'None', it uses the actual sunrise time at your location (HH:MM:SS)", + "max_sunrise_time": "max_sunrise_time: Manual override of the maximum sunrise time, if 'None', it uses the actual sunrise time at your location (HH:MM:SS)", "sunset_offset": "sunset_offset: How long before(-) or after(+) to define the sunset point of the cycle (+/- seconds)", "sunset_time": "sunset_time: Manual override of the sunset time, if 'None', it uses the actual sunrise time at your location (HH:MM:SS)", + "min_sunset_time": "min_sunset_time: Manual override of the minimum sunset time, if 'None', it uses the actual sunrise time at your location (HH:MM:SS)", "take_over_control": "take_over_control: If anything but Adaptive Lighting calls 'light.turn_on' when a light is already on, stop adapting that light until it (or the switch) toggles off -> on.", "detect_non_ha_changes": "detect_non_ha_changes: detects all >10% changes made to the lights (also outside of HA), requires 'take_over_control' to be enabled (calls 'homeassistant.update_entity' every 'interval'!)", "transition": "Transition time when applying a change to the lights (seconds)", diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index e3688a62..435f49e5 100755 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -118,8 +118,10 @@ from .const import ( CONF_SLEEP_TRANSITION, CONF_SUNRISE_OFFSET, CONF_SUNRISE_TIME, + CONF_MAX_SUNRISE_TIME, CONF_SUNSET_OFFSET, CONF_SUNSET_TIME, + CONF_MIN_SUNSET_TIME, CONF_TAKE_OVER_CONTROL, CONF_TRANSITION, CONF_TURN_ON_LIGHTS, @@ -593,8 +595,10 @@ 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], + 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], time_zone=self.hass.config.time_zone, transition=data[CONF_TRANSITION], ) @@ -1089,8 +1093,10 @@ class SunLightSettings: sleep_rgb_color: tuple[int, int, int] sunrise_offset: datetime.timedelta | None sunrise_time: datetime.time | None + max_sunrise_time: datetime.time | None sunset_offset: datetime.timedelta | None sunset_time: datetime.time | None + min_sunset_time: datetime.time | None time_zone: datetime.tzinfo transition: int @@ -1135,7 +1141,20 @@ class SunLightSettings: else _replace_time(date, "sunset") ) + self.sunset_offset - if self.sunrise_time is None and self.sunset_time is None: + if self.max_sunrise_time is not None: + max_sunrise = _replace_time(date, "max_sunrise") + if max_sunrise < sunrise: + sunrise = max_sunrise + + if self.min_sunset_time is not None: + min_sunset = _replace_time(date, "min_sunset") + if min_sunset > sunset: + sunset = min_sunset + + if ( + self.sunrise_time is None and self.sunset_time is None and + self.max_sunrise_time is None and self.min_sunset_time is None + ): try: # Astral v1 solar_noon = location.solar_noon(date, local=False) diff --git a/custom_components/adaptive_lighting/translations/en.json b/custom_components/adaptive_lighting/translations/en.json index 7da48c8f..e4920cdc 100644 --- a/custom_components/adaptive_lighting/translations/en.json +++ b/custom_components/adaptive_lighting/translations/en.json @@ -37,8 +37,10 @@ "sleep_color_temp": "sleep_color_temp: Color temperature setting for Sleep Mode. (Kelvin)", "sunrise_offset": "sunrise_offset: How long before(-) or after(+) to define the sunrise point of the cycle (+/- seconds)", "sunrise_time": "sunrise_time: Manual override of the sunrise time, if 'None', it uses the actual sunrise time at your location (HH:MM:SS)", + "max_sunrise_time": "max_sunrise_time: Manual override of the maximum sunrise time, if 'None', it uses the actual sunrise time at your location (HH:MM:SS)", "sunset_offset": "sunset_offset: How long before(-) or after(+) to define the sunset point of the cycle (+/- seconds)", "sunset_time": "sunset_time: Manual override of the sunset time, if 'None', it uses the actual sunrise time at your location (HH:MM:SS)", + "min_sunset_time": "min_sunset_time: Manual override of the minimum sunset time, if 'None', it uses the actual sunrise time at your location (HH:MM:SS)", "take_over_control": "take_over_control: If anything but Adaptive Lighting calls 'light.turn_on' when a light is already on, stop adapting that light until it (or the switch) toggles off -> on.", "detect_non_ha_changes": "detect_non_ha_changes: detects all >10% changes made to the lights (also outside of HA), requires 'take_over_control' to be enabled (calls 'homeassistant.update_entity' every 'interval'!)", "transition": "Transition time when applying a change to the lights (seconds)", From 60b1be43db741350c1e8d14b24901bfaf5b321fc Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Tue, 8 Nov 2022 09:11:32 -0800 Subject: [PATCH 2/2] Run pre-commit filters --- custom_components/adaptive_lighting/switch.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index 435f49e5..ff100a44 100755 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -106,8 +106,10 @@ from .const import ( CONF_MANUAL_CONTROL, CONF_MAX_BRIGHTNESS, CONF_MAX_COLOR_TEMP, + CONF_MAX_SUNRISE_TIME, CONF_MIN_BRIGHTNESS, CONF_MIN_COLOR_TEMP, + CONF_MIN_SUNSET_TIME, CONF_ONLY_ONCE, CONF_PREFER_RGB_COLOR, CONF_SEPARATE_TURN_ON_COMMANDS, @@ -118,10 +120,8 @@ from .const import ( CONF_SLEEP_TRANSITION, CONF_SUNRISE_OFFSET, CONF_SUNRISE_TIME, - CONF_MAX_SUNRISE_TIME, CONF_SUNSET_OFFSET, CONF_SUNSET_TIME, - CONF_MIN_SUNSET_TIME, CONF_TAKE_OVER_CONTROL, CONF_TRANSITION, CONF_TURN_ON_LIGHTS, @@ -1152,8 +1152,10 @@ class SunLightSettings: sunset = min_sunset if ( - self.sunrise_time is None and self.sunset_time is None and - self.max_sunrise_time is None and self.min_sunset_time is None + self.sunrise_time is None + and self.sunset_time is None + and self.max_sunrise_time is None + and self.min_sunset_time is None ): try: # Astral v1