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 <marhje52@gmail.com> * Update tests/components/awair/test_config_flow.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update tests/components/awair/test_config_flow.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update tests/components/awair/test_config_flow.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update tests/components/awair/test_config_flow.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update tests/components/awair/test_config_flow.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update tests/components/awair/test_config_flow.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> Co-authored-by: Martin Hjelmare <marhje52@gmail.com>pull/74084/head
parent
c62bfcaa4c
commit
b2c84a4c4a
|
@ -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,
|
||||
)
|
||||
|
|
|
@ -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%]",
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
"unknown": "Unexpected error"
|
||||
},
|
||||
"step": {
|
||||
"reauth": {
|
||||
"reauth_confirm": {
|
||||
"data": {
|
||||
"access_token": "Access Token",
|
||||
"email": "Email"
|
||||
|
|
|
@ -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"
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue