Config entry options (#18929)

Add support for options flow for config entries
This commit is contained in:
Robert Svensson authored and GitHub committed 2019-02-22 17:59:43 +01:00
1 parent caa3b123ae
commit d9712027e8
5 files changed
+365 -24

No files matched your search

+162 -14
View File
@@ -7,8 +7,9 @@ from unittest.mock import patch
import pytest
import voluptuous as vol
from homeassistant import config_entries as core_ce
from homeassistant import config_entries as core_ce, data_entry_flow
from homeassistant.config_entries import HANDLERS
from homeassistant.core import callback
from homeassistant.setup import async_setup_component
from homeassistant.components.config import config_entries
from homeassistant.loader import set_component
@@ -30,25 +31,37 @@ def client(hass, hass_client):
yield hass.loop.run_until_complete(hass_client())
@asyncio.coroutine
def test_get_entries(hass, client):
async def test_get_entries(hass, client):
"""Test get entries."""
MockConfigEntry(
domain='comp',
title='Test 1',
source='bla',
connection_class=core_ce.CONN_CLASS_LOCAL_POLL,
domain='comp',
title='Test 1',
source='bla',
connection_class=core_ce.CONN_CLASS_LOCAL_POLL,
).add_to_hass(hass)
MockConfigEntry(
domain='comp2',
title='Test 2',
source='bla2',
state=core_ce.ENTRY_STATE_LOADED,
connection_class=core_ce.CONN_CLASS_ASSUMED,
domain='comp2',
title='Test 2',
source='bla2',
state=core_ce.ENTRY_STATE_LOADED,
connection_class=core_ce.CONN_CLASS_ASSUMED,
).add_to_hass(hass)
resp = yield from client.get('/api/config/config_entries/entry')
class CompConfigFlow:
@staticmethod
@callback
def async_get_options_flow(config, options):
pass
HANDLERS['comp'] = CompConfigFlow()
class Comp2ConfigFlow:
def __init__(self):
pass
HANDLERS['comp2'] = Comp2ConfigFlow()
resp = await client.get('/api/config/config_entries/entry')
assert resp.status == 200
data = yield from resp.json()
data = await resp.json()
for entry in data:
entry.pop('entry_id')
assert data == [
@@ -58,6 +71,7 @@ def test_get_entries(hass, client):
'source': 'bla',
'state': 'not_loaded',
'connection_class': 'local_poll',
'supports_options': True,
},
{
'domain': 'comp2',
@@ -65,6 +79,7 @@ def test_get_entries(hass, client):
'source': 'bla2',
'state': 'loaded',
'connection_class': 'assumed',
'supports_options': False,
},
]
@@ -467,3 +482,136 @@ async def test_get_progress_flow_unauth(hass, client, hass_admin_user):
'/api/config/config_entries/flow/{}'.format(data['flow_id']))
assert resp2.status == 401
async def test_options_flow(hass, client):
"""Test we can change options."""
class TestFlow(core_ce.ConfigFlow):
@staticmethod
@callback
def async_get_options_flow(config, options):
class OptionsFlowHandler(data_entry_flow.FlowHandler):
def __init__(self, config, options):
self.config = config
self.options = options
async def async_step_init(self, user_input=None):
schema = OrderedDict()
schema[vol.Required('enabled')] = bool
return self.async_show_form(
step_id='user',
data_schema=schema,
description_placeholders={
'enabled': 'Set to true to be true',
}
)
return OptionsFlowHandler(config, options)
MockConfigEntry(
domain='test',
entry_id='test1',
source='bla',
connection_class=core_ce.CONN_CLASS_LOCAL_POLL,
).add_to_hass(hass)
entry = hass.config_entries._entries[0]
with patch.dict(HANDLERS, {'test': TestFlow}):
url = '/api/config/config_entries/entry/option/flow'
resp = await client.post(url, json={'handler': entry.entry_id})
assert resp.status == 200
data = await resp.json()
data.pop('flow_id')
assert data == {
'type': 'form',
'handler': 'test1',
'step_id': 'user',
'data_schema': [
{
'name': 'enabled',
'required': True,
'type': 'boolean'
},
],
'description_placeholders': {
'enabled': 'Set to true to be true',
},
'errors': None
}
async def test_two_step_options_flow(hass, client):
"""Test we can finish a two step options flow."""
set_component(
hass, 'test',
MockModule('test', async_setup_entry=mock_coro_func(True)))
class TestFlow(core_ce.ConfigFlow):
@staticmethod
@callback
def async_get_options_flow(config, options):
class OptionsFlowHandler(data_entry_flow.FlowHandler):
def __init__(self, config, options):
self.config = config
self.options = options
async def async_step_init(self, user_input=None):
return self.async_show_form(
step_id='finish',
data_schema=vol.Schema({
'enabled': bool
})
)
async def async_step_finish(self, user_input=None):
return self.async_create_entry(
title='Enable disable',
data=user_input
)
return OptionsFlowHandler(config, options)
MockConfigEntry(
domain='test',
entry_id='test1',
source='bla',
connection_class=core_ce.CONN_CLASS_LOCAL_POLL,
).add_to_hass(hass)
entry = hass.config_entries._entries[0]
with patch.dict(HANDLERS, {'test': TestFlow}):
url = '/api/config/config_entries/entry/option/flow'
resp = await client.post(url, json={'handler': entry.entry_id})
assert resp.status == 200
data = await resp.json()
flow_id = data.pop('flow_id')
assert data == {
'type': 'form',
'handler': 'test1',
'step_id': 'finish',
'data_schema': [
{
'name': 'enabled',
'type': 'boolean'
}
],
'description_placeholders': None,
'errors': None
}
with patch.dict(HANDLERS, {'test': TestFlow}):
resp = await client.post(
'/api/config/config_entries/options/flow/{}'.format(flow_id),
json={'enabled': True})
assert resp.status == 200
data = await resp.json()
data.pop('flow_id')
assert data == {
'handler': 'test1',
'type': 'create_entry',
'title': 'Enable disable',
'version': 1,
'description': None,
'description_placeholders': None,
}