From 907907e6f9ff2ff2c94a94ee39ff7a41c7e26f1b Mon Sep 17 00:00:00 2001 From: Aaron Bach Date: Fri, 13 Nov 2020 16:04:34 -0700 Subject: [PATCH] Revert "Remove YAML config for Tile (#43064)" (#43199) This reverts commit 19f48e180c7e6a34bd93d7a849147a8de491771c. --- homeassistant/components/tile/config_flow.py | 28 +++++++++----- .../components/tile/device_tracker.py | 26 ++++++++++++- tests/components/tile/test_config_flow.py | 38 +++++++++++++++++-- 3 files changed, 77 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/tile/config_flow.py b/homeassistant/components/tile/config_flow.py index 932c948defe..87f58193e9d 100644 --- a/homeassistant/components/tile/config_flow.py +++ b/homeassistant/components/tile/config_flow.py @@ -9,10 +9,6 @@ from homeassistant.helpers import aiohttp_client from .const import DOMAIN # pylint: disable=unused-import -DATA_SCHEMA = vol.Schema( - {vol.Required(CONF_USERNAME): str, vol.Required(CONF_PASSWORD): str} -) - class TileFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): """Handle a Tile config flow.""" @@ -20,10 +16,26 @@ class TileFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): VERSION = 1 CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL + def __init__(self): + """Initialize the config flow.""" + self.data_schema = vol.Schema( + {vol.Required(CONF_USERNAME): str, vol.Required(CONF_PASSWORD): str} + ) + + async def _show_form(self, errors=None): + """Show the form to the user.""" + return self.async_show_form( + step_id="user", data_schema=self.data_schema, errors=errors or {} + ) + + async def async_step_import(self, import_config): + """Import a config entry from configuration.yaml.""" + return await self.async_step_user(import_config) + async def async_step_user(self, user_input=None): """Handle the start of the config flow.""" if not user_input: - return self.async_show_form(step_id="user", data_schema=DATA_SCHEMA) + return await self._show_form() await self.async_set_unique_id(user_input[CONF_USERNAME]) self._abort_if_unique_id_configured() @@ -35,10 +47,6 @@ class TileFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): user_input[CONF_USERNAME], user_input[CONF_PASSWORD], session=session ) except TileError: - return self.async_show_form( - step_id="user", - data_schema=DATA_SCHEMA, - errors={"base": "invalid_auth"}, - ) + return await self._show_form({"base": "invalid_auth"}) return self.async_create_entry(title=user_input[CONF_USERNAME], data=user_input) diff --git a/homeassistant/components/tile/device_tracker.py b/homeassistant/components/tile/device_tracker.py index 286ffa98869..5b0065b2c4e 100644 --- a/homeassistant/components/tile/device_tracker.py +++ b/homeassistant/components/tile/device_tracker.py @@ -3,6 +3,8 @@ import logging from homeassistant.components.device_tracker.config_entry import TrackerEntity from homeassistant.components.device_tracker.const import SOURCE_TYPE_GPS +from homeassistant.config_entries import SOURCE_IMPORT +from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.core import callback from . import DATA_COORDINATOR, DOMAIN, TileEntity @@ -26,10 +28,32 @@ async def async_setup_entry(hass, config_entry, async_add_entities): [ TileDeviceTracker(coordinator, tile_uuid, tile) for tile_uuid, tile in coordinator.data.items() - ] + ], + True, ) +async def async_setup_scanner(hass, config, async_see, discovery_info=None): + """Detect a legacy configuration and import it.""" + hass.async_create_task( + hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_IMPORT}, + data={ + CONF_USERNAME: config[CONF_USERNAME], + CONF_PASSWORD: config[CONF_PASSWORD], + }, + ) + ) + + _LOGGER.info( + "Your Tile configuration has been imported into the UI; " + "please remove it from configuration.yaml" + ) + + return True + + class TileDeviceTracker(TileEntity, TrackerEntity): """Representation of a network infrastructure device.""" diff --git a/tests/components/tile/test_config_flow.py b/tests/components/tile/test_config_flow.py index edb95cc5b05..b9d4799dc2f 100644 --- a/tests/components/tile/test_config_flow.py +++ b/tests/components/tile/test_config_flow.py @@ -3,7 +3,7 @@ from pytile.errors import TileError from homeassistant import data_entry_flow from homeassistant.components.tile import DOMAIN -from homeassistant.config_entries import SOURCE_USER +from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from tests.async_mock import patch @@ -12,7 +12,10 @@ from tests.common import MockConfigEntry async def test_duplicate_error(hass): """Test that errors are shown when duplicates are added.""" - conf = {CONF_USERNAME: "user@host.com", CONF_PASSWORD: "123abc"} + conf = { + CONF_USERNAME: "user@host.com", + CONF_PASSWORD: "123abc", + } MockConfigEntry(domain=DOMAIN, unique_id="user@host.com", data=conf).add_to_hass( hass @@ -28,7 +31,10 @@ async def test_duplicate_error(hass): async def test_invalid_credentials(hass): """Test that invalid credentials key throws an error.""" - conf = {CONF_USERNAME: "user@host.com", CONF_PASSWORD: "123abc"} + conf = { + CONF_USERNAME: "user@host.com", + CONF_PASSWORD: "123abc", + } with patch( "homeassistant.components.tile.config_flow.async_login", @@ -41,9 +47,33 @@ async def test_invalid_credentials(hass): assert result["errors"] == {"base": "invalid_auth"} +async def test_step_import(hass): + """Test that the import step works.""" + conf = { + CONF_USERNAME: "user@host.com", + CONF_PASSWORD: "123abc", + } + + with patch( + "homeassistant.components.tile.async_setup_entry", return_value=True + ), patch("homeassistant.components.tile.config_flow.async_login"): + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_IMPORT}, data=conf + ) + assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY + assert result["title"] == "user@host.com" + assert result["data"] == { + CONF_USERNAME: "user@host.com", + CONF_PASSWORD: "123abc", + } + + async def test_step_user(hass): """Test that the user step works.""" - conf = {CONF_USERNAME: "user@host.com", CONF_PASSWORD: "123abc"} + conf = { + CONF_USERNAME: "user@host.com", + CONF_PASSWORD: "123abc", + } with patch( "homeassistant.components.tile.async_setup_entry", return_value=True