Show notification when user configures Nest client_id/secret (#14970)

* Show notification when user configures Nest client_id/secret

* Lint
pull/13677/merge
Paulus Schoutsen 2018-06-15 15:19:58 -04:00 committed by GitHub
parent ac13a2736b
commit 8a777f6e78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 65 additions and 12 deletions

View File

@ -9,6 +9,7 @@ import logging
import voluptuous as vol
from homeassistant import data_entry_flow
from homeassistant.const import CONF_FILENAME, CONF_HOST
from homeassistant.helpers import aiohttp_client, config_validation as cv
@ -107,7 +108,7 @@ async def async_setup(hass, config):
# deadlock: creating a config entry will set up the component but the
# setup would block till the entry is created!
hass.async_add_job(hass.config_entries.flow.async_init(
DOMAIN, source='import', data={
DOMAIN, source=data_entry_flow.SOURCE_IMPORT, data={
'host': bridge_conf[CONF_HOST],
'path': bridge_conf[CONF_FILENAME],
}

View File

@ -6,7 +6,6 @@ https://home-assistant.io/components/nest/
"""
from concurrent.futures import ThreadPoolExecutor
import logging
import os.path
import socket
from datetime import datetime, timedelta
@ -102,12 +101,11 @@ async def async_setup(hass, config):
filename = config.get(CONF_FILENAME, NEST_CONFIG_FILE)
access_token_cache_file = hass.config.path(filename)
if await hass.async_add_job(os.path.isfile, access_token_cache_file):
hass.async_add_job(hass.config_entries.flow.async_init(
DOMAIN, source='import', data={
'nest_conf_path': access_token_cache_file,
}
))
hass.async_add_job(hass.config_entries.flow.async_init(
DOMAIN, source='import', data={
'nest_conf_path': access_token_cache_file,
}
))
# Store config to be used during entry setup
hass.data[DATA_NEST_CONFIG] = conf

View File

@ -2,6 +2,7 @@
import asyncio
from collections import OrderedDict
import logging
import os
import async_timeout
import voluptuous as vol
@ -135,9 +136,14 @@ class NestFlowHandler(data_entry_flow.FlowHandler):
if self.hass.config_entries.async_entries(DOMAIN):
return self.async_abort(reason='already_setup')
config_path = info['nest_conf_path']
if not await self.hass.async_add_job(os.path.isfile, config_path):
self.flow_impl = DOMAIN
return await self.async_step_link()
flow = self.hass.data[DATA_FLOW_IMPL][DOMAIN]
tokens = await self.hass.async_add_job(
load_json, info['nest_conf_path'])
tokens = await self.hass.async_add_job(load_json, config_path)
return self._entry_from_tokens(
'Nest (import from configuration.yaml)', flow, tokens)

View File

@ -146,7 +146,10 @@ ENTRY_STATE_NOT_LOADED = 'not_loaded'
ENTRY_STATE_FAILED_UNLOAD = 'failed_unload'
DISCOVERY_NOTIFICATION_ID = 'config_entry_discovery'
DISCOVERY_SOURCES = (data_entry_flow.SOURCE_DISCOVERY,)
DISCOVERY_SOURCES = (
data_entry_flow.SOURCE_DISCOVERY,
data_entry_flow.SOURCE_IMPORT,
)
class ConfigEntry:

View File

@ -9,6 +9,7 @@ _LOGGER = logging.getLogger(__name__)
SOURCE_USER = 'user'
SOURCE_DISCOVERY = 'discovery'
SOURCE_IMPORT = 'import'
RESULT_TYPE_FORM = 'form'
RESULT_TYPE_CREATE_ENTRY = 'create_entry'

View File

@ -3,7 +3,8 @@ import asyncio
from unittest.mock import Mock, patch
from homeassistant import data_entry_flow
from homeassistant.components.nest import config_flow
from homeassistant.setup import async_setup_component
from homeassistant.components.nest import config_flow, DOMAIN
from tests.common import mock_coro
@ -172,3 +173,46 @@ async def test_verify_code_exception(hass):
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['step_id'] == 'link'
assert result['errors'] == {'code': 'internal_error'}
async def test_step_import(hass):
"""Test that we trigger import when configuring with client."""
with patch('os.path.isfile', return_value=False):
assert await async_setup_component(hass, DOMAIN, {
DOMAIN: {
'client_id': 'bla',
'client_secret': 'bla',
},
})
await hass.async_block_till_done()
flow = hass.config_entries.flow.async_progress()[0]
result = await hass.config_entries.flow.async_configure(flow['flow_id'])
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['step_id'] == 'link'
async def test_step_import_with_token_cache(hass):
"""Test that we import existing token cache."""
with patch('os.path.isfile', return_value=True), \
patch('homeassistant.components.nest.config_flow.load_json',
return_value={'access_token': 'yo'}), \
patch('homeassistant.components.nest.async_setup_entry',
return_value=mock_coro(True)):
assert await async_setup_component(hass, DOMAIN, {
DOMAIN: {
'client_id': 'bla',
'client_secret': 'bla',
},
})
await hass.async_block_till_done()
entry = hass.config_entries.async_entries(DOMAIN)[0]
assert entry.data == {
'impl_domain': 'nest',
'tokens': {
'access_token': 'yo'
}
}