From c0fff83aaf5309d4be4ca164ff16e2ad989a66fc Mon Sep 17 00:00:00 2001 From: Niklas Haas Date: Sun, 1 Dec 2024 22:07:14 +0100 Subject: [PATCH] Add feature for reducing the brightness of lights during the daytime Ideally, we would tie this to something like a Lux sensor input, but this is the next best thing I could easily implement. Closes: #1074 --- README.md | 2 ++ .../adaptive_lighting/color_and_brightness.py | 18 ++++++++++++------ custom_components/adaptive_lighting/const.py | 4 ++++ .../adaptive_lighting/services.yaml | 6 ++++++ .../adaptive_lighting/strings.json | 1 + custom_components/adaptive_lighting/switch.py | 2 ++ webapp/app.py | 2 ++ 7 files changed, 29 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 296b4aca5..bcfa72631 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,7 @@ The YAML and frontend configuration methods support all of the options listed be | `max_brightness` | Maximum brightness percentage. 💡 | `100` | `int` 1-100 | | `min_color_temp` | Warmest color temperature in Kelvin. 🔥 | `2000` | `int` 1000-10000 | | `max_color_temp` | Coldest color temperature in Kelvin. ❄️ | `5500` | `int` 1000-10000 | +| `reduce_daytime_brightness` | By how much to reduce the brightness during daytime, in percentage. 🌞 | `0` | `int` 0-100 | | `prefer_rgb_color` | Whether to prefer RGB color adjustment over light color temperature when possible. 🌈 | `False` | `bool` | | `sleep_brightness` | Brightness percentage of lights in sleep mode. 😴 | `1` | `int` 1-100 | | `sleep_rgb_or_color_temp` | Use either `"rgb_color"` or `"color_temp"` in sleep mode. 🌙 | `color_temp` | one of `['color_temp', 'rgb_color']` | @@ -161,6 +162,7 @@ adaptive_lighting: max_brightness: 100 min_color_temp: 2000 max_color_temp: 5500 + reduce_daytime_brightness: 50 sleep_brightness: 1 sleep_color_temp: 1000 sunrise_time: "08:00:00" # override the sunrise time diff --git a/custom_components/adaptive_lighting/color_and_brightness.py b/custom_components/adaptive_lighting/color_and_brightness.py index 52386bf71..5786a7fd8 100644 --- a/custom_components/adaptive_lighting/color_and_brightness.py +++ b/custom_components/adaptive_lighting/color_and_brightness.py @@ -208,6 +208,7 @@ class SunLightSettings: max_color_temp: int min_brightness: int min_color_temp: int + reduce_daytime_brightness: int sleep_brightness: int sleep_rgb_or_color_temp: Literal["color_temp", "rgb_color"] sleep_color_temp: int @@ -306,12 +307,17 @@ def brightness_pct(self, dt: datetime.datetime, is_sleep: bool) -> float: return self.sleep_brightness assert self.brightness_mode in ("default", "linear", "tanh") if self.brightness_mode == "default": - return self._brightness_pct_default(dt) - if self.brightness_mode == "linear": - return self._brightness_pct_linear(dt) - if self.brightness_mode == "tanh": - return self._brightness_pct_tanh(dt) - return None + brightness = self._brightness_pct_default(dt) + elif self.brightness_mode == "linear": + brightness = self._brightness_pct_linear(dt) + elif self.brightness_mode == "tanh": + brightness = self._brightness_pct_tanh(dt) + else: + return None + sun_position = self.sun.sun_position(dt) + if sun_position > 0 + brightness *= 1 - sun_position * (self.reduce_daytime_brightness / 100) + return brightness def color_temp_kelvin(self, sun_position: float) -> int: """Calculate the color temperature in Kelvin.""" diff --git a/custom_components/adaptive_lighting/const.py b/custom_components/adaptive_lighting/const.py index 79d571e25..bde134952 100644 --- a/custom_components/adaptive_lighting/const.py +++ b/custom_components/adaptive_lighting/const.py @@ -69,6 +69,9 @@ CONF_MIN_COLOR_TEMP, DEFAULT_MIN_COLOR_TEMP = "min_color_temp", 2000 DOCS[CONF_MIN_COLOR_TEMP] = "Warmest color temperature in Kelvin. 🔥" +CONF_REDUCE_DAYTIME_BRIGHTNESS, DEFAULT_REDUCE_DAYTIME_BRIGHTNESS = "reduce_daytime_brightness", 0 +DOCS[CONF_REDUCE_DAYTIME_BRIGHTNESS] = "Reduce brightness during the daytime (%)" + CONF_ONLY_ONCE, DEFAULT_ONLY_ONCE = "only_once", False DOCS[CONF_ONLY_ONCE] = ( "Adapt lights only when they are turned on (`true`) or keep adapting them " @@ -305,6 +308,7 @@ def int_between(min_int, max_int): (CONF_MAX_BRIGHTNESS, DEFAULT_MAX_BRIGHTNESS, int_between(1, 100)), (CONF_MIN_COLOR_TEMP, DEFAULT_MIN_COLOR_TEMP, int_between(1000, 10000)), (CONF_MAX_COLOR_TEMP, DEFAULT_MAX_COLOR_TEMP, int_between(1000, 10000)), + (CONF_REDUCE_DAYTIME_BRIGHTNESS, DEFAULT_REDUCE_DAYTIME_BRIGHTNESS , int_between(0, 100)), (CONF_PREFER_RGB_COLOR, DEFAULT_PREFER_RGB_COLOR, bool), (CONF_SLEEP_BRIGHTNESS, DEFAULT_SLEEP_BRIGHTNESS, int_between(1, 100)), ( diff --git a/custom_components/adaptive_lighting/services.yaml b/custom_components/adaptive_lighting/services.yaml index 4b79f88cf..903dfcd7a 100644 --- a/custom_components/adaptive_lighting/services.yaml +++ b/custom_components/adaptive_lighting/services.yaml @@ -129,6 +129,12 @@ change_switch_settings: example: 2000 selector: text: null + reduce_daytime_brightness: + description: Reduction of brightness during the day. 🌞 + required: false + example: 50 + selector: + text: null only_once: description: Adapt lights only when they are turned on (`true`) or keep adapting them (`false`). 🔄 example: false diff --git a/custom_components/adaptive_lighting/strings.json b/custom_components/adaptive_lighting/strings.json index f6f6890b5..d54a281cf 100644 --- a/custom_components/adaptive_lighting/strings.json +++ b/custom_components/adaptive_lighting/strings.json @@ -27,6 +27,7 @@ "max_brightness": "max_brightness: Maximum brightness percentage. 💡", "min_color_temp": "min_color_temp: Warmest color temperature in Kelvin. 🔥", "max_color_temp": "max_color_temp: Coldest color temperature in Kelvin. ❄️", + "reduce_daytime_brightness": "reduce_daytime_brightness: How much to reduce brightness during daytime in percent. 🌞", "prefer_rgb_color": "prefer_rgb_color: Whether to prefer RGB color adjustment over light color temperature when possible. 🌈", "sleep_brightness": "sleep_brightness", "sleep_rgb_or_color_temp": "sleep_rgb_or_color_temp", diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index 893e75658..8a39679c6 100644 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -127,6 +127,7 @@ CONF_MAX_SUNSET_TIME, CONF_MIN_BRIGHTNESS, CONF_MIN_COLOR_TEMP, + CONF_REDUCE_DAYTIME_BRIGHTNESS, CONF_MIN_SUNRISE_TIME, CONF_MIN_SUNSET_TIME, CONF_MULTI_LIGHT_INTERCEPT, @@ -917,6 +918,7 @@ def _set_changeable_settings( max_color_temp=data[CONF_MAX_COLOR_TEMP], min_brightness=data[CONF_MIN_BRIGHTNESS], min_color_temp=data[CONF_MIN_COLOR_TEMP], + reduce_daytime_brightness=data[CONF_REDUCE_DAYTIME_BRIGHTNESS], sleep_brightness=data[CONF_SLEEP_BRIGHTNESS], sleep_color_temp=data[CONF_SLEEP_COLOR_TEMP], sleep_rgb_color=data[CONF_SLEEP_RGB_COLOR], diff --git a/webapp/app.py b/webapp/app.py index ab8966875..4509547b9 100644 --- a/webapp/app.py +++ b/webapp/app.py @@ -227,6 +227,7 @@ def plot_color_temp(inputs: dict[str, Any], sleep_mode: bool) -> plt.Figure: ui.input_slider("max_brightness", "max_brightness", 1, 100, 100, post="%"), ui.input_numeric("min_color_temp", "min_color_temp", 2000), ui.input_numeric("max_color_temp", "max_color_temp", 6666), + ui.input_slider("reduce_daytime_brightness", "reduce_daytime_brightness", 0, 100, 0, post="%"), ui.input_slider( "sleep_brightness", "sleep_brightness", @@ -308,6 +309,7 @@ def _kw(input): "min_brightness": input.min_brightness(), "min_color_temp": input.min_color_temp(), "max_color_temp": input.max_color_temp(), + "reduce_daytime_brightness": input.reduce_daytime_brightness(), "sleep_brightness": input.sleep_brightness(), "sleep_rgb_or_color_temp": input.sleep_rgb_or_color_temp(), "sleep_color_temp": input.sleep_color_temp(),