From 8a777f6e7865b6d383663774fc9a05ac56b0c163 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen <paulus@paulusschoutsen.nl> Date: Fri, 15 Jun 2018 15:19:58 -0400 Subject: [PATCH] Show notification when user configures Nest client_id/secret (#14970) * Show notification when user configures Nest client_id/secret * Lint --- homeassistant/components/hue/__init__.py | 3 +- homeassistant/components/nest/__init__.py | 12 +++-- homeassistant/components/nest/config_flow.py | 10 ++++- homeassistant/config_entries.py | 5 ++- homeassistant/data_entry_flow.py | 1 + tests/components/nest/test_config_flow.py | 46 +++++++++++++++++++- 6 files changed, 65 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/hue/__init__.py b/homeassistant/components/hue/__init__.py index 251d8cba095..dbd86ef31f3 100644 --- a/homeassistant/components/hue/__init__.py +++ b/homeassistant/components/hue/__init__.py @@ -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], } diff --git a/homeassistant/components/nest/__init__.py b/homeassistant/components/nest/__init__.py index 19d65061a89..bd74897371a 100644 --- a/homeassistant/components/nest/__init__.py +++ b/homeassistant/components/nest/__init__.py @@ -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 diff --git a/homeassistant/components/nest/config_flow.py b/homeassistant/components/nest/config_flow.py index ee83598235c..b5c095f34b8 100644 --- a/homeassistant/components/nest/config_flow.py +++ b/homeassistant/components/nest/config_flow.py @@ -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) diff --git a/homeassistant/config_entries.py b/homeassistant/config_entries.py index 504c0850a93..4fbbbb77b79 100644 --- a/homeassistant/config_entries.py +++ b/homeassistant/config_entries.py @@ -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: diff --git a/homeassistant/data_entry_flow.py b/homeassistant/data_entry_flow.py index 3b0f264fd40..e51ba4d9718 100644 --- a/homeassistant/data_entry_flow.py +++ b/homeassistant/data_entry_flow.py @@ -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' diff --git a/tests/components/nest/test_config_flow.py b/tests/components/nest/test_config_flow.py index 9692d5ce129..e80d18a9862 100644 --- a/tests/components/nest/test_config_flow.py +++ b/tests/components/nest/test_config_flow.py @@ -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' + } + } -- GitLab