2018-04-13 14:14:53 +00:00
|
|
|
"""Test the flow classes."""
|
|
|
|
import pytest
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant import data_entry_flow
|
|
|
|
from homeassistant.util.decorator import Registry
|
|
|
|
|
2019-05-10 12:33:50 +00:00
|
|
|
from tests.common import async_capture_events
|
|
|
|
|
2018-04-13 14:14:53 +00:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def manager():
|
|
|
|
"""Return a flow manager."""
|
|
|
|
handlers = Registry()
|
|
|
|
entries = []
|
|
|
|
|
2018-08-09 11:24:14 +00:00
|
|
|
async def async_create_flow(handler_name, *, context, data):
|
2018-04-14 18:38:24 +00:00
|
|
|
handler = handlers.get(handler_name)
|
|
|
|
|
|
|
|
if handler is None:
|
|
|
|
raise data_entry_flow.UnknownHandler
|
|
|
|
|
2018-08-09 11:24:14 +00:00
|
|
|
flow = handler()
|
2019-07-31 19:25:30 +00:00
|
|
|
flow.init_step = context.get("init_step", "init")
|
|
|
|
flow.source = context.get("source")
|
2018-08-09 11:24:14 +00:00
|
|
|
return flow
|
2018-04-14 18:38:24 +00:00
|
|
|
|
2018-08-21 17:48:24 +00:00
|
|
|
async def async_add_entry(flow, result):
|
2019-07-31 19:25:30 +00:00
|
|
|
if result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY:
|
|
|
|
result["source"] = flow.context.get("source")
|
2018-05-24 18:24:14 +00:00
|
|
|
entries.append(result)
|
2018-08-21 17:48:24 +00:00
|
|
|
return result
|
2018-04-13 14:14:53 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
manager = data_entry_flow.FlowManager(None, async_create_flow, async_add_entry)
|
2018-04-13 14:14:53 +00:00
|
|
|
manager.mock_created_entries = entries
|
|
|
|
manager.mock_reg_handler = handlers.register
|
|
|
|
return manager
|
|
|
|
|
|
|
|
|
|
|
|
async def test_configure_reuses_handler_instance(manager):
|
|
|
|
"""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"
|
2018-04-13 14:14:53 +00:00
|
|
|
assert len(manager.async_progress()) == 1
|
|
|
|
assert len(manager.mock_created_entries) == 0
|
|
|
|
|
|
|
|
|
|
|
|
async def test_configure_two_steps(manager):
|
|
|
|
"""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:
|
|
|
|
self.init_data = user_input
|
|
|
|
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
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
form = await manager.async_init("test", context={"init_step": "first"})
|
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"], ["INIT-DATA"])
|
|
|
|
form = await manager.async_configure(form["flow_id"], ["SECOND-DATA"])
|
|
|
|
assert form["type"] == data_entry_flow.RESULT_TYPE_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
|
|
|
|
|
|
|
|
|
|
|
async def test_show_form(manager):
|
|
|
|
"""Test that abort removes the flow from progress."""
|
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")
|
|
|
|
assert form["type"] == "form"
|
|
|
|
assert form["data_schema"] is schema
|
|
|
|
assert form["errors"] == {"username": "Should be unique."}
|
2018-04-13 14:14:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_abort_removes_instance(manager):
|
|
|
|
"""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
|
|
|
|
|
|
|
|
|
|
|
|
async def test_create_saves_data(manager):
|
|
|
|
"""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
|
|
|
|
|
|
|
|
|
|
|
async def test_discovery_init_flow(manager):
|
|
|
|
"""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
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await manager.async_init("test", context={"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
|
|
|
|
assert entry["source"] == "discovery"
|
2018-08-21 17:48:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_finish_callback_change_result_type(hass):
|
|
|
|
"""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
|
|
|
|
|
|
|
async def async_create_flow(handler_name, *, context, data):
|
|
|
|
"""Create a test flow."""
|
|
|
|
return TestFlow()
|
|
|
|
|
|
|
|
async def async_finish_flow(flow, result):
|
|
|
|
"""Redirect to init form if count <= 1."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY:
|
|
|
|
if result["data"] is None or result["data"].get("count", 0) <= 1:
|
2018-08-21 17:48:24 +00:00
|
|
|
return flow.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
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
result["result"] = result["data"]["count"]
|
2018-08-21 17:48:24 +00:00
|
|
|
return result
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
manager = data_entry_flow.FlowManager(hass, async_create_flow, async_finish_flow)
|
2018-08-21 17:48:24 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
result = await manager.async_init("test")
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
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})
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
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})
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
|
|
assert result["result"] == 2
|
2019-05-10 12:33:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_external_step(hass, manager):
|
|
|
|
"""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")
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_EXTERNAL_STEP
|
2019-05-10 12:33:50 +00:00
|
|
|
assert len(manager.async_progress()) == 1
|
|
|
|
|
|
|
|
# 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"})
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_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
|
|
|
}
|
|
|
|
|
|
|
|
# Frontend refreshses the flow
|
2019-07-31 19:25:30 +00:00
|
|
|
result = await manager.async_configure(result["flow_id"])
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
|
|
assert result["title"] == "Hello"
|