Skip to content

Commit

Permalink
add #566
Browse files Browse the repository at this point in the history
  • Loading branch information
th3w1zard1 committed Apr 10, 2023
1 parent 2444fd3 commit 77a9018
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
19 changes: 18 additions & 1 deletion custom_components/adaptive_lighting/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,24 @@
False,
)
DOCS[CONF_ADAPT_UNTIL_SLEEP] = (
"This option ignores the current state of the sleep switch. "
"When enabled, Adaptive Lighting will treat sleep settings as the minimum, "
"transitioning to these values after sunset. 🌙"
"transitioning color temperature to these values after sunset. 🌙"
)
CONF_ADAPT_COLOR_TEMP_UNTIL_SLEEP, DEFAULT_ADAPT_COLOR_TEMP_UNTIL_SLEEP = (
"adapt_color_temp_until_sleep",
True,
)
DOCS[
CONF_ADAPT_COLOR_TEMP_UNTIL_SLEEP
] = "Only active when `transition_until_sleep` is true."
CONF_ADAPT_BRIGHTNESS_UNTIL_SLEEP, DEFAULT_ADAPT_BRIGHTNESS_UNTIL_SLEEP = (
"adapt_brightness_until_sleep",
False,
)
DOCS[
CONF_ADAPT_BRIGHTNESS_UNTIL_SLEEP
] = "Only active when `transition_until_sleep` is true."

CONF_ADAPT_DELAY, DEFAULT_ADAPT_DELAY = "adapt_delay", 0
DOCS[CONF_ADAPT_DELAY] = (
Expand Down Expand Up @@ -255,6 +270,8 @@ def int_between(min_int, max_int):
(CONF_SLEEP_TRANSITION, DEFAULT_SLEEP_TRANSITION, VALID_TRANSITION),
(CONF_TRANSITION, DEFAULT_TRANSITION, VALID_TRANSITION),
(CONF_ADAPT_UNTIL_SLEEP, DEFAULT_ADAPT_UNTIL_SLEEP, bool),
(CONF_ADAPT_BRIGHTNESS_UNTIL_SLEEP, DEFAULT_ADAPT_BRIGHTNESS_UNTIL_SLEEP, bool),
(CONF_ADAPT_COLOR_TEMP_UNTIL_SLEEP, DEFAULT_ADAPT_COLOR_TEMP_UNTIL_SLEEP, bool),
(CONF_INTERVAL, DEFAULT_INTERVAL, cv.positive_int),
(CONF_MIN_BRIGHTNESS, DEFAULT_MIN_BRIGHTNESS, int_between(1, 100)),
(CONF_MAX_BRIGHTNESS, DEFAULT_MAX_BRIGHTNESS, int_between(1, 100)),
Expand Down
12 changes: 11 additions & 1 deletion custom_components/adaptive_lighting/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@
ATTR_ADAPT_BRIGHTNESS,
ATTR_ADAPT_COLOR,
ATTR_TURN_ON_OFF_LISTENER,
CONF_ADAPT_BRIGHTNESS_UNTIL_SLEEP,
CONF_ADAPT_COLOR_TEMP_UNTIL_SLEEP,
CONF_ADAPT_DELAY,
CONF_ADAPT_UNTIL_SLEEP,
CONF_ALT_DETECT_METHOD,
Expand Down Expand Up @@ -952,6 +954,8 @@ def _set_changeable_settings(
self._sun_light_settings = SunLightSettings(
name=self._name,
astral_location=location,
adapt_brightness_until_sleep=data[CONF_ADAPT_BRIGHTNESS_UNTIL_SLEEP],
adapt_color_temp_until_sleep=data[CONF_ADAPT_COLOR_TEMP_UNTIL_SLEEP],
adapt_until_sleep=data[CONF_ADAPT_UNTIL_SLEEP],
flat_limits=data[CONF_FLAT_LIMITS],
max_brightness=data[CONF_MAX_BRIGHTNESS],
Expand Down Expand Up @@ -1502,6 +1506,8 @@ class SunLightSettings:

name: str
astral_location: astral.Location
adapt_brightness_until_sleep: bool
adapt_color_temp_until_sleep: bool
adapt_until_sleep: bool
flat_limits: bool
max_brightness: int
Expand Down Expand Up @@ -1643,6 +1649,10 @@ def calc_brightness_pct(self, percent: float, is_sleep: bool) -> float:
return self.sleep_brightness
if percent > 0:
return self.max_brightness
if self.adapt_until_sleep and self.adapt_brightness_until_sleep and percent < 0:
delta_brightness = abs(self.min_brightness - self.sleep_brightness)
return (delta_brightness * abs(1 + percent)) + self.sleep_brightness
delta_brightness = self.max_brightness - self.min_brightness
percent = 1 + percent
if self.flat_limits:
if percent * 100 > self.max_brightness:
Expand All @@ -1661,7 +1671,7 @@ def calc_color_temp_kelvin(self, percent: float) -> int:
return 5 * round(ct / 5) # round to nearest 5
if percent == 0 or not self.adapt_until_sleep:
return self.min_color_temp
if self.adapt_until_sleep and percent < 0:
if self.adapt_until_sleep and self.adapt_color_temp_until_sleep and percent < 0:
delta = abs(self.min_color_temp - self.sleep_color_temp)
ct = (delta * abs(1 + percent)) + self.sleep_color_temp
return 5 * round(ct / 5) # round to nearest 5
Expand Down

0 comments on commit 77a9018

Please sign in to comment.