From 7d10259bc8ba1a39bac07af7544ad1b41d63ed72 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Wed, 31 Aug 2022 22:01:21 -0700 Subject: [PATCH] Use RGB selector --- custom_components/adaptive_lighting/const.py | 7 +++++++ custom_components/adaptive_lighting/switch.py | 19 ++++++++++++------- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/custom_components/adaptive_lighting/const.py b/custom_components/adaptive_lighting/const.py index 6d144ee2..e860fa74 100644 --- a/custom_components/adaptive_lighting/const.py +++ b/custom_components/adaptive_lighting/const.py @@ -1,5 +1,6 @@ """Constants for the Adaptive Lighting integration.""" from homeassistant.components.light import VALID_TRANSITION +from homeassistant.helpers import selector import homeassistant.helpers.config_validation as cv import voluptuous as vol @@ -30,6 +31,7 @@ CONF_SEPARATE_TURN_ON_COMMANDS, DEFAULT_SEPARATE_TURN_ON_COMMANDS = ( ) CONF_SLEEP_BRIGHTNESS, DEFAULT_SLEEP_BRIGHTNESS = "sleep_brightness", 1 CONF_SLEEP_COLOR_TEMP, DEFAULT_SLEEP_COLOR_TEMP = "sleep_color_temp", 1000 +CONF_SLEEP_RGB_COLOR, DEFAULT_SLEEP_RGB_COLOR = "sleep_rgb_color", [255, 56, 0] CONF_SUNRISE_OFFSET, DEFAULT_SUNRISE_OFFSET = "sunrise_offset", 0 CONF_SUNRISE_TIME = "sunrise_time" CONF_SUNSET_OFFSET, DEFAULT_SUNSET_OFFSET = "sunset_offset", 0 @@ -73,6 +75,11 @@ VALIDATION_TUPLES = [ (CONF_MAX_COLOR_TEMP, DEFAULT_MAX_COLOR_TEMP, int_between(1000, 10000)), (CONF_SLEEP_BRIGHTNESS, DEFAULT_SLEEP_BRIGHTNESS, int_between(1, 100)), (CONF_SLEEP_COLOR_TEMP, DEFAULT_SLEEP_COLOR_TEMP, int_between(1000, 10000)), + ( + CONF_SLEEP_RGB_COLOR, + DEFAULT_SLEEP_RGB_COLOR, + selector.ColorRGBSelector(selector.ColorRGBSelectorConfig()), + ), (CONF_SUNRISE_TIME, NONE_STR, str), (CONF_SUNRISE_OFFSET, DEFAULT_SUNRISE_OFFSET, int), (CONF_SUNSET_TIME, NONE_STR, str), diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index 922d1e1d..bfea3b48 100755 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -113,6 +113,7 @@ from .const import ( CONF_SEPARATE_TURN_ON_COMMANDS, CONF_SLEEP_BRIGHTNESS, CONF_SLEEP_COLOR_TEMP, + CONF_SLEEP_RGB_COLOR, CONF_SLEEP_TRANSITION, CONF_SUNRISE_OFFSET, CONF_SUNRISE_TIME, @@ -587,6 +588,7 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): min_color_temp=data[CONF_MIN_COLOR_TEMP], sleep_brightness=data[CONF_SLEEP_BRIGHTNESS], sleep_color_temp=data[CONF_SLEEP_COLOR_TEMP], + sleep_rgb_color=data[CONF_SLEEP_RGB_COLOR], sunrise_offset=data[CONF_SUNRISE_OFFSET], sunrise_time=data[CONF_SUNRISE_TIME], sunset_offset=data[CONF_SUNSET_OFFSET], @@ -1074,6 +1076,7 @@ class SunLightSettings: min_color_temp: int sleep_brightness: int sleep_color_temp: int + sleep_rgb_color: tuple[int, int, int] sunrise_offset: datetime.timedelta | None sunrise_time: datetime.time | None sunset_offset: datetime.timedelta | None @@ -1192,10 +1195,8 @@ class SunLightSettings: percent = 1 + percent return (delta_brightness * percent) + self.min_brightness - def calc_color_temp_kelvin(self, percent: float, is_sleep: bool) -> float: + def calc_color_temp_kelvin(self, percent: float) -> float: """Calculate the color temperature in Kelvin.""" - if is_sleep: - return self.sleep_color_temp if percent > 0: delta = self.max_color_temp - self.min_color_temp return (delta * percent) + self.min_color_temp @@ -1214,11 +1215,15 @@ class SunLightSettings: else self.calc_percent(0) ) brightness_pct = self.calc_brightness_pct(percent, is_sleep) - color_temp_kelvin = self.calc_color_temp_kelvin(percent, is_sleep) + if is_sleep: + color_temp_kelvin = self.sleep_color_temp + rgb_color: tuple[float, float, float] = self.sleep_rgb_color + else: + color_temp_kelvin = self.calc_color_temp_kelvin(percent) + rgb_color: tuple[float, float, float] = color_temperature_to_rgb( + color_temp_kelvin + ) color_temp_mired: float = color_temperature_kelvin_to_mired(color_temp_kelvin) - rgb_color: tuple[float, float, float] = color_temperature_to_rgb( - color_temp_kelvin - ) xy_color: tuple[float, float] = color_RGB_to_xy(*rgb_color) hs_color: tuple[float, float] = color_xy_to_hs(*xy_color) return {