Add preview tests for number and sensor (#148426)

This commit is contained in:
Petro31 2025-07-08 11:04:06 -04:00 committed by GitHub
parent c97ad9657f
commit a35299d94c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 93 additions and 32 deletions

View File

@ -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"]

View File

@ -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"

View File

@ -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"

View File

@ -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(