Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature for reducing the brightness of lights during the daytime #1075

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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']` |
Expand Down Expand Up @@ -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
Expand Down
18 changes: 12 additions & 6 deletions custom_components/adaptive_lighting/color_and_brightness.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."""
Expand Down
11 changes: 11 additions & 0 deletions custom_components/adaptive_lighting/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@
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 "
Expand Down Expand Up @@ -305,6 +311,11 @@ 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)),
(
Expand Down
6 changes: 6 additions & 0 deletions custom_components/adaptive_lighting/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions custom_components/adaptive_lighting/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions custom_components/adaptive_lighting/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
CONF_MULTI_LIGHT_INTERCEPT,
CONF_ONLY_ONCE,
CONF_PREFER_RGB_COLOR,
CONF_REDUCE_DAYTIME_BRIGHTNESS,
CONF_SEND_SPLIT_DELAY,
CONF_SEPARATE_TURN_ON_COMMANDS,
CONF_SKIP_REDUNDANT_COMMANDS,
Expand Down Expand Up @@ -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],
Expand Down
9 changes: 9 additions & 0 deletions webapp/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,14 @@ 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",
Expand Down Expand Up @@ -308,6 +316,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(),
Expand Down