Skip to content

Commit 3220b32

Browse files
committed
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
1 parent ccf4652 commit 3220b32

File tree

7 files changed

+29
-6
lines changed

7 files changed

+29
-6
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ The YAML and frontend configuration methods support all of the options listed be
113113
| `max_brightness` | Maximum brightness percentage. 💡 | `100` | `int` 1-100 |
114114
| `min_color_temp` | Warmest color temperature in Kelvin. 🔥 | `2000` | `int` 1000-10000 |
115115
| `max_color_temp` | Coldest color temperature in Kelvin. ❄️ | `5500` | `int` 1000-10000 |
116+
| `reduce_daytime_brightness` | By how much to reduce the brightness during daytime, in percentage. 🌞 | `0` | `int` 0-100 |
116117
| `prefer_rgb_color` | Whether to prefer RGB color adjustment over light color temperature when possible. 🌈 | `False` | `bool` |
117118
| `sleep_brightness` | Brightness percentage of lights in sleep mode. 😴 | `1` | `int` 1-100 |
118119
| `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:
161162
max_brightness: 100
162163
min_color_temp: 2000
163164
max_color_temp: 5500
165+
reduce_daytime_brightness: 50
164166
sleep_brightness: 1
165167
sleep_color_temp: 1000
166168
sunrise_time: "08:00:00" # override the sunrise time

custom_components/adaptive_lighting/color_and_brightness.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ class SunLightSettings:
208208
max_color_temp: int
209209
min_brightness: int
210210
min_color_temp: int
211+
reduce_daytime_brightness: int
211212
sleep_brightness: int
212213
sleep_rgb_or_color_temp: Literal["color_temp", "rgb_color"]
213214
sleep_color_temp: int
@@ -306,12 +307,17 @@ def brightness_pct(self, dt: datetime.datetime, is_sleep: bool) -> float:
306307
return self.sleep_brightness
307308
assert self.brightness_mode in ("default", "linear", "tanh")
308309
if self.brightness_mode == "default":
309-
return self._brightness_pct_default(dt)
310-
if self.brightness_mode == "linear":
311-
return self._brightness_pct_linear(dt)
312-
if self.brightness_mode == "tanh":
313-
return self._brightness_pct_tanh(dt)
314-
return None
310+
brightness = self._brightness_pct_default(dt)
311+
elif self.brightness_mode == "linear":
312+
brightness = self._brightness_pct_linear(dt)
313+
elif self.brightness_mode == "tanh":
314+
brightness = self._brightness_pct_tanh(dt)
315+
else
316+
return None
317+
sun_position = self.sun.sun_position(dt)
318+
if sun_position > 0
319+
brightness *= 1 - sun_position * (self.reduce_daytime_brightness / 100)
320+
return brightness
315321

316322
def color_temp_kelvin(self, sun_position: float) -> int:
317323
"""Calculate the color temperature in Kelvin."""

custom_components/adaptive_lighting/const.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@
6969
CONF_MIN_COLOR_TEMP, DEFAULT_MIN_COLOR_TEMP = "min_color_temp", 2000
7070
DOCS[CONF_MIN_COLOR_TEMP] = "Warmest color temperature in Kelvin. 🔥"
7171

72+
CONF_REDUCE_DAYTIME_BRIGHTNESS, DEFAULT_REDUCE_DAYTIME_BRIGHTNESS = "reduce_daytime_brightness", 0
73+
DOCS[CONF_REDUCE_DAYTIME_BRIGHTNESS] = "Reduce brightness during the daytime (%)"
74+
7275
CONF_ONLY_ONCE, DEFAULT_ONLY_ONCE = "only_once", False
7376
DOCS[CONF_ONLY_ONCE] = (
7477
"Adapt lights only when they are turned on (`true`) or keep adapting them "
@@ -305,6 +308,7 @@ def int_between(min_int, max_int):
305308
(CONF_MAX_BRIGHTNESS, DEFAULT_MAX_BRIGHTNESS, int_between(1, 100)),
306309
(CONF_MIN_COLOR_TEMP, DEFAULT_MIN_COLOR_TEMP, int_between(1000, 10000)),
307310
(CONF_MAX_COLOR_TEMP, DEFAULT_MAX_COLOR_TEMP, int_between(1000, 10000)),
311+
(CONF_REDUCE_DAYTIME_BRIGHTNESS, DEFAULT_REDUCE_DAYTIME_BRIGHTNESS , int_between(0, 100)),
308312
(CONF_PREFER_RGB_COLOR, DEFAULT_PREFER_RGB_COLOR, bool),
309313
(CONF_SLEEP_BRIGHTNESS, DEFAULT_SLEEP_BRIGHTNESS, int_between(1, 100)),
310314
(

custom_components/adaptive_lighting/services.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ change_switch_settings:
129129
example: 2000
130130
selector:
131131
text: null
132+
reduce_daytime_brightness:
133+
description: Reduction of brightness during the day. 🌞
134+
required: false
135+
example: 50
136+
selector:
137+
text: null
132138
only_once:
133139
description: Adapt lights only when they are turned on (`true`) or keep adapting them (`false`). 🔄
134140
example: false

custom_components/adaptive_lighting/strings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"max_brightness": "max_brightness: Maximum brightness percentage. 💡",
2828
"min_color_temp": "min_color_temp: Warmest color temperature in Kelvin. 🔥",
2929
"max_color_temp": "max_color_temp: Coldest color temperature in Kelvin. ❄️",
30+
"reduce_daytime_brightness": "reduce_daytime_brightness: How much to reduce brightness during daytime in percent. 🌞",
3031
"prefer_rgb_color": "prefer_rgb_color: Whether to prefer RGB color adjustment over light color temperature when possible. 🌈",
3132
"sleep_brightness": "sleep_brightness",
3233
"sleep_rgb_or_color_temp": "sleep_rgb_or_color_temp",

custom_components/adaptive_lighting/switch.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
CONF_MAX_SUNSET_TIME,
128128
CONF_MIN_BRIGHTNESS,
129129
CONF_MIN_COLOR_TEMP,
130+
CONF_REDUCE_DAYTIME_BRIGHTNESS,
130131
CONF_MIN_SUNRISE_TIME,
131132
CONF_MIN_SUNSET_TIME,
132133
CONF_MULTI_LIGHT_INTERCEPT,
@@ -917,6 +918,7 @@ def _set_changeable_settings(
917918
max_color_temp=data[CONF_MAX_COLOR_TEMP],
918919
min_brightness=data[CONF_MIN_BRIGHTNESS],
919920
min_color_temp=data[CONF_MIN_COLOR_TEMP],
921+
reduce_daytime_brightness=data[CONF_REDUCE_DAYTIME_BRIGHTNESS],
920922
sleep_brightness=data[CONF_SLEEP_BRIGHTNESS],
921923
sleep_color_temp=data[CONF_SLEEP_COLOR_TEMP],
922924
sleep_rgb_color=data[CONF_SLEEP_RGB_COLOR],

webapp/app.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ def plot_color_temp(inputs: dict[str, Any], sleep_mode: bool) -> plt.Figure:
227227
ui.input_slider("max_brightness", "max_brightness", 1, 100, 100, post="%"),
228228
ui.input_numeric("min_color_temp", "min_color_temp", 2000),
229229
ui.input_numeric("max_color_temp", "max_color_temp", 6666),
230+
ui.input_slider("reduce_daytime_brightness", "reduce_daytime_brightness", 0, 100, 0, post="%"),
230231
ui.input_slider(
231232
"sleep_brightness",
232233
"sleep_brightness",
@@ -308,6 +309,7 @@ def _kw(input):
308309
"min_brightness": input.min_brightness(),
309310
"min_color_temp": input.min_color_temp(),
310311
"max_color_temp": input.max_color_temp(),
312+
"reduce_daytime_brightness": input.reduce_daytime_brightness(),
311313
"sleep_brightness": input.sleep_brightness(),
312314
"sleep_rgb_or_color_temp": input.sleep_rgb_or_color_temp(),
313315
"sleep_color_temp": input.sleep_color_temp(),

0 commit comments

Comments
 (0)