From 8696092879d4967dccefaec6472937a74e6c2616 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Tue, 25 Jul 2023 19:17:11 -0700 Subject: [PATCH] Avoid adapting lights with nothing in service_data, closes #661 (#662) --- custom_components/adaptive_lighting/manifest.json | 2 +- custom_components/adaptive_lighting/switch.py | 12 ++++++++++++ tests/test_switch.py | 8 ++++---- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/custom_components/adaptive_lighting/manifest.json b/custom_components/adaptive_lighting/manifest.json index 747fa2dd..1e232dab 100644 --- a/custom_components/adaptive_lighting/manifest.json +++ b/custom_components/adaptive_lighting/manifest.json @@ -8,5 +8,5 @@ "iot_class": "calculated", "issue_tracker": "https://github.com/basnijholt/adaptive-lighting/issues", "requirements": ["ulid-transform"], - "version": "1.17.1" + "version": "1.17.2" } diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index ce9f6922..6e337a30 100644 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -1159,6 +1159,7 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): use_transition = "transition" in features and transition > 0 if use_transition: service_data[ATTR_TRANSITION] = transition + if "brightness" in features and adapt_brightness: brightness = round(255 * self._settings["brightness_pct"] / 100) service_data[ATTR_BRIGHTNESS] = brightness @@ -1185,6 +1186,17 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): _LOGGER.debug("%s: Setting rgb_color of light %s", self._name, light) service_data[ATTR_RGB_COLOR] = self._settings["rgb_color"] + required_attrs = [ATTR_RGB_COLOR, ATTR_COLOR_TEMP_KELVIN, ATTR_BRIGHTNESS] + if not any(attr in service_data for attr in required_attrs): + _LOGGER.debug( + "%s: Skipping adaptation of %s because no relevant attributes" + " are set in service_data: %s", + self._name, + light, + service_data, + ) + return None + context = context or self.create_context("adapt_lights") self.manager.last_service_data[light] = service_data diff --git a/tests/test_switch.py b/tests/test_switch.py index 360fec55..55aa30ea 100644 --- a/tests/test_switch.py +++ b/tests/test_switch.py @@ -1672,25 +1672,25 @@ async def test_adapt_until_sleep_and_rgb_colors(hass): assert switch._settings["color_temp_kelvin"] > min_color_temp assert "color_temp_kelvin" in switch.manager.last_service_data[ENTITY_LIGHT] - # One hour after sunset the brightness should be down + # One hour after sunset the brightness should be down and use RGB await patch_time_and_update(after_sunset) assert switch._settings["force_rgb_color"] assert switch._settings[ATTR_BRIGHTNESS_PCT] < DEFAULT_MAX_BRIGHTNESS assert "rgb_color" in switch.manager.last_service_data[ENTITY_LIGHT] - # At sunrise the brightness should be max and color_temp at the smallest value + # At sunrise the brightness should be max and use Kelvin await patch_time_and_update(sunrise) assert switch._settings[ATTR_BRIGHTNESS_PCT] == DEFAULT_MAX_BRIGHTNESS assert switch._settings["color_temp_kelvin"] == min_color_temp assert "color_temp_kelvin" in switch.manager.last_service_data[ENTITY_LIGHT] # One hour before sunrise the brightness should smaller than max - # and color_temp at the min value. + # and use RGB await patch_time_and_update(before_sunrise) assert switch._settings[ATTR_BRIGHTNESS_PCT] < DEFAULT_MAX_BRIGHTNESS assert "rgb_color" in switch.manager.last_service_data[ENTITY_LIGHT] - # One hour after sunrise the brightness should be up + # One hour after sunrise the brightness should be up and it should use Kelvin await patch_time_and_update(after_sunrise) assert switch._settings[ATTR_BRIGHTNESS_PCT] == DEFAULT_MAX_BRIGHTNESS assert switch._settings["color_temp_kelvin"] > min_color_temp