Skip to content

Commit 3c87a3e

Browse files
authored
Add a preview to template config flow for alarm control panel, image, and select platforms (home-assistant#148441)
1 parent aacaa9a commit 3c87a3e

5 files changed

Lines changed: 71 additions & 7 deletions

File tree

homeassistant/components/template/alarm_control_panel.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,18 @@ async def async_setup_platform(
206206
)
207207

208208

209+
@callback
210+
def async_create_preview_alarm_control_panel(
211+
hass: HomeAssistant, name: str, config: dict[str, Any]
212+
) -> StateAlarmControlPanelEntity:
213+
"""Create a preview alarm control panel."""
214+
updated_config = rewrite_options_to_modern_conf(config)
215+
validated_config = ALARM_CONTROL_PANEL_CONFIG_SCHEMA(
216+
updated_config | {CONF_NAME: name}
217+
)
218+
return StateAlarmControlPanelEntity(hass, validated_config, None)
219+
220+
209221
class AbstractTemplateAlarmControlPanel(
210222
AbstractTemplateEntity, AlarmControlPanelEntity, RestoreEntity
211223
):

homeassistant/components/template/config_flow.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
CONF_DISARM_ACTION,
5151
CONF_TRIGGER_ACTION,
5252
TemplateCodeFormat,
53+
async_create_preview_alarm_control_panel,
5354
)
5455
from .binary_sensor import async_create_preview_binary_sensor
5556
from .const import CONF_PRESS, CONF_TURN_OFF, CONF_TURN_ON, DOMAIN
@@ -63,7 +64,7 @@
6364
DEFAULT_STEP,
6465
async_create_preview_number,
6566
)
66-
from .select import CONF_OPTIONS, CONF_SELECT_OPTION
67+
from .select import CONF_OPTIONS, CONF_SELECT_OPTION, async_create_preview_select
6768
from .sensor import async_create_preview_sensor
6869
from .switch import async_create_preview_switch
6970
from .template_entity import TemplateEntity
@@ -319,6 +320,7 @@ async def _validate_user_input(
319320
"user": SchemaFlowMenuStep(TEMPLATE_TYPES),
320321
Platform.ALARM_CONTROL_PANEL: SchemaFlowFormStep(
321322
config_schema(Platform.ALARM_CONTROL_PANEL),
323+
preview="template",
322324
validate_user_input=validate_user_input(Platform.ALARM_CONTROL_PANEL),
323325
),
324326
Platform.BINARY_SENSOR: SchemaFlowFormStep(
@@ -332,6 +334,7 @@ async def _validate_user_input(
332334
),
333335
Platform.IMAGE: SchemaFlowFormStep(
334336
config_schema(Platform.IMAGE),
337+
preview="template",
335338
validate_user_input=validate_user_input(Platform.IMAGE),
336339
),
337340
Platform.NUMBER: SchemaFlowFormStep(
@@ -341,6 +344,7 @@ async def _validate_user_input(
341344
),
342345
Platform.SELECT: SchemaFlowFormStep(
343346
config_schema(Platform.SELECT),
347+
preview="template",
344348
validate_user_input=validate_user_input(Platform.SELECT),
345349
),
346350
Platform.SENSOR: SchemaFlowFormStep(
@@ -360,6 +364,7 @@ async def _validate_user_input(
360364
"init": SchemaFlowFormStep(next_step=choose_options_step),
361365
Platform.ALARM_CONTROL_PANEL: SchemaFlowFormStep(
362366
options_schema(Platform.ALARM_CONTROL_PANEL),
367+
preview="template",
363368
validate_user_input=validate_user_input(Platform.ALARM_CONTROL_PANEL),
364369
),
365370
Platform.BINARY_SENSOR: SchemaFlowFormStep(
@@ -373,6 +378,7 @@ async def _validate_user_input(
373378
),
374379
Platform.IMAGE: SchemaFlowFormStep(
375380
options_schema(Platform.IMAGE),
381+
preview="template",
376382
validate_user_input=validate_user_input(Platform.IMAGE),
377383
),
378384
Platform.NUMBER: SchemaFlowFormStep(
@@ -382,6 +388,7 @@ async def _validate_user_input(
382388
),
383389
Platform.SELECT: SchemaFlowFormStep(
384390
options_schema(Platform.SELECT),
391+
preview="template",
385392
validate_user_input=validate_user_input(Platform.SELECT),
386393
),
387394
Platform.SENSOR: SchemaFlowFormStep(
@@ -400,10 +407,12 @@ async def _validate_user_input(
400407
str,
401408
Callable[[HomeAssistant, str, dict[str, Any]], TemplateEntity],
402409
] = {
403-
"binary_sensor": async_create_preview_binary_sensor,
404-
"number": async_create_preview_number,
405-
"sensor": async_create_preview_sensor,
406-
"switch": async_create_preview_switch,
410+
Platform.ALARM_CONTROL_PANEL: async_create_preview_alarm_control_panel,
411+
Platform.BINARY_SENSOR: async_create_preview_binary_sensor,
412+
Platform.NUMBER: async_create_preview_number,
413+
Platform.SELECT: async_create_preview_select,
414+
Platform.SENSOR: async_create_preview_sensor,
415+
Platform.SWITCH: async_create_preview_switch,
407416
}
408417

409418

homeassistant/components/template/select.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ async def async_setup_entry(
9090
async_add_entities([TemplateSelect(hass, validated_config, config_entry.entry_id)])
9191

9292

93+
@callback
94+
def async_create_preview_select(
95+
hass: HomeAssistant, name: str, config: dict[str, Any]
96+
) -> TemplateSelect:
97+
"""Create a preview select."""
98+
validated_config = SELECT_CONFIG_SCHEMA(config | {CONF_NAME: name})
99+
return TemplateSelect(hass, validated_config, None)
100+
101+
93102
class AbstractTemplateSelect(AbstractTemplateEntity, SelectEntity):
94103
"""Representation of a template select features."""
95104

tests/components/template/test_alarm_control_panel.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
from homeassistant.helpers import device_registry as dr, entity_registry as er
2424
from homeassistant.setup import async_setup_component
2525

26-
from .conftest import ConfigurationStyle
26+
from .conftest import ConfigurationStyle, async_get_flow_preview_state
2727

2828
from tests.common import MockConfigEntry, assert_setup_component, mock_restore_cache
29+
from tests.conftest import WebSocketGenerator
2930

3031
TEST_OBJECT_ID = "test_template_panel"
3132
TEST_ENTITY_ID = f"alarm_control_panel.{TEST_OBJECT_ID}"
@@ -915,3 +916,19 @@ async def test_device_id(
915916
template_entity = entity_registry.async_get("alarm_control_panel.my_template")
916917
assert template_entity is not None
917918
assert template_entity.device_id == device_entry.id
919+
920+
921+
async def test_flow_preview(
922+
hass: HomeAssistant,
923+
hass_ws_client: WebSocketGenerator,
924+
) -> None:
925+
"""Test the config flow preview."""
926+
927+
state = await async_get_flow_preview_state(
928+
hass,
929+
hass_ws_client,
930+
ALARM_DOMAIN,
931+
{"name": "My template", "state": "{{ 'disarmed' }}"},
932+
)
933+
934+
assert state["state"] == AlarmControlPanelState.DISARMED

tests/components/template/test_select.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@
3535
from homeassistant.helpers import device_registry as dr, entity_registry as er
3636
from homeassistant.setup import async_setup_component
3737

38-
from .conftest import ConfigurationStyle
38+
from .conftest import ConfigurationStyle, async_get_flow_preview_state
3939

4040
from tests.common import MockConfigEntry, assert_setup_component, async_capture_events
41+
from tests.conftest import WebSocketGenerator
4142

4243
_TEST_OBJECT_ID = "template_select"
4344
_TEST_SELECT = f"select.{_TEST_OBJECT_ID}"
@@ -645,3 +646,19 @@ async def test_availability(hass: HomeAssistant) -> None:
645646

646647
state = hass.states.get(_TEST_SELECT)
647648
assert state.state == "yes"
649+
650+
651+
async def test_flow_preview(
652+
hass: HomeAssistant,
653+
hass_ws_client: WebSocketGenerator,
654+
) -> None:
655+
"""Test the config flow preview."""
656+
657+
state = await async_get_flow_preview_state(
658+
hass,
659+
hass_ws_client,
660+
select.DOMAIN,
661+
{"name": "My template", **TEST_OPTIONS},
662+
)
663+
664+
assert state["state"] == "test"

0 commit comments

Comments
 (0)