From b2c84a4c4ae70eeb7ca39a92b8e77d28683b4f44 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 28 Jun 2022 01:49:10 +0200 Subject: [PATCH] Adjust reauth in awair config flow (#72386) * Adjust config-flow type hints in awair * Improve typing of dict arguments * Use mapping for async_step_reauth * Add async_step_reauth_confirm * Don't try old token * Adjust translations * Adjust tests * Update tests/components/awair/test_config_flow.py Co-authored-by: Martin Hjelmare * Update tests/components/awair/test_config_flow.py Co-authored-by: Martin Hjelmare * Update tests/components/awair/test_config_flow.py Co-authored-by: Martin Hjelmare * Update tests/components/awair/test_config_flow.py Co-authored-by: Martin Hjelmare * Update tests/components/awair/test_config_flow.py Co-authored-by: Martin Hjelmare * Update tests/components/awair/test_config_flow.py Co-authored-by: Martin Hjelmare * Update tests/components/awair/test_config_flow.py Co-authored-by: Martin Hjelmare Co-authored-by: Martin Hjelmare --- homeassistant/components/awair/config_flow.py | 18 +++- homeassistant/components/awair/strings.json | 2 +- .../components/awair/translations/en.json | 2 +- tests/components/awair/test_config_flow.py | 82 ++++++++++++------- 4 files changed, 69 insertions(+), 35 deletions(-) diff --git a/homeassistant/components/awair/config_flow.py b/homeassistant/components/awair/config_flow.py index 1eff98dd78d..fc7fd1e79a4 100644 --- a/homeassistant/components/awair/config_flow.py +++ b/homeassistant/components/awair/config_flow.py @@ -1,12 +1,16 @@ """Config flow for Awair.""" from __future__ import annotations +from collections.abc import Mapping +from typing import Any + from python_awair import Awair from python_awair.exceptions import AuthError, AwairError import voluptuous as vol from homeassistant.config_entries import ConfigFlow from homeassistant.const import CONF_ACCESS_TOKEN +from homeassistant.data_entry_flow import FlowResult from homeassistant.helpers.aiohttp_client import async_get_clientsession from .const import DOMAIN, LOGGER @@ -17,7 +21,9 @@ class AwairFlowHandler(ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_user(self, user_input: dict | None = None): + async def async_step_user( + self, user_input: dict[str, str] | None = None + ) -> FlowResult: """Handle a flow initialized by the user.""" errors = {} @@ -42,8 +48,14 @@ class AwairFlowHandler(ConfigFlow, domain=DOMAIN): errors=errors, ) - async def async_step_reauth(self, user_input: dict | None = None): + async def async_step_reauth(self, data: Mapping[str, Any]) -> FlowResult: """Handle re-auth if token invalid.""" + return await self.async_step_reauth_confirm() + + async def async_step_reauth_confirm( + self, user_input: dict[str, Any] | None = None + ) -> FlowResult: + """Confirm reauth dialog.""" errors = {} if user_input is not None: @@ -62,7 +74,7 @@ class AwairFlowHandler(ConfigFlow, domain=DOMAIN): errors = {CONF_ACCESS_TOKEN: error} return self.async_show_form( - step_id="reauth", + step_id="reauth_confirm", data_schema=vol.Schema({vol.Required(CONF_ACCESS_TOKEN): str}), errors=errors, ) diff --git a/homeassistant/components/awair/strings.json b/homeassistant/components/awair/strings.json index f9b1f40e047..5ed7c0e715e 100644 --- a/homeassistant/components/awair/strings.json +++ b/homeassistant/components/awair/strings.json @@ -8,7 +8,7 @@ "email": "[%key:common::config_flow::data::email%]" } }, - "reauth": { + "reauth_confirm": { "description": "Please re-enter your Awair developer access token.", "data": { "access_token": "[%key:common::config_flow::data::access_token%]", diff --git a/homeassistant/components/awair/translations/en.json b/homeassistant/components/awair/translations/en.json index 0e5a1e62bb5..52ffd13ec79 100644 --- a/homeassistant/components/awair/translations/en.json +++ b/homeassistant/components/awair/translations/en.json @@ -10,7 +10,7 @@ "unknown": "Unexpected error" }, "step": { - "reauth": { + "reauth_confirm": { "data": { "access_token": "Access Token", "email": "Email" diff --git a/tests/components/awair/test_config_flow.py b/tests/components/awair/test_config_flow.py index 8afe9a1c701..47e58fea421 100644 --- a/tests/components/awair/test_config_flow.py +++ b/tests/components/awair/test_config_flow.py @@ -8,6 +8,7 @@ from homeassistant import data_entry_flow from homeassistant.components.awair.const import DOMAIN from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER from homeassistant.const import CONF_ACCESS_TOKEN +from homeassistant.core import HomeAssistant from .const import CONFIG, DEVICES_FIXTURE, NO_DEVICES_FIXTURE, UNIQUE_ID, USER_FIXTURE @@ -82,46 +83,67 @@ async def test_no_devices_error(hass): assert result["reason"] == "no_devices_found" -async def test_reauth(hass): +async def test_reauth(hass: HomeAssistant) -> None: """Test reauth flow.""" - with patch( - "python_awair.AwairClient.query", side_effect=[USER_FIXTURE, DEVICES_FIXTURE] - ), patch( - "homeassistant.components.awair.sensor.async_setup_entry", - return_value=True, - ): - mock_config = MockConfigEntry(domain=DOMAIN, unique_id=UNIQUE_ID, data=CONFIG) - mock_config.add_to_hass(hass) - hass.config_entries.async_update_entry( - mock_config, data={**CONFIG, CONF_ACCESS_TOKEN: "blah"} - ) + mock_config = MockConfigEntry( + domain=DOMAIN, unique_id=UNIQUE_ID, data={**CONFIG, CONF_ACCESS_TOKEN: "blah"} + ) + mock_config.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH, "unique_id": UNIQUE_ID}, - data=CONFIG, - ) - - assert result["type"] == "abort" - assert result["reason"] == "reauth_successful" + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_REAUTH, "unique_id": UNIQUE_ID}, + data={**CONFIG, CONF_ACCESS_TOKEN: "blah"}, + ) + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result["step_id"] == "reauth_confirm" + assert result["errors"] == {} with patch("python_awair.AwairClient.query", side_effect=AuthError()): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH, "unique_id": UNIQUE_ID}, - data=CONFIG, + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input=CONFIG, ) + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result["step_id"] == "reauth_confirm" assert result["errors"] == {CONF_ACCESS_TOKEN: "invalid_access_token"} - with patch("python_awair.AwairClient.query", side_effect=AwairError()): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH, "unique_id": UNIQUE_ID}, - data=CONFIG, + with patch( + "python_awair.AwairClient.query", side_effect=[USER_FIXTURE, DEVICES_FIXTURE] + ), patch("homeassistant.components.awair.async_setup_entry", return_value=True): + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input=CONFIG, ) - assert result["type"] == "abort" + assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT + assert result["reason"] == "reauth_successful" + + +async def test_reauth_error(hass: HomeAssistant) -> None: + """Test reauth flow.""" + mock_config = MockConfigEntry( + domain=DOMAIN, unique_id=UNIQUE_ID, data={**CONFIG, CONF_ACCESS_TOKEN: "blah"} + ) + mock_config.add_to_hass(hass) + + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_REAUTH, "unique_id": UNIQUE_ID}, + data={**CONFIG, CONF_ACCESS_TOKEN: "blah"}, + ) + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result["step_id"] == "reauth_confirm" + assert result["errors"] == {} + + with patch("python_awair.AwairClient.query", side_effect=AwairError()): + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input=CONFIG, + ) + + assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == "unknown"