diff --git a/homeassistant/components/min_max/config_flow.py b/homeassistant/components/min_max/config_flow.py index ca3c0b2afb..f68261d0ab 100644 --- a/homeassistant/components/min_max/config_flow.py +++ b/homeassistant/components/min_max/config_flow.py @@ -74,16 +74,17 @@ class ConfigFlowHandler(SchemaConfigFlowHandler, domain=DOMAIN): config_flow = CONFIG_FLOW options_flow = OPTIONS_FLOW - def __init__(self) -> None: - """Initialize config flow.""" - super().__init__() - hass = async_get_hass() - websocket_api.async_register_command(hass, ws_preview_min_max) - def async_config_entry_title(self, options: Mapping[str, Any]) -> str: """Return config entry title.""" return cast(str, options["name"]) if "name" in options else "" + @callback + @staticmethod + def async_setup_preview() -> None: + """Set up preview WS API.""" + hass = async_get_hass() + websocket_api.async_register_command(hass, ws_preview_min_max) + @callback @websocket_api.websocket_command( diff --git a/homeassistant/helpers/schema_config_entry_flow.py b/homeassistant/helpers/schema_config_entry_flow.py index 2fe5e87ea7..32e209ccc4 100644 --- a/homeassistant/helpers/schema_config_entry_flow.py +++ b/homeassistant/helpers/schema_config_entry_flow.py @@ -274,6 +274,8 @@ class SchemaConfigFlowHandler(config_entries.ConfigFlow, ABC): if cls.options_flow is None: raise UnknownHandler + cls.async_setup_preview() + return SchemaOptionsFlowHandler( config_entry, cls.options_flow, cls.async_options_flow_finished ) @@ -288,6 +290,12 @@ class SchemaConfigFlowHandler(config_entries.ConfigFlow, ABC): def __init__(self) -> None: """Initialize config flow.""" self._common_handler = SchemaCommonFlowHandler(self, self.config_flow, None) + self.async_setup_preview() + + @callback + @staticmethod + def async_setup_preview() -> None: + """Set up preview.""" @classmethod @callback diff --git a/tests/components/min_max/test_config_flow.py b/tests/components/min_max/test_config_flow.py index c2d8b72ab5..9d5157929f 100644 --- a/tests/components/min_max/test_config_flow.py +++ b/tests/components/min_max/test_config_flow.py @@ -199,3 +199,59 @@ async def test_config_flow_preview(hass: HomeAssistant, hass_ws_client) -> None: }, "state": "20.0", } + + +async def test_option_flow_preview(hass: HomeAssistant, hass_ws_client) -> None: + """Test the option flow preview.""" + client = await hass_ws_client(hass) + + # Setup the config entry + config_entry = MockConfigEntry( + data={}, + domain=DOMAIN, + options={ + "entity_ids": ["sensor.input_one", "sensor.input_two"], + "name": "My min_max", + "round_digits": 0, + "type": "min", + }, + title="My min_max", + ) + config_entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + input_sensors = ["sensor.input_one", "sensor.input_two"] + + result = await hass.config_entries.options.async_init(config_entry.entry_id) + assert result["type"] == FlowResultType.FORM + assert result["errors"] is None + assert result["preview"] == "min_max_preview" + + hass.states.async_set("sensor.input_one", "10") + hass.states.async_set("sensor.input_two", "20") + + await client.send_json( + { + "id": 1, + "type": "min_max/preview", + "flow_id": "blah", + "flow_type": "config_flow", + "user_input": { + "name": "My min_max", + "entity_ids": input_sensors, + "type": "min", + }, + } + ) + msg = await client.receive_json() + assert msg["success"] + assert msg["result"] == { + "attributes": { + "friendly_name": "My min_max", + "icon": "mdi:calculator", + "min_entity_id": "sensor.input_one", + "state_class": "measurement", + }, + "state": "10.0", + }