mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 18:27:09 +00:00
Add preview tests for number and sensor (#148426)
This commit is contained in:
parent
c97ad9657f
commit
a35299d94c
@ -4,11 +4,15 @@ from enum import Enum
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
from homeassistant.components import template
|
||||||
|
from homeassistant.config_entries import SOURCE_USER
|
||||||
from homeassistant.core import HomeAssistant, ServiceCall
|
from homeassistant.core import HomeAssistant, ServiceCall
|
||||||
|
from homeassistant.data_entry_flow import FlowResultType
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
from tests.common import assert_setup_component, async_mock_service
|
from tests.common import assert_setup_component, async_mock_service
|
||||||
|
from tests.conftest import WebSocketGenerator
|
||||||
|
|
||||||
|
|
||||||
class ConfigurationStyle(Enum):
|
class ConfigurationStyle(Enum):
|
||||||
@ -51,3 +55,43 @@ async def caplog_setup_text(caplog: pytest.LogCaptureFixture) -> str:
|
|||||||
@pytest.fixture(autouse=True, name="stub_blueprint_populate")
|
@pytest.fixture(autouse=True, name="stub_blueprint_populate")
|
||||||
def stub_blueprint_populate_autouse(stub_blueprint_populate: None) -> None:
|
def stub_blueprint_populate_autouse(stub_blueprint_populate: None) -> None:
|
||||||
"""Stub copying the blueprints to the config folder."""
|
"""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"]
|
||||||
|
@ -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.helpers import device_registry as dr, entity_registry as er
|
||||||
from homeassistant.setup import async_setup_component
|
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.common import MockConfigEntry, assert_setup_component, async_capture_events
|
||||||
|
from tests.typing import WebSocketGenerator
|
||||||
|
|
||||||
_TEST_OBJECT_ID = "template_number"
|
_TEST_OBJECT_ID = "template_number"
|
||||||
_TEST_NUMBER = f"number.{_TEST_OBJECT_ID}"
|
_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)
|
state = hass.states.get(_TEST_NUMBER)
|
||||||
assert float(state.state) == 4
|
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"
|
||||||
|
@ -30,6 +30,8 @@ from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
|||||||
from homeassistant.setup import ATTR_COMPONENT, async_setup_component
|
from homeassistant.setup import ATTR_COMPONENT, async_setup_component
|
||||||
from homeassistant.util import dt as dt_util
|
from homeassistant.util import dt as dt_util
|
||||||
|
|
||||||
|
from .conftest import async_get_flow_preview_state
|
||||||
|
|
||||||
from tests.common import (
|
from tests.common import (
|
||||||
MockConfigEntry,
|
MockConfigEntry,
|
||||||
assert_setup_component,
|
assert_setup_component,
|
||||||
@ -37,6 +39,7 @@ from tests.common import (
|
|||||||
async_fire_time_changed,
|
async_fire_time_changed,
|
||||||
mock_restore_cache_with_extra_data,
|
mock_restore_cache_with_extra_data,
|
||||||
)
|
)
|
||||||
|
from tests.conftest import WebSocketGenerator
|
||||||
|
|
||||||
TEST_NAME = "sensor.test_template_sensor"
|
TEST_NAME = "sensor.test_template_sensor"
|
||||||
|
|
||||||
@ -2434,3 +2437,19 @@ async def test_device_id(
|
|||||||
template_entity = entity_registry.async_get("sensor.my_template")
|
template_entity = entity_registry.async_get("sensor.my_template")
|
||||||
assert template_entity is not None
|
assert template_entity is not None
|
||||||
assert template_entity.device_id == device_entry.id
|
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"
|
||||||
|
@ -8,7 +8,6 @@ from syrupy.assertion import SnapshotAssertion
|
|||||||
from homeassistant.components import switch, template
|
from homeassistant.components import switch, template
|
||||||
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
||||||
from homeassistant.components.template.switch import rewrite_legacy_to_modern_conf
|
from homeassistant.components.template.switch import rewrite_legacy_to_modern_conf
|
||||||
from homeassistant.config_entries import SOURCE_USER
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_ENTITY_ID,
|
ATTR_ENTITY_ID,
|
||||||
SERVICE_TURN_OFF,
|
SERVICE_TURN_OFF,
|
||||||
@ -18,12 +17,11 @@ from homeassistant.const import (
|
|||||||
STATE_UNAVAILABLE,
|
STATE_UNAVAILABLE,
|
||||||
)
|
)
|
||||||
from homeassistant.core import CoreState, HomeAssistant, ServiceCall, State
|
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 import device_registry as dr, entity_registry as er
|
||||||
from homeassistant.helpers.template import Template
|
from homeassistant.helpers.template import Template
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
from .conftest import ConfigurationStyle
|
from .conftest import ConfigurationStyle, async_get_flow_preview_state
|
||||||
|
|
||||||
from tests.common import (
|
from tests.common import (
|
||||||
MockConfigEntry,
|
MockConfigEntry,
|
||||||
@ -396,37 +394,15 @@ async def test_flow_preview(
|
|||||||
hass_ws_client: WebSocketGenerator,
|
hass_ws_client: WebSocketGenerator,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test the config flow preview."""
|
"""Test the config flow preview."""
|
||||||
client = await hass_ws_client(hass)
|
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
state = await async_get_flow_preview_state(
|
||||||
template.DOMAIN, context={"source": SOURCE_USER}
|
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(
|
assert state["state"] == STATE_ON
|
||||||
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"
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user