diff --git a/custom_components/adaptive_lighting/const.py b/custom_components/adaptive_lighting/const.py index 64620927..3b3488f2 100644 --- a/custom_components/adaptive_lighting/const.py +++ b/custom_components/adaptive_lighting/const.py @@ -68,6 +68,14 @@ DOCS[CONF_MIN_BRIGHTNESS] = "Minimum brightness percentage. 💡" CONF_MIN_COLOR_TEMP, DEFAULT_MIN_COLOR_TEMP = "min_color_temp", 2000 DOCS[CONF_MIN_COLOR_TEMP] = "Warmest color temperature in Kelvin. 🔥" +CONF_FLAT_LIMITS, DEFAULT_FLAT_LIMITS = "flat_limits", False +DOCS[CONF_FLAT_LIMITS] = ( + "When True, will not calculate between the" + " max/min supported limits of your light. Example: when adapting brightness to 50% while " + + CONF_MAX_BRIGHTNESS + + " is set to 80%, Adaptive Lighting will use 80% instead of 90%" +) + 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 " @@ -261,6 +269,7 @@ VALIDATION_TUPLES = [ (CONF_MIN_SUNSET_TIME, NONE_STR, str), (CONF_SUNSET_OFFSET, DEFAULT_SUNSET_OFFSET, int), (CONF_ONLY_ONCE, DEFAULT_ONLY_ONCE, bool), + (CONF_FLAT_LIMITS, DEFAULT_FLAT_LIMITS, bool), (CONF_TAKE_OVER_CONTROL, DEFAULT_TAKE_OVER_CONTROL, bool), (CONF_DETECT_NON_HA_CHANGES, DEFAULT_DETECT_NON_HA_CHANGES, bool), (CONF_SEPARATE_TURN_ON_COMMANDS, DEFAULT_SEPARATE_TURN_ON_COMMANDS, bool), diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index 98747a17..ba0575f9 100644 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -101,6 +101,7 @@ from .const import ( CONF_ADAPT_UNTIL_SLEEP, CONF_AUTORESET_CONTROL, CONF_DETECT_NON_HA_CHANGES, + CONF_FLAT_LIMITS, CONF_INCLUDE_CONFIG_IN_ATTRIBUTES, CONF_INITIAL_TRANSITION, CONF_INTERVAL, @@ -897,6 +898,7 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): name=self._name, astral_location=location, adapt_until_sleep=data[CONF_ADAPT_UNTIL_SLEEP], + flat_limits=data[CONF_FLAT_LIMITS], max_brightness=data[CONF_MAX_BRIGHTNESS], max_color_temp=data[CONF_MAX_COLOR_TEMP], min_brightness=data[CONF_MIN_BRIGHTNESS], @@ -1437,6 +1439,7 @@ class SunLightSettings: name: str astral_location: astral.Location adapt_until_sleep: bool + flat_limits: bool max_brightness: int max_color_temp: int min_brightness: int @@ -1576,8 +1579,14 @@ class SunLightSettings: return self.sleep_brightness if percent > 0: return self.max_brightness - delta_brightness = self.max_brightness - self.min_brightness percent = 1 + percent + if self.flat_limits: + if percent * 100 > self.max_brightness: + return self.max_brightness + elif percent * 100 < self.min_brightness: + return self.min_brightness + return percent * 100 + delta_brightness = self.max_brightness - self.min_brightness return (delta_brightness * percent) + self.min_brightness def calc_color_temp_kelvin(self, percent: float) -> int: