2018-04-13 14:14:53 +00:00
|
|
|
"""Test the flow classes."""
|
2023-02-17 20:51:19 +00:00
|
|
|
import dataclasses
|
2022-08-29 20:25:34 +00:00
|
|
|
import logging
|
|
|
|
from unittest.mock import Mock, patch
|
2021-04-15 17:13:42 +00:00
|
|
|
|
2018-04-13 14:14:53 +00:00
|
|
|
import pytest
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-04-25 09:27:40 +00:00
|
|
|
from homeassistant import config_entries, data_entry_flow
|
2021-10-13 15:37:14 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2018-04-13 14:14:53 +00:00
|
|
|
from homeassistant.util.decorator import Registry
|
|
|
|
|
2023-12-23 10:25:39 +00:00
|
|
|
from .common import async_capture_events, import_and_test_deprecated_constant_enum
|
2019-05-10 12:33:50 +00:00
|
|
|
|
2018-04-13 14:14:53 +00:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def manager():
|
|
|
|
"""Return a flow manager."""
|
|
|
|
handlers = Registry()
|
|
|
|
entries = []
|
|
|
|
|
2020-01-03 10:52:01 +00:00
|
|
|
class FlowManager(data_entry_flow.FlowManager):
|
|
|
|
"""Test flow manager."""
|
2018-04-14 18:38:24 +00:00
|
|
|
|
2020-01-03 10:52:01 +00:00
|
|
|
async def async_create_flow(self, handler_key, *, context, data):
|
|
|
|
"""Test create flow."""
|
|
|
|
handler = handlers.get(handler_key)
|
2018-04-14 18:38:24 +00:00
|
|
|
|
2020-01-03 10:52:01 +00:00
|
|
|
if handler is None:
|
|
|
|
raise data_entry_flow.UnknownHandler
|
2018-04-14 18:38:24 +00:00
|
|
|
|
2020-01-03 10:52:01 +00:00
|
|
|
flow = handler()
|
|
|
|
flow.init_step = context.get("init_step", "init")
|
|
|
|
return flow
|
2018-04-13 14:14:53 +00:00
|
|
|
|
2020-01-03 10:52:01 +00:00
|
|
|
async def async_finish_flow(self, flow, result):
|
|
|
|
"""Test finish flow."""
|
2022-07-07 16:57:36 +00:00
|
|
|
if result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY:
|
2020-01-03 10:52:01 +00:00
|
|
|
result["source"] = flow.context.get("source")
|
|
|
|
entries.append(result)
|
|
|
|
return result
|
|
|
|
|
|
|
|
mgr = FlowManager(None)
|
|
|
|
mgr.mock_created_entries = entries
|
|
|
|
mgr.mock_reg_handler = handlers.register
|
|
|
|
return mgr
|
2018-04-13 14:14:53 +00:00
|
|
|
|
|
|
|
|
2023-02-21 08:27:13 +00:00
|
|
|
async def test_configure_reuses_handler_instance(manager) -> None:
|
2018-04-13 14:14:53 +00:00
|
|
|
"""Test that we reuse instances."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
@manager.mock_reg_handler("test")
|
2018-04-13 14:14:53 +00:00
|
|
|
class TestFlow(data_entry_flow.FlowHandler):
|
|
|
|
handle_count = 0
|
|
|
|
|
|
|
|
async def async_step_init(self, user_input=None):
|
|
|
|
self.handle_count += 1
|
|
|
|
return self.async_show_form(
|
2019-07-31 19:25:30 +00:00
|
|
|
errors={"base": str(self.handle_count)}, step_id="init"
|
|
|
|
)
|
2018-04-13 14:14:53 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
form = await manager.async_init("test")
|
|
|
|
assert form["errors"]["base"] == "1"
|
|
|
|
form = await manager.async_configure(form["flow_id"])
|
|
|
|
assert form["errors"]["base"] == "2"
|
2020-05-06 04:34:51 +00:00
|
|
|
assert manager.async_progress() == [
|
|
|
|
{
|
|
|
|
"flow_id": form["flow_id"],
|
|
|
|
"handler": "test",
|
|
|
|
"step_id": "init",
|
|
|
|
"context": {},
|
|
|
|
}
|
|
|
|
]
|
2018-04-13 14:14:53 +00:00
|
|
|
assert len(manager.mock_created_entries) == 0
|
|
|
|
|
|
|
|
|
2023-02-17 20:51:19 +00:00
|
|
|
async def test_configure_two_steps(manager: data_entry_flow.FlowManager) -> None:
|
2018-04-13 14:14:53 +00:00
|
|
|
"""Test that we reuse instances."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
@manager.mock_reg_handler("test")
|
2018-04-13 14:14:53 +00:00
|
|
|
class TestFlow(data_entry_flow.FlowHandler):
|
|
|
|
VERSION = 1
|
|
|
|
|
2018-08-09 11:24:14 +00:00
|
|
|
async def async_step_first(self, user_input=None):
|
2018-04-13 14:14:53 +00:00
|
|
|
if user_input is not None:
|
|
|
|
return await self.async_step_second()
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_show_form(step_id="first", data_schema=vol.Schema([str]))
|
2018-04-13 14:14:53 +00:00
|
|
|
|
|
|
|
async def async_step_second(self, user_input=None):
|
|
|
|
if user_input is not None:
|
|
|
|
return self.async_create_entry(
|
2019-07-31 19:25:30 +00:00
|
|
|
title="Test Entry", data=self.init_data + user_input
|
2018-04-13 14:14:53 +00:00
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_show_form(step_id="second", data_schema=vol.Schema([str]))
|
2018-04-13 14:14:53 +00:00
|
|
|
|
2023-02-17 20:51:19 +00:00
|
|
|
form = await manager.async_init(
|
|
|
|
"test", context={"init_step": "first"}, data=["INIT-DATA"]
|
|
|
|
)
|
2018-04-13 14:14:53 +00:00
|
|
|
|
|
|
|
with pytest.raises(vol.Invalid):
|
2019-07-31 19:25:30 +00:00
|
|
|
form = await manager.async_configure(form["flow_id"], "INCORRECT-DATA")
|
|
|
|
|
|
|
|
form = await manager.async_configure(form["flow_id"], ["SECOND-DATA"])
|
2022-07-07 16:57:36 +00:00
|
|
|
assert form["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
2018-04-13 14:14:53 +00:00
|
|
|
assert len(manager.async_progress()) == 0
|
|
|
|
assert len(manager.mock_created_entries) == 1
|
|
|
|
result = manager.mock_created_entries[0]
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["handler"] == "test"
|
|
|
|
assert result["data"] == ["INIT-DATA", "SECOND-DATA"]
|
2018-04-13 14:14:53 +00:00
|
|
|
|
|
|
|
|
2023-02-21 08:27:13 +00:00
|
|
|
async def test_show_form(manager) -> None:
|
2019-12-16 11:27:43 +00:00
|
|
|
"""Test that we can show a form."""
|
2019-07-31 19:25:30 +00:00
|
|
|
schema = vol.Schema({vol.Required("username"): str, vol.Required("password"): str})
|
2018-04-13 14:14:53 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@manager.mock_reg_handler("test")
|
2018-04-13 14:14:53 +00:00
|
|
|
class TestFlow(data_entry_flow.FlowHandler):
|
|
|
|
async def async_step_init(self, user_input=None):
|
|
|
|
return self.async_show_form(
|
2019-07-31 19:25:30 +00:00
|
|
|
step_id="init",
|
2018-04-13 14:14:53 +00:00
|
|
|
data_schema=schema,
|
2019-07-31 19:25:30 +00:00
|
|
|
errors={"username": "Should be unique."},
|
2018-04-13 14:14:53 +00:00
|
|
|
)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
form = await manager.async_init("test")
|
2022-07-07 16:57:36 +00:00
|
|
|
assert form["type"] == data_entry_flow.FlowResultType.FORM
|
2019-07-31 19:25:30 +00:00
|
|
|
assert form["data_schema"] is schema
|
|
|
|
assert form["errors"] == {"username": "Should be unique."}
|
2018-04-13 14:14:53 +00:00
|
|
|
|
|
|
|
|
2023-02-21 08:27:13 +00:00
|
|
|
async def test_abort_removes_instance(manager) -> None:
|
2018-04-13 14:14:53 +00:00
|
|
|
"""Test that abort removes the flow from progress."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
@manager.mock_reg_handler("test")
|
2018-04-13 14:14:53 +00:00
|
|
|
class TestFlow(data_entry_flow.FlowHandler):
|
|
|
|
is_new = True
|
|
|
|
|
|
|
|
async def async_step_init(self, user_input=None):
|
|
|
|
old = self.is_new
|
|
|
|
self.is_new = False
|
|
|
|
return self.async_abort(reason=str(old))
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
form = await manager.async_init("test")
|
|
|
|
assert form["reason"] == "True"
|
2018-04-13 14:14:53 +00:00
|
|
|
assert len(manager.async_progress()) == 0
|
|
|
|
assert len(manager.mock_created_entries) == 0
|
2019-07-31 19:25:30 +00:00
|
|
|
form = await manager.async_init("test")
|
|
|
|
assert form["reason"] == "True"
|
2018-04-13 14:14:53 +00:00
|
|
|
assert len(manager.async_progress()) == 0
|
|
|
|
assert len(manager.mock_created_entries) == 0
|
|
|
|
|
|
|
|
|
2023-02-21 08:27:13 +00:00
|
|
|
async def test_abort_calls_async_remove(manager) -> None:
|
2022-08-29 20:25:34 +00:00
|
|
|
"""Test abort calling the async_remove FlowHandler method."""
|
|
|
|
|
|
|
|
@manager.mock_reg_handler("test")
|
|
|
|
class TestFlow(data_entry_flow.FlowHandler):
|
|
|
|
async def async_step_init(self, user_input=None):
|
|
|
|
return self.async_abort(reason="reason")
|
|
|
|
|
|
|
|
async_remove = Mock()
|
|
|
|
|
|
|
|
await manager.async_init("test")
|
|
|
|
|
|
|
|
TestFlow.async_remove.assert_called_once()
|
|
|
|
|
|
|
|
assert len(manager.async_progress()) == 0
|
|
|
|
assert len(manager.mock_created_entries) == 0
|
|
|
|
|
|
|
|
|
2023-02-21 08:27:13 +00:00
|
|
|
async def test_abort_calls_async_remove_with_exception(
|
|
|
|
manager, caplog: pytest.LogCaptureFixture
|
|
|
|
) -> None:
|
2022-08-29 20:25:34 +00:00
|
|
|
"""Test abort calling the async_remove FlowHandler method, with an exception."""
|
|
|
|
|
|
|
|
@manager.mock_reg_handler("test")
|
|
|
|
class TestFlow(data_entry_flow.FlowHandler):
|
|
|
|
async def async_step_init(self, user_input=None):
|
|
|
|
return self.async_abort(reason="reason")
|
|
|
|
|
|
|
|
async_remove = Mock(side_effect=[RuntimeError("error")])
|
|
|
|
|
|
|
|
with caplog.at_level(logging.ERROR):
|
|
|
|
await manager.async_init("test")
|
|
|
|
|
2023-01-17 14:26:17 +00:00
|
|
|
assert "Error removing test flow: error" in caplog.text
|
2022-08-29 20:25:34 +00:00
|
|
|
|
|
|
|
TestFlow.async_remove.assert_called_once()
|
|
|
|
|
|
|
|
assert len(manager.async_progress()) == 0
|
|
|
|
assert len(manager.mock_created_entries) == 0
|
|
|
|
|
|
|
|
|
2023-02-21 08:27:13 +00:00
|
|
|
async def test_create_saves_data(manager) -> None:
|
2018-04-13 14:14:53 +00:00
|
|
|
"""Test creating a config entry."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
@manager.mock_reg_handler("test")
|
2018-04-13 14:14:53 +00:00
|
|
|
class TestFlow(data_entry_flow.FlowHandler):
|
|
|
|
VERSION = 5
|
|
|
|
|
|
|
|
async def async_step_init(self, user_input=None):
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_create_entry(title="Test Title", data="Test Data")
|
2018-04-13 14:14:53 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await manager.async_init("test")
|
2018-04-13 14:14:53 +00:00
|
|
|
assert len(manager.async_progress()) == 0
|
|
|
|
assert len(manager.mock_created_entries) == 1
|
|
|
|
|
|
|
|
entry = manager.mock_created_entries[0]
|
2019-07-31 19:25:30 +00:00
|
|
|
assert entry["version"] == 5
|
|
|
|
assert entry["handler"] == "test"
|
|
|
|
assert entry["title"] == "Test Title"
|
|
|
|
assert entry["data"] == "Test Data"
|
|
|
|
assert entry["source"] is None
|
2018-04-13 14:14:53 +00:00
|
|
|
|
|
|
|
|
2023-02-21 08:27:13 +00:00
|
|
|
async def test_discovery_init_flow(manager) -> None:
|
2018-04-13 14:14:53 +00:00
|
|
|
"""Test a flow initialized by discovery."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
@manager.mock_reg_handler("test")
|
2018-04-13 14:14:53 +00:00
|
|
|
class TestFlow(data_entry_flow.FlowHandler):
|
|
|
|
VERSION = 5
|
|
|
|
|
2018-08-09 11:24:14 +00:00
|
|
|
async def async_step_init(self, info):
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_create_entry(title=info["id"], data=info)
|
2018-04-13 14:14:53 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
data = {"id": "hello", "token": "secret"}
|
2018-04-13 14:14:53 +00:00
|
|
|
|
2021-04-25 09:27:40 +00:00
|
|
|
await manager.async_init(
|
|
|
|
"test", context={"source": config_entries.SOURCE_DISCOVERY}, data=data
|
|
|
|
)
|
2018-04-13 14:14:53 +00:00
|
|
|
assert len(manager.async_progress()) == 0
|
|
|
|
assert len(manager.mock_created_entries) == 1
|
|
|
|
|
|
|
|
entry = manager.mock_created_entries[0]
|
2019-07-31 19:25:30 +00:00
|
|
|
assert entry["version"] == 5
|
|
|
|
assert entry["handler"] == "test"
|
|
|
|
assert entry["title"] == "hello"
|
|
|
|
assert entry["data"] == data
|
2021-04-25 09:27:40 +00:00
|
|
|
assert entry["source"] == config_entries.SOURCE_DISCOVERY
|
2018-08-21 17:48:24 +00:00
|
|
|
|
|
|
|
|
2023-02-08 07:51:43 +00:00
|
|
|
async def test_finish_callback_change_result_type(hass: HomeAssistant) -> None:
|
2018-08-21 17:48:24 +00:00
|
|
|
"""Test finish callback can change result type."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-08-21 17:48:24 +00:00
|
|
|
class TestFlow(data_entry_flow.FlowHandler):
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
async def async_step_init(self, input):
|
|
|
|
"""Return init form with one input field 'count'."""
|
|
|
|
if input is not None:
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_create_entry(title="init", data=input)
|
2018-08-21 17:48:24 +00:00
|
|
|
return self.async_show_form(
|
2019-07-31 19:25:30 +00:00
|
|
|
step_id="init", data_schema=vol.Schema({"count": int})
|
|
|
|
)
|
2018-08-21 17:48:24 +00:00
|
|
|
|
2020-01-03 10:52:01 +00:00
|
|
|
class FlowManager(data_entry_flow.FlowManager):
|
|
|
|
async def async_create_flow(self, handler_name, *, context, data):
|
|
|
|
"""Create a test flow."""
|
|
|
|
return TestFlow()
|
|
|
|
|
|
|
|
async def async_finish_flow(self, flow, result):
|
|
|
|
"""Redirect to init form if count <= 1."""
|
2022-07-07 16:57:36 +00:00
|
|
|
if result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY:
|
2020-01-03 10:52:01 +00:00
|
|
|
if result["data"] is None or result["data"].get("count", 0) <= 1:
|
|
|
|
return flow.async_show_form(
|
|
|
|
step_id="init", data_schema=vol.Schema({"count": int})
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
result["result"] = result["data"]["count"]
|
|
|
|
return result
|
|
|
|
|
|
|
|
manager = FlowManager(hass)
|
2018-08-21 17:48:24 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
result = await manager.async_init("test")
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["step_id"] == "init"
|
2018-08-21 17:48:24 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
result = await manager.async_configure(result["flow_id"], {"count": 0})
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["step_id"] == "init"
|
|
|
|
assert "result" not in result
|
2018-08-21 17:48:24 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
result = await manager.async_configure(result["flow_id"], {"count": 2})
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["result"] == 2
|
2019-05-10 12:33:50 +00:00
|
|
|
|
|
|
|
|
2023-02-21 08:27:13 +00:00
|
|
|
async def test_external_step(hass: HomeAssistant, manager) -> None:
|
2019-05-10 12:33:50 +00:00
|
|
|
"""Test external step logic."""
|
|
|
|
manager.hass = hass
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@manager.mock_reg_handler("test")
|
2019-05-10 12:33:50 +00:00
|
|
|
class TestFlow(data_entry_flow.FlowHandler):
|
|
|
|
VERSION = 5
|
|
|
|
data = None
|
|
|
|
|
|
|
|
async def async_step_init(self, user_input=None):
|
|
|
|
if not user_input:
|
|
|
|
return self.async_external_step(
|
2019-07-31 19:25:30 +00:00
|
|
|
step_id="init", url="https://example.com"
|
2019-05-10 12:33:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
self.data = user_input
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_external_step_done(next_step_id="finish")
|
2019-05-10 12:33:50 +00:00
|
|
|
|
|
|
|
async def async_step_finish(self, user_input=None):
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_create_entry(title=self.data["title"], data=self.data)
|
2019-05-10 12:33:50 +00:00
|
|
|
|
|
|
|
events = async_capture_events(
|
|
|
|
hass, data_entry_flow.EVENT_DATA_ENTRY_FLOW_PROGRESSED
|
|
|
|
)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
result = await manager.async_init("test")
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.EXTERNAL_STEP
|
2019-05-10 12:33:50 +00:00
|
|
|
assert len(manager.async_progress()) == 1
|
2021-10-22 17:19:49 +00:00
|
|
|
assert len(manager.async_progress_by_handler("test")) == 1
|
|
|
|
assert manager.async_get(result["flow_id"])["handler"] == "test"
|
2019-05-10 12:33:50 +00:00
|
|
|
|
|
|
|
# Mimic external step
|
|
|
|
# Called by integrations: `hass.config_entries.flow.async_configure(…)`
|
2019-07-31 19:25:30 +00:00
|
|
|
result = await manager.async_configure(result["flow_id"], {"title": "Hello"})
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.EXTERNAL_STEP_DONE
|
2019-05-10 12:33:50 +00:00
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(events) == 1
|
|
|
|
assert events[0].data == {
|
2019-07-31 19:25:30 +00:00
|
|
|
"handler": "test",
|
|
|
|
"flow_id": result["flow_id"],
|
|
|
|
"refresh": True,
|
2019-05-10 12:33:50 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 15:51:43 +00:00
|
|
|
# Frontend refreshes the flow
|
2019-07-31 19:25:30 +00:00
|
|
|
result = await manager.async_configure(result["flow_id"])
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["title"] == "Hello"
|
2019-12-16 11:27:43 +00:00
|
|
|
|
|
|
|
|
2023-02-21 08:27:13 +00:00
|
|
|
async def test_show_progress(hass: HomeAssistant, manager) -> None:
|
2020-11-09 17:39:28 +00:00
|
|
|
"""Test show progress logic."""
|
|
|
|
manager.hass = hass
|
|
|
|
|
|
|
|
@manager.mock_reg_handler("test")
|
|
|
|
class TestFlow(data_entry_flow.FlowHandler):
|
|
|
|
VERSION = 5
|
|
|
|
data = None
|
|
|
|
task_one_done = False
|
2023-10-02 12:15:54 +00:00
|
|
|
task_two_done = False
|
2020-11-09 17:39:28 +00:00
|
|
|
|
|
|
|
async def async_step_init(self, user_input=None):
|
2023-10-02 12:15:54 +00:00
|
|
|
if user_input and "task_finished" in user_input:
|
|
|
|
if user_input["task_finished"] == 1:
|
2020-11-09 17:39:28 +00:00
|
|
|
self.task_one_done = True
|
2023-10-02 12:15:54 +00:00
|
|
|
elif user_input["task_finished"] == 2:
|
|
|
|
self.task_two_done = True
|
|
|
|
|
|
|
|
if not self.task_one_done:
|
|
|
|
progress_action = "task_one"
|
|
|
|
elif not self.task_two_done:
|
|
|
|
progress_action = "task_two"
|
|
|
|
if not self.task_one_done or not self.task_two_done:
|
2020-11-09 17:39:28 +00:00
|
|
|
return self.async_show_progress(
|
|
|
|
step_id="init",
|
|
|
|
progress_action=progress_action,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.data = user_input
|
|
|
|
return self.async_show_progress_done(next_step_id="finish")
|
|
|
|
|
|
|
|
async def async_step_finish(self, user_input=None):
|
|
|
|
return self.async_create_entry(title=self.data["title"], data=self.data)
|
|
|
|
|
|
|
|
events = async_capture_events(
|
|
|
|
hass, data_entry_flow.EVENT_DATA_ENTRY_FLOW_PROGRESSED
|
|
|
|
)
|
|
|
|
|
|
|
|
result = await manager.async_init("test")
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.SHOW_PROGRESS
|
2020-11-09 17:39:28 +00:00
|
|
|
assert result["progress_action"] == "task_one"
|
|
|
|
assert len(manager.async_progress()) == 1
|
2021-10-22 17:19:49 +00:00
|
|
|
assert len(manager.async_progress_by_handler("test")) == 1
|
|
|
|
assert manager.async_get(result["flow_id"])["handler"] == "test"
|
2020-11-09 17:39:28 +00:00
|
|
|
|
|
|
|
# Mimic task one done and moving to task two
|
|
|
|
# Called by integrations: `hass.config_entries.flow.async_configure(…)`
|
2023-10-02 12:15:54 +00:00
|
|
|
result = await manager.async_configure(result["flow_id"], {"task_finished": 1})
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.SHOW_PROGRESS
|
2020-11-09 17:39:28 +00:00
|
|
|
assert result["progress_action"] == "task_two"
|
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(events) == 1
|
|
|
|
assert events[0].data == {
|
|
|
|
"handler": "test",
|
|
|
|
"flow_id": result["flow_id"],
|
|
|
|
"refresh": True,
|
|
|
|
}
|
|
|
|
|
2023-10-02 12:15:54 +00:00
|
|
|
# Frontend refreshes the flow
|
|
|
|
result = await manager.async_configure(result["flow_id"])
|
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.SHOW_PROGRESS
|
|
|
|
assert result["progress_action"] == "task_two"
|
|
|
|
|
2020-11-09 17:39:28 +00:00
|
|
|
# Mimic task two done and continuing step
|
|
|
|
# Called by integrations: `hass.config_entries.flow.async_configure(…)`
|
2023-10-02 12:15:54 +00:00
|
|
|
result = await manager.async_configure(
|
|
|
|
result["flow_id"], {"task_finished": 2, "title": "Hello"}
|
|
|
|
)
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.SHOW_PROGRESS_DONE
|
2020-11-09 17:39:28 +00:00
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
2023-10-02 12:15:54 +00:00
|
|
|
assert len(events) == 2 # 1 for task one and 1 for task two
|
2020-11-09 17:39:28 +00:00
|
|
|
assert events[1].data == {
|
|
|
|
"handler": "test",
|
|
|
|
"flow_id": result["flow_id"],
|
|
|
|
"refresh": True,
|
|
|
|
}
|
|
|
|
|
|
|
|
# Frontend refreshes the flow
|
|
|
|
result = await manager.async_configure(result["flow_id"])
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
2020-11-09 17:39:28 +00:00
|
|
|
assert result["title"] == "Hello"
|
|
|
|
|
|
|
|
|
2023-10-02 12:15:54 +00:00
|
|
|
async def test_show_progress_fires_only_when_changed(
|
|
|
|
hass: HomeAssistant, manager
|
|
|
|
) -> None:
|
|
|
|
"""Test show progress change logic."""
|
|
|
|
manager.hass = hass
|
|
|
|
|
|
|
|
@manager.mock_reg_handler("test")
|
|
|
|
class TestFlow(data_entry_flow.FlowHandler):
|
|
|
|
VERSION = 5
|
|
|
|
data = None
|
|
|
|
|
|
|
|
async def async_step_init(self, user_input=None):
|
|
|
|
if user_input:
|
|
|
|
progress_action = user_input["progress_action"]
|
|
|
|
description_placeholders = user_input["description_placeholders"]
|
|
|
|
return self.async_show_progress(
|
|
|
|
step_id="init",
|
|
|
|
progress_action=progress_action,
|
|
|
|
description_placeholders=description_placeholders,
|
|
|
|
)
|
|
|
|
return self.async_show_progress(step_id="init", progress_action="task_one")
|
|
|
|
|
|
|
|
async def async_step_finish(self, user_input=None):
|
|
|
|
return self.async_create_entry(title=self.data["title"], data=self.data)
|
|
|
|
|
|
|
|
events = async_capture_events(
|
|
|
|
hass, data_entry_flow.EVENT_DATA_ENTRY_FLOW_PROGRESSED
|
|
|
|
)
|
|
|
|
|
|
|
|
async def test_change(
|
|
|
|
flow_id,
|
|
|
|
events,
|
|
|
|
progress_action,
|
|
|
|
description_placeholders_progress,
|
|
|
|
number_of_events,
|
|
|
|
is_change,
|
|
|
|
) -> None:
|
|
|
|
# Called by integrations: `hass.config_entries.flow.async_configure(…)`
|
|
|
|
result = await manager.async_configure(
|
|
|
|
flow_id,
|
|
|
|
{
|
|
|
|
"progress_action": progress_action,
|
|
|
|
"description_placeholders": {
|
|
|
|
"progress": description_placeholders_progress
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.SHOW_PROGRESS
|
|
|
|
assert result["progress_action"] == progress_action
|
|
|
|
assert (
|
|
|
|
result["description_placeholders"]["progress"]
|
|
|
|
== description_placeholders_progress
|
|
|
|
)
|
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(events) == number_of_events
|
|
|
|
if is_change:
|
|
|
|
assert events[number_of_events - 1].data == {
|
|
|
|
"handler": "test",
|
|
|
|
"flow_id": result["flow_id"],
|
|
|
|
"refresh": True,
|
|
|
|
}
|
|
|
|
|
|
|
|
result = await manager.async_init("test")
|
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.SHOW_PROGRESS
|
|
|
|
assert result["progress_action"] == "task_one"
|
|
|
|
assert len(manager.async_progress()) == 1
|
|
|
|
assert len(manager.async_progress_by_handler("test")) == 1
|
|
|
|
assert manager.async_get(result["flow_id"])["handler"] == "test"
|
|
|
|
|
|
|
|
# Mimic task one tests
|
|
|
|
await test_change(
|
|
|
|
result["flow_id"], events, "task_one", 0, 1, True
|
|
|
|
) # change (progress action)
|
|
|
|
await test_change(result["flow_id"], events, "task_one", 0, 1, False) # no change
|
|
|
|
await test_change(
|
|
|
|
result["flow_id"], events, "task_one", 25, 2, True
|
|
|
|
) # change (description placeholder)
|
|
|
|
await test_change(
|
|
|
|
result["flow_id"], events, "task_two", 50, 3, True
|
|
|
|
) # change (progress action and description placeholder)
|
|
|
|
await test_change(result["flow_id"], events, "task_two", 50, 3, False) # no change
|
|
|
|
await test_change(
|
|
|
|
result["flow_id"], events, "task_two", 100, 4, True
|
|
|
|
) # change (description placeholder)
|
|
|
|
|
|
|
|
|
2023-02-21 08:27:13 +00:00
|
|
|
async def test_abort_flow_exception(manager) -> None:
|
2019-12-16 11:27:43 +00:00
|
|
|
"""Test that the AbortFlow exception works."""
|
|
|
|
|
|
|
|
@manager.mock_reg_handler("test")
|
|
|
|
class TestFlow(data_entry_flow.FlowHandler):
|
|
|
|
async def async_step_init(self, user_input=None):
|
|
|
|
raise data_entry_flow.AbortFlow("mock-reason", {"placeholder": "yo"})
|
|
|
|
|
|
|
|
form = await manager.async_init("test")
|
2022-07-07 16:57:36 +00:00
|
|
|
assert form["type"] == data_entry_flow.FlowResultType.ABORT
|
2019-12-16 11:27:43 +00:00
|
|
|
assert form["reason"] == "mock-reason"
|
|
|
|
assert form["description_placeholders"] == {"placeholder": "yo"}
|
2021-04-15 17:13:42 +00:00
|
|
|
|
|
|
|
|
2023-02-21 08:27:13 +00:00
|
|
|
async def test_init_unknown_flow(manager) -> None:
|
2021-04-15 17:13:42 +00:00
|
|
|
"""Test that UnknownFlow is raised when async_create_flow returns None."""
|
|
|
|
|
|
|
|
with pytest.raises(data_entry_flow.UnknownFlow), patch.object(
|
|
|
|
manager, "async_create_flow", return_value=None
|
|
|
|
):
|
|
|
|
await manager.async_init("test")
|
2021-10-13 15:37:14 +00:00
|
|
|
|
|
|
|
|
2023-02-21 08:27:13 +00:00
|
|
|
async def test_async_get_unknown_flow(manager) -> None:
|
2021-10-22 17:19:49 +00:00
|
|
|
"""Test that UnknownFlow is raised when async_get is called with a flow_id that does not exist."""
|
|
|
|
|
|
|
|
with pytest.raises(data_entry_flow.UnknownFlow):
|
|
|
|
await manager.async_get("does_not_exist")
|
|
|
|
|
|
|
|
|
2021-10-13 15:37:14 +00:00
|
|
|
async def test_async_has_matching_flow(
|
|
|
|
hass: HomeAssistant, manager: data_entry_flow.FlowManager
|
2023-02-21 08:27:13 +00:00
|
|
|
) -> None:
|
2021-10-13 15:37:14 +00:00
|
|
|
"""Test we can check for matching flows."""
|
|
|
|
manager.hass = hass
|
|
|
|
|
|
|
|
@manager.mock_reg_handler("test")
|
|
|
|
class TestFlow(data_entry_flow.FlowHandler):
|
|
|
|
VERSION = 5
|
|
|
|
|
|
|
|
async def async_step_init(self, user_input=None):
|
|
|
|
return self.async_show_progress(
|
|
|
|
step_id="init",
|
|
|
|
progress_action="task_one",
|
|
|
|
)
|
|
|
|
|
|
|
|
result = await manager.async_init(
|
|
|
|
"test",
|
|
|
|
context={"source": config_entries.SOURCE_HOMEKIT},
|
|
|
|
data={"properties": {"id": "aa:bb:cc:dd:ee:ff"}},
|
|
|
|
)
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.SHOW_PROGRESS
|
2021-10-13 15:37:14 +00:00
|
|
|
assert result["progress_action"] == "task_one"
|
|
|
|
assert len(manager.async_progress()) == 1
|
2021-10-22 17:19:49 +00:00
|
|
|
assert len(manager.async_progress_by_handler("test")) == 1
|
2023-06-11 08:41:38 +00:00
|
|
|
assert (
|
|
|
|
len(
|
|
|
|
manager.async_progress_by_handler(
|
|
|
|
"test", match_context={"source": config_entries.SOURCE_HOMEKIT}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
== 1
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
len(
|
|
|
|
manager.async_progress_by_handler(
|
|
|
|
"test", match_context={"source": config_entries.SOURCE_BLUETOOTH}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
== 0
|
|
|
|
)
|
2021-10-22 17:19:49 +00:00
|
|
|
assert manager.async_get(result["flow_id"])["handler"] == "test"
|
2021-10-13 15:37:14 +00:00
|
|
|
|
|
|
|
assert (
|
|
|
|
manager.async_has_matching_flow(
|
|
|
|
"test",
|
|
|
|
{"source": config_entries.SOURCE_HOMEKIT},
|
|
|
|
{"properties": {"id": "aa:bb:cc:dd:ee:ff"}},
|
|
|
|
)
|
|
|
|
is True
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
manager.async_has_matching_flow(
|
|
|
|
"test",
|
|
|
|
{"source": config_entries.SOURCE_SSDP},
|
|
|
|
{"properties": {"id": "aa:bb:cc:dd:ee:ff"}},
|
|
|
|
)
|
|
|
|
is False
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
manager.async_has_matching_flow(
|
|
|
|
"other",
|
|
|
|
{"source": config_entries.SOURCE_HOMEKIT},
|
|
|
|
{"properties": {"id": "aa:bb:cc:dd:ee:ff"}},
|
|
|
|
)
|
|
|
|
is False
|
|
|
|
)
|
2021-10-22 17:19:49 +00:00
|
|
|
|
|
|
|
|
2023-02-21 08:27:13 +00:00
|
|
|
async def test_move_to_unknown_step_raises_and_removes_from_in_progress(
|
|
|
|
manager,
|
|
|
|
) -> None:
|
2021-10-22 17:19:49 +00:00
|
|
|
"""Test that moving to an unknown step raises and removes the flow from in progress."""
|
|
|
|
|
|
|
|
@manager.mock_reg_handler("test")
|
|
|
|
class TestFlow(data_entry_flow.FlowHandler):
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
with pytest.raises(data_entry_flow.UnknownStep):
|
|
|
|
await manager.async_init("test", context={"init_step": "does_not_exist"})
|
|
|
|
|
|
|
|
assert manager.async_progress() == []
|
|
|
|
|
|
|
|
|
2023-10-19 11:34:10 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("result_type", "params"),
|
|
|
|
[
|
|
|
|
("async_external_step_done", {"next_step_id": "does_not_exist"}),
|
|
|
|
("async_external_step", {"step_id": "does_not_exist", "url": "blah"}),
|
|
|
|
("async_show_form", {"step_id": "does_not_exist"}),
|
|
|
|
("async_show_menu", {"step_id": "does_not_exist", "menu_options": []}),
|
|
|
|
("async_show_progress_done", {"next_step_id": "does_not_exist"}),
|
|
|
|
("async_show_progress", {"step_id": "does_not_exist", "progress_action": ""}),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_next_step_unknown_step_raises_and_removes_from_in_progress(
|
|
|
|
manager, result_type: str, params: dict[str, str]
|
|
|
|
) -> None:
|
|
|
|
"""Test that moving to an unknown step raises and removes the flow from in progress."""
|
|
|
|
|
|
|
|
@manager.mock_reg_handler("test")
|
|
|
|
class TestFlow(data_entry_flow.FlowHandler):
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
async def async_step_init(self, user_input=None):
|
|
|
|
return getattr(self, result_type)(**params)
|
|
|
|
|
|
|
|
with pytest.raises(data_entry_flow.UnknownStep):
|
|
|
|
await manager.async_init("test", context={"init_step": "init"})
|
|
|
|
|
|
|
|
assert manager.async_progress() == []
|
|
|
|
|
|
|
|
|
2023-02-21 08:27:13 +00:00
|
|
|
async def test_configure_raises_unknown_flow_if_not_in_progress(manager) -> None:
|
2021-10-22 17:19:49 +00:00
|
|
|
"""Test configure raises UnknownFlow if the flow is not in progress."""
|
|
|
|
with pytest.raises(data_entry_flow.UnknownFlow):
|
|
|
|
await manager.async_configure("wrong_flow_id")
|
|
|
|
|
|
|
|
|
2023-02-21 08:27:13 +00:00
|
|
|
async def test_abort_raises_unknown_flow_if_not_in_progress(manager) -> None:
|
2021-10-22 17:19:49 +00:00
|
|
|
"""Test abort raises UnknownFlow if the flow is not in progress."""
|
|
|
|
with pytest.raises(data_entry_flow.UnknownFlow):
|
|
|
|
await manager.async_abort("wrong_flow_id")
|
2022-03-16 21:14:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"menu_options",
|
|
|
|
(["target1", "target2"], {"target1": "Target 1", "target2": "Target 2"}),
|
|
|
|
)
|
2023-02-21 08:27:13 +00:00
|
|
|
async def test_show_menu(hass: HomeAssistant, manager, menu_options) -> None:
|
2022-03-16 21:14:50 +00:00
|
|
|
"""Test show menu."""
|
|
|
|
manager.hass = hass
|
|
|
|
|
|
|
|
@manager.mock_reg_handler("test")
|
|
|
|
class TestFlow(data_entry_flow.FlowHandler):
|
|
|
|
VERSION = 5
|
|
|
|
data = None
|
|
|
|
task_one_done = False
|
|
|
|
|
|
|
|
async def async_step_init(self, user_input=None):
|
|
|
|
return self.async_show_menu(
|
|
|
|
step_id="init",
|
|
|
|
menu_options=menu_options,
|
|
|
|
description_placeholders={"name": "Paulus"},
|
|
|
|
)
|
|
|
|
|
|
|
|
async def async_step_target1(self, user_input=None):
|
|
|
|
return self.async_show_form(step_id="target1")
|
|
|
|
|
|
|
|
async def async_step_target2(self, user_input=None):
|
|
|
|
return self.async_show_form(step_id="target2")
|
|
|
|
|
|
|
|
result = await manager.async_init("test")
|
2022-07-07 19:28:18 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.MENU
|
2022-03-16 21:14:50 +00:00
|
|
|
assert result["menu_options"] == menu_options
|
|
|
|
assert result["description_placeholders"] == {"name": "Paulus"}
|
|
|
|
assert len(manager.async_progress()) == 1
|
|
|
|
assert len(manager.async_progress_by_handler("test")) == 1
|
|
|
|
assert manager.async_get(result["flow_id"])["handler"] == "test"
|
|
|
|
|
|
|
|
# Mimic picking a step
|
|
|
|
result = await manager.async_configure(
|
|
|
|
result["flow_id"], {"next_step_id": "target1"}
|
|
|
|
)
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
2022-03-16 21:14:50 +00:00
|
|
|
assert result["step_id"] == "target1"
|
2023-02-17 20:51:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_find_flows_by_init_data_type(
|
|
|
|
manager: data_entry_flow.FlowManager,
|
|
|
|
) -> None:
|
|
|
|
"""Test we can find flows by init data type."""
|
|
|
|
|
|
|
|
@dataclasses.dataclass
|
|
|
|
class BluetoothDiscoveryData:
|
|
|
|
"""Bluetooth Discovery data."""
|
|
|
|
|
|
|
|
address: str
|
|
|
|
|
|
|
|
@dataclasses.dataclass
|
|
|
|
class WiFiDiscoveryData:
|
|
|
|
"""WiFi Discovery data."""
|
|
|
|
|
|
|
|
address: str
|
|
|
|
|
|
|
|
@manager.mock_reg_handler("test")
|
|
|
|
class TestFlow(data_entry_flow.FlowHandler):
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
async def async_step_first(self, user_input=None):
|
|
|
|
if user_input is not None:
|
|
|
|
return await self.async_step_second()
|
|
|
|
return self.async_show_form(step_id="first", data_schema=vol.Schema([str]))
|
|
|
|
|
|
|
|
async def async_step_second(self, user_input=None):
|
|
|
|
if user_input is not None:
|
|
|
|
return self.async_create_entry(
|
|
|
|
title="Test Entry",
|
|
|
|
data={"init": self.init_data, "user": user_input},
|
|
|
|
)
|
|
|
|
return self.async_show_form(step_id="second", data_schema=vol.Schema([str]))
|
|
|
|
|
|
|
|
bluetooth_data = BluetoothDiscoveryData("aa:bb:cc:dd:ee:ff")
|
|
|
|
wifi_data = WiFiDiscoveryData("host")
|
|
|
|
|
|
|
|
bluetooth_form = await manager.async_init(
|
|
|
|
"test", context={"init_step": "first"}, data=bluetooth_data
|
|
|
|
)
|
|
|
|
await manager.async_init("test", context={"init_step": "first"}, data=wifi_data)
|
|
|
|
|
|
|
|
assert (
|
|
|
|
len(
|
|
|
|
manager.async_progress_by_init_data_type(
|
|
|
|
BluetoothDiscoveryData, lambda data: True
|
|
|
|
)
|
|
|
|
)
|
|
|
|
) == 1
|
|
|
|
assert (
|
|
|
|
len(
|
|
|
|
manager.async_progress_by_init_data_type(
|
|
|
|
BluetoothDiscoveryData,
|
|
|
|
lambda data: bool(data.address == "aa:bb:cc:dd:ee:ff"),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
) == 1
|
|
|
|
assert (
|
|
|
|
len(
|
|
|
|
manager.async_progress_by_init_data_type(
|
|
|
|
BluetoothDiscoveryData, lambda data: bool(data.address == "not it")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
) == 0
|
|
|
|
|
|
|
|
wifi_flows = manager.async_progress_by_init_data_type(
|
|
|
|
WiFiDiscoveryData, lambda data: True
|
|
|
|
)
|
|
|
|
assert len(wifi_flows) == 1
|
|
|
|
|
|
|
|
bluetooth_result = await manager.async_configure(
|
|
|
|
bluetooth_form["flow_id"], ["SECOND-DATA"]
|
|
|
|
)
|
|
|
|
assert bluetooth_result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
|
|
|
assert len(manager.async_progress()) == 1
|
|
|
|
assert len(manager.mock_created_entries) == 1
|
|
|
|
result = manager.mock_created_entries[0]
|
|
|
|
assert result["handler"] == "test"
|
|
|
|
assert result["data"] == {"init": bluetooth_data, "user": ["SECOND-DATA"]}
|
|
|
|
|
|
|
|
bluetooth_flows = manager.async_progress_by_init_data_type(
|
|
|
|
BluetoothDiscoveryData, lambda data: True
|
|
|
|
)
|
|
|
|
assert len(bluetooth_flows) == 0
|
|
|
|
|
|
|
|
wifi_flows = manager.async_progress_by_init_data_type(
|
|
|
|
WiFiDiscoveryData, lambda data: True
|
|
|
|
)
|
|
|
|
assert len(wifi_flows) == 1
|
|
|
|
|
|
|
|
manager.async_abort(wifi_flows[0]["flow_id"])
|
|
|
|
|
|
|
|
wifi_flows = manager.async_progress_by_init_data_type(
|
|
|
|
WiFiDiscoveryData, lambda data: True
|
|
|
|
)
|
|
|
|
assert len(wifi_flows) == 0
|
|
|
|
assert len(manager.async_progress()) == 0
|
2023-12-23 10:25:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(("enum"), list(data_entry_flow.FlowResultType))
|
|
|
|
def test_deprecated_constants(
|
|
|
|
caplog: pytest.LogCaptureFixture,
|
|
|
|
enum: data_entry_flow.FlowResultType,
|
|
|
|
) -> None:
|
|
|
|
"""Test deprecated constants."""
|
|
|
|
import_and_test_deprecated_constant_enum(
|
|
|
|
caplog, data_entry_flow, enum, "RESULT_TYPE_", "2025.1"
|
|
|
|
)
|