mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Fix an issue with the switch preview in beta (#141617)
Fix an issue with the switch preview
This commit is contained in:
parent
9f0976d94a
commit
31479056ed
@ -120,7 +120,7 @@ def rewrite_legacy_to_modern_conf(
|
|||||||
return switches
|
return switches
|
||||||
|
|
||||||
|
|
||||||
def rewrite_options_to_moder_conf(option_config: dict[str, dict]) -> dict[str, dict]:
|
def rewrite_options_to_modern_conf(option_config: dict[str, dict]) -> dict[str, dict]:
|
||||||
"""Rewrite option configuration to modern configuration."""
|
"""Rewrite option configuration to modern configuration."""
|
||||||
option_config = {**option_config}
|
option_config = {**option_config}
|
||||||
|
|
||||||
@ -189,7 +189,7 @@ async def async_setup_entry(
|
|||||||
"""Initialize config entry."""
|
"""Initialize config entry."""
|
||||||
_options = dict(config_entry.options)
|
_options = dict(config_entry.options)
|
||||||
_options.pop("template_type")
|
_options.pop("template_type")
|
||||||
_options = rewrite_options_to_moder_conf(_options)
|
_options = rewrite_options_to_modern_conf(_options)
|
||||||
validated_config = SWITCH_CONFIG_SCHEMA(_options)
|
validated_config = SWITCH_CONFIG_SCHEMA(_options)
|
||||||
async_add_entities([SwitchTemplate(hass, validated_config, config_entry.entry_id)])
|
async_add_entities([SwitchTemplate(hass, validated_config, config_entry.entry_id)])
|
||||||
|
|
||||||
@ -199,7 +199,8 @@ def async_create_preview_switch(
|
|||||||
hass: HomeAssistant, name: str, config: dict[str, Any]
|
hass: HomeAssistant, name: str, config: dict[str, Any]
|
||||||
) -> SwitchTemplate:
|
) -> SwitchTemplate:
|
||||||
"""Create a preview switch."""
|
"""Create a preview switch."""
|
||||||
validated_config = SWITCH_CONFIG_SCHEMA(config | {CONF_NAME: name})
|
updated_config = rewrite_options_to_modern_conf(config)
|
||||||
|
validated_config = SWITCH_CONFIG_SCHEMA(updated_config | {CONF_NAME: name})
|
||||||
return SwitchTemplate(hass, validated_config, None)
|
return SwitchTemplate(hass, validated_config, None)
|
||||||
|
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ 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,
|
||||||
@ -17,6 +18,7 @@ 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
|
||||||
@ -29,6 +31,7 @@ from tests.common import (
|
|||||||
mock_component,
|
mock_component,
|
||||||
mock_restore_cache,
|
mock_restore_cache,
|
||||||
)
|
)
|
||||||
|
from tests.typing import WebSocketGenerator
|
||||||
|
|
||||||
TEST_OBJECT_ID = "test_template_switch"
|
TEST_OBJECT_ID = "test_template_switch"
|
||||||
TEST_ENTITY_ID = f"switch.{TEST_OBJECT_ID}"
|
TEST_ENTITY_ID = f"switch.{TEST_OBJECT_ID}"
|
||||||
@ -279,6 +282,46 @@ async def test_setup_config_entry(
|
|||||||
assert state == snapshot
|
assert state == snapshot
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("state_key", ["value_template", "state"])
|
||||||
|
async def test_flow_preview(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
state_key: str,
|
||||||
|
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}
|
||||||
|
)
|
||||||
|
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"
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
("count", "state_template"), [(1, "{{ states.switch.test_state.state }}")]
|
("count", "state_template"), [(1, "{{ states.switch.test_state.state }}")]
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user