From 97fe7101873cd52eae12c4be0fa2c00d3ac6b0e4 Mon Sep 17 00:00:00 2001 From: Petro31 <35082313+Petro31@users.noreply.github.com> Date: Thu, 9 Apr 2026 14:34:02 -0400 Subject: [PATCH] Update template select tests to use new framework (#167825) --- tests/components/template/test_select.py | 55 +++++++++++++++--------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/tests/components/template/test_select.py b/tests/components/template/test_select.py index e6ac5919eec..7f6a629ef1d 100644 --- a/tests/components/template/test_select.py +++ b/tests/components/template/test_select.py @@ -30,8 +30,10 @@ from homeassistant.helpers import device_registry as dr, entity_registry as er from .conftest import ( ConfigurationStyle, TemplatePlatformSetup, + assert_action, async_get_flow_preview_state, async_trigger, + make_test_action, make_test_trigger, setup_and_test_nested_unique_id, setup_and_test_unique_id, @@ -41,7 +43,7 @@ from .conftest import ( from tests.common import MockConfigEntry, assert_setup_component from tests.conftest import WebSocketGenerator -TEST_STATE_ENTITY_ID = "select.test_state" +TEST_STATE_ENTITY_ID = "sensor.test_state" TEST_AVAILABILITY_ENTITY_ID = "binary_sensor.test_availability" TEST_SELECT = TemplatePlatformSetup( @@ -56,14 +58,7 @@ TEST_OPTIONS_WITHOUT_STATE = { "select_option": [], } TEST_OPTIONS = {"state": "test", **TEST_OPTIONS_WITHOUT_STATE} -TEST_OPTION_ACTION = { - "action": "test.automation", - "data": { - "action": "select_option", - "caller": "{{ this.entity_id }}", - "option": "{{ option }}", - }, -} +TEST_OPTION_ACTION = make_test_action("select_option", {"option": "{{ option }}"}) @pytest.fixture @@ -181,9 +176,9 @@ async def test_missing_required_keys(hass: HomeAssistant) -> None: ( 1, { - "options": "{{ state_attr('select.test_state', 'options') or [] }}", - "select_option": [TEST_OPTION_ACTION], - "state": "{{ states('select.test_state') }}", + "options": "{{ state_attr('sensor.test_state', 'options') or [] }}", + **TEST_OPTION_ACTION, + "state": "{{ states('sensor.test_state') }}", }, ) ], @@ -214,10 +209,7 @@ async def test_template_select(hass: HomeAssistant, calls: list[ServiceCall]) -> ) # Check this variable can be used in set_value script - assert len(calls) == 1 - assert calls[-1].data["action"] == "select_option" - assert calls[-1].data["caller"] == TEST_SELECT.entity_id - assert calls[-1].data["option"] == "c" + assert_action(TEST_SELECT, calls, 1, "select_option", option="c") await async_trigger(hass, TEST_STATE_ENTITY_ID, "c", attributes) _verify(hass, "c", ["a", "b", "c"]) @@ -247,7 +239,7 @@ def _verify( ( { **TEST_OPTIONS, - CONF_ICON: "{% if states.select.test_state.state == 'yes' %}mdi:check{% endif %}", + CONF_ICON: "{% if states.sensor.test_state.state == 'yes' %}mdi:check{% endif %}", }, ATTR_ICON, "mdi:check", @@ -255,7 +247,7 @@ def _verify( ( { **TEST_OPTIONS, - CONF_PICTURE: "{% if states.select.test_state.state == 'yes' %}check.jpg{% endif %}", + CONF_PICTURE: "{% if states.sensor.test_state.state == 'yes' %}check.jpg{% endif %}", }, ATTR_ENTITY_PICTURE, "check.jpg", @@ -410,7 +402,7 @@ async def test_optimistic(hass: HomeAssistant) -> None: ( 1, { - "state": "{{ states('select.test_state') }}", + "state": "{{ states('sensor.test_state') }}", "optimistic": False, "options": "{{ ['test', 'yes', 'no'] }}", "select_option": [], @@ -448,7 +440,7 @@ async def test_not_optimistic(hass: HomeAssistant) -> None: { "options": "{{ ['test', 'yes', 'no'] }}", "select_option": [], - "state": "{{ states('select.test_state') }}", + "state": "{{ states('sensor.test_state') }}", "availability": "{{ is_state('binary_sensor.test_availability', 'on') }}", }, ) @@ -487,6 +479,29 @@ async def test_availability(hass: HomeAssistant) -> None: assert state.state == "yes" +@pytest.mark.parametrize( + ("count", "config"), + [ + ( + 1, + {"availability": "{{ x - 12 }}", **TEST_OPTIONS}, + ) + ], +) +@pytest.mark.parametrize( + "style", [ConfigurationStyle.MODERN, ConfigurationStyle.TRIGGER] +) +@pytest.mark.usefixtures("setup_select") +async def test_invalid_availability_template_keeps_component_available( + hass: HomeAssistant, caplog_setup_text: str, caplog: pytest.LogCaptureFixture +) -> None: + """Test that an invalid availability keeps the device available.""" + await async_trigger(hass, TEST_AVAILABILITY_ENTITY_ID, "anything") + assert hass.states.get(TEST_SELECT.entity_id).state != STATE_UNAVAILABLE + error = "UndefinedError: 'x' is undefined" + assert error in caplog_setup_text or error in caplog.text + + async def test_flow_preview( hass: HomeAssistant, hass_ws_client: WebSocketGenerator,