Skip to content

Commit

Permalink
Add feature for reducing the brightness of lights during the daytime
Browse files Browse the repository at this point in the history
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
  • Loading branch information
haasn committed Dec 1, 2024
1 parent ccf4652 commit fed965a
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 6 deletions.
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
4 changes: 4 additions & 0 deletions custom_components/adaptive_lighting/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down Expand Up @@ -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)),
(
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 @@ -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,
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
2 changes: 2 additions & 0 deletions webapp/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit fed965a

Please sign in to comment.