From 744e43f4bf057775125de2ffc576d9902df8e530 Mon Sep 17 00:00:00 2001 From: "allcontributors[bot]" <46447321+allcontributors[bot]@users.noreply.github.com> Date: Tue, 4 Apr 2023 08:54:54 -0700 Subject: [PATCH 1/4] docs: add th3w1zard1 as a contributor for maintenance (#538) * docs: update README.md * docs: update .all-contributorsrc --------- Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com> --- .all-contributorsrc | 3 ++- README.md | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index 3872e09b..6c825dad 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -401,7 +401,8 @@ "profile": "https://github.com/th3w1zard1", "contributions": [ "code", - "bug" + "bug", + "maintenance" ] }, { diff --git a/README.md b/README.md index 65fc0d74..f013d6c5 100644 --- a/README.md +++ b/README.md @@ -445,7 +445,7 @@ These graphs were generated using the values calculated by the Adaptive Lighting TomΓ‘Ε‘ Valigura
TomΓ‘Ε‘ Valigura

🌍 - Benjamin Auquite
Benjamin Auquite

πŸ’» πŸ› + Benjamin Auquite
Benjamin Auquite

πŸ’» πŸ› 🚧 Skyler Carlson
Skyler Carlson

πŸ“– Chris
Chris

πŸ’» Raman Gupta
Raman Gupta

πŸ’» From 4fcf238360f9cd4528d89f3c17c86f44ac61ec3a Mon Sep 17 00:00:00 2001 From: Benjamin Auquite Date: Tue, 4 Apr 2023 22:52:54 -0500 Subject: [PATCH 2/4] Update README.md on transition_until_sleep parameter (#539) * Update README.md I believe you changed the config option's name after I posted the graph, so I renamed the config option there too. There was also a deleted user on the contributions list so I went ahead and removed that too. * chore(docs): update TOC * Update README.md --------- Co-authored-by: th3w1zard1 --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f013d6c5..9ba7cc03 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,9 @@ ![](https://github.com/home-assistant/brands/raw/b4a168b9af282ef916e120d31091ecd5e3c35e66/core_integrations/adaptive_lighting/icon.png) -Adaptive Lighting is a custom component for Home Assistant that intelligently adjusts the brightness and color of your lights πŸ’‘ based on the sun's position, while still allowing for manual control. Try it out now by finding it in HACS (Home Assistant Community Store) and installing it! +Adaptive Lighting is a custom component for [Home Assistant](https://www.home-assistant.io/) that intelligently adjusts the brightness and color of your lights πŸ’‘ based on the sun's position, while still allowing for manual control. + +Download and install directly through [HACS (Home Assistant Community Store)](https://hacs.xyz/) By automatically adapting the settings of your lights throughout the day, Adaptive Lighting helps maintain your natural circadian rhythm 😴, which can lead to improved sleep, mood, and overall well-being. Experience cooler color temperatures at noon, gradually transitioning to warmer colors at sunset and sunrise. @@ -58,7 +60,7 @@ The `adaptive_lighting.manual_control` event is fired when a light is marked as - [:sunny: Sun Position](#sunny-sun-position) - [:thermometer: Color Temperature](#thermometer-color-temperature) - [:high_brightness: Brightness](#high_brightness-brightness) - - [While using `adapt_until_sleep: true`](#while-using-adapt_until_sleep-true) + - [While using `transition_until_sleep: true`](#while-using-transition_until_sleep-true) - [:busts_in_silhouette: Contributors](#busts_in_silhouette-contributors) @@ -379,7 +381,7 @@ These graphs were generated using the values calculated by the Adaptive Lighting #### :high_brightness: Brightness ![cl_brightness|690x130](https://community-home-assistant-assets.s3.dualstack.us-west-2.amazonaws.com/original/3X/5/8/58ebd994b62a8b1abfb3497a5288d923ff4e2330.PNG) -#### While using `adapt_until_sleep: true` +#### While using `transition_until_sleep: true` ![image](https://user-images.githubusercontent.com/2219836/228949675-f9699624-8abc-466c-bb04-250ce0f495b8.png) @@ -421,7 +423,6 @@ These graphs were generated using the values calculated by the Adaptive Lighting Hudson Brendon
Hudson Brendon

🌍 Gabriel Visser
Gabriel Visser

πŸ“– Gleb
Gleb

🌍 - Deleted user
Deleted user

🌍 Avi Miller
Avi Miller

πŸ“– πŸ’» Denys Dovhan
Denys Dovhan

🌍 David Stenbeck
David Stenbeck

πŸ“– From f5abf034c4653777d8c8b0e4e51b556caeb2660c Mon Sep 17 00:00:00 2001 From: Benjamin Auquite Date: Wed, 5 Apr 2023 11:13:57 -0500 Subject: [PATCH 3/4] Fix the docker tests instructions (#543) * Tested on multiple hardware Turns out windows 10 and 11 can't use `$(pwd):/app` OR `%cd%:/app` in PowerShell (which replaced cmd prompt), so I looked up the docs and made the necessary changes (again, sorry!) These changes have been tested on all terminal environments except macOS (the docs say it'll work there) * allow use of --exitfirst for faster debug * Remove install in actions --------- Co-authored-by: Bas Nijholt Co-authored-by: Bas Nijholt --- tests/README.md | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/tests/README.md b/tests/README.md index a576d518..1c472541 100644 --- a/tests/README.md +++ b/tests/README.md @@ -5,17 +5,15 @@ Alternatively, you can use the provided Docker image to run the tests locally. To run the tests using the Docker image, navigate to the `adaptive-lighting` repo folder and execute the following command: -Linux or MacOS: - +Linux / MacOS / Windows PowerShell: ```bash -docker run -v $(pwd):/app basnijholt/adaptive-lighting:latest +docker run -v ${PWD}:/app basnijholt/adaptive-lighting:latest ``` -Windows: - -```bash -docker run -v %cd%:/app basnijholt/adaptive-lighting:latest -``` +- In windows command prompt, the command is: + ```bash + docker run -v %cd%:/app basnijholt/adaptive-lighting:latest + ``` This command will download the Docker image from [the adaptive-lighting Docker Hub repo](https://hub.docker.com/r/basnijholt/adaptive-lighting) and run the tests. From 03a2d9cbf67964ec3cae59f4f9d6ace32d113d99 Mon Sep 17 00:00:00 2001 From: Benjamin Auquite Date: Wed, 5 Apr 2023 18:39:31 -0500 Subject: [PATCH 4/4] Fix RGB Color Temp Swaps (#514) * cherry pick from 486 * Refactor `_add_missing_attributes` --------- Co-authored-by: Bas Nijholt --- custom_components/adaptive_lighting/switch.py | 56 ++++++++++++++----- tests/test_switch.py | 27 +++++++-- 2 files changed, 64 insertions(+), 19 deletions(-) diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index 52821774..347ee4cb 100644 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -85,6 +85,7 @@ from homeassistant.util.color import ( color_RGB_to_xy, color_temperature_to_rgb, color_xy_to_hs, + color_xy_to_RGB, ) import homeassistant.util.dt as dt_util import voluptuous as vol @@ -628,6 +629,41 @@ def color_difference_redmean( return math.sqrt(red_term + green_term + blue_term) +# All comparisons should be done with RGB since +# converting anything to color temp is inaccurate. +def _convert_attributes(attributes: dict[str, Any]) -> dict[str, Any]: + if ATTR_RGB_COLOR in attributes: + return attributes + + rgb = None + if ATTR_COLOR_TEMP_KELVIN in attributes: + rgb = color_temperature_to_rgb(attributes[ATTR_COLOR_TEMP_KELVIN]) + elif ATTR_XY_COLOR in attributes: + rgb = color_xy_to_RGB(*attributes[ATTR_XY_COLOR]) + + if rgb is not None: + attributes[ATTR_RGB_COLOR] = rgb + _LOGGER.debug(f"Converted {attributes} to rgb {rgb}") + else: + _LOGGER.debug("No suitable conversion found") + + return attributes + + +def _add_missing_attributes( + old_attributes: dict[str, Any], + new_attributes: dict[str, Any], +) -> dict[str, Any]: + if not any( + attr in old_attributes and attr in new_attributes + for attr in [ATTR_COLOR_TEMP_KELVIN, ATTR_RGB_COLOR] + ): + old_attributes = _convert_attributes(old_attributes) + new_attributes = _convert_attributes(new_attributes) + + return old_attributes, new_attributes + + def _attributes_have_changed( light: str, old_attributes: dict[str, Any], @@ -636,6 +672,11 @@ def _attributes_have_changed( adapt_color: bool, context: Context, ) -> bool: + if adapt_color: + old_attributes, new_attributes = _add_missing_attributes( + old_attributes, new_attributes + ) + if ( adapt_brightness and ATTR_BRIGHTNESS in old_attributes @@ -690,21 +731,6 @@ def _attributes_have_changed( context.id, ) return True - - switched_color_temp = ( - ATTR_RGB_COLOR in old_attributes and ATTR_RGB_COLOR not in new_attributes - ) - switched_to_rgb_color = ( - ATTR_COLOR_TEMP_KELVIN in old_attributes - and ATTR_COLOR_TEMP_KELVIN not in new_attributes - ) - if switched_color_temp or switched_to_rgb_color: - # Light switched from RGB mode to color_temp or visa versa - _LOGGER.debug( - "'%s' switched from RGB mode to color_temp or visa versa", - light, - ) - return True return False diff --git a/tests/test_switch.py b/tests/test_switch.py index 9e88b332..db4900dc 100644 --- a/tests/test_switch.py +++ b/tests/test_switch.py @@ -47,6 +47,7 @@ from homeassistant.components.light import ( ATTR_BRIGHTNESS_PCT, ATTR_COLOR_TEMP_KELVIN, ATTR_RGB_COLOR, + ATTR_XY_COLOR, ) from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN from homeassistant.components.light import SERVICE_TURN_OFF @@ -873,10 +874,28 @@ def test_attributes_have_changed(): assert _attributes_have_changed( old_attributes=attributes_1, new_attributes=attrs, **kwargs ) - # Switch from rgb_color to color_temp - assert _attributes_have_changed( - old_attributes={ATTR_BRIGHTNESS: 1, ATTR_COLOR_TEMP_KELVIN: 100}, - new_attributes={ATTR_BRIGHTNESS: 1, ATTR_RGB_COLOR: (0, 0, 0)}, + _LOGGER.debug("Test switch from color_temp to rgb_color") + assert not _attributes_have_changed( + old_attributes={ATTR_BRIGHTNESS: 1, ATTR_COLOR_TEMP_KELVIN: 2702}, + new_attributes={ATTR_BRIGHTNESS: 1, ATTR_RGB_COLOR: (255, 166, 87)}, + **kwargs, + ) + _LOGGER.debug("Test switch from rgb_color to color_temp") + assert not _attributes_have_changed( + old_attributes={ATTR_BRIGHTNESS: 1, ATTR_RGB_COLOR: (255, 166, 87)}, + new_attributes={ATTR_BRIGHTNESS: 1, ATTR_COLOR_TEMP_KELVIN: 2702}, + **kwargs, + ) + _LOGGER.debug("Test switch from color_temp to color_xy") + assert not _attributes_have_changed( + old_attributes={ATTR_BRIGHTNESS: 1, ATTR_COLOR_TEMP_KELVIN: 2702}, + new_attributes={ATTR_BRIGHTNESS: 1, ATTR_XY_COLOR: (0.526, 0.387)}, + **kwargs, + ) + _LOGGER.debug("Test switch from color_xy to color_temp") + assert not _attributes_have_changed( + old_attributes={ATTR_BRIGHTNESS: 1, ATTR_XY_COLOR: (0.526, 0.387)}, + new_attributes={ATTR_BRIGHTNESS: 1, ATTR_COLOR_TEMP_KELVIN: 2702}, **kwargs, )