From cb9ae39ee4693a82deeb5e2b2bc80300fada7401 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Sun, 6 Aug 2023 11:15:14 -0700 Subject: [PATCH] Fix adaptive_lighting.change_switch_settings service (#712) * Fix adaptive_lighting.change_switch_settings service Closes #623 * filter defaults * fix mutable * Revert debugging logs --- custom_components/adaptive_lighting/switch.py | 21 ++++++++++++------- tests/test_switch.py | 6 ++++++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index b0aa7f8c..88d94009 100644 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -363,19 +363,19 @@ async def handle_change_switch_settings( ) -> None: """Allows HASS to change config values via a service call.""" data = service_call.data - which = data.get(CONF_USE_DEFAULTS, "current") if which == "current": # use whatever we're already using. defaults = switch._current_settings # pylint: disable=protected-access elif which == "factory": # use actual defaults listed in the documentation - defaults = {key: default for key, default, _ in VALIDATION_TUPLES} + defaults = None elif which == "configuration": # use whatever's in the config flow or configuration.yaml - defaults = switch._config_backup # pylint: disable=protected-access + defaults = switch._config_backup else: defaults = None - switch._set_changeable_settings(data=data, defaults=defaults) + # deep copy the defaults so we don't modify the original dicts + switch._set_changeable_settings(data=data, defaults=deepcopy(defaults)) switch._update_time_interval_listener() _LOGGER.debug( @@ -589,7 +589,7 @@ def validate( if defaults is None: data = {key: default for key, default, _ in VALIDATION_TUPLES} else: - data = defaults + data = deepcopy(defaults) if config_entry is not None: assert service_data is None @@ -598,7 +598,12 @@ def validate( data.update(config_entry.data) # all yaml settings come from data else: assert service_data is not None - data.update(service_data) + changed_settings = { + key: value + for key, value in service_data.items() + if key not in (CONF_USE_DEFAULTS, ATTR_ENTITY_ID) + } + data.update(changed_settings) data = {key: replace_none_str(value) for key, value in data.items()} for key, (validate_value, _) in EXTRA_VALIDATION.items(): value = data.get(key) @@ -843,8 +848,8 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): def _set_changeable_settings( self, - data: dict, - defaults: dict | None = None, + data: dict[str, Any], + defaults: dict[str, Any] | None = None, ): # Only pass settings users can change during runtime data = validate( diff --git a/tests/test_switch.py b/tests/test_switch.py index 692f0fde..79dc9b20 100644 --- a/tests/test_switch.py +++ b/tests/test_switch.py @@ -1353,6 +1353,12 @@ async def test_change_switch_settings_service(hass): await change_switch_settings(**{CONF_USE_DEFAULTS: "current"}) assert switch._sun_light_settings.min_color_temp == 2000 + # testing with "configuration" and setting a new value + await change_switch_settings( + **{CONF_USE_DEFAULTS: "configuration", CONF_MIN_COLOR_TEMP: 3000} + ) + assert switch._sun_light_settings.min_color_temp == 3000 + # testing with "configuration" should revert back to 2500 await change_switch_settings(**{CONF_USE_DEFAULTS: "configuration"}) assert switch._sun_light_settings.min_color_temp == 2500