diff --git a/README.md b/README.md index 2fe5fd06..aa955ee2 100644 --- a/README.md +++ b/README.md @@ -47,30 +47,32 @@ adaptive_lighting: ``` ### Options -| option | description | required | default | type | -|-----------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------|-----------|---------| -| name | The name to use when displaying this switch. | False | default | string | -| lights | List of light entities for Adaptive Lighting to control (may be empty). | False | list | [] | -| prefer_rgb_color | Whether to use RGB color adjustment instead of native light color temperature. | False | False | boolean | -| initial_transition | How long the first transition is when the lights go from `off` to `on`. | False | 1 | time | -| sleep_transition | How long the transition is when when "sleep mode" is toggled | False | 1 | time | -| transition | How long the transition is when the lights change, in seconds. | False | 45 | integer | -| interval | How often to adapt the lights, in seconds. | False | 90 | integer | -| min_brightness | The minimum percent of brightness to set the lights to. | False | 1 | integer | -| max_brightness | The maximum percent of brightness to set the lights to. | False | 100 | integer | -| min_color_temp | The warmest color temperature to set the lights to, in Kelvin. | False | 2000 | integer | -| max_color_temp | The coldest color temperature to set the lights to, in Kelvin. | False | 5500 | integer | -| sleep_brightness | Brightness of lights while the sleep mode is enabled. | False | 1 | integer | -| sleep_color_temp | Color temperature of lights while the sleep mode is enabled. | False | 1000 | integer | -| sunrise_time | Override the sunrise time with a fixed time. | False | time | | -| sunrise_offset | Change the sunrise time with a positive or negative offset. | False | 0 | time | -| sunset_time | Override the sunset time with a fixed time. | False | time | | -| sunset_offset | Change the sunset time with a positive or negative offset. | False | 0 | time | -| only_once | Whether to keep adapting the lights (false) or to only adapt the lights as soon as they are turned on (true). | False | False | boolean | -| take_over_control | If another source calls `light.turn_on` while the lights are on and being adapted, disable Adaptive Lighting. | False | True | boolean | -| detect_non_ha_changes | Whether to detect state changes and stop adapting lights, even not from `light.turn_on`. Needs `take_over_control` to be enabled. Note that by enabling this option, it calls 'homeassistant.update_entity' every 'interval'! | False | False | boolean | -| separate_turn_on_commands | Whether to use separate `light.turn_on` calls for color and brightness, needed for some types of lights | False | False | boolean | -| adapt_delay | Wait time in seconds between light turn on, and Adaptive Lights applying changes to the light state. May avoid flickering. | False | 0 | integer | +| option | description | required | default | type | +| ------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | -------------- | ------- | +| name | The name to use when displaying this switch. | False | default | string | +| lights | List of light entities for Adaptive Lighting to control (may be empty). | False | list | [] | +| prefer_rgb_color | Whether to use RGB color adjustment instead of native light color temperature. | False | False | boolean | +| initial_transition | How long the first transition is when the lights go from `off` to `on`. | False | 1 | time | +| sleep_transition | How long the transition is when when "sleep mode" is toggled | False | 1 | time | +| transition | How long the transition is when the lights change, in seconds. | False | 45 | integer | +| interval | How often to adapt the lights, in seconds. | False | 90 | integer | +| min_brightness | The minimum percent of brightness to set the lights to. | False | 1 | integer | +| max_brightness | The maximum percent of brightness to set the lights to. | False | 100 | integer | +| min_color_temp | The warmest color temperature to set the lights to, in Kelvin. | False | 2000 | integer | +| max_color_temp | The coldest color temperature to set the lights to, in Kelvin. | False | 5500 | integer | +| sleep_brightness | Brightness of lights while the sleep mode is enabled. | False | 1 | integer | +| sleep_rgb_or_color_temp | Use either 'rgb_color' or 'color_temp' when in sleep mode. | False | 'color_temp' | string | +| sleep_rgb_color | List of three numbers between 0-255, indicating the RGB color in sleep mode (only used when sleep_rgb_or_color_temp is 'rgb_color'). | False | `[255, 56, 0]` | list | +| sleep_color_temp | Color temperature of lights while the sleep mode is enabled (only used when sleep_rgb_or_color_temp is 'color_temp'). | False | 1000 | integer | +| sunrise_time | Override the sunrise time with a fixed time. | False | time | | +| sunrise_offset | Change the sunrise time with a positive or negative offset. | False | 0 | time | +| sunset_time | Override the sunset time with a fixed time. | False | time | | +| sunset_offset | Change the sunset time with a positive or negative offset. | False | 0 | time | +| only_once | Whether to keep adapting the lights (false) or to only adapt the lights as soon as they are turned on (true). | False | False | boolean | +| take_over_control | If another source calls `light.turn_on` while the lights are on and being adapted, disable Adaptive Lighting. | False | True | boolean | +| detect_non_ha_changes | Whether to detect state changes and stop adapting lights, even not from `light.turn_on`. Needs `take_over_control` to be enabled. Note that by enabling this option, it calls 'homeassistant.update_entity' every 'interval'! | False | False | boolean | +| separate_turn_on_commands | Whether to use separate `light.turn_on` calls for color and brightness, needed for some types of lights | False | False | boolean | +| adapt_delay | Wait time in seconds between light turn on, and Adaptive Lights applying changes to the light state. May avoid flickering. | False | 0 | integer | Full example: @@ -101,25 +103,25 @@ adaptive_lighting: ### Services -`adaptive_lighting.apply` applies Adaptive Lighting settings to lights on demand. +`adaptive_lighting.**apply**` applies Adaptive Lighting settings to lights on demand. -| Service data attribute | Optional | Description | -|---------------------------|----------|-------------------------------------------------------------------------| -| `entity_id` | no | The `entity_id` of the switch with the settings to apply. | -| `lights` | no | A light (or list of lights) to apply the settings to. | -| `transition` | yes | The number of seconds for the transition. | -| `adapt_brightness` | yes | Whether to change the brightness of the light or not. | -| `adapt_color` | yes | Whether to adapt the color on supporting lights. | -| `prefer_rgb_color` | yes | Whether to prefer RGB color adjustment over of native light color temperature when possible. | -| `turn_on_lights` | yes | Whether to turn on lights that are currently off. | +| Service data attribute | Optional | Description | +| ---------------------- | -------- | -------------------------------------------------------------------------------------------- | +| `entity_id` | no | The `entity_id` of the switch with the settings to apply. | +| `lights` | no | A light (or list of lights) to apply the settings to. | +| `transition` | yes | The number of seconds for the transition. | +| `adapt_brightness` | yes | Whether to change the brightness of the light or not. | +| `adapt_color` | yes | Whether to adapt the color on supporting lights. | +| `prefer_rgb_color` | yes | Whether to prefer RGB color adjustment over of native light color temperature when possible. | +| `turn_on_lights` | yes | Whether to turn on lights that are currently off. | `adaptive_lighting.set_manual_control` can mark (or unmark) whether a light is "manually controlled", meaning that when a light has `manual_control`, the light is not adapted. -| Service data attribute | Optional | Description | -|------------------------|----------|--------------------------------------------------------------------------------------------------------------------------------------| -| `entity_id` | no | The `entity_id` of the switch in which to (un)mark the light as being "manually controlled". | -| `lights` | yes | entity_id(s) of lights, if not specified, all lights in the switch are selected. | -| `manual_control` | yes | Whether to add ('true') or remove ('false') the light from the 'manual_control' list, default: true | +| Service data attribute | Optional | Description | +| ---------------------- | -------- | --------------------------------------------------------------------------------------------------- | +| `entity_id` | no | The `entity_id` of the switch in which to (un)mark the light as being "manually controlled". | +| `lights` | yes | entity_id(s) of lights, if not specified, all lights in the switch are selected. | +| `manual_control` | yes | Whether to add ('true') or remove ('false') the light from the 'manual_control' list, default: true | ## Automation examples diff --git a/custom_components/adaptive_lighting/const.py b/custom_components/adaptive_lighting/const.py index 6d144ee2..b800015b 100644 --- a/custom_components/adaptive_lighting/const.py +++ b/custom_components/adaptive_lighting/const.py @@ -1,5 +1,7 @@ """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 +32,11 @@ 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_SLEEP_RGB_OR_COLOR_TEMP, DEFAULT_SLEEP_RGB_OR_COLOR_TEMP = ( + "sleep_rgb_or_color_temp", + "color_temp", +) CONF_SUNRISE_OFFSET, DEFAULT_SUNRISE_OFFSET = "sunrise_offset", 0 CONF_SUNRISE_TIME = "sunrise_time" CONF_SUNSET_OFFSET, DEFAULT_SUNSET_OFFSET = "sunset_offset", 0 @@ -72,7 +79,23 @@ VALIDATION_TUPLES = [ (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_SLEEP_BRIGHTNESS, DEFAULT_SLEEP_BRIGHTNESS, int_between(1, 100)), + ( + CONF_SLEEP_RGB_OR_COLOR_TEMP, + DEFAULT_SLEEP_RGB_OR_COLOR_TEMP, + selector.SelectSelector( + selector.SelectSelectorConfig( + options=["color_temp", "rgb_color"], + multiple=False, + mode=selector.SelectSelectorMode.DROPDOWN, + ), + ), + ), (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/manifest.json b/custom_components/adaptive_lighting/manifest.json index 529820f3..5b852cb4 100644 --- a/custom_components/adaptive_lighting/manifest.json +++ b/custom_components/adaptive_lighting/manifest.json @@ -6,7 +6,7 @@ "config_flow": true, "dependencies": [], "codeowners": ["@basnijholt", "@RubenKelevra"], - "version": "1.0.20", + "version": "1.1.0", "requirements": [], "iot_class": "calculated" } diff --git a/custom_components/adaptive_lighting/strings.json b/custom_components/adaptive_lighting/strings.json index 5a185ea3..0755cca8 100644 --- a/custom_components/adaptive_lighting/strings.json +++ b/custom_components/adaptive_lighting/strings.json @@ -20,26 +20,28 @@ "description": "All settings for a Adaptive Lighting component. The option names correspond with the YAML settings. No options are shown if you have this entry defined in YAML.", "data": { "lights": "lights", - "initial_transition": "initial_transition, when lights go 'off' to 'on'", - "sleep_transition": "sleep_transition, when 'sleep_state' changes", - "interval": "interval, time between switch updates in seconds", - "max_brightness": "max_brightness, in %", - "max_color_temp": "max_color_temp, in Kelvin", - "min_brightness": "min_brightness, in %", - "min_color_temp": "min_color_temp, in Kelvin", - "only_once": "only_once, only adapt the lights when turning them on", - "prefer_rgb_color": "prefer_rgb_color, use 'rgb_color' over 'color_temp' when possible", - "separate_turn_on_commands": "separate_turn_on_commands, for each attribute (color, brightness, etc.) in 'light.turn_on', required for some lights.", - "sleep_brightness": "sleep_brightness, in %", - "sleep_color_temp": "sleep_color_temp, in Kelvin", - "sunrise_offset": "sunrise_offset, in +/- seconds", - "sunrise_time": "sunrise_time, in 'HH:MM:SS' format (if 'None', it uses the actual sunrise time at your location)", - "sunset_offset": "sunset_offset, in +/- seconds", - "sunset_time": "sunset_time, in 'HH:MM:SS' format (if 'None', it uses the actual sunset time at your location)", - "take_over_control": "take_over_control, if anything but Adaptive Lighting calls 'light.turn_on' when a light is already on, stop adapting that light until it (or the switch) toggles off -> on.", - "detect_non_ha_changes": "detect_non_ha_changes, detects all >5% changes made to the lights (also outside of HA), requires 'take_over_control' to be enabled (calls 'homeassistant.update_entity' every 'interval'!)", - "transition": "transition, in seconds", - "adapt_delay": "Wait time between light turn on, and Adaptive Lights applying changes to the light state. May avoid flickering." + "initial_transition": "initial_transition: When lights turn 'off' to 'on'. (seconds)", + "sleep_transition": "sleep_transition: When 'sleep_state' changes. (seconds)", + "interval": "interval: Time between switch updates. (seconds)", + "max_brightness": "max_brightness: Highest brightness of lights during a cycle. (%)", + "max_color_temp": "max_color_temp: Coldest hue of the color temperature cycle. (Kelvin)", + "min_brightness": "min_brightness: Lowest brightness of lights during a cycle. (%)", + "min_color_temp": "min_color_temp, Warmest hue of the color temperature cycle. (Kelvin)", + "only_once": "only_once: Only adapt the lights when turning them on.", + "prefer_rgb_color": "prefer_rgb_color: Use 'rgb_color' rather than 'color_temp' when possible.", + "separate_turn_on_commands": "separate_turn_on_commands: Separate the commands for each attribute (color, brightness, etc.) in 'light.turn_on' (required for some lights).", + "sleep_brightness": "sleep_brightness, Brightness setting for Sleep Mode. (%)", + "sleep_rgb_or_color_temp": "sleep_rgb_or_color_temp, use 'rgb_color' or 'color_temp'", + "sleep_rgb_color": "sleep_rgb_color, in RGB", + "sleep_color_temp": "sleep_color_temp: Color temperature setting for Sleep Mode. (Kelvin)", + "sunrise_offset": "sunrise_offset: How long before(-) or after(+) to define the sunrise point of the cycle (+/- seconds)", + "sunrise_time": "sunrise_time: Manual override of the sunrise time, if 'None', it uses the actual sunrise time at your location (HH:MM:SS)", + "sunset_offset": "sunset_offset: How long before(-) or after(+) to define the sunset point of the cycle (+/- seconds)", + "sunset_time": "sunset_time: Manual override of the sunset time, if 'None', it uses the actual sunrise time at your location (HH:MM:SS)", + "take_over_control": "take_over_control: If anything but Adaptive Lighting calls 'light.turn_on' when a light is already on, stop adapting that light until it (or the switch) toggles off -> on.", + "detect_non_ha_changes": "detect_non_ha_changes: detects all >10% changes made to the lights (also outside of HA), requires 'take_over_control' to be enabled (calls 'homeassistant.update_entity' every 'interval'!)", + "transition": "Transition time when applying a change to the lights (seconds)", + "adapt_delay": "adapt_delay: wait time between light turn on (seconds), and Adaptive Lights applying changes to the light state. May avoid flickering." } } }, diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index 922d1e1d..e3688a62 100755 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -12,7 +12,7 @@ from datetime import timedelta import functools import logging import math -from typing import Any +from typing import Any, Literal import astral from homeassistant.components.light import ( @@ -113,6 +113,8 @@ from .const import ( CONF_SEPARATE_TURN_ON_COMMANDS, CONF_SLEEP_BRIGHTNESS, CONF_SLEEP_COLOR_TEMP, + CONF_SLEEP_RGB_COLOR, + CONF_SLEEP_RGB_OR_COLOR_TEMP, CONF_SLEEP_TRANSITION, CONF_SUNRISE_OFFSET, CONF_SUNRISE_TIME, @@ -587,6 +589,8 @@ 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], + sleep_rgb_or_color_temp=data[CONF_SLEEP_RGB_OR_COLOR_TEMP], sunrise_offset=data[CONF_SUNRISE_OFFSET], sunrise_time=data[CONF_SUNRISE_TIME], sunset_offset=data[CONF_SUNSET_OFFSET], @@ -802,17 +806,24 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): brightness = round(255 * self._settings["brightness_pct"] / 100) service_data[ATTR_BRIGHTNESS] = brightness + sleep_rgb = ( + self.sleep_mode_switch.is_on + and self._sun_light_settings.sleep_rgb_or_color_temp == "rgb_color" + ) if ( "color_temp" in features and adapt_color and not (prefer_rgb_color and "color" in features) + and not (sleep_rgb and "color" in features) ): + _LOGGER.debug("%s: Setting color_temp of light %s", self._name, light) attributes = self.hass.states.get(light).attributes min_mireds, max_mireds = attributes["min_mireds"], attributes["max_mireds"] color_temp_mired = self._settings["color_temp_mired"] color_temp_mired = max(min(color_temp_mired, max_mireds), min_mireds) service_data[ATTR_COLOR_TEMP] = color_temp_mired elif "color" in features and adapt_color: + _LOGGER.debug("%s: Setting rgb_color of light %s", self._name, light) service_data[ATTR_RGB_COLOR] = self._settings["rgb_color"] context = context or self.create_context("adapt_lights") @@ -1073,7 +1084,9 @@ class SunLightSettings: min_brightness: int min_color_temp: int sleep_brightness: int + sleep_rgb_or_color_temp: Literal["color_temp", "rgb_color"] 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 +1205,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 +1225,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 { diff --git a/custom_components/adaptive_lighting/translations/en.json b/custom_components/adaptive_lighting/translations/en.json index ecc6eff8..d4070a30 100644 --- a/custom_components/adaptive_lighting/translations/en.json +++ b/custom_components/adaptive_lighting/translations/en.json @@ -32,6 +32,8 @@ "prefer_rgb_color": "prefer_rgb_color: Use 'rgb_color' rather than 'color_temp' when possible.", "separate_turn_on_commands": "separate_turn_on_commands: Separate the commands for each attribute (color, brightness, etc.) in 'light.turn_on' (required for some lights).", "sleep_brightness": "sleep_brightness, Brightness setting for Sleep Mode. (%)", + "sleep_rgb_or_color_temp": "sleep_rgb_or_color_temp, use 'rgb_color' or 'color_temp'", + "sleep_rgb_color": "sleep_rgb_color, in RGB", "sleep_color_temp": "sleep_color_temp: Color temperature setting for Sleep Mode. (Kelvin)", "sunrise_offset": "sunrise_offset: How long before(-) or after(+) to define the sunrise point of the cycle (+/- seconds)", "sunrise_time": "sunrise_time: Manual override of the sunrise time, if 'None', it uses the actual sunrise time at your location (HH:MM:SS)", @@ -40,7 +42,7 @@ "take_over_control": "take_over_control: If anything but Adaptive Lighting calls 'light.turn_on' when a light is already on, stop adapting that light until it (or the switch) toggles off -> on.", "detect_non_ha_changes": "detect_non_ha_changes: detects all >10% changes made to the lights (also outside of HA), requires 'take_over_control' to be enabled (calls 'homeassistant.update_entity' every 'interval'!)", "transition": "Transition time when applying a change to the lights (seconds)", - "adapt_delay": "Wait time between light turn on, and Adaptive Lights applying changes to the light state. May avoid flickering." + "adapt_delay": "adapt_delay: wait time between light turn on (seconds), and Adaptive Lights applying changes to the light state. May avoid flickering." } } },