Initial support for Config Entries (#12079)
* Introduce Config Entries * Rebase fail * Address comments * Address more comments * RequestDataValidator moved
This commit is contained in:
1 parent
2053c8a908
commit
b3a47722f0
15 files changed
+1622
-29
No files matched your search
@@ -3,7 +3,6 @@ import unittest
|
||||
|
||||
from homeassistant.const import (STATE_ON, STATE_OFF)
|
||||
from homeassistant.components.binary_sensor import command_line
|
||||
from homeassistant import setup
|
||||
from homeassistant.helpers import template
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
@@ -42,16 +41,6 @@ class TestCommandSensorBinarySensor(unittest.TestCase):
|
||||
self.assertEqual('Test', entity.name)
|
||||
self.assertEqual(STATE_ON, entity.state)
|
||||
|
||||
def test_setup_bad_config(self):
|
||||
"""Test the setup with a bad configuration."""
|
||||
config = {'name': 'test',
|
||||
'platform': 'not_command_line',
|
||||
}
|
||||
|
||||
self.assertFalse(setup.setup_component(self.hass, 'test', {
|
||||
'command_line': config,
|
||||
}))
|
||||
|
||||
def test_template(self):
|
||||
"""Test setting the state with a template."""
|
||||
data = command_line.CommandSensorData(self.hass, 'echo 10')
|
||||
|
||||
@@ -0,0 +1,317 @@
|
||||
"""Test config entries API."""
|
||||
|
||||
import asyncio
|
||||
from collections import OrderedDict
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries as core_ce
|
||||
from homeassistant.config_entries import ConfigFlowHandler, HANDLERS
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.components.config import config_entries
|
||||
from homeassistant.loader import set_component
|
||||
|
||||
from tests.common import MockConfigEntry, MockModule, mock_coro_func
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(hass, test_client):
|
||||
"""Fixture that can interact with the config manager API."""
|
||||
hass.loop.run_until_complete(async_setup_component(hass, 'http', {}))
|
||||
hass.loop.run_until_complete(config_entries.async_setup(hass))
|
||||
yield hass.loop.run_until_complete(test_client(hass.http.app))
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_get_entries(hass, client):
|
||||
"""Test get entries."""
|
||||
MockConfigEntry(
|
||||
domain='comp',
|
||||
title='Test 1',
|
||||
source='bla'
|
||||
).add_to_hass(hass)
|
||||
MockConfigEntry(
|
||||
domain='comp2',
|
||||
title='Test 2',
|
||||
source='bla2',
|
||||
state=core_ce.ENTRY_STATE_LOADED,
|
||||
).add_to_hass(hass)
|
||||
resp = yield from client.get('/api/config/config_entries/entry')
|
||||
assert resp.status == 200
|
||||
data = yield from resp.json()
|
||||
for entry in data:
|
||||
entry.pop('entry_id')
|
||||
assert data == [
|
||||
{
|
||||
'domain': 'comp',
|
||||
'title': 'Test 1',
|
||||
'source': 'bla',
|
||||
'state': 'not_loaded'
|
||||
},
|
||||
{
|
||||
'domain': 'comp2',
|
||||
'title': 'Test 2',
|
||||
'source': 'bla2',
|
||||
'state': 'loaded',
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_remove_entry(hass, client):
|
||||
"""Test removing an entry via the API."""
|
||||
entry = MockConfigEntry(domain='demo')
|
||||
entry.add_to_hass(hass)
|
||||
resp = yield from client.delete(
|
||||
'/api/config/config_entries/entry/{}'.format(entry.entry_id))
|
||||
assert resp.status == 200
|
||||
data = yield from resp.json()
|
||||
assert data == {
|
||||
'require_restart': True
|
||||
}
|
||||
assert len(hass.config_entries.async_entries()) == 0
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_available_flows(hass, client):
|
||||
"""Test querying the available flows."""
|
||||
with patch.object(core_ce, 'FLOWS', ['hello', 'world']):
|
||||
resp = yield from client.get(
|
||||
'/api/config/config_entries/flow_handlers')
|
||||
assert resp.status == 200
|
||||
data = yield from resp.json()
|
||||
assert data == ['hello', 'world']
|
||||
|
||||
|
||||
############################
|
||||
# FLOW MANAGER API TESTS #
|
||||
############################
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_initialize_flow(hass, client):
|
||||
"""Test we can initialize a flow."""
|
||||
class TestFlow(ConfigFlowHandler):
|
||||
@asyncio.coroutine
|
||||
def async_step_init(self, user_input=None):
|
||||
schema = OrderedDict()
|
||||
schema[vol.Required('username')] = str
|
||||
schema[vol.Required('password')] = str
|
||||
|
||||
return self.async_show_form(
|
||||
title='test-title',
|
||||
step_id='init',
|
||||
description='test-description',
|
||||
data_schema=schema,
|
||||
errors={
|
||||
'username': 'Should be unique.'
|
||||
}
|
||||
)
|
||||
|
||||
with patch.dict(HANDLERS, {'test': TestFlow}):
|
||||
resp = yield from client.post('/api/config/config_entries/flow',
|
||||
json={'domain': 'test'})
|
||||
|
||||
assert resp.status == 200
|
||||
data = yield from resp.json()
|
||||
|
||||
data.pop('flow_id')
|
||||
|
||||
assert data == {
|
||||
'type': 'form',
|
||||
'title': 'test-title',
|
||||
'description': 'test-description',
|
||||
'data_schema': [
|
||||
{
|
||||
'name': 'username',
|
||||
'required': True,
|
||||
'type': 'string'
|
||||
},
|
||||
{
|
||||
'name': 'password',
|
||||
'required': True,
|
||||
'type': 'string'
|
||||
}
|
||||
],
|
||||
'errors': {
|
||||
'username': 'Should be unique.'
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_abort(hass, client):
|
||||
"""Test a flow that aborts."""
|
||||
class TestFlow(ConfigFlowHandler):
|
||||
@asyncio.coroutine
|
||||
def async_step_init(self, user_input=None):
|
||||
return self.async_abort(reason='bla')
|
||||
|
||||
with patch.dict(HANDLERS, {'test': TestFlow}):
|
||||
resp = yield from client.post('/api/config/config_entries/flow',
|
||||
json={'domain': 'test'})
|
||||
|
||||
assert resp.status == 200
|
||||
data = yield from resp.json()
|
||||
data.pop('flow_id')
|
||||
assert data == {
|
||||
'reason': 'bla',
|
||||
'type': 'abort'
|
||||
}
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_create_account(hass, client):
|
||||
"""Test a flow that creates an account."""
|
||||
set_component(
|
||||
'test', MockModule('test', async_setup_entry=mock_coro_func(True)))
|
||||
|
||||
class TestFlow(ConfigFlowHandler):
|
||||
VERSION = 1
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_step_init(self, user_input=None):
|
||||
return self.async_create_entry(
|
||||
title='Test Entry',
|
||||
data={'secret': 'account_token'}
|
||||
)
|
||||
|
||||
with patch.dict(HANDLERS, {'test': TestFlow}):
|
||||
resp = yield from client.post('/api/config/config_entries/flow',
|
||||
json={'domain': 'test'})
|
||||
|
||||
assert resp.status == 200
|
||||
data = yield from resp.json()
|
||||
data.pop('flow_id')
|
||||
assert data == {
|
||||
'title': 'Test Entry',
|
||||
'type': 'create_entry'
|
||||
}
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_two_step_flow(hass, client):
|
||||
"""Test we can finish a two step flow."""
|
||||
set_component(
|
||||
'test', MockModule('test', async_setup_entry=mock_coro_func(True)))
|
||||
|
||||
class TestFlow(ConfigFlowHandler):
|
||||
VERSION = 1
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_step_init(self, user_input=None):
|
||||
return self.async_show_form(
|
||||
title='test-title',
|
||||
step_id='account',
|
||||
data_schema=vol.Schema({
|
||||
'user_title': str
|
||||
}))
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_step_account(self, user_input=None):
|
||||
return self.async_create_entry(
|
||||
title=user_input['user_title'],
|
||||
data={'secret': 'account_token'}
|
||||
)
|
||||
|
||||
with patch.dict(HANDLERS, {'test': TestFlow}):
|
||||
resp = yield from client.post('/api/config/config_entries/flow',
|
||||
json={'domain': 'test'})
|
||||
assert resp.status == 200
|
||||
data = yield from resp.json()
|
||||
flow_id = data.pop('flow_id')
|
||||
assert data == {
|
||||
'type': 'form',
|
||||
'title': 'test-title',
|
||||
'description': None,
|
||||
'data_schema': [
|
||||
{
|
||||
'name': 'user_title',
|
||||
'type': 'string'
|
||||
}
|
||||
],
|
||||
'errors': None
|
||||
}
|
||||
|
||||
with patch.dict(HANDLERS, {'test': TestFlow}):
|
||||
resp = yield from client.post(
|
||||
'/api/config/config_entries/flow/{}'.format(flow_id),
|
||||
json={'user_title': 'user-title'})
|
||||
assert resp.status == 200
|
||||
data = yield from resp.json()
|
||||
data.pop('flow_id')
|
||||
assert data == {
|
||||
'type': 'create_entry',
|
||||
'title': 'user-title',
|
||||
}
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_get_progress_index(hass, client):
|
||||
"""Test querying for the flows that are in progress."""
|
||||
class TestFlow(ConfigFlowHandler):
|
||||
VERSION = 5
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_step_hassio(self, info):
|
||||
return (yield from self.async_step_account())
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_step_account(self, user_input=None):
|
||||
return self.async_show_form(
|
||||
step_id='account',
|
||||
title='Finish setup'
|
||||
)
|
||||
|
||||
with patch.dict(HANDLERS, {'test': TestFlow}):
|
||||
form = yield from hass.config_entries.flow.async_init(
|
||||
'test', source='hassio')
|
||||
|
||||
resp = yield from client.get('/api/config/config_entries/flow')
|
||||
assert resp.status == 200
|
||||
data = yield from resp.json()
|
||||
assert data == [
|
||||
{
|
||||
'flow_id': form['flow_id'],
|
||||
'domain': 'test',
|
||||
'source': 'hassio'
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_get_progress_flow(hass, client):
|
||||
"""Test we can query the API for same result as we get from init a flow."""
|
||||
class TestFlow(ConfigFlowHandler):
|
||||
@asyncio.coroutine
|
||||
def async_step_init(self, user_input=None):
|
||||
schema = OrderedDict()
|
||||
schema[vol.Required('username')] = str
|
||||
schema[vol.Required('password')] = str
|
||||
|
||||
return self.async_show_form(
|
||||
title='test-title',
|
||||
step_id='init',
|
||||
description='test-description',
|
||||
data_schema=schema,
|
||||
errors={
|
||||
'username': 'Should be unique.'
|
||||
}
|
||||
)
|
||||
|
||||
with patch.dict(HANDLERS, {'test': TestFlow}):
|
||||
resp = yield from client.post('/api/config/config_entries/flow',
|
||||
json={'domain': 'test'})
|
||||
|
||||
assert resp.status == 200
|
||||
data = yield from resp.json()
|
||||
|
||||
resp2 = yield from client.get(
|
||||
'/api/config/config_entries/flow/{}'.format(data['flow_id']))
|
||||
|
||||
assert resp2.status == 200
|
||||
data2 = yield from resp2.json()
|
||||
|
||||
assert data == data2
|
||||
@@ -3,7 +3,6 @@ import unittest
|
||||
|
||||
from homeassistant.helpers.template import Template
|
||||
from homeassistant.components.sensor import command_line
|
||||
from homeassistant import setup
|
||||
from tests.common import get_test_home_assistant
|
||||
|
||||
|
||||
@@ -40,16 +39,6 @@ class TestCommandSensorSensor(unittest.TestCase):
|
||||
self.assertEqual('in', entity.unit_of_measurement)
|
||||
self.assertEqual('5', entity.state)
|
||||
|
||||
def test_setup_bad_config(self):
|
||||
"""Test setup with a bad configuration."""
|
||||
config = {'name': 'test',
|
||||
'platform': 'not_command_line',
|
||||
}
|
||||
|
||||
self.assertFalse(setup.setup_component(self.hass, 'test', {
|
||||
'command_line': config,
|
||||
}))
|
||||
|
||||
def test_template(self):
|
||||
"""Test command sensor with template."""
|
||||
data = command_line.CommandSensorData(self.hass, 'echo 50')
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
"""Test the config entry example component."""
|
||||
import asyncio
|
||||
|
||||
from homeassistant import config_entries
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_flow_works(hass):
|
||||
"""Test that the config flow works."""
|
||||
result = yield from hass.config_entries.flow.async_init(
|
||||
'config_entry_example')
|
||||
|
||||
assert result['type'] == config_entries.RESULT_TYPE_FORM
|
||||
|
||||
result = yield from hass.config_entries.flow.async_configure(
|
||||
result['flow_id'], {
|
||||
'object_id': 'bla'
|
||||
})
|
||||
|
||||
assert result['type'] == config_entries.RESULT_TYPE_FORM
|
||||
|
||||
result = yield from hass.config_entries.flow.async_configure(
|
||||
result['flow_id'], {
|
||||
'name': 'Hello'
|
||||
})
|
||||
|
||||
assert result['type'] == config_entries.RESULT_TYPE_CREATE_ENTRY
|
||||
state = hass.states.get('config_entry_example.bla')
|
||||
assert state is not None
|
||||
assert state.name == 'Hello'
|
||||
assert 'config_entry_example' in hass.config.components
|
||||
assert len(hass.config_entries.async_entries()) == 1
|
||||
|
||||
# Test removing entry.
|
||||
entry = hass.config_entries.async_entries()[0]
|
||||
yield from hass.config_entries.async_remove(entry.entry_id)
|
||||
state = hass.states.get('config_entry_example.bla')
|
||||
assert state is None
|
||||
Reference in new issue
Block a user