From 6c08ae39de536f667df6411a79a48eebbf5aec53 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Sat, 29 Jul 2023 18:31:06 -0700 Subject: [PATCH] Add test --- custom_components/adaptive_lighting/switch.py | 8 +++- tests/test_switch.py | 45 +++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index 0f1d87a1..92c30d25 100644 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -20,6 +20,7 @@ import ulid_transform import voluptuous as vol from homeassistant.components.light import ( ATTR_BRIGHTNESS, + ATTR_COLOR_TEMP, ATTR_COLOR_TEMP_KELVIN, ATTR_RGB_COLOR, ATTR_SUPPORTED_COLOR_MODES, @@ -2083,14 +2084,17 @@ class AdaptiveLightingManager: modify_service_data(data, skipped) # Call light turn_on service for skipped entities _LOGGER.debug( - "_service_interceptor_turn_on_handler: calling `light.turn_on` with skipped='%s', data: ", + "_service_interceptor_turn_on_handler: calling `light.turn_on` with skipped='%s', data: '%s'", skipped, data, ) + service_data = data.copy() + service_data.update(service_data.pop(CONF_PARAMS, {})) + service_data.pop(ATTR_COLOR_TEMP, None) await self.hass.services.async_call( LIGHT_DOMAIN, SERVICE_TURN_ON, - data, + service_data, blocking=True, context=call.context, ) diff --git a/tests/test_switch.py b/tests/test_switch.py index 0838a843..a9ac9827 100644 --- a/tests/test_switch.py +++ b/tests/test_switch.py @@ -1563,6 +1563,51 @@ async def test_proactive_adaptation_transition_override(hass): switch.manager.cancel_ongoing_adaptation_calls(ENTITY_LIGHT3) +async def test_proactive_multiple_lights(hass): + """Create switch and demo lights.""" + # Setup demo lights and turn on + lights_instances = await setup_lights(hass) + # Setup switches + lights = [ + ENTITY_LIGHT, + "light.ceiling_lights", + ENTITY_LIGHT3, + ] + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: lights}, + blocking=True, + ) + await hass.async_block_till_done() + defaults = { + CONF_SUNRISE_TIME: datetime.time(SUNRISE.hour), + CONF_SUNSET_TIME: datetime.time(SUNSET.hour), + CONF_INITIAL_TRANSITION: 0, + CONF_TRANSITION: 0, + CONF_DETECT_NON_HA_CHANGES: True, + CONF_PREFER_RGB_COLOR: False, + CONF_MIN_COLOR_TEMP: 2500, # to not coincide with sleep_color_temp} + INTERNAL_CONF_PROACTIVE_SERVICE_CALL_ADAPTATION: True, + } + assert all(hass.states.get(light) is not None for light in lights) + _, switch1 = await setup_switch( + hass, {CONF_NAME: "switch1", CONF_LIGHTS: [lights[0]], **defaults} + ) + _, switch2 = await setup_switch( + hass, {CONF_NAME: "switch2", CONF_LIGHTS: [lights[1]], **defaults} + ) + await hass.async_block_till_done() + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: lights}, + blocking=True, + context=Context(id="test1"), + ) + assert switch1.manager.is_proactively_adapting("test1") + + async def test_two_switches_for_single_light(hass): """Test the case where someone has two switches for a single light.