diff --git a/tests/components/template/conftest.py b/tests/components/template/conftest.py index 410ce21e4c6..5ccc9e6479a 100644 --- a/tests/components/template/conftest.py +++ b/tests/components/template/conftest.py @@ -1,6 +1,4 @@ """template conftest.""" -import json - import pytest from homeassistant.setup import async_setup_component @@ -15,18 +13,8 @@ def calls(hass): @pytest.fixture -def config_addon(): - """Add entra configuration items.""" - return None - - -@pytest.fixture -async def start_ha(hass, count, domain, config_addon, config, caplog): +async def start_ha(hass, count, domain, config, caplog): """Do setup of integration.""" - if config_addon: - for key, value in config_addon.items(): - config = config.replace(key, value) - config = json.loads(config) with assert_setup_component(count, domain): assert await async_setup_component( hass, diff --git a/tests/components/template/test_light.py b/tests/components/template/test_light.py index 924a5c25538..394eb1cb9ba 100644 --- a/tests/components/template/test_light.py +++ b/tests/components/template/test_light.py @@ -26,48 +26,90 @@ from homeassistant.const import ( STATE_ON, STATE_UNAVAILABLE, ) +from homeassistant.setup import async_setup_component + +from tests.common import assert_setup_component # Represent for light's availability _STATE_AVAILABILITY_BOOLEAN = "availability_boolean.state" -@pytest.mark.parametrize("count,domain", [(1, light.DOMAIN)]) +OPTIMISTIC_ON_OFF_LIGHT_CONFIG = { + "turn_on": { + "service": "light.turn_on", + "entity_id": "light.test_state", + }, + "turn_off": { + "service": "light.turn_off", + "entity_id": "light.test_state", + }, +} + + +OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG = { + **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, + "set_level": { + "service": "light.turn_on", + "data_template": { + "entity_id": "light.test_state", + "brightness": "{{brightness}}", + }, + }, +} + + +OPTIMISTIC_WHITE_VALUE_LIGHT_CONFIG = { + **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, + "set_white_value": { + "service": "light.turn_on", + "data_template": { + "entity_id": "light.test_state", + "white_value": "{{white_value}}", + }, + }, +} + + +async def async_setup_light(hass, count, light_config): + """Do setup of light integration.""" + config = {"light": {"platform": "template", "lights": light_config}} + + with assert_setup_component(count, light.DOMAIN): + assert await async_setup_component( + hass, + light.DOMAIN, + config, + ) + + await hass.async_block_till_done() + await hass.async_start() + await hass.async_block_till_done() + + +@pytest.fixture +async def setup_light(hass, count, light_config): + """Do setup of light integration.""" + await async_setup_light(hass, count, light_config) + + +@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( "supported_features,supported_color_modes", [(SUPPORT_BRIGHTNESS, [ColorMode.BRIGHTNESS])], ) @pytest.mark.parametrize( - "config", + "light_config", [ { - "light": { - "platform": "template", - "lights": { - "test_template_light": { - "value_template": "{{states.test['big.fat...']}}", - "turn_on": { - "service": "light.turn_on", - "entity_id": "light.test_state", - }, - "turn_off": { - "service": "light.turn_off", - "entity_id": "light.test_state", - }, - "set_level": { - "service": "light.turn_on", - "data_template": { - "entity_id": "light.test_state", - "brightness": "{{brightness}}", - }, - }, - } - }, + "test_template_light": { + **OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, + "value_template": "{{states.test['big.fat...']}}", } }, ], ) async def test_template_state_invalid( - hass, supported_features, supported_color_modes, start_ha + hass, supported_features, supported_color_modes, setup_light ): """Test template state with render error.""" state = hass.states.get("light.test_template_light") @@ -77,43 +119,24 @@ async def test_template_state_invalid( assert state.attributes["supported_features"] == supported_features -@pytest.mark.parametrize("count,domain", [(1, light.DOMAIN)]) +@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( "supported_features,supported_color_modes", [(SUPPORT_BRIGHTNESS, [ColorMode.BRIGHTNESS])], ) @pytest.mark.parametrize( - "config", + "light_config", [ { - "light": { - "platform": "template", - "lights": { - "test_template_light": { - "value_template": "{{ states.light.test_state.state }}", - "turn_on": { - "service": "light.turn_on", - "entity_id": "light.test_state", - }, - "turn_off": { - "service": "light.turn_off", - "entity_id": "light.test_state", - }, - "set_level": { - "service": "light.turn_on", - "data_template": { - "entity_id": "light.test_state", - "brightness": "{{brightness}}", - }, - }, - } - }, + "test_template_light": { + **OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, + "value_template": "{{ states.light.test_state.state }}", } }, ], ) async def test_template_state_text( - hass, supported_features, supported_color_modes, start_ha + hass, supported_features, supported_color_modes, setup_light ): """Test the state text of a template.""" set_state = STATE_ON @@ -135,46 +158,24 @@ async def test_template_state_text( assert state.attributes["supported_features"] == supported_features -@pytest.mark.parametrize("count,domain", [(1, light.DOMAIN)]) +@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( "supported_features,supported_color_modes", [(SUPPORT_BRIGHTNESS, [ColorMode.BRIGHTNESS])], ) @pytest.mark.parametrize( - "config_addon,expected_state,expected_color_mode", + "value_template,expected_state,expected_color_mode", [ - ({"replace1": '"{{ 1 == 1 }}"'}, STATE_ON, ColorMode.UNKNOWN), - ({"replace1": '"{{ 1 == 2 }}"'}, STATE_OFF, None), - ], -) -@pytest.mark.parametrize( - "config", - [ - """{ - "light": { - "platform": "template", - "lights": { - "test_template_light": { - "value_template": replace1, - "turn_on": { - "service": "light.turn_on", - "entity_id": "light.test_state" - }, - "turn_off": { - "service": "light.turn_off", - "entity_id": "light.test_state" - }, - "set_level": { - "service": "light.turn_on", - "data_template": { - "entity_id": "light.test_state", - "brightness": "{{brightness}}" - } - } - } - } - } - }""", + ( + "{{ 1 == 1 }}", + STATE_ON, + ColorMode.UNKNOWN, + ), + ( + "{{ 1 == 2 }}", + STATE_OFF, + None, + ), ], ) async def test_templatex_state_boolean( @@ -183,9 +184,17 @@ async def test_templatex_state_boolean( expected_state, supported_features, supported_color_modes, - start_ha, + count, + value_template, ): """Test the setting of the state with boolean on.""" + light_config = { + "test_template_light": { + **OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, + "value_template": value_template, + } + } + await async_setup_light(hass, count, light_config) state = hass.states.get("light.test_template_light") assert state.state == expected_state assert state.attributes.get("color_mode") == expected_color_mode @@ -193,99 +202,55 @@ async def test_templatex_state_boolean( assert state.attributes["supported_features"] == supported_features -@pytest.mark.parametrize("count,domain", [(0, light.DOMAIN)]) +@pytest.mark.parametrize("count", [0]) @pytest.mark.parametrize( - "config", + "light_config", [ { - "light": { - "platform": "template", - "lights": { - "test_template_light": { - "value_template": "{%- if false -%}", - "turn_on": { - "service": "light.turn_on", - "entity_id": "light.test_state", - }, - "turn_off": { - "service": "light.turn_off", - "entity_id": "light.test_state", - }, - "set_level": { - "service": "light.turn_on", - "data_template": { - "entity_id": "light.test_state", - "brightness": "{{brightness}}", - }, - }, - } - }, + "test_template_light": { + **OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, + "value_template": "{%- if false -%}", } }, { - "light": { - "platform": "template", - "lights": { - "bad name here": { - "value_template": "{{ 1== 1}}", - "turn_on": { - "service": "light.turn_on", - "entity_id": "light.test_state", - }, - "turn_off": { - "service": "light.turn_off", - "entity_id": "light.test_state", - }, - "set_level": { - "service": "light.turn_on", - "data_template": { - "entity_id": "light.test_state", - "brightness": "{{brightness}}", - }, - }, - } - }, - } - }, - { - "light": { - "platform": "template", - "switches": {"test_template_light": "Invalid"}, + "bad name here": { + **OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, + "value_template": "{{ 1== 1}}", } }, + {"test_template_light": "Invalid"}, ], ) -async def test_template_syntax_error(hass, start_ha): +async def test_template_syntax_error(hass, setup_light): """Test templating syntax error.""" assert hass.states.async_all("light") == [] -SET_VAL1 = '"value_template": "{{ 1== 1}}",' -SET_VAL2 = '"turn_on": {"service": "light.turn_on","entity_id": "light.test_state"},' -SET_VAL3 = '"turn_off": {"service": "light.turn_off","entity_id": "light.test_state"},' - - -@pytest.mark.parametrize("domain", [light.DOMAIN]) @pytest.mark.parametrize( - "config_addon, count", + "light_config, count", [ - ({"replace2": f"{SET_VAL2}{SET_VAL3}"}, 1), - ({"replace2": f"{SET_VAL1}{SET_VAL2}"}, 0), - ({"replace2": f"{SET_VAL2}{SET_VAL3}"}, 1), + ( + { + "light_one": { + "value_template": "{{ 1== 1}}", + "turn_on": { + "service": "light.turn_on", + "entity_id": "light.test_state", + }, + "set_level": { + "service": "light.turn_on", + "data_template": { + "entity_id": "light.test_state", + "brightness": "{{brightness}}", + }, + }, + } + }, + 0, + ), ], ) -@pytest.mark.parametrize( - "config", - [ - """{"light": {"platform": "template", "lights": { - "light_one": { - replace2 - "set_level": {"service": "light.turn_on", - "data_template": {"entity_id": "light.test_state","brightness": "{{brightness}}" - }}}}}}""" - ], -) -async def test_missing_key(hass, count, start_ha): +async def test_missing_key(hass, count, setup_light): """Test missing template.""" if count: assert hass.states.async_all("light") != [] @@ -293,40 +258,25 @@ async def test_missing_key(hass, count, start_ha): assert hass.states.async_all("light") == [] -@pytest.mark.parametrize("count,domain", [(1, light.DOMAIN)]) +@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( "supported_features,supported_color_modes", [(SUPPORT_BRIGHTNESS, [ColorMode.BRIGHTNESS])], ) @pytest.mark.parametrize( - "config", + "light_config", [ { - "light": { - "platform": "template", - "lights": { - "test_template_light": { - "value_template": "{{states.light.test_state.state}}", - "turn_on": {"service": "test.automation"}, - "turn_off": { - "service": "light.turn_off", - "entity_id": "light.test_state", - }, - "set_level": { - "service": "light.turn_on", - "data_template": { - "entity_id": "light.test_state", - "brightness": "{{brightness}}", - }, - }, - } - }, + "test_template_light": { + **OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, + "value_template": "{{states.light.test_state.state}}", + "turn_on": {"service": "test.automation"}, } }, ], ) async def test_on_action( - hass, start_ha, calls, supported_features, supported_color_modes + hass, setup_light, calls, supported_features, supported_color_modes ): """Test on action.""" hass.states.async_set("light.test_state", STATE_OFF) @@ -353,47 +303,42 @@ async def test_on_action( assert state.attributes["supported_features"] == supported_features -@pytest.mark.parametrize("count,domain", [(1, light.DOMAIN)]) +@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( "supported_features,supported_color_modes", [(SUPPORT_BRIGHTNESS | SUPPORT_TRANSITION, [ColorMode.BRIGHTNESS])], ) @pytest.mark.parametrize( - "config", + "light_config", [ { - "light": { - "platform": "template", - "lights": { - "test_template_light": { - "value_template": "{{states.light.test_state.state}}", - "turn_on": { - "service": "test.automation", - "data_template": { - "transition": "{{transition}}", - }, - }, - "turn_off": { - "service": "light.turn_off", - "entity_id": "light.test_state", - }, - "supports_transition_template": "{{true}}", - "set_level": { - "service": "light.turn_on", - "data_template": { - "entity_id": "light.test_state", - "brightness": "{{brightness}}", - "transition": "{{transition}}", - }, - }, - } + "test_template_light": { + "value_template": "{{states.light.test_state.state}}", + "turn_on": { + "service": "test.automation", + "data_template": { + "transition": "{{transition}}", + }, + }, + "turn_off": { + "service": "light.turn_off", + "entity_id": "light.test_state", + }, + "supports_transition_template": "{{true}}", + "set_level": { + "service": "light.turn_on", + "data_template": { + "entity_id": "light.test_state", + "brightness": "{{brightness}}", + "transition": "{{transition}}", + }, }, } }, ], ) async def test_on_action_with_transition( - hass, start_ha, calls, supported_features, supported_color_modes + hass, setup_light, calls, supported_features, supported_color_modes ): """Test on action with transition.""" hass.states.async_set("light.test_state", STATE_OFF) @@ -421,40 +366,25 @@ async def test_on_action_with_transition( assert state.attributes["supported_features"] == supported_features -@pytest.mark.parametrize("count,domain", [(1, light.DOMAIN)]) +@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( "supported_features,supported_color_modes,expected_color_mode", [(SUPPORT_BRIGHTNESS, [ColorMode.BRIGHTNESS], ColorMode.BRIGHTNESS)], ) @pytest.mark.parametrize( - "config", + "light_config", [ { - "light": { - "platform": "template", - "lights": { - "test_template_light": { - "turn_on": {"service": "test.automation"}, - "turn_off": { - "service": "light.turn_off", - "entity_id": "light.test_state", - }, - "set_level": { - "service": "light.turn_on", - "data_template": { - "entity_id": "light.test_state", - "brightness": "{{brightness}}", - }, - }, - } - }, + "test_template_light": { + **OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, + "turn_on": {"service": "test.automation"}, } }, ], ) async def test_on_action_optimistic( hass, - start_ha, + setup_light, calls, supported_features, supported_color_modes, @@ -499,42 +429,25 @@ async def test_on_action_optimistic( assert state.attributes["supported_features"] == supported_features -@pytest.mark.parametrize("count,domain", [(1, light.DOMAIN)]) +@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( "supported_features,supported_color_modes", [(SUPPORT_BRIGHTNESS, [ColorMode.BRIGHTNESS])], ) @pytest.mark.parametrize( - "config", + "light_config", [ { - "light": { - "platform": "template", - "lights": { - "test_template_light": { - "value_template": "{{states.light.test_state.state}}", - "turn_on": { - "service": "light.turn_on", - "entity_id": "light.test_state", - }, - "turn_off": { - "service": "test.automation", - }, - "set_level": { - "service": "light.turn_on", - "data_template": { - "entity_id": "light.test_state", - "brightness": "{{brightness}}", - }, - }, - } - }, + "test_template_light": { + **OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, + "value_template": "{{states.light.test_state.state}}", + "turn_off": {"service": "test.automation"}, } }, ], ) async def test_off_action( - hass, start_ha, calls, supported_features, supported_color_modes + hass, setup_light, calls, supported_features, supported_color_modes ): """Test off action.""" hass.states.async_set("light.test_state", STATE_ON) @@ -560,47 +473,42 @@ async def test_off_action( assert state.attributes["supported_features"] == supported_features -@pytest.mark.parametrize("count,domain", [(1, light.DOMAIN)]) +@pytest.mark.parametrize("count", [(1)]) @pytest.mark.parametrize( "supported_features,supported_color_modes", [(SUPPORT_BRIGHTNESS | SUPPORT_TRANSITION, [ColorMode.BRIGHTNESS])], ) @pytest.mark.parametrize( - "config", + "light_config", [ { - "light": { - "platform": "template", - "lights": { - "test_template_light": { - "value_template": "{{states.light.test_state.state}}", - "turn_on": { - "service": "light.turn_on", - "entity_id": "light.test_state", - }, - "turn_off": { - "service": "test.automation", - "data_template": { - "transition": "{{transition}}", - }, - }, - "supports_transition_template": "{{true}}", - "set_level": { - "service": "light.turn_on", - "data_template": { - "entity_id": "light.test_state", - "brightness": "{{brightness}}", - "transition": "{{transition}}", - }, - }, - } + "test_template_light": { + "value_template": "{{states.light.test_state.state}}", + "turn_on": { + "service": "light.turn_on", + "entity_id": "light.test_state", + }, + "turn_off": { + "service": "test.automation", + "data_template": { + "transition": "{{transition}}", + }, + }, + "supports_transition_template": "{{true}}", + "set_level": { + "service": "light.turn_on", + "data_template": { + "entity_id": "light.test_state", + "brightness": "{{brightness}}", + "transition": "{{transition}}", + }, }, } }, ], ) async def test_off_action_with_transition( - hass, start_ha, calls, supported_features, supported_color_modes + hass, setup_light, calls, supported_features, supported_color_modes ): """Test off action with transition.""" hass.states.async_set("light.test_state", STATE_ON) @@ -627,39 +535,24 @@ async def test_off_action_with_transition( assert state.attributes["supported_features"] == supported_features -@pytest.mark.parametrize("count,domain", [(1, light.DOMAIN)]) +@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( "supported_features,supported_color_modes", [(SUPPORT_BRIGHTNESS, [ColorMode.BRIGHTNESS])], ) @pytest.mark.parametrize( - "config", + "light_config", [ { - "light": { - "platform": "template", - "lights": { - "test_template_light": { - "turn_on": { - "service": "light.turn_on", - "entity_id": "light.test_state", - }, - "turn_off": {"service": "test.automation"}, - "set_level": { - "service": "light.turn_on", - "data_template": { - "entity_id": "light.test_state", - "brightness": "{{brightness}}", - }, - }, - } - }, + "test_template_light": { + **OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, + "turn_off": {"service": "test.automation"}, } }, ], ) async def test_off_action_optimistic( - hass, start_ha, calls, supported_features, supported_color_modes + hass, setup_light, calls, supported_features, supported_color_modes ): """Test off action with optimistic state.""" state = hass.states.get("light.test_template_light") @@ -683,36 +576,24 @@ async def test_off_action_optimistic( assert state.attributes["supported_features"] == supported_features -@pytest.mark.parametrize("count,domain", [(1, light.DOMAIN)]) +@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( "supported_features,supported_color_modes,expected_color_mode", [(SUPPORT_WHITE_VALUE, [ColorMode.RGBW], ColorMode.UNKNOWN)], ) @pytest.mark.parametrize( - "config", + "light_config", [ { - "light": { - "platform": "template", - "lights": { - "test_template_light": { - "value_template": "{{1 == 1}}", - "turn_on": { - "service": "light.turn_on", - "entity_id": "light.test_state", - }, - "turn_off": { - "service": "light.turn_off", - "entity_id": "light.test_state", - }, - "set_white_value": { - "service": "test.automation", - "data_template": { - "entity_id": "test.test_state", - "white_value": "{{white_value}}", - }, - }, - } + "test_template_light": { + **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, + "value_template": "{{1 == 1}}", + "set_white_value": { + "service": "test.automation", + "data_template": { + "entity_id": "test.test_state", + "white_value": "{{white_value}}", + }, }, } }, @@ -720,7 +601,7 @@ async def test_off_action_optimistic( ) async def test_white_value_action_no_template( hass, - start_ha, + setup_light, calls, supported_color_modes, supported_features, @@ -748,46 +629,40 @@ async def test_white_value_action_no_template( assert state.attributes["supported_features"] == supported_features -@pytest.mark.parametrize( - "expected_white_value,config_addon", - [ - (255, {"replace3": "{{255}}"}), - (None, {"replace3": "{{256}}"}), - (None, {"replace3": "{{x - 12}}"}), - (None, {"replace3": "{{ none }}"}), - (None, {"replace3": ""}), - ], -) -@pytest.mark.parametrize("count,domain", [(1, light.DOMAIN)]) +@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( "supported_features,supported_color_modes,expected_color_mode", [(SUPPORT_WHITE_VALUE, [ColorMode.RGBW], ColorMode.UNKNOWN)], ) @pytest.mark.parametrize( - "config", + "expected_white_value,white_value_template", [ - """{ - "light": {"platform": "template","lights": { - "test_template_light": { - "value_template": "{{ 1 == 1 }}", - "turn_on": {"service": "light.turn_on","entity_id": "light.test_state"}, - "turn_off": {"service": "light.turn_off","entity_id": "light.test_state"}, - "set_white_value": {"service": "light.turn_on", - "data_template": {"entity_id": "light.test_state", - "white_value": "{{white_value}}"}}, - "white_value_template": "replace3" - }}}}""", + (255, "{{255}}"), + (None, "{{256}}"), + (None, "{{x-12}}"), + (None, "{{ none }}"), + (None, ""), ], ) async def test_white_value_template( hass, expected_white_value, - start_ha, supported_features, supported_color_modes, expected_color_mode, + count, + white_value_template, ): """Test the template for the white value.""" + light_config = { + "test_template_light": { + **OPTIMISTIC_WHITE_VALUE_LIGHT_CONFIG, + "value_template": "{{ 1 == 1 }}", + "white_value_template": white_value_template, + } + } + await async_setup_light(hass, count, light_config) + state = hass.states.get("light.test_template_light") assert state is not None assert state.attributes.get("white_value") == expected_white_value @@ -797,36 +672,24 @@ async def test_white_value_template( assert state.attributes["supported_features"] == supported_features -@pytest.mark.parametrize("count,domain", [(1, light.DOMAIN)]) +@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( "supported_features,supported_color_modes,expected_color_mode", [(SUPPORT_BRIGHTNESS, [ColorMode.BRIGHTNESS], ColorMode.BRIGHTNESS)], ) @pytest.mark.parametrize( - "config", + "light_config", [ { - "light": { - "platform": "template", - "lights": { - "test_template_light": { - "value_template": "{{1 == 1}}", - "turn_on": { - "service": "light.turn_on", - "entity_id": "light.test_state", - }, - "turn_off": { - "service": "light.turn_off", - "entity_id": "light.test_state", - }, - "set_level": { - "service": "test.automation", - "data_template": { - "entity_id": "test.test_state", - "brightness": "{{brightness}}", - }, - }, - } + "test_template_light": { + **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, + "value_template": "{{1 == 1}}", + "set_level": { + "service": "test.automation", + "data_template": { + "entity_id": "test.test_state", + "brightness": "{{brightness}}", + }, }, } }, @@ -834,7 +697,7 @@ async def test_white_value_template( ) async def test_level_action_no_template( hass, - start_ha, + setup_light, calls, supported_features, supported_color_modes, @@ -862,18 +725,18 @@ async def test_level_action_no_template( assert state.attributes["supported_features"] == supported_features -@pytest.mark.parametrize("count,domain", [(1, light.DOMAIN)]) +@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( - "expected_level,config_addon,expected_color_mode", + "expected_level,level_template,expected_color_mode", [ - (255, {"replace4": '"{{255}}"'}, ColorMode.BRIGHTNESS), - (None, {"replace4": '"{{256}}"'}, ColorMode.UNKNOWN), - (None, {"replace4": '"{{x - 12}}"'}, ColorMode.UNKNOWN), - (None, {"replace4": '"{{ none }}"'}, ColorMode.UNKNOWN), - (None, {"replace4": '""'}, ColorMode.UNKNOWN), + (255, "{{255}}", ColorMode.BRIGHTNESS), + (None, "{{256}}", ColorMode.UNKNOWN), + (None, "{{x - 12}}", ColorMode.UNKNOWN), + (None, "{{ none }}", ColorMode.UNKNOWN), + (None, "", ColorMode.UNKNOWN), ( None, - {"replace4": "\"{{ state_attr('light.nolight', 'brightness') }}\""}, + "{{ state_attr('light.nolight', 'brightness') }}", ColorMode.UNKNOWN, ), ], @@ -882,29 +745,31 @@ async def test_level_action_no_template( "supported_features,supported_color_modes", [(SUPPORT_BRIGHTNESS, [ColorMode.BRIGHTNESS])], ) -@pytest.mark.parametrize( - "config", - [ - """{"light": {"platform": "template", "lights": { - "test_template_light": { - "value_template": "{{ 1 == 1 }}", - "turn_on": {"service": "light.turn_on","entity_id": "light.test_state"}, - "turn_off": {"service": "light.turn_off","entity_id": "light.test_state"}, - "set_level": {"service": "light.turn_on","data_template": { - "entity_id": "light.test_state","brightness": "{{brightness}}"}}, - "level_template": replace4 - }}}}""", - ], -) async def test_level_template( hass, expected_level, - start_ha, supported_features, supported_color_modes, expected_color_mode, + count, + level_template, ): """Test the template for the level.""" + light_config = { + "test_template_light": { + **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, + "value_template": "{{ 1 == 1 }}", + "set_level": { + "service": "light.turn_on", + "data_template": { + "entity_id": "light.test_state", + "brightness": "{{brightness}}", + }, + }, + "level_template": level_template, + } + } + await async_setup_light(hass, count, light_config) state = hass.states.get("light.test_template_light") assert state.attributes.get("brightness") == expected_level assert state.state == STATE_ON @@ -913,62 +778,47 @@ async def test_level_template( assert state.attributes["supported_features"] == supported_features -@pytest.mark.parametrize("count,domain", [(1, light.DOMAIN)]) +@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( - "expected_temp,config_addon,expected_color_mode", + "expected_temp,temperature_template,expected_color_mode", [ - (500, {"replace5": '"{{500}}"'}, ColorMode.COLOR_TEMP), - (None, {"replace5": '"{{501}}"'}, ColorMode.UNKNOWN), - (None, {"replace5": '"{{x - 12}}"'}, ColorMode.UNKNOWN), - (None, {"replace5": '"None"'}, ColorMode.UNKNOWN), - (None, {"replace5": '"{{ none }}"'}, ColorMode.UNKNOWN), - (None, {"replace5": '""'}, ColorMode.UNKNOWN), + (500, "{{500}}", ColorMode.COLOR_TEMP), + (None, "{{501}}", ColorMode.UNKNOWN), + (None, "{{x - 12}}", ColorMode.UNKNOWN), + (None, "None", ColorMode.UNKNOWN), + (None, "{{ none }}", ColorMode.UNKNOWN), + (None, "", ColorMode.UNKNOWN), ], ) @pytest.mark.parametrize( "supported_features,supported_color_modes", [(SUPPORT_COLOR_TEMP, [ColorMode.COLOR_TEMP])], ) -@pytest.mark.parametrize( - "config", - [ - """{ - "light": { - "platform": "template", - "lights": { - "test_template_light": { - "value_template": "{{ 1 == 1 }}", - "turn_on": { - "service": "light.turn_on", - "entity_id": "light.test_state" - }, - "turn_off": { - "service": "light.turn_off", - "entity_id": "light.test_state" - }, - "set_temperature": { - "service": "light.turn_on", - "data_template": { - "entity_id": "light.test_state", - "color_temp": "{{color_temp}}" - } - }, - "temperature_template": replace5 - } - } - } - }""" - ], -) async def test_temperature_template( hass, expected_temp, - start_ha, supported_features, supported_color_modes, expected_color_mode, + count, + temperature_template, ): """Test the template for the temperature.""" + light_config = { + "test_template_light": { + **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, + "value_template": "{{ 1 == 1 }}", + "set_temperature": { + "service": "light.turn_on", + "data_template": { + "entity_id": "light.test_state", + "color_temp": "{{color_temp}}", + }, + }, + "temperature_template": temperature_template, + } + } + await async_setup_light(hass, count, light_config) state = hass.states.get("light.test_template_light") assert state.attributes.get("color_temp") == expected_temp assert state.state == STATE_ON @@ -977,36 +827,24 @@ async def test_temperature_template( assert state.attributes["supported_features"] == supported_features -@pytest.mark.parametrize("count,domain", [(1, light.DOMAIN)]) +@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( "supported_features,supported_color_modes,expected_color_mode", [(SUPPORT_COLOR_TEMP, [ColorMode.COLOR_TEMP], ColorMode.COLOR_TEMP)], ) @pytest.mark.parametrize( - "config", + "light_config", [ { - "light": { - "platform": "template", - "lights": { - "test_template_light": { - "value_template": "{{1 == 1}}", - "turn_on": { - "service": "light.turn_on", - "entity_id": "light.test_state", - }, - "turn_off": { - "service": "light.turn_off", - "entity_id": "light.test_state", - }, - "set_temperature": { - "service": "test.automation", - "data_template": { - "entity_id": "test.test_state", - "color_temp": "{{color_temp}}", - }, - }, - } + "test_template_light": { + **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, + "value_template": "{{1 == 1}}", + "set_temperature": { + "service": "test.automation", + "data_template": { + "entity_id": "test.test_state", + "color_temp": "{{color_temp}}", + }, }, } }, @@ -1014,7 +852,7 @@ async def test_temperature_template( ) async def test_temperature_action_no_template( hass, - start_ha, + setup_light, calls, supported_features, supported_color_modes, @@ -1043,39 +881,20 @@ async def test_temperature_action_no_template( assert state.attributes["supported_features"] == supported_features -@pytest.mark.parametrize("count,domain", [(1, light.DOMAIN)]) +@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( - "config", + "light_config", [ { - "light": { - "platform": "template", - "lights": { - "test_template_light": { - "friendly_name": "Template light", - "value_template": "{{ 1 == 1 }}", - "turn_on": { - "service": "light.turn_on", - "entity_id": "light.test_state", - }, - "turn_off": { - "service": "light.turn_off", - "entity_id": "light.test_state", - }, - "set_level": { - "service": "light.turn_on", - "data_template": { - "entity_id": "light.test_state", - "brightness": "{{brightness}}", - }, - }, - } - }, + "test_template_light": { + **OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, + "friendly_name": "Template light", + "value_template": "{{ 1 == 1 }}", } }, ], ) -async def test_friendly_name(hass, start_ha): +async def test_friendly_name(hass, setup_light): """Test the accessibility of the friendly_name attribute.""" state = hass.states.get("light.test_template_light") @@ -1084,42 +903,23 @@ async def test_friendly_name(hass, start_ha): assert state.attributes.get("friendly_name") == "Template light" -@pytest.mark.parametrize("count,domain", [(1, light.DOMAIN)]) +@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( - "config", + "light_config", [ { - "light": { - "platform": "template", - "lights": { - "test_template_light": { - "friendly_name": "Template light", - "value_template": "{{ 1 == 1 }}", - "turn_on": { - "service": "light.turn_on", - "entity_id": "light.test_state", - }, - "turn_off": { - "service": "light.turn_off", - "entity_id": "light.test_state", - }, - "set_level": { - "service": "light.turn_on", - "data_template": { - "entity_id": "light.test_state", - "brightness": "{{brightness}}", - }, - }, - "icon_template": "{% if states.light.test_state.state %}" - "mdi:check" - "{% endif %}", - } - }, + "test_template_light": { + **OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, + "friendly_name": "Template light", + "value_template": "{{ 1 == 1 }}", + "icon_template": "{% if states.light.test_state.state %}" + "mdi:check" + "{% endif %}", } }, ], ) -async def test_icon_template(hass, start_ha): +async def test_icon_template(hass, setup_light): """Test icon template.""" state = hass.states.get("light.test_template_light") assert state.attributes.get("icon") == "" @@ -1132,42 +932,23 @@ async def test_icon_template(hass, start_ha): assert state.attributes["icon"] == "mdi:check" -@pytest.mark.parametrize("count,domain", [(1, light.DOMAIN)]) +@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( - "config", + "light_config", [ { - "light": { - "platform": "template", - "lights": { - "test_template_light": { - "friendly_name": "Template light", - "value_template": "{{ 1 == 1 }}", - "turn_on": { - "service": "light.turn_on", - "entity_id": "light.test_state", - }, - "turn_off": { - "service": "light.turn_off", - "entity_id": "light.test_state", - }, - "set_level": { - "service": "light.turn_on", - "data_template": { - "entity_id": "light.test_state", - "brightness": "{{brightness}}", - }, - }, - "entity_picture_template": "{% if states.light.test_state.state %}" - "/local/light.png" - "{% endif %}", - } - }, + "test_template_light": { + **OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, + "friendly_name": "Template light", + "value_template": "{{ 1 == 1 }}", + "entity_picture_template": "{% if states.light.test_state.state %}" + "/local/light.png" + "{% endif %}", } }, ], ) -async def test_entity_picture_template(hass, start_ha): +async def test_entity_picture_template(hass, setup_light): """Test entity_picture template.""" state = hass.states.get("light.test_template_light") assert state.attributes.get("entity_picture") == "" @@ -1180,55 +961,35 @@ async def test_entity_picture_template(hass, start_ha): assert state.attributes["entity_picture"] == "/local/light.png" -@pytest.mark.parametrize("count,domain", [(1, light.DOMAIN)]) +@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( "supported_features,supported_color_modes, expected_color_mode", [(SUPPORT_COLOR, [ColorMode.HS], ColorMode.HS)], ) @pytest.mark.parametrize( - "config", + "light_config", [ { - "light": { - "platform": "template", - "lights": { - "test_template_light": { - "value_template": "{{1 == 1}}", - "turn_on": { - "service": "light.turn_on", - "entity_id": "light.test_state", + "test_template_light": { + **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, + "value_template": "{{1 == 1}}", + "set_color": [ + { + "service": "test.automation", + "data_template": { + "entity_id": "test.test_state", + "s": "{{s}}", + "h": "{{h}}", }, - "turn_off": { - "service": "light.turn_off", - "entity_id": "light.test_state", - }, - "set_color": [ - { - "service": "test.automation", - "data_template": { - "entity_id": "test.test_state", - "h": "{{h}}", - "s": "{{s}}", - }, - }, - { - "service": "test.automation", - "data_template": { - "entity_id": "test.test_state", - "s": "{{s}}", - "h": "{{h}}", - }, - }, - ], - } - }, + }, + ], } }, ], ) async def test_color_action_no_template( hass, - start_ha, + setup_light, calls, supported_features, supported_color_modes, @@ -1245,11 +1006,9 @@ async def test_color_action_no_template( blocking=True, ) - assert len(calls) == 2 + assert len(calls) == 1 assert calls[0].data["h"] == 40 assert calls[0].data["s"] == 50 - assert calls[1].data["h"] == 40 - assert calls[1].data["s"] == 50 state = hass.states.get("light.test_template_light") assert state.state == STATE_ON @@ -1259,47 +1018,51 @@ async def test_color_action_no_template( assert state.attributes["supported_features"] == supported_features -@pytest.mark.parametrize("count,domain", [(1, light.DOMAIN)]) +@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( - "expected_hs,config_addon,expected_color_mode", + "expected_hs,color_template,expected_color_mode", [ - ((360, 100), {"replace6": '"{{(360, 100)}}"'}, ColorMode.HS), - ((359.9, 99.9), {"replace6": '"{{(359.9, 99.9)}}"'}, ColorMode.HS), - (None, {"replace6": '"{{(361, 100)}}"'}, ColorMode.UNKNOWN), - (None, {"replace6": '"{{(360, 101)}}"'}, ColorMode.UNKNOWN), - (None, {"replace6": '"[{{(360)}},{{null}}]"'}, ColorMode.UNKNOWN), - (None, {"replace6": '"{{x - 12}}"'}, ColorMode.UNKNOWN), - (None, {"replace6": '""'}, ColorMode.UNKNOWN), - (None, {"replace6": '"{{ none }}"'}, ColorMode.UNKNOWN), + ((360, 100), "{{(360, 100)}}", ColorMode.HS), + ((359.9, 99.9), "{{(359.9, 99.9)}}", ColorMode.HS), + (None, "{{(361, 100)}}", ColorMode.UNKNOWN), + (None, "{{(360, 101)}}", ColorMode.UNKNOWN), + (None, "[{{(360)}},{{null}}]", ColorMode.UNKNOWN), + (None, "{{x - 12}}", ColorMode.UNKNOWN), + (None, "", ColorMode.UNKNOWN), + (None, "{{ none }}", ColorMode.UNKNOWN), ], ) @pytest.mark.parametrize( "supported_features,supported_color_modes", [(SUPPORT_COLOR, [ColorMode.HS])], ) -@pytest.mark.parametrize( - "config", - [ - """{"light": {"platform": "template","lights": {"test_template_light": { - "value_template": "{{ 1 == 1 }}", - "turn_on": {"service": "light.turn_on","entity_id": "light.test_state"}, - "turn_off": {"service": "light.turn_off","entity_id": "light.test_state"}, - "set_color": [{"service": "input_number.set_value", - "data_template": {"entity_id": "input_number.h","color_temp": "{{h}}" - }}], - "color_template": replace6 - }}}}""" - ], -) async def test_color_template( hass, expected_hs, - start_ha, supported_features, supported_color_modes, expected_color_mode, + count, + color_template, ): """Test the template for the color.""" + light_config = { + "test_template_light": { + **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, + "value_template": "{{ 1 == 1 }}", + "set_color": [ + { + "service": "input_number.set_value", + "data_template": { + "entity_id": "input_number.h", + "color_temp": "{{h}}", + }, + } + ], + "color_template": color_template, + } + } + await async_setup_light(hass, count, light_config) state = hass.states.get("light.test_template_light") assert state.attributes.get("hs_color") == expected_hs assert state.state == STATE_ON @@ -1308,53 +1071,41 @@ async def test_color_template( assert state.attributes["supported_features"] == supported_features -@pytest.mark.parametrize("count,domain", [(1, light.DOMAIN)]) +@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( "supported_features,supported_color_modes", [(SUPPORT_COLOR | SUPPORT_COLOR_TEMP, [ColorMode.COLOR_TEMP, ColorMode.HS])], ) @pytest.mark.parametrize( - "config", + "light_config", [ { - "light": { - "platform": "template", - "lights": { - "test_template_light": { - "value_template": "{{1 == 1}}", - "turn_on": { - "service": "light.turn_on", - "entity_id": "light.test_state", + "test_template_light": { + **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, + "value_template": "{{1 == 1}}", + "set_color": [ + { + "service": "test.automation", + "data_template": { + "entity_id": "test.test_state", + "h": "{{h}}", + "s": "{{s}}", }, - "turn_off": { - "service": "light.turn_off", - "entity_id": "light.test_state", - }, - "set_color": [ - { - "service": "test.automation", - "data_template": { - "entity_id": "test.test_state", - "h": "{{h}}", - "s": "{{s}}", - }, - }, - ], - "set_temperature": { - "service": "test.automation", - "data_template": { - "entity_id": "test.test_state", - "color_temp": "{{color_temp}}", - }, - }, - } + }, + ], + "set_temperature": { + "service": "test.automation", + "data_template": { + "entity_id": "test.test_state", + "color_temp": "{{color_temp}}", + }, }, } }, ], ) async def test_color_and_temperature_actions_no_template( - hass, start_ha, calls, supported_features, supported_color_modes + hass, setup_light, calls, supported_features, supported_color_modes ): """Test setting color and color temperature with optimistic template.""" state = hass.states.get("light.test_template_light") @@ -1435,44 +1186,35 @@ async def test_color_and_temperature_actions_no_template( assert state.attributes["supported_features"] == supported_features -@pytest.mark.parametrize("count,domain", [(1, light.DOMAIN)]) +@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( - "config", + "light_config", [ { - "light": { - "platform": "template", - "lights": { - "test_template_light": { - "value_template": "{{true}}", - "turn_on": {"service": "test.automation"}, - "turn_off": { - "service": "light.turn_off", - "entity_id": "light.test_state", - }, - "set_level": { - "service": "light.turn_on", - "data_template": { - "entity_id": "light.test_state", - "brightness": "{{brightness}}", - }, - }, - "set_effect": { - "service": "test.automation", - "data_template": { - "entity_id": "test.test_state", - "effect": "{{effect}}", - }, - }, - "effect_list_template": "{{ ['Disco', 'Police'] }}", - "effect_template": "{{ 'Disco' }}", - } + "test_template_light": { + **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, + "value_template": "{{true}}", + "set_level": { + "service": "light.turn_on", + "data_template": { + "entity_id": "light.test_state", + "brightness": "{{brightness}}", + }, }, + "set_effect": { + "service": "test.automation", + "data_template": { + "entity_id": "test.test_state", + "effect": "{{effect}}", + }, + }, + "effect_list_template": "{{ ['Disco', 'Police'] }}", + "effect_template": "{{ 'Disco' }}", } }, ], ) -async def test_effect_action_valid_effect(hass, start_ha, calls): +async def test_effect_action_valid_effect(hass, setup_light, calls): """Test setting valid effect with template.""" state = hass.states.get("light.test_template_light") assert state is not None @@ -1492,44 +1234,35 @@ async def test_effect_action_valid_effect(hass, start_ha, calls): assert state.attributes.get("effect") == "Disco" -@pytest.mark.parametrize("count,domain", [(1, light.DOMAIN)]) +@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( - "config", + "light_config", [ { - "light": { - "platform": "template", - "lights": { - "test_template_light": { - "value_template": "{{true}}", - "turn_on": {"service": "test.automation"}, - "turn_off": { - "service": "light.turn_off", - "entity_id": "light.test_state", - }, - "set_level": { - "service": "light.turn_on", - "data_template": { - "entity_id": "light.test_state", - "brightness": "{{brightness}}", - }, - }, - "set_effect": { - "service": "test.automation", - "data_template": { - "entity_id": "test.test_state", - "effect": "{{effect}}", - }, - }, - "effect_list_template": "{{ ['Disco', 'Police'] }}", - "effect_template": "{{ None }}", - } + "test_template_light": { + **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, + "value_template": "{{true}}", + "set_level": { + "service": "light.turn_on", + "data_template": { + "entity_id": "light.test_state", + "brightness": "{{brightness}}", + }, }, + "set_effect": { + "service": "test.automation", + "data_template": { + "entity_id": "test.test_state", + "effect": "{{effect}}", + }, + }, + "effect_list_template": "{{ ['Disco', 'Police'] }}", + "effect_template": "{{ None }}", } }, ], ) -async def test_effect_action_invalid_effect(hass, start_ha, calls): +async def test_effect_action_invalid_effect(hass, setup_light, calls): """Test setting invalid effect with template.""" state = hass.states.get("light.test_template_light") assert state is not None @@ -1549,176 +1282,191 @@ async def test_effect_action_invalid_effect(hass, start_ha, calls): assert state.attributes.get("effect") is None -@pytest.mark.parametrize("count,domain", [(1, light.DOMAIN)]) +@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( - "expected_effect_list,config_addon", + "expected_effect_list,effect_list_template", [ ( ["Strobe color", "Police", "Christmas", "RGB", "Random Loop"], - { - "replace7": "\"{{ ['Strobe color', 'Police', 'Christmas', 'RGB', 'Random Loop'] }}\"" - }, + "{{ ['Strobe color', 'Police', 'Christmas', 'RGB', 'Random Loop'] }}", ), ( ["Police", "RGB", "Random Loop"], - {"replace7": "\"{{ ['Police', 'RGB', 'Random Loop'] }}\""}, + "{{ ['Police', 'RGB', 'Random Loop'] }}", ), - (None, {"replace7": '"{{ [] }}"'}), - (None, {"replace7": "\"{{ '[]' }}\""}), - (None, {"replace7": '"{{ 124 }}"'}), - (None, {"replace7": "\"{{ '124' }}\""}), - (None, {"replace7": '"{{ none }}"'}), - (None, {"replace7": '""'}), + (None, "{{ [] }}"), + (None, "{{ '[]' }}"), + (None, "{{ 124 }}"), + (None, "{{ '124' }}"), + (None, "{{ none }}"), + (None, ""), ], ) -@pytest.mark.parametrize( - "config", - [ - """{"light": {"platform": "template","lights": {"test_template_light": { - "value_template": "{{ 1 == 1 }}", - "turn_on": {"service": "light.turn_on","entity_id": "light.test_state"}, - "turn_off": {"service": "light.turn_off","entity_id": "light.test_state"}, - "set_effect": {"service": "test.automation", - "data_template": {"entity_id": "test.test_state","effect": "{{effect}}"}}, - "effect_template": "{{ None }}", - "effect_list_template": replace7 - }}}}""", - ], -) -async def test_effect_list_template(hass, expected_effect_list, start_ha): +async def test_effect_list_template( + hass, expected_effect_list, count, effect_list_template +): """Test the template for the effect list.""" + light_config = { + "test_template_light": { + **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, + "value_template": "{{ 1 == 1 }}", + "set_effect": { + "service": "test.automation", + "data_template": { + "entity_id": "test.test_state", + "effect": "{{effect}}", + }, + }, + "effect_template": "{{ None }}", + "effect_list_template": effect_list_template, + } + } + await async_setup_light(hass, count, light_config) state = hass.states.get("light.test_template_light") assert state is not None assert state.attributes.get("effect_list") == expected_effect_list -@pytest.mark.parametrize("count,domain", [(1, light.DOMAIN)]) +@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( - "expected_effect,config_addon", + "expected_effect,effect_template", [ - (None, {"replace8": '"Disco"'}), - (None, {"replace8": '"None"'}), - (None, {"replace8": '"{{ None }}"'}), - ("Police", {"replace8": '"Police"'}), - ("Strobe color", {"replace8": "\"{{ 'Strobe color' }}\""}), + (None, "Disco"), + (None, "None"), + (None, "{{ None }}"), + ("Police", "Police"), + ("Strobe color", "{{ 'Strobe color' }}"), ], ) -@pytest.mark.parametrize( - "config", - [ - """{"light": {"platform": "template","lights": {"test_template_light": { - "value_template": "{{ 1 == 1 }}", - "turn_on": {"service": "light.turn_on","entity_id": "light.test_state"}, - "turn_off": {"service": "light.turn_off","entity_id": "light.test_state"}, - "set_effect": {"service": "test.automation","data_template": { - "entity_id": "test.test_state","effect": "{{effect}}"}}, - "effect_list_template": "{{ ['Strobe color', 'Police', 'Christmas', 'RGB', 'Random Loop'] }}", - "effect_template": replace8 - }}}}""", - ], -) -async def test_effect_template(hass, expected_effect, start_ha): +async def test_effect_template(hass, expected_effect, count, effect_template): """Test the template for the effect.""" + light_config = { + "test_template_light": { + **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, + "value_template": "{{ 1 == 1 }}", + "set_effect": { + "service": "test.automation", + "data_template": { + "entity_id": "test.test_state", + "effect": "{{effect}}", + }, + }, + "effect_list_template": "{{ ['Strobe color', 'Police', 'Christmas', 'RGB', 'Random Loop'] }}", + "effect_template": effect_template, + } + } + await async_setup_light(hass, count, light_config) state = hass.states.get("light.test_template_light") assert state is not None assert state.attributes.get("effect") == expected_effect -@pytest.mark.parametrize("count,domain", [(1, light.DOMAIN)]) +@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( - "expected_min_mireds,config_addon", + "expected_min_mireds,min_mireds_template", [ - (118, {"replace9": '"{{118}}"'}), - (153, {"replace9": '"{{x - 12}}"'}), - (153, {"replace9": '"None"'}), - (153, {"replace9": '"{{ none }}"'}), - (153, {"replace9": '""'}), - (153, {"replace9": "\"{{ 'a' }}\""}), + (118, "{{118}}"), + (153, "{{x - 12}}"), + (153, "None"), + (153, "{{ none }}"), + (153, ""), + (153, "{{ 'a' }}"), ], ) -@pytest.mark.parametrize( - "config", - [ - """{"light": {"platform": "template","lights": {"test_template_light": { - "value_template": "{{ 1 == 1 }}", - "turn_on": {"service": "light.turn_on","entity_id": "light.test_state"}, - "turn_off": {"service": "light.turn_off","entity_id": "light.test_state"}, - "set_temperature": {"service": "light.turn_on","data_template": { - "entity_id": "light.test_state","color_temp": "{{color_temp}}"}}, - "temperature_template": "{{200}}", - "min_mireds_template": replace9 - }}}}""", - ], -) -async def test_min_mireds_template(hass, expected_min_mireds, start_ha): +async def test_min_mireds_template( + hass, expected_min_mireds, count, min_mireds_template +): """Test the template for the min mireds.""" + light_config = { + "test_template_light": { + **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, + "value_template": "{{ 1 == 1 }}", + "set_temperature": { + "service": "light.turn_on", + "data_template": { + "entity_id": "light.test_state", + "color_temp": "{{color_temp}}", + }, + }, + "temperature_template": "{{200}}", + "min_mireds_template": min_mireds_template, + } + } + await async_setup_light(hass, count, light_config) state = hass.states.get("light.test_template_light") assert state is not None assert state.attributes.get("min_mireds") == expected_min_mireds -@pytest.mark.parametrize("count,domain", [(1, light.DOMAIN)]) +@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( - "expected_max_mireds,config_addon", + "expected_max_mireds,max_mireds_template", [ - (488, {"template1": '"{{488}}"'}), - (500, {"template1": '"{{x - 12}}"'}), - (500, {"template1": '"None"'}), - (500, {"template1": '"{{ none }}"'}), - (500, {"template1": '""'}), - (500, {"template1": "\"{{ 'a' }}\""}), + (488, "{{488}}"), + (500, "{{x - 12}}"), + (500, "None"), + (500, "{{ none }}"), + (500, ""), + (500, "{{ 'a' }}"), ], ) -@pytest.mark.parametrize( - "config", - [ - """{"light": {"platform": "template","lights": {"test_template_light": { - "value_template": "{{ 1 == 1 }}", - "turn_on": {"service": "light.turn_on","entity_id": "light.test_state"}, - "turn_off": {"service": "light.turn_off","entity_id": "light.test_state"}, - "set_temperature": {"service": "light.turn_on","data_template": { - "entity_id": "light.test_state","color_temp": "{{color_temp}}"}}, - "temperature_template": "{{200}}", - "max_mireds_template": template1 - }}}}""", - ], -) -async def test_max_mireds_template(hass, expected_max_mireds, start_ha): +async def test_max_mireds_template( + hass, expected_max_mireds, count, max_mireds_template +): """Test the template for the max mireds.""" + light_config = { + "test_template_light": { + **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, + "value_template": "{{ 1 == 1 }}", + "set_temperature": { + "service": "light.turn_on", + "data_template": { + "entity_id": "light.test_state", + "color_temp": "{{color_temp}}", + }, + }, + "temperature_template": "{{200}}", + "max_mireds_template": max_mireds_template, + } + } + await async_setup_light(hass, count, light_config) state = hass.states.get("light.test_template_light") assert state is not None assert state.attributes.get("max_mireds") == expected_max_mireds -@pytest.mark.parametrize("count,domain", [(1, light.DOMAIN)]) +@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( - "expected_supports_transition,config_addon", + "expected_supports_transition,supports_transition_template", [ - (True, {"template2": '"{{true}}"'}), - (True, {"template2": '"{{1 == 1}}"'}), - (False, {"template2": '"{{false}}"'}), - (False, {"template2": '"{{ none }}"'}), - (False, {"template2": '""'}), - (False, {"template2": '"None"'}), - ], -) -@pytest.mark.parametrize( - "config", - [ - """{"light": {"platform": "template","lights": {"test_template_light": { - "value_template": "{{ 1 == 1 }}", - "turn_on": {"service": "light.turn_on","entity_id": "light.test_state"}, - "turn_off": {"service": "light.turn_off","entity_id": "light.test_state"}, - "set_temperature": {"service": "light.turn_on","data_template": { - "entity_id": "light.test_state","color_temp": "{{color_temp}}"}}, - "supports_transition_template": template2 - }}}}""", + (True, "{{true}}"), + (True, "{{1 == 1}}"), + (False, "{{false}}"), + (False, "{{ none }}"), + (False, ""), + (False, "None"), ], ) async def test_supports_transition_template( - hass, expected_supports_transition, start_ha + hass, expected_supports_transition, count, supports_transition_template ): """Test the template for the supports transition.""" + light_config = { + "test_template_light": { + "value_template": "{{ 1 == 1 }}", + "turn_on": {"service": "light.turn_on", "entity_id": "light.test_state"}, + "turn_off": {"service": "light.turn_off", "entity_id": "light.test_state"}, + "set_temperature": { + "service": "light.turn_on", + "data_template": { + "entity_id": "light.test_state", + "color_temp": "{{color_temp}}", + }, + }, + "supports_transition_template": supports_transition_template, + } + } + await async_setup_light(hass, count, light_config) state = hass.states.get("light.test_template_light") expected_value = 1 @@ -1732,38 +1480,19 @@ async def test_supports_transition_template( ) != expected_value -@pytest.mark.parametrize("count,domain", [(1, light.DOMAIN)]) +@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( - "config", + "light_config", [ { - "light": { - "platform": "template", - "lights": { - "test_template_light": { - "availability_template": "{{ is_state('availability_boolean.state', 'on') }}", - "turn_on": { - "service": "light.turn_on", - "entity_id": "light.test_state", - }, - "turn_off": { - "service": "light.turn_off", - "entity_id": "light.test_state", - }, - "set_level": { - "service": "light.turn_on", - "data_template": { - "entity_id": "light.test_state", - "brightness": "{{brightness}}", - }, - }, - } - }, + "test_template_light": { + **OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, + "availability_template": "{{ is_state('availability_boolean.state', 'on') }}", } }, ], ) -async def test_available_template_with_entities(hass, start_ha): +async def test_available_template_with_entities(hass, setup_light): """Test availability templates with values from other entities.""" # When template returns true.. hass.states.async_set(_STATE_AVAILABILITY_BOOLEAN, STATE_ON) @@ -1780,80 +1509,42 @@ async def test_available_template_with_entities(hass, start_ha): assert hass.states.get("light.test_template_light").state == STATE_UNAVAILABLE -@pytest.mark.parametrize("count,domain", [(1, light.DOMAIN)]) +@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( - "config", + "light_config", [ { - "light": { - "platform": "template", - "lights": { - "test_template_light": { - "availability_template": "{{ x - 12 }}", - "turn_on": { - "service": "light.turn_on", - "entity_id": "light.test_state", - }, - "turn_off": { - "service": "light.turn_off", - "entity_id": "light.test_state", - }, - "set_level": { - "service": "light.turn_on", - "data_template": { - "entity_id": "light.test_state", - "brightness": "{{brightness}}", - }, - }, - } - }, + "test_template_light": { + **OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, + "availability_template": "{{ x - 12 }}", } }, ], ) async def test_invalid_availability_template_keeps_component_available( - hass, start_ha, caplog_setup_text + hass, setup_light, caplog_setup_text ): """Test that an invalid availability keeps the device available.""" assert hass.states.get("light.test_template_light").state != STATE_UNAVAILABLE assert ("UndefinedError: 'x' is undefined") in caplog_setup_text -@pytest.mark.parametrize("count,domain", [(1, light.DOMAIN)]) +@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( - "config", + "light_config", [ { - "light": { - "platform": "template", - "lights": { - "test_template_light_01": { - "unique_id": "not-so-unique-anymore", - "turn_on": { - "service": "light.turn_on", - "entity_id": "light.test_state", - }, - "turn_off": { - "service": "light.turn_off", - "entity_id": "light.test_state", - }, - }, - "test_template_light_02": { - "unique_id": "not-so-unique-anymore", - "turn_on": { - "service": "light.turn_on", - "entity_id": "light.test_state", - }, - "turn_off": { - "service": "light.turn_off", - "entity_id": "light.test_state", - }, - }, - }, - } + "test_template_light_01": { + **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, + "unique_id": "not-so-unique-anymore", + }, + "test_template_light_02": { + **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, + "unique_id": "not-so-unique-anymore", + }, }, ], ) -async def test_unique_id(hass, start_ha): +async def test_unique_id(hass, setup_light): """Test unique_id option only creates one light per id.""" assert len(hass.states.async_all("light")) == 1