From df4f6236399a9088ce9a7c5a6e2acf0d9fd19ff5 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Mon, 19 Jan 2026 23:49:37 -0800 Subject: [PATCH] test: add backward compatibility test for change_switch_settings Add a regression test to verify that change_switch_settings works with both calling conventions: - entity_id in data (new domain service style) - entity_id in target (old entity service style) Both conventions work because Home Assistant's core automatically merges the target parameter into service_data before calling the handler. This test serves as documentation and ensures future changes don't break either calling convention for existing automations. --- tests/test_switch.py | 51 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/tests/test_switch.py b/tests/test_switch.py index 62266d9f..fd4d1ec7 100644 --- a/tests/test_switch.py +++ b/tests/test_switch.py @@ -2994,3 +2994,54 @@ async def test_set_manual_control_validation(hass): {ATTR_ENTITY_ID: "switch.non_existent"}, blocking=True, ) + + +@pytest.mark.parametrize( + "use_target", + [False, True], + ids=["entity_id_in_data", "entity_id_in_target"], +) +async def test_change_switch_settings_backward_compatibility(hass, use_target): + """Test change_switch_settings works with both calling conventions. + + This is a regression test to ensure backward compatibility when + change_switch_settings was converted from an entity service to a domain service. + + Previously (entity service): entity_id was passed via `target` parameter + Now (domain service): entity_id is passed via `data` parameter + + Both conventions should work to avoid breaking existing automations. + """ + switch, _ = await setup_lights_and_switch(hass) + + # Verify initial state + original_min_color_temp = switch._sun_light_settings.min_color_temp + new_min_color_temp = 3000 + assert original_min_color_temp != new_min_color_temp + + if use_target: + # Old convention: entity_id in target (entity service style) + await hass.services.async_call( + DOMAIN, + SERVICE_CHANGE_SWITCH_SETTINGS, + {"min_color_temp": new_min_color_temp}, + target={"entity_id": ENTITY_SWITCH}, + blocking=True, + ) + else: + # New convention: entity_id in data (domain service style) + await hass.services.async_call( + DOMAIN, + SERVICE_CHANGE_SWITCH_SETTINGS, + { + ATTR_ENTITY_ID: ENTITY_SWITCH, + "min_color_temp": new_min_color_temp, + }, + blocking=True, + ) + + # Both conventions should result in the setting being changed + assert switch._sun_light_settings.min_color_temp == new_min_color_temp, ( + f"change_switch_settings failed with {'target' if use_target else 'data'} convention. " + f"Expected min_color_temp={new_min_color_temp}, got {switch._sun_light_settings.min_color_temp}" + )