diff --git a/tests/components/template/conftest.py b/tests/components/template/conftest.py index 4ba1c64577b6..50040940cda9 100644 --- a/tests/components/template/conftest.py +++ b/tests/components/template/conftest.py @@ -51,6 +51,32 @@ def make_test_trigger(*entities: str) -> dict: } +def make_test_action(action: str, extra_data: ConfigType | None = None) -> ConfigType: + """Make a test action.""" + data = extra_data or {} + return { + action: { + "action": "test.automation", + "data": {"caller": "{{ this.entity_id }}", "action": action, **data}, + } + } + + +def assert_action( + platform_setup: TemplatePlatformSetup, + calls: list[ServiceCall], + expected_calls: int, + expected_action: str, + **kwargs, +) -> None: + """Validate the action was properly called.""" + assert len(calls) == expected_calls + assert calls[-1].data["action"] == expected_action + assert calls[-1].data["caller"] == platform_setup.entity_id + for key, value in kwargs.items(): + assert calls[-1].data[key] == value + + async def async_trigger( hass: HomeAssistant, entity_id: str, diff --git a/tests/components/template/test_light.py b/tests/components/template/test_light.py index d9e4ebf8c4e0..80073e7c4b28 100644 --- a/tests/components/template/test_light.py +++ b/tests/components/template/test_light.py @@ -29,389 +29,166 @@ from homeassistant.const import ( ) from homeassistant.core import HomeAssistant, ServiceCall from homeassistant.helpers import entity_registry as er -from homeassistant.setup import async_setup_component +from homeassistant.helpers.typing import ConfigType -from .conftest import ConfigurationStyle, async_get_flow_preview_state +from .conftest import ( + ConfigurationStyle, + TemplatePlatformSetup, + assert_action, + async_get_flow_preview_state, + async_setup_legacy_platforms, + async_trigger, + make_test_action, + make_test_trigger, + setup_and_test_nested_unique_id, + setup_and_test_unique_id, + setup_entity, +) -from tests.common import MockConfigEntry, assert_setup_component +from tests.common import MockConfigEntry from tests.typing import WebSocketGenerator -# Represent for light's availability -_STATE_AVAILABILITY_BOOLEAN = "availability_boolean.state" - -TEST_OBJECT_ID = "test_light" -TEST_ENTITY_ID = f"light.{TEST_OBJECT_ID}" TEST_STATE_ENTITY_ID = "light.test_state" +TEST_AVAILABILITY_ENTITY = "binary_sensor.availability" -OPTIMISTIC_ON_OFF_LIGHT_CONFIG = { - "turn_on": { - "service": "test.automation", - "data_template": { - "action": "turn_on", - "caller": "{{ this.entity_id }}", - }, - }, - "turn_off": { - "service": "test.automation", - "data_template": { - "action": "turn_off", - "caller": "{{ this.entity_id }}", - }, - }, +TEST_LIGHT = TemplatePlatformSetup( + light.DOMAIN, + "lights", + "test_light", + make_test_trigger( + TEST_STATE_ENTITY_ID, + TEST_AVAILABILITY_ENTITY, + ), +) + +ON_ACTION = make_test_action("turn_on") +OFF_ACTION = make_test_action("turn_off") +ON_OFF_ACTIONS = { + **ON_ACTION, + **OFF_ACTION, } -OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG = { - **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, - "set_level": { - "service": "test.automation", - "data_template": { - "action": "set_level", - "brightness": "{{brightness}}", - "caller": "{{ this.entity_id }}", - }, +BRIGHTNESS_DATA = {"brightness": "{{ brightness }}"} +SET_LEVEL_ACTION = make_test_action("set_level", BRIGHTNESS_DATA) +ON_OFF_SET_LEVEL_ACTIONS = { + **ON_OFF_ACTIONS, + **SET_LEVEL_ACTION, +} + +COLOR_TEMP_ACTION = make_test_action( + "set_temperature", + { + "color_temp": "{{color_temp}}", + "color_temp_kelvin": "{{color_temp_kelvin}}", }, +) +ON_OFF_COLOR_TEMP_ACTIONS = { + **ON_OFF_ACTIONS, + **COLOR_TEMP_ACTION, } -OPTIMISTIC_COLOR_TEMP_LIGHT_CONFIG = { - **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, - "set_temperature": { - "service": "test.automation", - "data_template": { - "action": "set_temperature", - "caller": "{{ this.entity_id }}", - "color_temp": "{{color_temp}}", - "color_temp_kelvin": "{{color_temp_kelvin}}", - }, +ON_OFF_LEGACY_COLOR_ACTIONS = { + **ON_OFF_ACTIONS, + **make_test_action( + "set_color", + {"s": "{{ s }}", "h": "{{ h }}"}, + ), +} + +HS_ACTION = make_test_action( + "set_hs", + {"s": "{{ s }}", "h": "{{ h }}"}, +) +ON_OFF_HS_ACTIONS = { + **ON_OFF_ACTIONS, + **HS_ACTION, +} + +RGB_ACTION = make_test_action( + "set_rgb", + {"r": "{{ r }}", "g": "{{ g }}", "b": "{{ b }}"}, +) +ON_OFF_RGB_ACTIONS = { + **ON_OFF_ACTIONS, + **RGB_ACTION, +} + +RGBW_ACTION = make_test_action( + "set_rgbw", + {"r": "{{ r }}", "g": "{{ g }}", "b": "{{ b }}", "w": "{{ w }}"}, +) +ON_OFF_RGBW_ACTIONS = { + **ON_OFF_ACTIONS, + **RGBW_ACTION, +} + +RGBWW_ACTION = make_test_action( + "set_rgbww", + { + "r": "{{ r }}", + "g": "{{ g }}", + "b": "{{ b }}", + "cw": "{{ cw }}", + "ww": "{{ ww }}", }, +) +ON_OFF_RGBWW_ACTIONS = { + **ON_OFF_ACTIONS, + **RGBWW_ACTION, +} + +SET_EFFECT_ACTION = make_test_action("set_effect", {"effect": "{{ effect }}"}) + +TRANSITION_DATA = {"transition": "{{ transition }}"} +OFF_TRANSITION_ACTION = make_test_action("turn_off", TRANSITION_DATA) +ON_ACTION_WITH_TRANSITION = { + **make_test_action("turn_on", TRANSITION_DATA), + **OFF_ACTION, + **make_test_action("set_level", {**BRIGHTNESS_DATA, **TRANSITION_DATA}), } -OPTIMISTIC_LEGACY_COLOR_LIGHT_CONFIG = { - **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, - "set_color": { - "service": "test.automation", - "data_template": { - "action": "set_color", - "caller": "{{ this.entity_id }}", - "s": "{{s}}", - "h": "{{h}}", - }, - }, +OFF_ACTION_WITH_TRANSITION = { + **ON_ACTION, + **make_test_action("turn_off", TRANSITION_DATA), + **make_test_action("set_level", {**BRIGHTNESS_DATA, **TRANSITION_DATA}), } -OPTIMISTIC_HS_COLOR_LIGHT_CONFIG = { - **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, - "set_hs": { - "service": "test.automation", - "data_template": { - "action": "set_hs", - "caller": "{{ this.entity_id }}", - "s": "{{s}}", - "h": "{{h}}", - }, - }, +ALL_COLOR_ACTIONS = { + **HS_ACTION, + **COLOR_TEMP_ACTION, + **RGB_ACTION, + **RGBW_ACTION, + **RGBWW_ACTION, } -OPTIMISTIC_RGB_COLOR_LIGHT_CONFIG = { - **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, - "set_rgb": { - "service": "test.automation", - "data_template": { - "action": "set_rgb", - "caller": "{{ this.entity_id }}", - "r": "{{r}}", - "g": "{{g}}", - "b": "{{b}}", - }, - }, -} - - -OPTIMISTIC_RGBW_COLOR_LIGHT_CONFIG = { - **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, - "set_rgbw": { - "service": "test.automation", - "data_template": { - "action": "set_rgbw", - "caller": "{{ this.entity_id }}", - "r": "{{r}}", - "g": "{{g}}", - "b": "{{b}}", - "w": "{{w}}", - }, - }, -} - - -OPTIMISTIC_RGBWW_COLOR_LIGHT_CONFIG = { - **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, - "set_rgbww": { - "service": "test.automation", - "data_template": { - "action": "set_rgbww", - "caller": "{{ this.entity_id }}", - "r": "{{r}}", - "g": "{{g}}", - "b": "{{b}}", - "cw": "{{cw}}", - "ww": "{{ww}}", - }, - }, -} - - -TEST_STATE_TRIGGER = { - "trigger": {"trigger": "state", "entity_id": "light.test_state"}, - "variables": {"triggering_entity": "{{ trigger.entity_id }}"}, - "action": [{"event": "action_event", "event_data": {"what": "triggering_entity"}}], -} - - -TEST_EVENT_TRIGGER = { - "trigger": {"platform": "event", "event_type": "test_event"}, - "variables": {"type": "{{ trigger.event.data.type }}"}, - "action": [{"event": "action_event", "event_data": {"type": "{{ type }}"}}], -} - - -TEST_MISSING_KEY_CONFIG = { - "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}}", - }, - }, -} - - -TEST_ON_ACTION_WITH_TRANSITION_CONFIG = { - "turn_on": { - "service": "test.automation", - "data_template": { - "transition": "{{transition}}", - }, - }, - "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}}", - "transition": "{{transition}}", - }, - }, -} - - -TEST_OFF_ACTION_WITH_TRANSITION_CONFIG = { - "turn_on": { - "service": "light.turn_on", - "entity_id": "light.test_state", - }, - "turn_off": { - "service": "test.automation", - "data_template": { - "transition": "{{transition}}", - }, - }, - "set_level": { - "service": "light.turn_on", - "data_template": { - "entity_id": "light.test_state", - "brightness": "{{brightness}}", - "transition": "{{transition}}", - }, - }, -} - - -TEST_ALL_COLORS_NO_TEMPLATE_CONFIG = { - "set_hs": { - "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_rgb": { - "service": "test.automation", - "data_template": { - "entity_id": "test.test_state", - "r": "{{r}}", - "g": "{{g}}", - "b": "{{b}}", - }, - }, - "set_rgbw": { - "service": "test.automation", - "data_template": { - "entity_id": "test.test_state", - "r": "{{r}}", - "g": "{{g}}", - "b": "{{b}}", - "w": "{{w}}", - }, - }, - "set_rgbww": { - "service": "test.automation", - "data_template": { - "entity_id": "test.test_state", - "r": "{{r}}", - "g": "{{g}}", - "b": "{{b}}", - "cw": "{{cw}}", - "ww": "{{ww}}", - }, - }, -} - - -TEST_UNIQUE_ID_CONFIG = { - **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, - "unique_id": "not-so-unique-anymore", -} - - -async def async_setup_legacy_format( - hass: HomeAssistant, count: int, light_config: dict[str, Any] -) -> None: - """Do setup of light integration via legacy format.""" - 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() - - -async def async_setup_legacy_format_with_attribute( +async def _call_and_assert_action( hass: HomeAssistant, - count: int, - attribute: str, - attribute_template: str, - extra_config: dict, + calls: list[ServiceCall], + service: str, + service_data: ConfigType | None = None, + expected_data: ConfigType | None = None, + expected_action: str | None = None, ) -> None: - """Do setup of a legacy light that has a single templated attribute.""" - extra = {attribute: attribute_template} if attribute and attribute_template else {} - await async_setup_legacy_format( - hass, - count, - { - "test_template_light": { - **extra_config, - "value_template": "{{ 1 == 1 }}", - **extra, - } - }, + """Call a service and validate that it was called properly. + + The service is validated when expected_action is omitted. + """ + if expected_action is None: + expected_action = service + current = len(calls) + await hass.services.async_call( + light.DOMAIN, + service, + {**(service_data or {}), ATTR_ENTITY_ID: TEST_LIGHT.entity_id}, + blocking=True, ) - - -async def async_setup_modern_format( - hass: HomeAssistant, count: int, light_config: dict[str, Any] -) -> None: - """Do setup of light integration via new format.""" - config = {"template": {"light": light_config}} - - with assert_setup_component(count, template.DOMAIN): - assert await async_setup_component( - hass, - template.DOMAIN, - config, - ) - - await hass.async_block_till_done() - await hass.async_start() - await hass.async_block_till_done() - - -async def async_setup_modern_format_with_attribute( - hass: HomeAssistant, - count: int, - attribute: str, - attribute_template: str, - extra_config: dict, -) -> None: - """Do setup of a legacy light that has a single templated attribute.""" - extra = {attribute: attribute_template} if attribute and attribute_template else {} - await async_setup_modern_format( - hass, - count, - { - "name": "test_template_light", - **extra_config, - "state": "{{ 1 == 1 }}", - **extra, - }, - ) - - -async def async_setup_trigger_format( - hass: HomeAssistant, count: int, light_config: dict[str, Any] -) -> None: - """Do setup of light integration via new format.""" - config = { - "template": { - **TEST_STATE_TRIGGER, - "light": light_config, - } - } - - with assert_setup_component(count, template.DOMAIN): - assert await async_setup_component( - hass, - template.DOMAIN, - config, - ) - - await hass.async_block_till_done() - await hass.async_start() - await hass.async_block_till_done() - - -async def async_setup_trigger_format_with_attribute( - hass: HomeAssistant, - count: int, - attribute: str, - attribute_template: str, - extra_config: dict, -) -> None: - """Do setup of a legacy light that has a single templated attribute.""" - extra = {attribute: attribute_template} if attribute and attribute_template else {} - await async_setup_trigger_format( - hass, - count, - { - "name": "test_template_light", - **extra_config, - "state": "{{ 1 == 1 }}", - **extra, - }, + assert_action( + TEST_LIGHT, calls, current + 1, expected_action, **(expected_data or {}) ) @@ -420,15 +197,10 @@ async def setup_light( hass: HomeAssistant, count: int, style: ConfigurationStyle, - light_config: dict[str, Any], + config: dict[str, Any], ) -> None: """Do setup of light integration.""" - if style == ConfigurationStyle.LEGACY: - await async_setup_legacy_format(hass, count, light_config) - elif style == ConfigurationStyle.MODERN: - await async_setup_modern_format(hass, count, light_config) - elif style == ConfigurationStyle.TRIGGER: - await async_setup_trigger_format(hass, count, light_config) + await setup_entity(hass, TEST_LIGHT, style, count, config) @pytest.fixture @@ -437,39 +209,18 @@ async def setup_state_light( count: int, style: ConfigurationStyle, state_template: str, + extra_config: ConfigType, ): """Do setup of light integration.""" - if style == ConfigurationStyle.LEGACY: - await async_setup_legacy_format( - hass, - count, - { - "test_template_light": { - **OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, - "value_template": state_template, - } - }, - ) - elif style == ConfigurationStyle.MODERN: - await async_setup_modern_format( - hass, - count, - { - **OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, - "name": "test_template_light", - "state": state_template, - }, - ) - elif style == ConfigurationStyle.TRIGGER: - await async_setup_trigger_format( - hass, - count, - { - **OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, - "name": "test_template_light", - "state": state_template, - }, - ) + await setup_entity( + hass, + TEST_LIGHT, + style, + count, + ON_OFF_SET_LEVEL_ACTIONS, + state_template, + extra_config, + ) @pytest.fixture @@ -482,18 +233,15 @@ async def setup_single_attribute_light( extra_config: dict, ) -> None: """Do setup of light integration.""" - if style == ConfigurationStyle.LEGACY: - await async_setup_legacy_format_with_attribute( - hass, count, attribute, attribute_template, extra_config - ) - elif style == ConfigurationStyle.MODERN: - await async_setup_modern_format_with_attribute( - hass, count, attribute, attribute_template, extra_config - ) - elif style == ConfigurationStyle.TRIGGER: - await async_setup_trigger_format_with_attribute( - hass, count, attribute, attribute_template, extra_config - ) + await setup_entity( + hass, + TEST_LIGHT, + style, + count, + {attribute: attribute_template} if attribute and attribute_template else {}, + "{{ 1 == 1 }}", + extra_config, + ) @pytest.fixture @@ -504,18 +252,13 @@ async def setup_single_action_light( extra_config: dict, ) -> None: """Do setup of light integration.""" - if style == ConfigurationStyle.LEGACY: - await async_setup_legacy_format_with_attribute( - hass, count, "", "", extra_config - ) - elif style == ConfigurationStyle.MODERN: - await async_setup_modern_format_with_attribute( - hass, count, "", "", extra_config - ) - elif style == ConfigurationStyle.TRIGGER: - await async_setup_trigger_format_with_attribute( - hass, count, "", "", extra_config - ) + await setup_entity( + hass, + TEST_LIGHT, + style, + count, + extra_config, + ) @pytest.fixture @@ -527,31 +270,14 @@ async def setup_empty_action_light( extra_config: dict, ) -> None: """Do setup of light integration.""" - if style == ConfigurationStyle.LEGACY: - await async_setup_legacy_format( - hass, - count, - { - "test_template_light": { - "turn_on": [], - "turn_off": [], - action: [], - **extra_config, - } - }, - ) - elif style == ConfigurationStyle.MODERN: - await async_setup_modern_format( - hass, - count, - { - "name": "test_template_light", - "turn_on": [], - "turn_off": [], - action: [], - **extra_config, - }, - ) + await setup_entity( + hass, + TEST_LIGHT, + style, + count, + {"turn_on": [], "turn_off": [], action: []}, + extra_config=extra_config, + ) @pytest.fixture @@ -563,57 +289,29 @@ async def setup_light_with_effects( effect_template: str, ) -> None: """Do setup of light with effects.""" - common = { - "set_effect": { - "service": "test.automation", - "data_template": { - "action": "set_effect", - "caller": "{{ this.entity_id }}", - "entity_id": "test.test_state", - "effect": "{{effect}}", - }, - }, - } - if style == ConfigurationStyle.LEGACY: - await async_setup_legacy_format( - hass, - count, - { - "test_template_light": { - **OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, - "value_template": "{{true}}", - **common, + + await setup_entity( + hass, + TEST_LIGHT, + style, + count, + { + **SET_EFFECT_ACTION, + **( + { "effect_list_template": effect_list_template, "effect_template": effect_template, } - }, - ) - elif style == ConfigurationStyle.MODERN: - await async_setup_modern_format( - hass, - count, - { - "name": "test_template_light", - **OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, - "state": "{{true}}", - **common, - "effect_list": effect_list_template, - "effect": effect_template, - }, - ) - elif style == ConfigurationStyle.TRIGGER: - await async_setup_trigger_format( - hass, - count, - { - "name": "test_template_light", - **OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, - "state": "{{true}}", - **common, - "effect_list": effect_list_template, - "effect": effect_template, - }, - ) + if style == ConfigurationStyle.LEGACY + else { + "effect_list": effect_list_template, + "effect": effect_template, + } + ), + }, + "{{ true }}", + ON_OFF_ACTIONS, + ) @pytest.fixture @@ -625,53 +323,23 @@ async def setup_light_with_mireds( attribute_template: str, ) -> None: """Do setup of light that uses mireds.""" - common = { - "set_temperature": { - "service": "light.turn_on", - "data_template": { - "entity_id": "light.test_state", - "color_temp": "{{color_temp}}", - }, + await setup_entity( + hass, + TEST_LIGHT, + style, + count, + { + attribute: attribute_template, + **make_test_action("set_temperature", {"color_temp": "{{ color_temp }}"}), + **( + {"temperature_template": "{{ 200 }}"} + if style == ConfigurationStyle.LEGACY + else {"temperature": "{{ 200 }}"} + ), }, - attribute: attribute_template, - } - if style == ConfigurationStyle.LEGACY: - await async_setup_legacy_format( - hass, - count, - { - "test_template_light": { - **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, - "value_template": "{{ 1 == 1 }}", - **common, - "temperature_template": "{{200}}", - } - }, - ) - elif style == ConfigurationStyle.MODERN: - await async_setup_modern_format( - hass, - count, - { - "name": "test_template_light", - **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, - "state": "{{ 1 == 1 }}", - **common, - "temperature": "{{200}}", - }, - ) - elif style == ConfigurationStyle.TRIGGER: - await async_setup_trigger_format( - hass, - count, - { - "name": "test_template_light", - **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, - "state": "{{ 1 == 1 }}", - **common, - "temperature": "{{200}}", - }, - ) + "{{ 1==1 }}", + ON_OFF_ACTIONS, + ) @pytest.fixture @@ -682,62 +350,35 @@ async def setup_light_with_transition_template( transition_template: str, ) -> None: """Do setup of light that uses mireds.""" - common = { - "set_effect": { - "service": "test.automation", - "data_template": { - "entity_id": "test.test_state", - "effect": "{{effect}}", - }, - }, - } - if style == ConfigurationStyle.LEGACY: - await async_setup_legacy_format( - hass, - count, - { - "test_template_light": { - **OPTIMISTIC_COLOR_TEMP_LIGHT_CONFIG, - "value_template": "{{ 1 == 1 }}", - **common, + await setup_entity( + hass, + TEST_LIGHT, + style, + count, + { + **SET_EFFECT_ACTION, + **( + { "effect_list_template": "{{ ['Disco', 'Police'] }}", "effect_template": "{{ None }}", "supports_transition_template": transition_template, } - }, - ) - elif style == ConfigurationStyle.MODERN: - await async_setup_modern_format( - hass, - count, - { - "name": "test_template_light", - **OPTIMISTIC_COLOR_TEMP_LIGHT_CONFIG, - "state": "{{ 1 == 1 }}", - **common, - "effect_list": "{{ ['Disco', 'Police'] }}", - "effect": "{{ None }}", - "supports_transition": transition_template, - }, - ) - elif style == ConfigurationStyle.TRIGGER: - await async_setup_trigger_format( - hass, - count, - { - "name": "test_template_light", - **OPTIMISTIC_COLOR_TEMP_LIGHT_CONFIG, - "state": "{{ 1 == 1 }}", - **common, - "effect_list": "{{ ['Disco', 'Police'] }}", - "effect": "{{ None }}", - "supports_transition": transition_template, - }, - ) + if style == ConfigurationStyle.LEGACY + else { + "effect_list": "{{ ['Disco', 'Police'] }}", + "effect": "{{ None }}", + "supports_transition": transition_template, + } + ), + }, + "{{ 1==1 }}", + ON_OFF_ACTIONS, + ) @pytest.mark.parametrize( - ("count", "state_template"), [(1, "{{states.test['big.fat...']}}")] + ("count", "state_template", "extra_config"), + [(1, "{{states.test['big.fat...']}}", {})], ) @pytest.mark.parametrize( "style", @@ -746,55 +387,46 @@ async def setup_light_with_transition_template( @pytest.mark.usefixtures("setup_state_light") async def test_template_state_invalid(hass: HomeAssistant) -> None: """Test template state with render error.""" - # Trigger - hass.states.async_set("light.test_state", None) - await hass.async_block_till_done() + await async_trigger(hass, TEST_STATE_ENTITY_ID, None) - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) assert state.state == STATE_UNAVAILABLE assert state.attributes["supported_color_modes"] == [ColorMode.BRIGHTNESS] assert state.attributes["supported_features"] == 0 -@pytest.mark.parametrize("count", [1]) +@pytest.mark.parametrize( + ("count", "state_template", "extra_config"), + [(1, "{{ states.light.test_state.state }}", {})], +) @pytest.mark.parametrize( "style", - [ - ConfigurationStyle.LEGACY, - ConfigurationStyle.MODERN, - ConfigurationStyle.TRIGGER, - ], + [ConfigurationStyle.LEGACY, ConfigurationStyle.MODERN, ConfigurationStyle.TRIGGER], ) -@pytest.mark.parametrize("state_template", ["{{ states.light.test_state.state }}"]) -async def test_template_state_text(hass: HomeAssistant, setup_state_light) -> None: +@pytest.mark.usefixtures("setup_state_light") +async def test_template_state_text(hass: HomeAssistant) -> None: """Test the state text of a template.""" set_state = STATE_ON - hass.states.async_set("light.test_state", set_state) - await hass.async_block_till_done() - state = hass.states.get("light.test_template_light") + await async_trigger(hass, TEST_STATE_ENTITY_ID, set_state) + state = hass.states.get(TEST_LIGHT.entity_id) assert state.state == set_state assert state.attributes["color_mode"] == ColorMode.BRIGHTNESS assert state.attributes["supported_color_modes"] == [ColorMode.BRIGHTNESS] assert state.attributes["supported_features"] == 0 set_state = STATE_OFF - hass.states.async_set("light.test_state", set_state) - await hass.async_block_till_done() - state = hass.states.get("light.test_template_light") + await async_trigger(hass, TEST_STATE_ENTITY_ID, set_state) + state = hass.states.get(TEST_LIGHT.entity_id) assert state.state == set_state assert state.attributes["color_mode"] is None assert state.attributes["supported_color_modes"] == [ColorMode.BRIGHTNESS] assert state.attributes["supported_features"] == 0 -@pytest.mark.parametrize("count", [1]) +@pytest.mark.parametrize(("count", "extra_config"), [(1, {})]) @pytest.mark.parametrize( "style", - [ - ConfigurationStyle.LEGACY, - ConfigurationStyle.MODERN, - ConfigurationStyle.TRIGGER, - ], + [ConfigurationStyle.LEGACY, ConfigurationStyle.MODERN, ConfigurationStyle.TRIGGER], ) @pytest.mark.parametrize( ("state_template", "expected_state", "expected_color_mode"), @@ -811,135 +443,84 @@ async def test_template_state_text(hass: HomeAssistant, setup_state_light) -> No ), ], ) +@pytest.mark.usefixtures("setup_state_light") async def test_template_state_boolean( hass: HomeAssistant, - expected_color_mode, - expected_state, - style, - setup_state_light, + expected_color_mode: ColorMode | None, + expected_state: str, ) -> None: """Test the setting of the state with boolean on.""" - if style == ConfigurationStyle.TRIGGER: - hass.states.async_set("light.test_state", expected_state) - await hass.async_block_till_done() + await async_trigger(hass, TEST_STATE_ENTITY_ID, expected_state) - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) assert state.state == expected_state assert state.attributes.get("color_mode") == expected_color_mode assert state.attributes["supported_color_modes"] == [ColorMode.BRIGHTNESS] assert state.attributes["supported_features"] == 0 -@pytest.mark.parametrize("count", [0]) +async def test_legacy_template_config_errors(hass: HomeAssistant) -> None: + """Test legacy template light configuration errors.""" + await async_setup_legacy_platforms( + hass, + light.DOMAIN, + "bad name here", + 0, + { + **ON_OFF_SET_LEVEL_ACTIONS, + "value_template": "{{ 1== 1}}", + }, + ) + assert hass.states.async_all("light") == [] + + @pytest.mark.parametrize( - ("light_config", "style"), - [ - ( - { - "test_template_light": { - **OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, - "value_template": "{%- if false -%}", - } - }, - ConfigurationStyle.LEGACY, - ), - ( - { - "bad name here": { - **OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, - "value_template": "{{ 1== 1}}", - } - }, - ConfigurationStyle.LEGACY, - ), - ( - {"test_template_light": "Invalid"}, - ConfigurationStyle.LEGACY, - ), - ( - { - **OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, - "name": "test_template_light", - "state": "{%- if false -%}", - }, - ConfigurationStyle.MODERN, - ), - ( - { - **OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, - "name": "test_template_light", - "state": "{%- if false -%}", - }, - ConfigurationStyle.TRIGGER, - ), - ], + ("count", "state_template", "extra_config"), [(0, "{%- if false -%}", {})] ) -async def test_template_config_errors(hass: HomeAssistant, setup_light) -> None: +@pytest.mark.parametrize( + "style", + [ConfigurationStyle.LEGACY, ConfigurationStyle.MODERN, ConfigurationStyle.TRIGGER], +) +@pytest.mark.usefixtures("setup_state_light") +async def test_template_config_errors(hass: HomeAssistant) -> None: """Test template light configuration errors.""" assert hass.states.async_all("light") == [] @pytest.mark.parametrize( - ("light_config", "style", "count"), - [ - ( - {"light_one": {"value_template": "{{ 1== 1}}", **TEST_MISSING_KEY_CONFIG}}, - ConfigurationStyle.LEGACY, - 0, - ), - ( - {"name": "light_one", "state": "{{ 1== 1}}", **TEST_MISSING_KEY_CONFIG}, - ConfigurationStyle.MODERN, - 0, - ), - ( - {"name": "light_one", "state": "{{ 1== 1}}", **TEST_MISSING_KEY_CONFIG}, - ConfigurationStyle.TRIGGER, - 0, - ), - ], + ("count", "config"), + [(0, {**ON_ACTION, **SET_LEVEL_ACTION})], ) -async def test_missing_key(hass: HomeAssistant, count, setup_light) -> None: - """Test missing template.""" - if count: - assert hass.states.async_all("light") != [] - else: - assert hass.states.async_all("light") == [] - - -@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( "style", - [ - ConfigurationStyle.LEGACY, - ConfigurationStyle.MODERN, - ConfigurationStyle.TRIGGER, - ], + [ConfigurationStyle.LEGACY, ConfigurationStyle.MODERN, ConfigurationStyle.TRIGGER], ) -@pytest.mark.parametrize("state_template", ["{{ states.light.test_state.state }}"]) -async def test_on_action( - hass: HomeAssistant, setup_state_light, calls: list[ServiceCall] -) -> None: +@pytest.mark.usefixtures("setup_light") +async def test_missing_key(hass: HomeAssistant) -> None: + """Test missing template.""" + assert hass.states.async_all("light") == [] + + +@pytest.mark.parametrize( + ("count", "state_template", "extra_config"), + [(1, "{{ states.light.test_state.state }}", {})], +) +@pytest.mark.parametrize( + "style", + [ConfigurationStyle.LEGACY, ConfigurationStyle.MODERN, ConfigurationStyle.TRIGGER], +) +@pytest.mark.usefixtures("setup_state_light") +async def test_on_action(hass: HomeAssistant, calls: list[ServiceCall]) -> None: """Test on action.""" - hass.states.async_set("light.test_state", STATE_OFF) - await hass.async_block_till_done() + await async_trigger(hass, TEST_STATE_ENTITY_ID, STATE_OFF) - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) assert state.state == STATE_OFF assert state.attributes["color_mode"] is None assert state.attributes["supported_color_modes"] == [ColorMode.BRIGHTNESS] assert state.attributes["supported_features"] == 0 - await hass.services.async_call( - light.DOMAIN, - SERVICE_TURN_ON, - {ATTR_ENTITY_ID: "light.test_template_light"}, - blocking=True, - ) - - assert len(calls) == 1 - assert calls[-1].data["action"] == "turn_on" - assert calls[-1].data["caller"] == "light.test_template_light" + await _call_and_assert_action(hass, calls, SERVICE_TURN_ON) assert state.state == STATE_OFF assert state.attributes["color_mode"] is None @@ -949,176 +530,119 @@ async def test_on_action( @pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( - ("light_config", "style"), + ("config", "style"), [ ( { - "test_template_light": { - "value_template": "{{states.light.test_state.state}}", - **TEST_ON_ACTION_WITH_TRANSITION_CONFIG, - "supports_transition_template": "{{true}}", - } + "value_template": "{{states.light.test_state.state}}", + **ON_ACTION_WITH_TRANSITION, + "supports_transition_template": "{{true}}", }, ConfigurationStyle.LEGACY, ), ( { - "name": "test_template_light", "state": "{{states.light.test_state.state}}", - **TEST_ON_ACTION_WITH_TRANSITION_CONFIG, + **ON_ACTION_WITH_TRANSITION, "supports_transition": "{{true}}", }, ConfigurationStyle.MODERN, ), ( { - "name": "test_template_light", "state": "{{states.light.test_state.state}}", - **TEST_ON_ACTION_WITH_TRANSITION_CONFIG, + **ON_ACTION_WITH_TRANSITION, "supports_transition": "{{true}}", }, ConfigurationStyle.TRIGGER, ), ], ) -async def test_on_action_with_transition( - hass: HomeAssistant, setup_light, calls: list[ServiceCall] -) -> None: - """Test on action with transition.""" - hass.states.async_set("light.test_state", STATE_OFF) - await hass.async_block_till_done() - - state = hass.states.get("light.test_template_light") - assert state.state == STATE_OFF - assert state.attributes["color_mode"] is None - assert state.attributes["supported_color_modes"] == [ColorMode.BRIGHTNESS] - assert state.attributes["supported_features"] == LightEntityFeature.TRANSITION - - await hass.services.async_call( - light.DOMAIN, - SERVICE_TURN_ON, - {ATTR_ENTITY_ID: "light.test_template_light", ATTR_TRANSITION: 5}, - blocking=True, - ) - - assert len(calls) == 1 - assert calls[0].data["transition"] == 5 - - assert state.state == STATE_OFF - assert state.attributes["color_mode"] is None - assert state.attributes["supported_color_modes"] == [ColorMode.BRIGHTNESS] - assert state.attributes["supported_features"] == LightEntityFeature.TRANSITION - - -@pytest.mark.parametrize("count", [1]) -@pytest.mark.parametrize( - ("light_config", "style"), - [ - ( - { - "test_template_light": { - **OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, - } - }, - ConfigurationStyle.LEGACY, - ), - ( - { - "name": "test_template_light", - **OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, - }, - ConfigurationStyle.MODERN, - ), - ( - { - "name": "test_template_light", - **OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, - }, - ConfigurationStyle.TRIGGER, - ), - ], -) +@pytest.mark.usefixtures("setup_light") +async def test_on_action_with_transition( + hass: HomeAssistant, calls: list[ServiceCall] +) -> None: + """Test on action with transition.""" + await async_trigger(hass, TEST_STATE_ENTITY_ID, STATE_OFF) + + state = hass.states.get(TEST_LIGHT.entity_id) + assert state.state == STATE_OFF + assert state.attributes["color_mode"] is None + assert state.attributes["supported_color_modes"] == [ColorMode.BRIGHTNESS] + assert state.attributes["supported_features"] == LightEntityFeature.TRANSITION + + await _call_and_assert_action( + hass, calls, SERVICE_TURN_ON, {ATTR_TRANSITION: 5}, {ATTR_TRANSITION: 5} + ) + + assert state.state == STATE_OFF + assert state.attributes["color_mode"] is None + assert state.attributes["supported_color_modes"] == [ColorMode.BRIGHTNESS] + assert state.attributes["supported_features"] == LightEntityFeature.TRANSITION + + +@pytest.mark.parametrize(("count", "config"), [(1, ON_OFF_SET_LEVEL_ACTIONS)]) +@pytest.mark.parametrize( + "style", + [ConfigurationStyle.LEGACY, ConfigurationStyle.MODERN, ConfigurationStyle.TRIGGER], +) @pytest.mark.usefixtures("setup_light") async def test_on_action_optimistic( hass: HomeAssistant, calls: list[ServiceCall] ) -> None: """Test on action with optimistic state.""" - hass.states.async_set("light.test_state", STATE_OFF) - await hass.async_block_till_done() + await async_trigger(hass, TEST_STATE_ENTITY_ID, STATE_OFF) - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) assert state.state == STATE_UNKNOWN assert state.attributes["color_mode"] is None assert state.attributes["supported_color_modes"] == [ColorMode.BRIGHTNESS] assert state.attributes["supported_features"] == 0 - await hass.services.async_call( - light.DOMAIN, - SERVICE_TURN_ON, - {ATTR_ENTITY_ID: "light.test_template_light"}, - blocking=True, - ) + await _call_and_assert_action(hass, calls, SERVICE_TURN_ON) - state = hass.states.get("light.test_template_light") - assert len(calls) == 1 - assert calls[-1].data["action"] == "turn_on" - assert calls[-1].data["caller"] == "light.test_template_light" + state = hass.states.get(TEST_LIGHT.entity_id) assert state.state == STATE_ON assert state.attributes["color_mode"] == ColorMode.BRIGHTNESS assert state.attributes["supported_color_modes"] == [ColorMode.BRIGHTNESS] assert state.attributes["supported_features"] == 0 - await hass.services.async_call( - light.DOMAIN, + await _call_and_assert_action( + hass, + calls, SERVICE_TURN_ON, - {ATTR_ENTITY_ID: "light.test_template_light", ATTR_BRIGHTNESS: 100}, - blocking=True, + {ATTR_BRIGHTNESS: 100}, + {ATTR_BRIGHTNESS: 100}, + "set_level", ) - state = hass.states.get("light.test_template_light") - assert len(calls) == 2 - assert calls[-1].data["action"] == "set_level" - assert calls[-1].data["brightness"] == 100 - assert calls[-1].data["caller"] == "light.test_template_light" + state = hass.states.get(TEST_LIGHT.entity_id) assert state.state == STATE_ON assert state.attributes["color_mode"] == ColorMode.BRIGHTNESS assert state.attributes["supported_color_modes"] == [ColorMode.BRIGHTNESS] assert state.attributes["supported_features"] == 0 -@pytest.mark.parametrize("count", [1]) +@pytest.mark.parametrize( + ("count", "state_template", "extra_config"), + [(1, "{{ states.light.test_state.state }}", {})], +) @pytest.mark.parametrize( "style", - [ - ConfigurationStyle.LEGACY, - ConfigurationStyle.MODERN, - ConfigurationStyle.TRIGGER, - ], + [ConfigurationStyle.LEGACY, ConfigurationStyle.MODERN, ConfigurationStyle.TRIGGER], ) -@pytest.mark.parametrize("state_template", ["{{ states.light.test_state.state }}"]) -async def test_off_action( - hass: HomeAssistant, setup_state_light, calls: list[ServiceCall] -) -> None: +@pytest.mark.usefixtures("setup_state_light") +async def test_off_action(hass: HomeAssistant, calls: list[ServiceCall]) -> None: """Test off action.""" - hass.states.async_set("light.test_state", STATE_ON) - await hass.async_block_till_done() - - state = hass.states.get("light.test_template_light") + await async_trigger(hass, TEST_STATE_ENTITY_ID, STATE_ON) + state = hass.states.get(TEST_LIGHT.entity_id) assert state.state == STATE_ON assert state.attributes["color_mode"] == ColorMode.BRIGHTNESS assert state.attributes["supported_color_modes"] == [ColorMode.BRIGHTNESS] assert state.attributes["supported_features"] == 0 - await hass.services.async_call( - light.DOMAIN, - SERVICE_TURN_OFF, - {ATTR_ENTITY_ID: "light.test_template_light"}, - blocking=True, - ) + await _call_and_assert_action(hass, calls, SERVICE_TURN_OFF) - assert len(calls) == 1 - assert calls[-1].data["action"] == "turn_off" - assert calls[-1].data["caller"] == "light.test_template_light" assert state.state == STATE_ON assert state.attributes["color_mode"] == ColorMode.BRIGHTNESS assert state.attributes["supported_color_modes"] == [ColorMode.BRIGHTNESS] @@ -1127,151 +651,108 @@ async def test_off_action( @pytest.mark.parametrize("count", [(1)]) @pytest.mark.parametrize( - ("light_config", "style"), + ("config", "style"), [ ( { - "test_template_light": { - "value_template": "{{states.light.test_state.state}}", - **TEST_OFF_ACTION_WITH_TRANSITION_CONFIG, - "supports_transition_template": "{{true}}", - } + "value_template": "{{states.light.test_state.state}}", + **OFF_ACTION_WITH_TRANSITION, + "supports_transition_template": "{{true}}", }, ConfigurationStyle.LEGACY, ), ( { - "name": "test_template_light", "state": "{{states.light.test_state.state}}", - **TEST_OFF_ACTION_WITH_TRANSITION_CONFIG, + **OFF_ACTION_WITH_TRANSITION, "supports_transition": "{{true}}", }, ConfigurationStyle.MODERN, ), ( { - "name": "test_template_light", "state": "{{states.light.test_state.state}}", - **TEST_OFF_ACTION_WITH_TRANSITION_CONFIG, + **OFF_ACTION_WITH_TRANSITION, "supports_transition": "{{true}}", }, ConfigurationStyle.TRIGGER, ), ], ) -async def test_off_action_with_transition( - hass: HomeAssistant, setup_light, calls: list[ServiceCall] -) -> None: - """Test off action with transition.""" - hass.states.async_set("light.test_state", STATE_ON) - await hass.async_block_till_done() - - state = hass.states.get("light.test_template_light") - assert state.state == STATE_ON - assert state.attributes["color_mode"] == ColorMode.BRIGHTNESS - assert state.attributes["supported_color_modes"] == [ColorMode.BRIGHTNESS] - assert state.attributes["supported_features"] == LightEntityFeature.TRANSITION - - await hass.services.async_call( - light.DOMAIN, - SERVICE_TURN_OFF, - {ATTR_ENTITY_ID: "light.test_template_light", ATTR_TRANSITION: 2}, - blocking=True, - ) - - assert len(calls) == 1 - assert calls[0].data["transition"] == 2 - assert state.state == STATE_ON - assert state.attributes["color_mode"] == ColorMode.BRIGHTNESS - assert state.attributes["supported_color_modes"] == [ColorMode.BRIGHTNESS] - assert state.attributes["supported_features"] == LightEntityFeature.TRANSITION - - -@pytest.mark.parametrize("count", [1]) -@pytest.mark.parametrize( - ("light_config", "style"), - [ - ( - { - "test_template_light": { - **OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, - } - }, - ConfigurationStyle.LEGACY, - ), - ( - { - "name": "test_template_light", - **OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, - }, - ConfigurationStyle.MODERN, - ), - ( - { - "name": "test_template_light", - **OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, - }, - ConfigurationStyle.TRIGGER, - ), - ], -) +@pytest.mark.usefixtures("setup_light") +async def test_off_action_with_transition( + hass: HomeAssistant, calls: list[ServiceCall] +) -> None: + """Test off action with transition.""" + await async_trigger(hass, TEST_STATE_ENTITY_ID, STATE_ON) + state = hass.states.get(TEST_LIGHT.entity_id) + assert state.state == STATE_ON + assert state.attributes["color_mode"] == ColorMode.BRIGHTNESS + assert state.attributes["supported_color_modes"] == [ColorMode.BRIGHTNESS] + assert state.attributes["supported_features"] == LightEntityFeature.TRANSITION + + await _call_and_assert_action( + hass, calls, SERVICE_TURN_OFF, {ATTR_TRANSITION: 2}, {ATTR_TRANSITION: 2} + ) + + assert state.state == STATE_ON + assert state.attributes["color_mode"] == ColorMode.BRIGHTNESS + assert state.attributes["supported_color_modes"] == [ColorMode.BRIGHTNESS] + assert state.attributes["supported_features"] == LightEntityFeature.TRANSITION + + +@pytest.mark.parametrize(("count", "config"), [(1, ON_OFF_SET_LEVEL_ACTIONS)]) +@pytest.mark.parametrize( + "style", + [ConfigurationStyle.LEGACY, ConfigurationStyle.MODERN, ConfigurationStyle.TRIGGER], +) @pytest.mark.usefixtures("setup_light") async def test_off_action_optimistic( hass: HomeAssistant, calls: list[ServiceCall] ) -> None: """Test off action with optimistic state.""" - hass.states.async_set("light.test_state", STATE_OFF) - await hass.async_block_till_done() - - state = hass.states.get("light.test_template_light") + await async_trigger(hass, TEST_STATE_ENTITY_ID, STATE_OFF) + state = hass.states.get(TEST_LIGHT.entity_id) assert state.state == STATE_UNKNOWN assert state.attributes["color_mode"] is None assert state.attributes["supported_color_modes"] == [ColorMode.BRIGHTNESS] assert state.attributes["supported_features"] == 0 - await hass.services.async_call( - light.DOMAIN, - SERVICE_TURN_OFF, - {ATTR_ENTITY_ID: "light.test_template_light"}, - blocking=True, - ) + await _call_and_assert_action(hass, calls, SERVICE_TURN_OFF) - assert len(calls) == 1 - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) assert state.state == STATE_OFF assert state.attributes["color_mode"] is None assert state.attributes["supported_color_modes"] == [ColorMode.BRIGHTNESS] assert state.attributes["supported_features"] == 0 -@pytest.mark.parametrize("count", [1]) +@pytest.mark.parametrize( + ("count", "state_template", "extra_config"), + [(1, "{{ 1 == 1 }}", {})], +) @pytest.mark.parametrize( "style", [ConfigurationStyle.LEGACY, ConfigurationStyle.MODERN, ConfigurationStyle.TRIGGER], ) -@pytest.mark.parametrize("state_template", ["{{1 == 1}}"]) +@pytest.mark.usefixtures("setup_state_light") async def test_level_action_no_template( - hass: HomeAssistant, - setup_state_light, - calls: list[ServiceCall], + hass: HomeAssistant, calls: list[ServiceCall] ) -> None: """Test setting brightness with optimistic template.""" - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) assert state.attributes.get("brightness") is None - await hass.services.async_call( - light.DOMAIN, + await _call_and_assert_action( + hass, + calls, SERVICE_TURN_ON, - {ATTR_ENTITY_ID: "light.test_template_light", ATTR_BRIGHTNESS: 124}, - blocking=True, + {ATTR_BRIGHTNESS: 124}, + {ATTR_BRIGHTNESS: 124}, + "set_level", ) - assert len(calls) == 1 - assert calls[-1].data["action"] == "set_level" - assert calls[-1].data["brightness"] == 124 - assert calls[-1].data["caller"] == "light.test_template_light" - - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) assert state.state == STATE_ON assert state.attributes["brightness"] == 124 assert state.attributes["color_mode"] == ColorMode.BRIGHTNESS @@ -1279,9 +760,7 @@ async def test_level_action_no_template( assert state.attributes["supported_features"] == 0 -@pytest.mark.parametrize( - ("count", "extra_config"), [(1, OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG)] -) +@pytest.mark.parametrize(("count", "extra_config"), [(1, ON_OFF_SET_LEVEL_ACTIONS)]) @pytest.mark.parametrize( ("style", "attribute"), [ @@ -1306,19 +785,15 @@ async def test_level_action_no_template( (None, "{{'one'}}", ColorMode.BRIGHTNESS), ], ) +@pytest.mark.usefixtures("setup_single_attribute_light") async def test_level_template( hass: HomeAssistant, - style: ConfigurationStyle, expected_level: Any, expected_color_mode: ColorMode, - setup_single_attribute_light, ) -> None: """Test the template for the level.""" - if style == ConfigurationStyle.TRIGGER: - hass.states.async_set("light.test_state", STATE_ON) - await hass.async_block_till_done() - - state = hass.states.get("light.test_template_light") + await async_trigger(hass, TEST_STATE_ENTITY_ID, STATE_ON) + state = hass.states.get(TEST_LIGHT.entity_id) assert state.attributes.get("brightness") == expected_level assert state.state == STATE_ON @@ -1327,9 +802,7 @@ async def test_level_template( assert state.attributes["supported_features"] == 0 -@pytest.mark.parametrize( - ("count", "extra_config"), [(1, OPTIMISTIC_COLOR_TEMP_LIGHT_CONFIG)] -) +@pytest.mark.parametrize(("count", "extra_config"), [(1, ON_OFF_COLOR_TEMP_ACTIONS)]) @pytest.mark.parametrize( ("style", "attribute"), [ @@ -1350,19 +823,15 @@ async def test_level_template( (None, "{{ 'one' }}", ColorMode.COLOR_TEMP), ], ) +@pytest.mark.usefixtures("setup_single_attribute_light") async def test_temperature_template( hass: HomeAssistant, - style: ConfigurationStyle, expected_temp: Any, expected_color_mode: ColorMode, - setup_single_attribute_light, ) -> None: """Test the template for the temperature.""" - if style == ConfigurationStyle.TRIGGER: - hass.states.async_set("light.test_state", STATE_ON) - await hass.async_block_till_done() - - state = hass.states.get("light.test_template_light") + await async_trigger(hass, TEST_STATE_ENTITY_ID, STATE_ON) + state = hass.states.get(TEST_LIGHT.entity_id) assert state.attributes.get("color_temp_kelvin") == expected_temp assert state.state == STATE_ON assert state.attributes.get("color_mode") == expected_color_mode @@ -1370,9 +839,7 @@ async def test_temperature_template( assert state.attributes["supported_features"] == 0 -@pytest.mark.parametrize( - ("count", "extra_config"), [(1, OPTIMISTIC_COLOR_TEMP_LIGHT_CONFIG)] -) +@pytest.mark.parametrize(("count", "extra_config"), [(1, ON_OFF_COLOR_TEMP_ACTIONS)]) @pytest.mark.parametrize( "style", [ @@ -1381,28 +848,25 @@ async def test_temperature_template( ConfigurationStyle.TRIGGER, ], ) +@pytest.mark.usefixtures("setup_single_action_light") async def test_temperature_action_no_template( hass: HomeAssistant, - setup_single_action_light, calls: list[ServiceCall], ) -> None: """Test setting temperature with optimistic template.""" - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) assert state.attributes.get("color_template") is None - await hass.services.async_call( - light.DOMAIN, + await _call_and_assert_action( + hass, + calls, SERVICE_TURN_ON, - {ATTR_ENTITY_ID: "light.test_template_light", ATTR_COLOR_TEMP_KELVIN: 2898}, - blocking=True, + {ATTR_COLOR_TEMP_KELVIN: 2898}, + {ATTR_COLOR_TEMP_KELVIN: 2898}, + "set_temperature", ) - assert len(calls) == 1 - assert calls[-1].data["action"] == "set_temperature" - assert calls[-1].data["caller"] == "light.test_template_light" - assert calls[-1].data["color_temp_kelvin"] == 2898 - - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) assert state is not None assert state.attributes.get("color_temp_kelvin") == 2898 assert state.state == STATE_ON @@ -1411,42 +875,20 @@ async def test_temperature_action_no_template( assert state.attributes["supported_features"] == 0 -@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( - ("light_config", "style", "entity_id"), + ("count", "attribute_template", "extra_config"), + [(1, "Template light", ON_OFF_SET_LEVEL_ACTIONS)], +) +@pytest.mark.parametrize( + ("style", "attribute", "entity_id"), [ - ( - { - "test_template_light": { - **OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, - "friendly_name": "Template light", - "value_template": "{{ 1 == 1 }}", - } - }, - ConfigurationStyle.LEGACY, - "light.test_template_light", - ), - ( - { - **OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, - "name": "Template light", - "state": "{{ 1 == 1 }}", - }, - ConfigurationStyle.MODERN, - "light.template_light", - ), - ( - { - **OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, - "name": "Template light", - "state": "{{ 1 == 1 }}", - }, - ConfigurationStyle.TRIGGER, - "light.template_light", - ), + (ConfigurationStyle.LEGACY, "friendly_name", TEST_LIGHT.entity_id), + (ConfigurationStyle.MODERN, "name", "light.template_light"), + (ConfigurationStyle.TRIGGER, "name", "light.template_light"), ], ) -async def test_friendly_name(hass: HomeAssistant, entity_id: str, setup_light) -> None: +@pytest.mark.usefixtures("setup_single_attribute_light") +async def test_friendly_name(hass: HomeAssistant, entity_id: str) -> None: """Test the accessibility of the friendly_name attribute.""" state = hass.states.get(entity_id) @@ -1455,9 +897,7 @@ async def test_friendly_name(hass: HomeAssistant, entity_id: str, setup_light) - assert state.attributes.get("friendly_name") == "Template light" -@pytest.mark.parametrize( - ("count", "extra_config"), [(1, OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG)] -) +@pytest.mark.parametrize(("count", "extra_config"), [(1, ON_OFF_SET_LEVEL_ACTIONS)]) @pytest.mark.parametrize( ("style", "attribute"), [ @@ -1469,22 +909,19 @@ async def test_friendly_name(hass: HomeAssistant, entity_id: str, setup_light) - @pytest.mark.parametrize( "attribute_template", ["{% if states.light.test_state.state %}mdi:check{% endif %}"] ) -async def test_icon_template(hass: HomeAssistant, setup_single_attribute_light) -> None: +@pytest.mark.usefixtures("setup_single_attribute_light") +async def test_icon_template(hass: HomeAssistant) -> None: """Test icon template.""" - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) assert state.attributes.get("icon") in ("", None) - state = hass.states.async_set("light.test_state", STATE_ON) - await hass.async_block_till_done() - - state = hass.states.get("light.test_template_light") + await async_trigger(hass, TEST_STATE_ENTITY_ID, STATE_ON) + state = hass.states.get(TEST_LIGHT.entity_id) assert state.attributes["icon"] == "mdi:check" -@pytest.mark.parametrize( - ("count", "extra_config"), [(1, OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG)] -) +@pytest.mark.parametrize(("count", "extra_config"), [(1, ON_OFF_SET_LEVEL_ACTIONS)]) @pytest.mark.parametrize( ("style", "attribute"), [ @@ -1497,56 +934,42 @@ async def test_icon_template(hass: HomeAssistant, setup_single_attribute_light) "attribute_template", ["{% if states.light.test_state.state %}/local/light.png{% endif %}"], ) -async def test_entity_picture_template( - hass: HomeAssistant, setup_single_attribute_light -) -> None: +@pytest.mark.usefixtures("setup_single_attribute_light") +async def test_entity_picture_template(hass: HomeAssistant) -> None: """Test entity_picture template.""" - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) assert state.attributes.get("entity_picture") in ("", None) - state = hass.states.async_set("light.test_state", STATE_ON) - await hass.async_block_till_done() - - state = hass.states.get("light.test_template_light") - + await async_trigger(hass, TEST_STATE_ENTITY_ID, STATE_ON) + state = hass.states.get(TEST_LIGHT.entity_id) assert state.attributes["entity_picture"] == "/local/light.png" -@pytest.mark.parametrize( - ("count", "extra_config"), - [ - (1, OPTIMISTIC_LEGACY_COLOR_LIGHT_CONFIG), - ], -) +@pytest.mark.parametrize(("count", "extra_config"), [(1, ON_OFF_LEGACY_COLOR_ACTIONS)]) @pytest.mark.parametrize( "style", [ ConfigurationStyle.LEGACY, ], ) +@pytest.mark.usefixtures("setup_single_action_light") async def test_legacy_color_action_no_template( - hass: HomeAssistant, - setup_single_action_light, - calls: list[ServiceCall], + hass: HomeAssistant, calls: list[ServiceCall] ) -> None: """Test setting color with optimistic template.""" - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) assert state.attributes.get("hs_color") is None - await hass.services.async_call( - light.DOMAIN, + await _call_and_assert_action( + hass, + calls, SERVICE_TURN_ON, - {ATTR_ENTITY_ID: "light.test_template_light", ATTR_HS_COLOR: (40, 50)}, - blocking=True, + {ATTR_HS_COLOR: (40, 50)}, + {"h": 40, "s": 50}, + "set_color", ) - assert len(calls) == 1 - assert calls[-1].data["action"] == "set_color" - assert calls[-1].data["caller"] == "light.test_template_light" - assert calls[-1].data["h"] == 40 - assert calls[-1].data["s"] == 50 - - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) assert state.state == STATE_ON assert state.attributes["color_mode"] == ColorMode.HS assert state.attributes.get("hs_color") == (40, 50) @@ -1555,190 +978,18 @@ async def test_legacy_color_action_no_template( @pytest.mark.parametrize( - ("count", "extra_config"), + ("count", "style", "extra_config", "attribute"), [ - (1, OPTIMISTIC_HS_COLOR_LIGHT_CONFIG), + ( + 1, + ConfigurationStyle.LEGACY, + ON_OFF_LEGACY_COLOR_ACTIONS, + "color_template", + ), ], ) @pytest.mark.parametrize( - "style", - [ - ConfigurationStyle.LEGACY, - ConfigurationStyle.MODERN, - ConfigurationStyle.TRIGGER, - ], -) -async def test_hs_color_action_no_template( - hass: HomeAssistant, - setup_single_action_light, - calls: list[ServiceCall], -) -> None: - """Test setting color with optimistic template.""" - state = hass.states.get("light.test_template_light") - assert state.attributes.get("hs_color") is None - - await hass.services.async_call( - light.DOMAIN, - SERVICE_TURN_ON, - {ATTR_ENTITY_ID: "light.test_template_light", ATTR_HS_COLOR: (40, 50)}, - blocking=True, - ) - - assert len(calls) == 1 - assert calls[-1].data["action"] == "set_hs" - assert calls[-1].data["caller"] == "light.test_template_light" - 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 - assert state.attributes["color_mode"] == ColorMode.HS - assert state.attributes.get("hs_color") == (40, 50) - assert state.attributes["supported_color_modes"] == [ColorMode.HS] - assert state.attributes["supported_features"] == 0 - - -@pytest.mark.parametrize( - ("count", "extra_config"), - [(1, OPTIMISTIC_RGB_COLOR_LIGHT_CONFIG)], -) -@pytest.mark.parametrize( - "style", - [ - ConfigurationStyle.LEGACY, - ConfigurationStyle.MODERN, - ConfigurationStyle.TRIGGER, - ], -) -async def test_rgb_color_action_no_template( - hass: HomeAssistant, - setup_single_action_light, - calls: list[ServiceCall], -) -> None: - """Test setting rgb color with optimistic template.""" - state = hass.states.get("light.test_template_light") - assert state.attributes.get("rgb_color") is None - - await hass.services.async_call( - light.DOMAIN, - SERVICE_TURN_ON, - {ATTR_ENTITY_ID: "light.test_template_light", ATTR_RGB_COLOR: (160, 78, 192)}, - blocking=True, - ) - - assert len(calls) == 1 - assert calls[-1].data["action"] == "set_rgb" - assert calls[-1].data["caller"] == "light.test_template_light" - assert calls[-1].data["r"] == 160 - assert calls[-1].data["g"] == 78 - assert calls[-1].data["b"] == 192 - - state = hass.states.get("light.test_template_light") - assert state.state == STATE_ON - assert state.attributes["color_mode"] == ColorMode.RGB - assert state.attributes.get("rgb_color") == (160, 78, 192) - assert state.attributes["supported_color_modes"] == [ColorMode.RGB] - assert state.attributes["supported_features"] == 0 - - -@pytest.mark.parametrize( - ("count", "extra_config"), - [(1, OPTIMISTIC_RGBW_COLOR_LIGHT_CONFIG)], -) -@pytest.mark.parametrize( - "style", - [ - ConfigurationStyle.LEGACY, - ConfigurationStyle.MODERN, - ConfigurationStyle.TRIGGER, - ], -) -async def test_rgbw_color_action_no_template( - hass: HomeAssistant, - setup_single_action_light, - calls: list[ServiceCall], -) -> None: - """Test setting rgbw color with optimistic template.""" - state = hass.states.get("light.test_template_light") - assert state.attributes.get("rgbw_color") is None - - await hass.services.async_call( - light.DOMAIN, - SERVICE_TURN_ON, - { - ATTR_ENTITY_ID: "light.test_template_light", - ATTR_RGBW_COLOR: (160, 78, 192, 25), - }, - blocking=True, - ) - - assert len(calls) == 1 - assert calls[-1].data["action"] == "set_rgbw" - assert calls[-1].data["caller"] == "light.test_template_light" - assert calls[-1].data["r"] == 160 - assert calls[-1].data["g"] == 78 - assert calls[-1].data["b"] == 192 - assert calls[-1].data["w"] == 25 - - state = hass.states.get("light.test_template_light") - assert state.state == STATE_ON - assert state.attributes["color_mode"] == ColorMode.RGBW - assert state.attributes.get("rgbw_color") == (160, 78, 192, 25) - assert state.attributes["supported_color_modes"] == [ColorMode.RGBW] - assert state.attributes["supported_features"] == 0 - - -@pytest.mark.parametrize( - ("count", "extra_config"), - [(1, OPTIMISTIC_RGBWW_COLOR_LIGHT_CONFIG)], -) -@pytest.mark.parametrize( - "style", - [ - ConfigurationStyle.LEGACY, - ConfigurationStyle.MODERN, - ConfigurationStyle.TRIGGER, - ], -) -async def test_rgbww_color_action_no_template( - hass: HomeAssistant, - setup_single_action_light, - calls: list[ServiceCall], -) -> None: - """Test setting rgbww color with optimistic template.""" - state = hass.states.get("light.test_template_light") - assert state.attributes.get("rgbww_color") is None - - await hass.services.async_call( - light.DOMAIN, - SERVICE_TURN_ON, - { - ATTR_ENTITY_ID: "light.test_template_light", - ATTR_RGBWW_COLOR: (160, 78, 192, 25, 55), - }, - blocking=True, - ) - - assert len(calls) == 1 - assert calls[-1].data["action"] == "set_rgbww" - assert calls[-1].data["caller"] == "light.test_template_light" - assert calls[-1].data["r"] == 160 - assert calls[-1].data["g"] == 78 - assert calls[-1].data["b"] == 192 - assert calls[-1].data["cw"] == 25 - assert calls[-1].data["ww"] == 55 - - state = hass.states.get("light.test_template_light") - assert state.state == STATE_ON - assert state.attributes["color_mode"] == ColorMode.RGBWW - assert state.attributes.get("rgbww_color") == (160, 78, 192, 25, 55) - assert state.attributes["supported_color_modes"] == [ColorMode.RGBWW] - assert state.attributes["supported_features"] == 0 - - -@pytest.mark.parametrize("count", [1]) -@pytest.mark.parametrize( - ("expected_hs", "color_template", "expected_color_mode"), + ("expected_hs", "attribute_template", "expected_color_mode"), [ ((360, 100), "{{(360, 100)}}", ColorMode.HS), ((359.9, 99.9), "{{(359.9, 99.9)}}", ColorMode.HS), @@ -1751,23 +1002,14 @@ async def test_rgbww_color_action_no_template( (None, "{{('one','two')}}", ColorMode.HS), ], ) +@pytest.mark.usefixtures("setup_single_attribute_light") async def test_legacy_color_template( hass: HomeAssistant, expected_hs: tuple[float, float] | None, expected_color_mode: ColorMode, - count: int, - color_template: str, ) -> None: """Test the template for the color.""" - light_config = { - "test_template_light": { - **OPTIMISTIC_LEGACY_COLOR_LIGHT_CONFIG, - "value_template": "{{ 1 == 1 }}", - "color_template": color_template, - } - } - await async_setup_legacy_format(hass, count, light_config) - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) assert state.attributes.get("hs_color") == expected_hs assert state.state == STATE_ON assert state.attributes["color_mode"] == expected_color_mode @@ -1775,9 +1017,87 @@ async def test_legacy_color_template( assert state.attributes["supported_features"] == 0 +@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( - ("count", "extra_config"), [(1, OPTIMISTIC_HS_COLOR_LIGHT_CONFIG)] + "style", + [ConfigurationStyle.LEGACY, ConfigurationStyle.MODERN, ConfigurationStyle.TRIGGER], ) +@pytest.mark.parametrize( + ( + "extra_config", + "attribute", + "attribute_value", + "expected_action", + "expected_data", + "expected_color_mode", + ), + [ + ( + ON_OFF_HS_ACTIONS, + ATTR_HS_COLOR, + (40, 50), + "set_hs", + {"h": 40, "s": 50}, + ColorMode.HS, + ), + ( + ON_OFF_RGB_ACTIONS, + ATTR_RGB_COLOR, + (160, 78, 192), + "set_rgb", + {"r": 160, "g": 78, "b": 192}, + ColorMode.RGB, + ), + ( + ON_OFF_RGBW_ACTIONS, + ATTR_RGBW_COLOR, + (160, 78, 192, 25), + "set_rgbw", + {"r": 160, "g": 78, "b": 192, "w": 25}, + ColorMode.RGBW, + ), + ( + ON_OFF_RGBWW_ACTIONS, + ATTR_RGBWW_COLOR, + (160, 78, 192, 25, 50), + "set_rgbww", + {"r": 160, "g": 78, "b": 192, "cw": 25, "ww": 50}, + ColorMode.RGBWW, + ), + ], +) +@pytest.mark.usefixtures("setup_single_action_light") +async def test_color_actions_no_template( + hass: HomeAssistant, + calls: list[ServiceCall], + attribute: str, + attribute_value: tuple[int | float, ...], + expected_action: str, + expected_data: dict[str, int | float], + expected_color_mode: ColorMode, +) -> None: + """Test setting colors with an optimistic template light.""" + state = hass.states.get(TEST_LIGHT.entity_id) + assert state.attributes.get(attribute) is None + + await _call_and_assert_action( + hass, + calls, + SERVICE_TURN_ON, + {attribute: attribute_value}, + expected_data, + expected_action, + ) + + state = hass.states.get(TEST_LIGHT.entity_id) + assert state.state == STATE_ON + assert state.attributes["color_mode"] == expected_color_mode + assert state.attributes.get(attribute) == attribute_value + assert state.attributes["supported_color_modes"] == [expected_color_mode] + assert state.attributes["supported_features"] == 0 + + +@pytest.mark.parametrize(("count", "extra_config"), [(1, ON_OFF_HS_ACTIONS)]) @pytest.mark.parametrize( ("style", "attribute"), [ @@ -1801,19 +1121,15 @@ async def test_legacy_color_template( (None, "{{('one','two')}}", ColorMode.HS), ], ) +@pytest.mark.usefixtures("setup_single_attribute_light") async def test_hs_template( hass: HomeAssistant, - expected_hs, - expected_color_mode, - style: ConfigurationStyle, - setup_single_attribute_light, + expected_hs: tuple[float, float] | None, + expected_color_mode: ColorMode, ) -> None: """Test the template for the color.""" - if style == ConfigurationStyle.TRIGGER: - hass.states.async_set("light.test_state", STATE_ON) - await hass.async_block_till_done() - - state = hass.states.get("light.test_template_light") + await async_trigger(hass, TEST_STATE_ENTITY_ID, STATE_ON) + state = hass.states.get(TEST_LIGHT.entity_id) assert state.attributes.get("hs_color") == expected_hs assert state.state == STATE_ON assert state.attributes["color_mode"] == expected_color_mode @@ -1821,9 +1137,7 @@ async def test_hs_template( assert state.attributes["supported_features"] == 0 -@pytest.mark.parametrize( - ("count", "extra_config"), [(1, OPTIMISTIC_RGB_COLOR_LIGHT_CONFIG)] -) +@pytest.mark.parametrize(("count", "extra_config"), [(1, ON_OFF_RGB_ACTIONS)]) @pytest.mark.parametrize( ("style", "attribute"), [ @@ -1848,19 +1162,15 @@ async def test_hs_template( (None, "{{('one','two','tree')}}", ColorMode.RGB), ], ) +@pytest.mark.usefixtures("setup_single_attribute_light") async def test_rgb_template( hass: HomeAssistant, - expected_rgb, - expected_color_mode, - style: ConfigurationStyle, - setup_single_attribute_light, + expected_rgb: tuple[int, int, int] | None, + expected_color_mode: ColorMode, ) -> None: """Test the template for the color.""" - if style == ConfigurationStyle.TRIGGER: - hass.states.async_set("light.test_state", STATE_ON) - await hass.async_block_till_done() - - state = hass.states.get("light.test_template_light") + await async_trigger(hass, TEST_STATE_ENTITY_ID, STATE_ON) + state = hass.states.get(TEST_LIGHT.entity_id) assert state.attributes.get("rgb_color") == expected_rgb assert state.state == STATE_ON assert state.attributes["color_mode"] == expected_color_mode @@ -1868,9 +1178,7 @@ async def test_rgb_template( assert state.attributes["supported_features"] == 0 -@pytest.mark.parametrize( - ("count", "extra_config"), [(1, OPTIMISTIC_RGBW_COLOR_LIGHT_CONFIG)] -) +@pytest.mark.parametrize(("count", "extra_config"), [(1, ON_OFF_RGBW_ACTIONS)]) @pytest.mark.parametrize( ("style", "attribute"), [ @@ -1896,19 +1204,16 @@ async def test_rgb_template( (None, "{{('one','two','tree','four')}}", ColorMode.RGBW), ], ) +@pytest.mark.usefixtures("setup_single_attribute_light") async def test_rgbw_template( hass: HomeAssistant, - expected_rgbw, - expected_color_mode, - style: ConfigurationStyle, - setup_single_attribute_light, + expected_rgbw: tuple[int, int, int, int] | None, + expected_color_mode: ColorMode, ) -> None: """Test the template for the color.""" - if style == ConfigurationStyle.TRIGGER: - hass.states.async_set("light.test_state", STATE_ON) - await hass.async_block_till_done() + await async_trigger(hass, TEST_STATE_ENTITY_ID, STATE_ON) - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) assert state.attributes.get("rgbw_color") == expected_rgbw assert state.state == STATE_ON assert state.attributes["color_mode"] == expected_color_mode @@ -1916,9 +1221,7 @@ async def test_rgbw_template( assert state.attributes["supported_features"] == 0 -@pytest.mark.parametrize( - ("count", "extra_config"), [(1, OPTIMISTIC_RGBWW_COLOR_LIGHT_CONFIG)] -) +@pytest.mark.parametrize(("count", "extra_config"), [(1, ON_OFF_RGBWW_ACTIONS)]) @pytest.mark.parametrize( ("style", "attribute"), [ @@ -1949,19 +1252,15 @@ async def test_rgbw_template( (None, "{{('one','two','tree','four','five')}}", ColorMode.RGBWW), ], ) +@pytest.mark.usefixtures("setup_single_attribute_light") async def test_rgbww_template( hass: HomeAssistant, - expected_rgbww, - expected_color_mode, - style: ConfigurationStyle, - setup_single_attribute_light, + expected_rgbww: tuple[int, int, int, int, int] | None, + expected_color_mode: ColorMode, ) -> None: """Test the template for the color.""" - if style == ConfigurationStyle.TRIGGER: - hass.states.async_set("light.test_state", STATE_ON) - await hass.async_block_till_done() - - state = hass.states.get("light.test_template_light") + await async_trigger(hass, TEST_STATE_ENTITY_ID, STATE_ON) + state = hass.states.get(TEST_LIGHT.entity_id) assert state.attributes.get("rgbww_color") == expected_rgbww assert state.state == STATE_ON assert state.attributes["color_mode"] == expected_color_mode @@ -1971,58 +1270,52 @@ async def test_rgbww_template( @pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize( - ("light_config", "style"), + ("config", "style"), [ ( { - "test_template_light": { - **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, - "value_template": "{{1 == 1}}", - **TEST_ALL_COLORS_NO_TEMPLATE_CONFIG, - } + **ON_OFF_ACTIONS, + "value_template": "{{1 == 1}}", + **ALL_COLOR_ACTIONS, }, ConfigurationStyle.LEGACY, ), ( { - "name": "test_template_light", - **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, + **ON_OFF_ACTIONS, "state": "{{1 == 1}}", - **TEST_ALL_COLORS_NO_TEMPLATE_CONFIG, + **ALL_COLOR_ACTIONS, }, ConfigurationStyle.MODERN, ), ( { - "name": "test_template_light", - **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, + **ON_OFF_ACTIONS, "state": "{{1 == 1}}", - **TEST_ALL_COLORS_NO_TEMPLATE_CONFIG, + **ALL_COLOR_ACTIONS, }, ConfigurationStyle.TRIGGER, ), ], ) +@pytest.mark.usefixtures("setup_light") async def test_all_colors_mode_no_template( - hass: HomeAssistant, setup_light, calls: list[ServiceCall] + hass: HomeAssistant, calls: list[ServiceCall] ) -> None: """Test setting color and color temperature with optimistic template.""" - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) assert state.attributes.get("hs_color") is None - # Optimistically set hs color, light should be in hs_color mode - await hass.services.async_call( - light.DOMAIN, + await _call_and_assert_action( + hass, + calls, SERVICE_TURN_ON, - {ATTR_ENTITY_ID: "light.test_template_light", ATTR_HS_COLOR: (40, 50)}, - blocking=True, + {ATTR_HS_COLOR: (40, 50)}, + {"h": 40, "s": 50}, + "set_hs", ) - assert len(calls) == 1 - assert calls[-1].data["h"] == 40 - assert calls[-1].data["s"] == 50 - - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) assert state.attributes["color_mode"] == ColorMode.HS assert state.attributes["color_temp_kelvin"] is None assert state.attributes["hs_color"] == (40, 50) @@ -2035,18 +1328,16 @@ async def test_all_colors_mode_no_template( ] assert state.attributes["supported_features"] == 0 - # Optimistically set color temp, light should be in color temp mode - await hass.services.async_call( - light.DOMAIN, + await _call_and_assert_action( + hass, + calls, SERVICE_TURN_ON, - {ATTR_ENTITY_ID: "light.test_template_light", ATTR_COLOR_TEMP_KELVIN: 8130}, - blocking=True, + {ATTR_COLOR_TEMP_KELVIN: 8130}, + {ATTR_COLOR_TEMP_KELVIN: 8130, "color_temp": 123}, + "set_temperature", ) - assert len(calls) == 2 - assert calls[-1].data["color_temp"] == 123 - - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) assert state.attributes["color_mode"] == ColorMode.COLOR_TEMP assert state.attributes["color_temp_kelvin"] == 8130 assert "hs_color" in state.attributes # Color temp represented as hs_color @@ -2059,20 +1350,16 @@ async def test_all_colors_mode_no_template( ] assert state.attributes["supported_features"] == 0 - # Optimistically set rgb color, light should be in rgb_color mode - await hass.services.async_call( - light.DOMAIN, + await _call_and_assert_action( + hass, + calls, SERVICE_TURN_ON, - {ATTR_ENTITY_ID: "light.test_template_light", ATTR_RGB_COLOR: (160, 78, 192)}, - blocking=True, + {ATTR_RGB_COLOR: (160, 78, 192)}, + {"r": 160, "g": 78, "b": 192}, + "set_rgb", ) - assert len(calls) == 3 - assert calls[-1].data["r"] == 160 - assert calls[-1].data["g"] == 78 - assert calls[-1].data["b"] == 192 - - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) assert state.attributes["color_mode"] == ColorMode.RGB assert state.attributes["color_temp_kelvin"] is None assert state.attributes["rgb_color"] == (160, 78, 192) @@ -2085,24 +1372,16 @@ async def test_all_colors_mode_no_template( ] assert state.attributes["supported_features"] == 0 - # Optimistically set rgbw color, light should be in rgb_color mode - await hass.services.async_call( - light.DOMAIN, + await _call_and_assert_action( + hass, + calls, SERVICE_TURN_ON, - { - ATTR_ENTITY_ID: "light.test_template_light", - ATTR_RGBW_COLOR: (160, 78, 192, 25), - }, - blocking=True, + {ATTR_RGBW_COLOR: (160, 78, 192, 25)}, + {"r": 160, "g": 78, "b": 192, "w": 25}, + "set_rgbw", ) - assert len(calls) == 4 - assert calls[-1].data["r"] == 160 - assert calls[-1].data["g"] == 78 - assert calls[-1].data["b"] == 192 - assert calls[-1].data["w"] == 25 - - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) assert state.attributes["color_mode"] == ColorMode.RGBW assert state.attributes["color_temp_kelvin"] is None assert state.attributes["rgbw_color"] == (160, 78, 192, 25) @@ -2115,25 +1394,16 @@ async def test_all_colors_mode_no_template( ] assert state.attributes["supported_features"] == 0 - # Optimistically set rgbww color, light should be in rgb_color mode - await hass.services.async_call( - light.DOMAIN, + await _call_and_assert_action( + hass, + calls, SERVICE_TURN_ON, - { - ATTR_ENTITY_ID: "light.test_template_light", - ATTR_RGBWW_COLOR: (160, 78, 192, 25, 55), - }, - blocking=True, + {ATTR_RGBWW_COLOR: (160, 78, 192, 25, 55)}, + {"r": 160, "g": 78, "b": 192, "cw": 25, "ww": 55}, + "set_rgbww", ) - assert len(calls) == 5 - assert calls[-1].data["r"] == 160 - assert calls[-1].data["g"] == 78 - assert calls[-1].data["b"] == 192 - assert calls[-1].data["cw"] == 25 - assert calls[-1].data["ww"] == 55 - - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) assert state.attributes["color_mode"] == ColorMode.RGBWW assert state.attributes["color_temp_kelvin"] is None assert state.attributes["rgbww_color"] == (160, 78, 192, 25, 55) @@ -2146,19 +1416,16 @@ async def test_all_colors_mode_no_template( ] assert state.attributes["supported_features"] == 0 - # Optimistically set hs color, light should again be in hs_color mode - await hass.services.async_call( - light.DOMAIN, + await _call_and_assert_action( + hass, + calls, SERVICE_TURN_ON, - {ATTR_ENTITY_ID: "light.test_template_light", ATTR_HS_COLOR: (10, 20)}, - blocking=True, + {ATTR_HS_COLOR: (10, 20)}, + {"h": 10, "s": 20}, + "set_hs", ) - assert len(calls) == 6 - assert calls[-1].data["h"] == 10 - assert calls[-1].data["s"] == 20 - - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) assert state.attributes["color_mode"] == ColorMode.HS assert state.attributes["color_temp_kelvin"] is None assert state.attributes["hs_color"] == (10, 20) @@ -2171,18 +1438,16 @@ async def test_all_colors_mode_no_template( ] assert state.attributes["supported_features"] == 0 - # Optimistically set color temp, light should again be in color temp mode - await hass.services.async_call( - light.DOMAIN, + await _call_and_assert_action( + hass, + calls, SERVICE_TURN_ON, - {ATTR_ENTITY_ID: "light.test_template_light", ATTR_COLOR_TEMP_KELVIN: 4273}, - blocking=True, + {ATTR_COLOR_TEMP_KELVIN: 4273}, + {ATTR_COLOR_TEMP_KELVIN: 4273, "color_temp": 234}, + "set_temperature", ) - assert len(calls) == 7 - assert calls[-1].data["color_temp"] == 234 - - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) assert state.attributes["color_mode"] == ColorMode.COLOR_TEMP assert state.attributes["color_temp_kelvin"] == 4273 assert "hs_color" in state.attributes # Color temp represented as hs_color @@ -2208,37 +1473,27 @@ async def test_all_colors_mode_no_template( ("{{ ['Disco', 'Police'] }}", "{{ 'None' }}", "RGB", None), ], ) +@pytest.mark.usefixtures("setup_light_with_effects") async def test_effect_action( - hass: HomeAssistant, - effect: str, - expected: Any, - style: ConfigurationStyle, - setup_light_with_effects, - calls: list[ServiceCall], + hass: HomeAssistant, effect: str, expected: Any, calls: list[ServiceCall] ) -> None: """Test setting valid effect with template.""" - if style == ConfigurationStyle.TRIGGER: - # Ensures the trigger template entity updates - hass.states.async_set("light.test_state", STATE_ON) - await hass.async_block_till_done() + await async_trigger(hass, TEST_STATE_ENTITY_ID, STATE_ON) - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) assert state is not None - await hass.services.async_call( - light.DOMAIN, + await _call_and_assert_action( + hass, + calls, SERVICE_TURN_ON, - {ATTR_ENTITY_ID: "light.test_template_light", ATTR_EFFECT: effect}, - blocking=True, + {ATTR_EFFECT: effect}, + {ATTR_EFFECT: effect}, + "set_effect", ) - assert len(calls) == 1 - assert calls[-1].data["action"] == "set_effect" - assert calls[-1].data["caller"] == "light.test_template_light" - assert calls[-1].data["effect"] == effect - - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) assert state is not None assert state.attributes.get("effect") == expected @@ -2267,18 +1522,13 @@ async def test_effect_action( (None, ""), ], ) +@pytest.mark.usefixtures("setup_light_with_effects") async def test_effect_list_template( - hass: HomeAssistant, - expected_effect_list, - style: ConfigurationStyle, - setup_light_with_effects, + hass: HomeAssistant, expected_effect_list: list[str] | None ) -> None: """Test the template for the effect list.""" - if style == ConfigurationStyle.TRIGGER: - hass.states.async_set("light.test_state", STATE_ON) - await hass.async_block_till_done() - - state = hass.states.get("light.test_template_light") + await async_trigger(hass, TEST_STATE_ENTITY_ID, STATE_ON) + state = hass.states.get(TEST_LIGHT.entity_id) assert state is not None assert state.attributes.get("effect_list") == expected_effect_list @@ -2301,18 +1551,13 @@ async def test_effect_list_template( ("Strobe color", "{{ 'Strobe color' }}"), ], ) +@pytest.mark.usefixtures("setup_light_with_effects") async def test_effect_template( - hass: HomeAssistant, - expected_effect, - style: ConfigurationStyle, - setup_light_with_effects, + hass: HomeAssistant, expected_effect: str | None ) -> None: """Test the template for the effect.""" - if style == ConfigurationStyle.TRIGGER: - hass.states.async_set("light.test_state", STATE_ON) - await hass.async_block_till_done() - - state = hass.states.get("light.test_template_light") + await async_trigger(hass, TEST_STATE_ENTITY_ID, STATE_ON) + state = hass.states.get(TEST_LIGHT.entity_id) assert state is not None assert state.attributes.get("effect") == expected_effect @@ -2337,18 +1582,13 @@ async def test_effect_template( (6535, "{{ 'a' }}"), ], ) +@pytest.mark.usefixtures("setup_light_with_mireds") async def test_min_mireds_template( - hass: HomeAssistant, - expected_max_kelvin: int, - style: ConfigurationStyle, - setup_light_with_mireds, + hass: HomeAssistant, expected_max_kelvin: int ) -> None: """Test the template for the min mireds.""" - if style == ConfigurationStyle.TRIGGER: - hass.states.async_set("light.test_state", STATE_ON) - await hass.async_block_till_done() - - state = hass.states.get("light.test_template_light") + await async_trigger(hass, TEST_STATE_ENTITY_ID, STATE_ON) + state = hass.states.get(TEST_LIGHT.entity_id) assert state is not None assert state.attributes.get("max_color_temp_kelvin") == expected_max_kelvin @@ -2373,25 +1613,19 @@ async def test_min_mireds_template( (2000, "{{ 'a' }}"), ], ) +@pytest.mark.usefixtures("setup_light_with_mireds") async def test_max_mireds_template( hass: HomeAssistant, expected_min_kelvin: int, - style: ConfigurationStyle, - setup_light_with_mireds, ) -> None: """Test the template for the max mireds.""" - if style == ConfigurationStyle.TRIGGER: - hass.states.async_set("light.test_state", STATE_ON) - await hass.async_block_till_done() - - state = hass.states.get("light.test_template_light") + await async_trigger(hass, TEST_STATE_ENTITY_ID, STATE_ON) + state = hass.states.get(TEST_LIGHT.entity_id) assert state is not None assert state.attributes.get("min_color_temp_kelvin") == expected_min_kelvin -@pytest.mark.parametrize( - ("count", "extra_config"), [(1, OPTIMISTIC_COLOR_TEMP_LIGHT_CONFIG)] -) +@pytest.mark.parametrize(("count", "extra_config"), [(1, ON_OFF_COLOR_TEMP_ACTIONS)]) @pytest.mark.parametrize( ("style", "attribute"), [ @@ -2411,19 +1645,15 @@ async def test_max_mireds_template( (False, "None"), ], ) +@pytest.mark.usefixtures("setup_single_attribute_light") async def test_supports_transition_template( hass: HomeAssistant, - style: ConfigurationStyle, - expected_supports_transition, - setup_single_attribute_light, + expected_supports_transition: bool, ) -> None: """Test the template for the supports transition.""" - if style == ConfigurationStyle.TRIGGER: - # Ensures the trigger template entity updates - hass.states.async_set("light.test_state", STATE_ON) - await hass.async_block_till_done() + await async_trigger(hass, TEST_STATE_ENTITY_ID, STATE_ON) - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) expected_value = 1 @@ -2443,34 +1673,27 @@ async def test_supports_transition_template( "style", [ConfigurationStyle.LEGACY, ConfigurationStyle.MODERN, ConfigurationStyle.TRIGGER], ) -async def test_supports_transition_template_updates( - hass: HomeAssistant, style: ConfigurationStyle, setup_light_with_transition_template -) -> None: +@pytest.mark.usefixtures("setup_light_with_transition_template") +async def test_supports_transition_template_updates(hass: HomeAssistant) -> None: """Test the template for the supports transition dynamically.""" - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) assert state is not None hass.states.async_set("sensor.test", 0) await hass.async_block_till_done() - if style == ConfigurationStyle.TRIGGER: - # Ensures the trigger template entity updates - hass.states.async_set("light.test_state", STATE_ON) - await hass.async_block_till_done() + await async_trigger(hass, TEST_STATE_ENTITY_ID, STATE_ON) - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) supported_features = state.attributes.get("supported_features") assert supported_features == LightEntityFeature.EFFECT hass.states.async_set("sensor.test", 1) await hass.async_block_till_done() - if style == ConfigurationStyle.TRIGGER: - # Ensures the trigger template entity updates - hass.states.async_set("light.test_state", STATE_OFF) - await hass.async_block_till_done() + await async_trigger(hass, TEST_STATE_ENTITY_ID, STATE_OFF) - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) supported_features = state.attributes.get("supported_features") assert ( supported_features == LightEntityFeature.TRANSITION | LightEntityFeature.EFFECT @@ -2479,12 +1702,9 @@ async def test_supports_transition_template_updates( hass.states.async_set("sensor.test", 0) await hass.async_block_till_done() - if style == ConfigurationStyle.TRIGGER: - # Ensures the trigger template entity updates - hass.states.async_set("light.test_state", STATE_ON) - await hass.async_block_till_done() + await async_trigger(hass, TEST_STATE_ENTITY_ID, STATE_ON) - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) supported_features = state.attributes.get("supported_features") assert supported_features == LightEntityFeature.EFFECT @@ -2494,8 +1714,8 @@ async def test_supports_transition_template_updates( [ ( 1, - OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, - "{{ is_state('availability_boolean.state', 'on') }}", + ON_OFF_SET_LEVEL_ACTIONS, + "{{ is_state('binary_sensor.availability', 'on') }}", ) ], ) @@ -2507,33 +1727,26 @@ async def test_supports_transition_template_updates( (ConfigurationStyle.TRIGGER, "availability"), ], ) -async def test_available_template_with_entities( - hass: HomeAssistant, style: ConfigurationStyle, setup_single_attribute_light -) -> None: +@pytest.mark.usefixtures("setup_single_attribute_light") +async def test_available_template_with_entities(hass: HomeAssistant) -> None: """Test availability templates with values from other entities.""" # When template returns true.. - hass.states.async_set(_STATE_AVAILABILITY_BOOLEAN, STATE_ON) + hass.states.async_set(TEST_AVAILABILITY_ENTITY, STATE_ON) await hass.async_block_till_done() - if style == ConfigurationStyle.TRIGGER: - # Ensures the trigger template entity updates - hass.states.async_set("light.test_state", STATE_ON) - await hass.async_block_till_done() + await async_trigger(hass, TEST_STATE_ENTITY_ID, STATE_ON) # Device State should not be unavailable - assert hass.states.get("light.test_template_light").state != STATE_UNAVAILABLE + assert hass.states.get(TEST_LIGHT.entity_id).state != STATE_UNAVAILABLE # When Availability template returns false - hass.states.async_set(_STATE_AVAILABILITY_BOOLEAN, STATE_OFF) + hass.states.async_set(TEST_AVAILABILITY_ENTITY, STATE_OFF) await hass.async_block_till_done() - if style == ConfigurationStyle.TRIGGER: - # Ensures the trigger template entity updates - hass.states.async_set("light.test_state", STATE_OFF) - await hass.async_block_till_done() + await async_trigger(hass, TEST_STATE_ENTITY_ID, STATE_OFF) # device state should be unavailable - assert hass.states.get("light.test_template_light").state == STATE_UNAVAILABLE + assert hass.states.get(TEST_LIGHT.entity_id).state == STATE_UNAVAILABLE @pytest.mark.parametrize( @@ -2541,7 +1754,7 @@ async def test_available_template_with_entities( [ ( 1, - OPTIMISTIC_BRIGHTNESS_LIGHT_CONFIG, + ON_OFF_SET_LEVEL_ACTIONS, "{{ x - 12 }}", ) ], @@ -2553,101 +1766,41 @@ async def test_available_template_with_entities( (ConfigurationStyle.MODERN, "availability"), ], ) +@pytest.mark.usefixtures("setup_single_attribute_light") async def test_invalid_availability_template_keeps_component_available( - hass: HomeAssistant, - setup_single_attribute_light, - caplog_setup_text, + hass: HomeAssistant, caplog_setup_text ) -> None: """Test that an invalid availability keeps the device available.""" - assert hass.states.get("light.test_template_light").state != STATE_UNAVAILABLE + assert hass.states.get(TEST_LIGHT.entity_id).state != STATE_UNAVAILABLE assert "UndefinedError: 'x' is undefined" in caplog_setup_text -@pytest.mark.parametrize("count", [1]) +@pytest.mark.parametrize("config", [ON_OFF_ACTIONS]) @pytest.mark.parametrize( - ("light_config", "style"), - [ - ( - { - "test_template_light_01": TEST_UNIQUE_ID_CONFIG, - "test_template_light_02": TEST_UNIQUE_ID_CONFIG, - }, - ConfigurationStyle.LEGACY, - ), - ( - [ - { - "name": "test_template_light_01", - **TEST_UNIQUE_ID_CONFIG, - }, - { - "name": "test_template_light_02", - **TEST_UNIQUE_ID_CONFIG, - }, - ], - ConfigurationStyle.MODERN, - ), - ( - [ - { - "name": "test_template_light_01", - **TEST_UNIQUE_ID_CONFIG, - }, - { - "name": "test_template_light_02", - **TEST_UNIQUE_ID_CONFIG, - }, - ], - ConfigurationStyle.TRIGGER, - ), - ], + "style", + [ConfigurationStyle.LEGACY, ConfigurationStyle.MODERN, ConfigurationStyle.TRIGGER], ) -async def test_unique_id(hass: HomeAssistant, setup_light) -> None: - """Test unique_id option only creates one light per id.""" - assert len(hass.states.async_all("light")) == 1 - - -async def test_nested_unique_id( - hass: HomeAssistant, entity_registry: er.EntityRegistry +async def test_unique_id( + hass: HomeAssistant, style: ConfigurationStyle, config: ConfigType ) -> None: - """Test unique_id option creates one light per nested id.""" + """Test unique_id option only creates one light per id.""" + await setup_and_test_unique_id(hass, TEST_LIGHT, style, config) - with assert_setup_component(1, template.DOMAIN): - assert await async_setup_component( - hass, - template.DOMAIN, - { - "template": { - "unique_id": "x", - "light": [ - { - "name": "test_a", - **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, - "unique_id": "a", - }, - { - "name": "test_b", - **OPTIMISTIC_ON_OFF_LIGHT_CONFIG, - "unique_id": "b", - }, - ], - } - }, - ) - await hass.async_block_till_done() - await hass.async_start() - await hass.async_block_till_done() - - assert len(hass.states.async_all("light")) == 2 - - entry = entity_registry.async_get("light.test_a") - assert entry - assert entry.unique_id == "x-a" - - entry = entity_registry.async_get("light.test_b") - assert entry - assert entry.unique_id == "x-b" +@pytest.mark.parametrize("config", [ON_OFF_ACTIONS]) +@pytest.mark.parametrize( + "style", [ConfigurationStyle.MODERN, ConfigurationStyle.TRIGGER] +) +async def test_nested_unique_id( + hass: HomeAssistant, + style: ConfigurationStyle, + config: ConfigType, + entity_registry: er.EntityRegistry, +) -> None: + """Test a template unique_id propagates to light unique_ids.""" + await setup_and_test_nested_unique_id( + hass, TEST_LIGHT, style, entity_registry, config + ) @pytest.mark.parametrize(("count", "extra_config"), [(1, {})]) @@ -2669,33 +1822,32 @@ async def test_nested_unique_id( ("set_rgbww", ColorMode.RGBWW), ], ) +@pytest.mark.usefixtures("setup_empty_action_light") async def test_empty_color_mode_action_config( - hass: HomeAssistant, - color_mode: ColorMode, - setup_empty_action_light, + hass: HomeAssistant, color_mode: ColorMode ) -> None: """Test empty actions for color mode actions.""" - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) assert state.attributes["supported_color_modes"] == [color_mode] await hass.services.async_call( light.DOMAIN, SERVICE_TURN_ON, - {ATTR_ENTITY_ID: "light.test_template_light"}, + {ATTR_ENTITY_ID: TEST_LIGHT.entity_id}, blocking=True, ) - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) assert state.state == STATE_ON await hass.services.async_call( light.DOMAIN, SERVICE_TURN_OFF, - {ATTR_ENTITY_ID: "light.test_template_light"}, + {ATTR_ENTITY_ID: TEST_LIGHT.entity_id}, blocking=True, ) - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) assert state.state == STATE_OFF @@ -2719,23 +1871,20 @@ async def test_empty_color_mode_action_config( ), ], ) +@pytest.mark.usefixtures("setup_empty_action_light") @pytest.mark.parametrize("action", ["set_effect"]) -async def test_effect_with_empty_action( - hass: HomeAssistant, - setup_empty_action_light, -) -> None: +async def test_effect_with_empty_action(hass: HomeAssistant) -> None: """Test empty set_effect action.""" - state = hass.states.get("light.test_template_light") + state = hass.states.get(TEST_LIGHT.entity_id) assert state.attributes["supported_features"] == LightEntityFeature.EFFECT @pytest.mark.parametrize( - ("count", "light_config"), + ("count", "config"), [ ( 1, { - "name": TEST_OBJECT_ID, "state": "{{ is_state('light.test_state', 'on') }}", "turn_on": [], "turn_off": [], @@ -2751,39 +1900,35 @@ async def test_effect_with_empty_action( @pytest.mark.usefixtures("setup_light") async def test_optimistic_option(hass: HomeAssistant) -> None: """Test optimistic yaml option.""" - hass.states.async_set(TEST_STATE_ENTITY_ID, STATE_OFF) - await hass.async_block_till_done() + await async_trigger(hass, TEST_STATE_ENTITY_ID, STATE_OFF) - state = hass.states.get(TEST_ENTITY_ID) + state = hass.states.get(TEST_LIGHT.entity_id) assert state.state == STATE_OFF await hass.services.async_call( light.DOMAIN, "turn_on", - {"entity_id": TEST_ENTITY_ID}, + {"entity_id": TEST_LIGHT.entity_id}, blocking=True, ) - state = hass.states.get(TEST_ENTITY_ID) + state = hass.states.get(TEST_LIGHT.entity_id) assert state.state == STATE_ON - hass.states.async_set(TEST_STATE_ENTITY_ID, STATE_ON) - await hass.async_block_till_done() + await async_trigger(hass, TEST_STATE_ENTITY_ID, STATE_ON) - hass.states.async_set(TEST_STATE_ENTITY_ID, STATE_OFF) - await hass.async_block_till_done() + await async_trigger(hass, TEST_STATE_ENTITY_ID, STATE_OFF) - state = hass.states.get(TEST_ENTITY_ID) + state = hass.states.get(TEST_LIGHT.entity_id) assert state.state == STATE_OFF @pytest.mark.parametrize( - ("count", "light_config"), + ("count", "config"), [ ( 1, { - "name": TEST_OBJECT_ID, "state": "{{ is_state('light.test_state', 'on') }}", "turn_on": [], "turn_off": [], @@ -2805,11 +1950,11 @@ async def test_not_optimistic(hass: HomeAssistant, expected: str) -> None: await hass.services.async_call( light.DOMAIN, "turn_on", - {"entity_id": TEST_ENTITY_ID}, + {"entity_id": TEST_LIGHT.entity_id}, blocking=True, ) - state = hass.states.get(TEST_ENTITY_ID) + state = hass.states.get(TEST_LIGHT.entity_id) assert state.state == expected