From 6a02c6540e2a5349a4186d2878fec71e61edc440 Mon Sep 17 00:00:00 2001 From: jjlawren Date: Sun, 1 Dec 2019 00:07:12 -0600 Subject: [PATCH] Stop Plex config flow imports where more user input needed (#29241) * Abort imports that require user interaction, update logs and tests * Disable lint --- homeassistant/components/plex/config_flow.py | 15 +++++++++++ homeassistant/components/plex/strings.json | 1 + tests/components/plex/test_config_flow.py | 26 +++++++++++++++++--- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/plex/config_flow.py b/homeassistant/components/plex/config_flow.py index cb79c08b16e..350f1b3d577 100644 --- a/homeassistant/components/plex/config_flow.py +++ b/homeassistant/components/plex/config_flow.py @@ -80,12 +80,17 @@ class PlexFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): """Validate a provided configuration.""" errors = {} self.current_login = server_config + is_importing = ( + self.context["source"] # pylint: disable=no-member + == config_entries.SOURCE_IMPORT + ) plex_server = PlexServer(self.hass, server_config) try: await self.hass.async_add_executor_job(plex_server.connect) except NoServersFound: + _LOGGER.error("No servers linked to Plex account") errors["base"] = "no_servers" except (plexapi.exceptions.BadRequest, plexapi.exceptions.Unauthorized): _LOGGER.error("Invalid credentials provided, config not created") @@ -98,6 +103,11 @@ class PlexFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): errors["base"] = "not_found" except ServerNotSpecified as available_servers: + if is_importing: + _LOGGER.warning( + "Imported configuration has multiple available Plex servers. Specify server in configuration or add a new Integration." + ) + return self.async_abort(reason="non-interactive") self.available_servers = available_servers.args[0] return await self.async_step_select_server() @@ -106,12 +116,17 @@ class PlexFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): return self.async_abort(reason="unknown") if errors: + if is_importing: + return self.async_abort(reason="non-interactive") return self.async_show_form(step_id="start_website_auth", errors=errors) server_id = plex_server.machine_identifier for entry in self._async_current_entries(): if entry.data[CONF_SERVER_IDENTIFIER] == server_id: + _LOGGER.debug( + "Plex server already configured: %s", entry.data[CONF_SERVER] + ) return self.async_abort(reason="already_configured") url = plex_server.url_in_use diff --git a/homeassistant/components/plex/strings.json b/homeassistant/components/plex/strings.json index aff79acc2ed..b6491db350c 100644 --- a/homeassistant/components/plex/strings.json +++ b/homeassistant/components/plex/strings.json @@ -25,6 +25,7 @@ "already_in_progress": "Plex is being configured", "discovery_no_file": "No legacy config file found", "invalid_import": "Imported configuration is invalid", + "non-interactive": "Non-interactive import", "token_request_timeout": "Timed out obtaining token", "unknown": "Failed for unknown reason" } diff --git a/tests/components/plex/test_config_flow.py b/tests/components/plex/test_config_flow.py index c0d14f1efdc..668ac3b2a17 100644 --- a/tests/components/plex/test_config_flow.py +++ b/tests/components/plex/test_config_flow.py @@ -178,9 +178,8 @@ async def test_import_bad_hostname(hass): CONF_URL: f"http://{MOCK_SERVERS[0][CONF_HOST]}:{MOCK_SERVERS[0][CONF_PORT]}", }, ) - assert result["type"] == "form" - assert result["step_id"] == "start_website_auth" - assert result["errors"]["base"] == "not_found" + assert result["type"] == "abort" + assert result["reason"] == "non-interactive" async def test_unknown_exception(hass): @@ -384,12 +383,14 @@ async def test_already_configured(hass): mock_plex_server = MockPlexServer() flow = init_config_flow(hass) + flow.context = {"source": "import"} MockConfigEntry( domain=config_flow.DOMAIN, data={ + config_flow.CONF_SERVER: MOCK_SERVERS[0][config_flow.CONF_SERVER], config_flow.CONF_SERVER_IDENTIFIER: MOCK_SERVERS[0][ config_flow.CONF_SERVER_IDENTIFIER - ] + ], }, ).add_to_hass(hass) @@ -530,3 +531,20 @@ async def test_callback_view(hass, aiohttp_client): resp = await client.get(forward_url) assert resp.status == 200 + + +async def test_multiple_servers_with_import(hass): + """Test importing a config with multiple servers available.""" + + with patch( + "plexapi.myplex.MyPlexAccount", return_value=MockPlexAccount(servers=2) + ), asynctest.patch("plexauth.PlexAuth.initiate_auth"), asynctest.patch( + "plexauth.PlexAuth.token", return_value=MOCK_TOKEN + ): + result = await hass.config_entries.flow.async_init( + config_flow.DOMAIN, + context={"source": "import"}, + data={CONF_TOKEN: MOCK_TOKEN}, + ) + assert result["type"] == "abort" + assert result["reason"] == "non-interactive"