Nest config flow (#14921)

* Move nest to dir based component

* Add config flow for Nest

* Load Nest platforms via config entry

* Add tests for Nest config flow

* Import existing access tokens as config entries

* Lint

* Update coverage

* Update translation

* Fix tests

* Address strings

* Use python-nest token resolution

* Lint

* Do not do I/O inside constructor

* Lint

* Update test requirements
This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2018-06-13 11:14:52 -04:00
1 parent d549e26a9b
commit e014a84215
21 files changed
+666 -153

No files matched your search

@@ -110,6 +110,9 @@ def test_initialize_flow(hass, client):
return self.async_show_form(
step_id='init',
data_schema=schema,
description_placeholders={
'url': 'https://example.com',
},
errors={
'username': 'Should be unique.'
}
@@ -140,6 +143,9 @@ def test_initialize_flow(hass, client):
'type': 'string'
}
],
'description_placeholders': {
'url': 'https://example.com',
},
'errors': {
'username': 'Should be unique.'
}
@@ -242,6 +248,7 @@ def test_two_step_flow(hass, client):
'type': 'string'
}
],
'description_placeholders': None,
'errors': None
}
+1
View File
@@ -0,0 +1 @@
"""Tests for the Nest component."""
+174
View File
@@ -0,0 +1,174 @@
"""Tests for the Nest config flow."""
import asyncio
from unittest.mock import Mock, patch
from homeassistant import data_entry_flow
from homeassistant.components.nest import config_flow
from tests.common import mock_coro
async def test_abort_if_no_implementation_registered(hass):
"""Test we abort if no implementation is registered."""
flow = config_flow.NestFlowHandler()
flow.hass = hass
result = await flow.async_step_init()
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
assert result['reason'] == 'no_flows'
async def test_abort_if_already_setup(hass):
"""Test we abort if Nest is already setup."""
flow = config_flow.NestFlowHandler()
flow.hass = hass
with patch.object(hass.config_entries, 'async_entries', return_value=[{}]):
result = await flow.async_step_init()
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
assert result['reason'] == 'already_setup'
async def test_full_flow_implementation(hass):
"""Test registering an implementation and finishing flow works."""
gen_authorize_url = Mock(return_value=mock_coro('https://example.com'))
convert_code = Mock(return_value=mock_coro({'access_token': 'yoo'}))
config_flow.register_flow_implementation(
hass, 'test', 'Test', gen_authorize_url, convert_code)
config_flow.register_flow_implementation(
hass, 'test-other', 'Test Other', None, None)
flow = config_flow.NestFlowHandler()
flow.hass = hass
result = await flow.async_step_init()
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['step_id'] == 'init'
result = await flow.async_step_init({'flow_impl': 'test'})
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['step_id'] == 'link'
assert result['description_placeholders'] == {
'url': 'https://example.com',
}
result = await flow.async_step_link({'code': '123ABC'})
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result['data']['tokens'] == {'access_token': 'yoo'}
assert result['data']['impl_domain'] == 'test'
assert result['title'] == 'Nest (via Test)'
async def test_not_pick_implementation_if_only_one(hass):
"""Test we allow picking implementation if we have two."""
gen_authorize_url = Mock(return_value=mock_coro('https://example.com'))
config_flow.register_flow_implementation(
hass, 'test', 'Test', gen_authorize_url, None)
flow = config_flow.NestFlowHandler()
flow.hass = hass
result = await flow.async_step_init()
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['step_id'] == 'link'
async def test_abort_if_timeout_generating_auth_url(hass):
"""Test we abort if generating authorize url fails."""
gen_authorize_url = Mock(side_effect=asyncio.TimeoutError)
config_flow.register_flow_implementation(
hass, 'test', 'Test', gen_authorize_url, None)
flow = config_flow.NestFlowHandler()
flow.hass = hass
result = await flow.async_step_init()
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
assert result['reason'] == 'authorize_url_timeout'
async def test_abort_if_exception_generating_auth_url(hass):
"""Test we abort if generating authorize url blows up."""
gen_authorize_url = Mock(side_effect=ValueError)
config_flow.register_flow_implementation(
hass, 'test', 'Test', gen_authorize_url, None)
flow = config_flow.NestFlowHandler()
flow.hass = hass
result = await flow.async_step_init()
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
assert result['reason'] == 'authorize_url_fail'
async def test_verify_code_timeout(hass):
"""Test verify code timing out."""
gen_authorize_url = Mock(return_value=mock_coro('https://example.com'))
convert_code = Mock(side_effect=asyncio.TimeoutError)
config_flow.register_flow_implementation(
hass, 'test', 'Test', gen_authorize_url, convert_code)
flow = config_flow.NestFlowHandler()
flow.hass = hass
result = await flow.async_step_init()
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['step_id'] == 'link'
result = await flow.async_step_link({'code': '123ABC'})
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['step_id'] == 'link'
assert result['errors'] == {'code': 'timeout'}
async def test_verify_code_invalid(hass):
"""Test verify code invalid."""
gen_authorize_url = Mock(return_value=mock_coro('https://example.com'))
convert_code = Mock(side_effect=config_flow.CodeInvalid)
config_flow.register_flow_implementation(
hass, 'test', 'Test', gen_authorize_url, convert_code)
flow = config_flow.NestFlowHandler()
flow.hass = hass
result = await flow.async_step_init()
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['step_id'] == 'link'
result = await flow.async_step_link({'code': '123ABC'})
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['step_id'] == 'link'
assert result['errors'] == {'code': 'invalid_code'}
async def test_verify_code_unknown_error(hass):
"""Test verify code unknown error."""
gen_authorize_url = Mock(return_value=mock_coro('https://example.com'))
convert_code = Mock(side_effect=config_flow.NestAuthError)
config_flow.register_flow_implementation(
hass, 'test', 'Test', gen_authorize_url, convert_code)
flow = config_flow.NestFlowHandler()
flow.hass = hass
result = await flow.async_step_init()
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['step_id'] == 'link'
result = await flow.async_step_link({'code': '123ABC'})
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['step_id'] == 'link'
assert result['errors'] == {'code': 'unknown'}
async def test_verify_code_exception(hass):
"""Test verify code blows up."""
gen_authorize_url = Mock(return_value=mock_coro('https://example.com'))
convert_code = Mock(side_effect=ValueError)
config_flow.register_flow_implementation(
hass, 'test', 'Test', gen_authorize_url, convert_code)
flow = config_flow.NestFlowHandler()
flow.hass = hass
result = await flow.async_step_init()
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['step_id'] == 'link'
result = await flow.async_step_link({'code': '123ABC'})
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['step_id'] == 'link'
assert result['errors'] == {'code': 'internal_error'}
+51
View File
@@ -0,0 +1,51 @@
"""Test Nest local auth."""
from homeassistant.components.nest import const, config_flow, local_auth
from urllib.parse import parse_qsl
import pytest
import requests_mock as rmock
@pytest.fixture
def registered_flow(hass):
"""Mock a registered flow."""
local_auth.initialize(hass, 'TEST-CLIENT-ID', 'TEST-CLIENT-SECRET')
return hass.data[config_flow.DATA_FLOW_IMPL][const.DOMAIN]
async def test_generate_auth_url(registered_flow):
"""Test generating an auth url.
Mainly testing that it doesn't blow up.
"""
url = await registered_flow['gen_authorize_url']('TEST-FLOW-ID')
assert url is not None
async def test_convert_code(requests_mock, registered_flow):
"""Test converting a code."""
from nest.nest import ACCESS_TOKEN_URL
def token_matcher(request):
"""Match a fetch token request."""
if request.url != ACCESS_TOKEN_URL:
return None
assert dict(parse_qsl(request.text)) == {
'client_id': 'TEST-CLIENT-ID',
'client_secret': 'TEST-CLIENT-SECRET',
'code': 'TEST-CODE',
'grant_type': 'authorization_code'
}
return rmock.create_response(request, json={
'access_token': 'TEST-ACCESS-TOKEN'
})
requests_mock.add_matcher(token_matcher)
tokens = await registered_flow['convert_code']('TEST-CODE')
assert tokens == {
'access_token': 'TEST-ACCESS-TOKEN'
}