From daf24dc508f5712f00db69c6560ee21598baf88c Mon Sep 17 00:00:00 2001 From: Alexei Chetroi Date: Sat, 23 Jan 2021 00:20:53 -0500 Subject: [PATCH] Always apply default light profiles, unless a profile is given (#45450) --- homeassistant/components/light/__init__.py | 6 +- tests/components/light/test_init.py | 69 +++++++++++++++++++++- 2 files changed, 69 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/light/__init__.py b/homeassistant/components/light/__init__.py index d2fbc641be5..c46b7568b59 100644 --- a/homeassistant/components/light/__init__.py +++ b/homeassistant/components/light/__init__.py @@ -207,9 +207,6 @@ async def async_setup(hass, config): """ params = call.data["params"] - if not params: - profiles.apply_default(light.entity_id, params) - # Only process params once we processed brightness step if params and ( ATTR_BRIGHTNESS_STEP in params or ATTR_BRIGHTNESS_STEP_PCT in params @@ -226,6 +223,9 @@ async def async_setup(hass, config): preprocess_turn_on_alternatives(hass, params) + if ATTR_PROFILE not in params: + profiles.apply_default(light.entity_id, params) + # Zero brightness: Light will be turned off if params.get(ATTR_BRIGHTNESS) == 0: await light.async_turn_off(**filter_turn_off_params(params)) diff --git a/tests/components/light/test_init.py b/tests/components/light/test_init.py index 12a64417987..c36fa623e37 100644 --- a/tests/components/light/test_init.py +++ b/tests/components/light/test_init.py @@ -561,7 +561,58 @@ async def test_default_profiles_group(hass, mock_light_profiles): } -async def test_default_profiles_light(hass, mock_light_profiles): +@pytest.mark.parametrize( + "extra_call_params, expected_params", + ( + ( + {}, + { + light.ATTR_HS_COLOR: (50.353, 100), + light.ATTR_BRIGHTNESS: 100, + light.ATTR_TRANSITION: 3, + }, + ), + ( + {light.ATTR_BRIGHTNESS: 22}, + { + light.ATTR_HS_COLOR: (50.353, 100), + light.ATTR_BRIGHTNESS: 22, + light.ATTR_TRANSITION: 3, + }, + ), + ( + {light.ATTR_TRANSITION: 22}, + { + light.ATTR_HS_COLOR: (50.353, 100), + light.ATTR_BRIGHTNESS: 100, + light.ATTR_TRANSITION: 22, + }, + ), + ( + { + light.ATTR_XY_COLOR: [0.4448, 0.4066], + light.ATTR_BRIGHTNESS: 11, + light.ATTR_TRANSITION: 1, + }, + { + light.ATTR_HS_COLOR: (38.88, 49.02), + light.ATTR_BRIGHTNESS: 11, + light.ATTR_TRANSITION: 1, + }, + ), + ( + {light.ATTR_BRIGHTNESS: 11, light.ATTR_TRANSITION: 1}, + { + light.ATTR_HS_COLOR: (50.353, 100), + light.ATTR_BRIGHTNESS: 11, + light.ATTR_TRANSITION: 1, + }, + ), + ), +) +async def test_default_profiles_light( + hass, mock_light_profiles, extra_call_params, expected_params +): """Test default turn-on light profile for a specific light.""" platform = getattr(hass.components, "test.light") platform.init() @@ -582,14 +633,26 @@ async def test_default_profiles_light(hass, mock_light_profiles): SERVICE_TURN_ON, { ATTR_ENTITY_ID: dev.entity_id, + **extra_call_params, }, blocking=True, ) _, data = dev.last_call("turn_on") + assert data == expected_params + + await hass.services.async_call( + light.DOMAIN, + SERVICE_TURN_ON, + { + ATTR_ENTITY_ID: dev.entity_id, + light.ATTR_BRIGHTNESS: 0, + }, + blocking=True, + ) + + _, data = dev.last_call("turn_off") assert data == { - light.ATTR_HS_COLOR: (50.353, 100), - light.ATTR_BRIGHTNESS: 100, light.ATTR_TRANSITION: 3, }