From a35299d94ce3ad790fe4c435e041de78549f20de Mon Sep 17 00:00:00 2001 From: Petro31 <35082313+Petro31@users.noreply.github.com> Date: Tue, 8 Jul 2025 11:04:06 -0400 Subject: [PATCH] Add preview tests for number and sensor (#148426) --- tests/components/template/conftest.py | 44 ++++++++++++++++++++++++ tests/components/template/test_number.py | 24 ++++++++++++- tests/components/template/test_sensor.py | 19 ++++++++++ tests/components/template/test_switch.py | 38 ++++---------------- 4 files changed, 93 insertions(+), 32 deletions(-) diff --git a/tests/components/template/conftest.py b/tests/components/template/conftest.py index c69c9e9e9a4..6d1776f24cd 100644 --- a/tests/components/template/conftest.py +++ b/tests/components/template/conftest.py @@ -4,11 +4,15 @@ from enum import Enum import pytest +from homeassistant.components import template +from homeassistant.config_entries import SOURCE_USER from homeassistant.core import HomeAssistant, ServiceCall +from homeassistant.data_entry_flow import FlowResultType from homeassistant.helpers.typing import ConfigType from homeassistant.setup import async_setup_component from tests.common import assert_setup_component, async_mock_service +from tests.conftest import WebSocketGenerator class ConfigurationStyle(Enum): @@ -51,3 +55,43 @@ async def caplog_setup_text(caplog: pytest.LogCaptureFixture) -> str: @pytest.fixture(autouse=True, name="stub_blueprint_populate") def stub_blueprint_populate_autouse(stub_blueprint_populate: None) -> None: """Stub copying the blueprints to the config folder.""" + + +async def async_get_flow_preview_state( + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, + domain: str, + user_input: ConfigType, +) -> ConfigType: + """Test the config flow preview.""" + client = await hass_ws_client(hass) + + result = await hass.config_entries.flow.async_init( + template.DOMAIN, context={"source": SOURCE_USER} + ) + assert result["type"] is FlowResultType.MENU + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {"next_step_id": domain}, + ) + await hass.async_block_till_done() + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == domain + assert result["errors"] is None + assert result["preview"] == "template" + + await client.send_json_auto_id( + { + "type": "template/start_preview", + "flow_id": result["flow_id"], + "flow_type": "config_flow", + "user_input": user_input, + } + ) + msg = await client.receive_json() + assert msg["success"] + assert msg["result"] is None + + msg = await client.receive_json() + return msg["event"] diff --git a/tests/components/template/test_number.py b/tests/components/template/test_number.py index a15ae1e46c0..21dea28b73f 100644 --- a/tests/components/template/test_number.py +++ b/tests/components/template/test_number.py @@ -35,9 +35,10 @@ from homeassistant.core import Context, HomeAssistant, ServiceCall from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.setup import async_setup_component -from .conftest import ConfigurationStyle +from .conftest import ConfigurationStyle, async_get_flow_preview_state from tests.common import MockConfigEntry, assert_setup_component, async_capture_events +from tests.typing import WebSocketGenerator _TEST_OBJECT_ID = "template_number" _TEST_NUMBER = f"number.{_TEST_OBJECT_ID}" @@ -608,3 +609,24 @@ async def test_empty_action_config(hass: HomeAssistant, setup_number) -> None: state = hass.states.get(_TEST_NUMBER) assert float(state.state) == 4 + + +async def test_flow_preview( + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, +) -> None: + """Test the config flow preview.""" + + state = await async_get_flow_preview_state( + hass, + hass_ws_client, + number.DOMAIN, + { + "name": "My template", + "min": 0.0, + "max": 100.0, + **TEST_REQUIRED, + }, + ) + + assert state["state"] == "0.0" diff --git a/tests/components/template/test_sensor.py b/tests/components/template/test_sensor.py index eb4f6c3596b..e89e98601d6 100644 --- a/tests/components/template/test_sensor.py +++ b/tests/components/template/test_sensor.py @@ -30,6 +30,8 @@ from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.setup import ATTR_COMPONENT, async_setup_component from homeassistant.util import dt as dt_util +from .conftest import async_get_flow_preview_state + from tests.common import ( MockConfigEntry, assert_setup_component, @@ -37,6 +39,7 @@ from tests.common import ( async_fire_time_changed, mock_restore_cache_with_extra_data, ) +from tests.conftest import WebSocketGenerator TEST_NAME = "sensor.test_template_sensor" @@ -2434,3 +2437,19 @@ async def test_device_id( template_entity = entity_registry.async_get("sensor.my_template") assert template_entity is not None assert template_entity.device_id == device_entry.id + + +async def test_flow_preview( + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, +) -> None: + """Test the config flow preview.""" + + state = await async_get_flow_preview_state( + hass, + hass_ws_client, + sensor.DOMAIN, + {"name": "My template", "state": "{{ 0.0 }}"}, + ) + + assert state["state"] == "0.0" diff --git a/tests/components/template/test_switch.py b/tests/components/template/test_switch.py index de6894c73a8..c6ed303af7b 100644 --- a/tests/components/template/test_switch.py +++ b/tests/components/template/test_switch.py @@ -8,7 +8,6 @@ from syrupy.assertion import SnapshotAssertion from homeassistant.components import switch, template from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN from homeassistant.components.template.switch import rewrite_legacy_to_modern_conf -from homeassistant.config_entries import SOURCE_USER from homeassistant.const import ( ATTR_ENTITY_ID, SERVICE_TURN_OFF, @@ -18,12 +17,11 @@ from homeassistant.const import ( STATE_UNAVAILABLE, ) from homeassistant.core import CoreState, HomeAssistant, ServiceCall, State -from homeassistant.data_entry_flow import FlowResultType from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers.template import Template from homeassistant.setup import async_setup_component -from .conftest import ConfigurationStyle +from .conftest import ConfigurationStyle, async_get_flow_preview_state from tests.common import ( MockConfigEntry, @@ -396,37 +394,15 @@ async def test_flow_preview( hass_ws_client: WebSocketGenerator, ) -> None: """Test the config flow preview.""" - client = await hass_ws_client(hass) - result = await hass.config_entries.flow.async_init( - template.DOMAIN, context={"source": SOURCE_USER} + state = await async_get_flow_preview_state( + hass, + hass_ws_client, + switch.DOMAIN, + {"name": "My template", state_key: "{{ 'on' }}"}, ) - assert result["type"] is FlowResultType.MENU - result = await hass.config_entries.flow.async_configure( - result["flow_id"], - {"next_step_id": SWITCH_DOMAIN}, - ) - await hass.async_block_till_done() - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == SWITCH_DOMAIN - assert result["errors"] is None - assert result["preview"] == "template" - - await client.send_json_auto_id( - { - "type": "template/start_preview", - "flow_id": result["flow_id"], - "flow_type": "config_flow", - "user_input": {"name": "My template", state_key: "{{ 'on' }}"}, - } - ) - msg = await client.receive_json() - assert msg["success"] - assert msg["result"] is None - - msg = await client.receive_json() - assert msg["event"]["state"] == "on" + assert state["state"] == STATE_ON @pytest.mark.parametrize(