Add support for an external step in config flow (#23782)

* Add support for an external step in config flow

* Types

* Lint
This commit is contained in:
Paulus Schoutsen 2019-05-10 05:33:50 -07:00 committed by Pascal Vizeli
parent 4214a354a7
commit 5888e32360
4 changed files with 129 additions and 11 deletions

View file

@ -951,3 +951,16 @@ def mock_entity_platform(hass, platform_path, module):
_LOGGER.info("Adding mock integration platform: %s", platform_path)
module_cache["{}.{}".format(platform_name, domain)] = module
def async_capture_events(hass, event_name):
"""Create a helper that captures events."""
events = []
@ha.callback
def capture_events(event):
events.append(event)
hass.bus.async_listen(event_name, capture_events)
return events

View file

@ -5,6 +5,8 @@ import voluptuous as vol
from homeassistant import data_entry_flow
from homeassistant.util.decorator import Registry
from tests.common import async_capture_events
@pytest.fixture
def manager():
@ -245,3 +247,57 @@ async def test_finish_callback_change_result_type(hass):
result = await manager.async_configure(result['flow_id'], {'count': 2})
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result['result'] == 2
async def test_external_step(hass, manager):
"""Test external step 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 not user_input:
return self.async_external_step(
step_id='init',
url='https://example.com',
)
self.data = user_input
return self.async_external_step_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')
assert result['type'] == data_entry_flow.RESULT_TYPE_EXTERNAL_STEP
assert len(manager.async_progress()) == 1
# Mimic external step
# Called by integrations: `hass.config_entries.flow.async_configure(…)`
result = await manager.async_configure(result['flow_id'], {
'title': 'Hello'
})
assert result['type'] == data_entry_flow.RESULT_TYPE_EXTERNAL_STEP_DONE
await hass.async_block_till_done()
assert len(events) == 1
assert events[0].data == {
'handler': 'test',
'flow_id': result['flow_id'],
'refresh': True
}
# Frontend refreshses the flow
result = await manager.async_configure(result['flow_id'])
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result['title'] == "Hello"