Fix preview during option flow

This commit is contained in:
Erik
2023-03-28 17:50:33 +02:00
parent 4bc6b2c92e
commit 9c8aebf8d0
3 changed files with 71 additions and 6 deletions
@@ -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(
@@ -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
@@ -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",
}