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>
This commit is contained in:
parent
c62bfcaa4c
commit
b2c84a4c4a
4 changed files with 69 additions and 35 deletions
|
@ -1,12 +1,16 @@
|
||||||
"""Config flow for Awair."""
|
"""Config flow for Awair."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Mapping
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
from python_awair import Awair
|
from python_awair import Awair
|
||||||
from python_awair.exceptions import AuthError, AwairError
|
from python_awair.exceptions import AuthError, AwairError
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigFlow
|
from homeassistant.config_entries import ConfigFlow
|
||||||
from homeassistant.const import CONF_ACCESS_TOKEN
|
from homeassistant.const import CONF_ACCESS_TOKEN
|
||||||
|
from homeassistant.data_entry_flow import FlowResult
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
|
|
||||||
from .const import DOMAIN, LOGGER
|
from .const import DOMAIN, LOGGER
|
||||||
|
@ -17,7 +21,9 @@ class AwairFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||||
|
|
||||||
VERSION = 1
|
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."""
|
"""Handle a flow initialized by the user."""
|
||||||
errors = {}
|
errors = {}
|
||||||
|
|
||||||
|
@ -42,8 +48,14 @@ class AwairFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||||
errors=errors,
|
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."""
|
"""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 = {}
|
errors = {}
|
||||||
|
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
|
@ -62,7 +74,7 @@ class AwairFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||||
errors = {CONF_ACCESS_TOKEN: error}
|
errors = {CONF_ACCESS_TOKEN: error}
|
||||||
|
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id="reauth",
|
step_id="reauth_confirm",
|
||||||
data_schema=vol.Schema({vol.Required(CONF_ACCESS_TOKEN): str}),
|
data_schema=vol.Schema({vol.Required(CONF_ACCESS_TOKEN): str}),
|
||||||
errors=errors,
|
errors=errors,
|
||||||
)
|
)
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
"email": "[%key:common::config_flow::data::email%]"
|
"email": "[%key:common::config_flow::data::email%]"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"reauth": {
|
"reauth_confirm": {
|
||||||
"description": "Please re-enter your Awair developer access token.",
|
"description": "Please re-enter your Awair developer access token.",
|
||||||
"data": {
|
"data": {
|
||||||
"access_token": "[%key:common::config_flow::data::access_token%]",
|
"access_token": "[%key:common::config_flow::data::access_token%]",
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
"unknown": "Unexpected error"
|
"unknown": "Unexpected error"
|
||||||
},
|
},
|
||||||
"step": {
|
"step": {
|
||||||
"reauth": {
|
"reauth_confirm": {
|
||||||
"data": {
|
"data": {
|
||||||
"access_token": "Access Token",
|
"access_token": "Access Token",
|
||||||
"email": "Email"
|
"email": "Email"
|
||||||
|
|
|
@ -8,6 +8,7 @@ from homeassistant import data_entry_flow
|
||||||
from homeassistant.components.awair.const import DOMAIN
|
from homeassistant.components.awair.const import DOMAIN
|
||||||
from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER
|
from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER
|
||||||
from homeassistant.const import CONF_ACCESS_TOKEN
|
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
|
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"
|
assert result["reason"] == "no_devices_found"
|
||||||
|
|
||||||
|
|
||||||
async def test_reauth(hass):
|
async def test_reauth(hass: HomeAssistant) -> None:
|
||||||
"""Test reauth flow."""
|
"""Test reauth flow."""
|
||||||
with patch(
|
mock_config = MockConfigEntry(
|
||||||
"python_awair.AwairClient.query", side_effect=[USER_FIXTURE, DEVICES_FIXTURE]
|
domain=DOMAIN, unique_id=UNIQUE_ID, data={**CONFIG, CONF_ACCESS_TOKEN: "blah"}
|
||||||
), patch(
|
)
|
||||||
"homeassistant.components.awair.sensor.async_setup_entry",
|
mock_config.add_to_hass(hass)
|
||||||
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"}
|
|
||||||
)
|
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
context={"source": SOURCE_REAUTH, "unique_id": UNIQUE_ID},
|
context={"source": SOURCE_REAUTH, "unique_id": UNIQUE_ID},
|
||||||
data=CONFIG,
|
data={**CONFIG, CONF_ACCESS_TOKEN: "blah"},
|
||||||
)
|
)
|
||||||
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||||
assert result["type"] == "abort"
|
assert result["step_id"] == "reauth_confirm"
|
||||||
assert result["reason"] == "reauth_successful"
|
assert result["errors"] == {}
|
||||||
|
|
||||||
with patch("python_awair.AwairClient.query", side_effect=AuthError()):
|
with patch("python_awair.AwairClient.query", side_effect=AuthError()):
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_configure(
|
||||||
DOMAIN,
|
result["flow_id"],
|
||||||
context={"source": SOURCE_REAUTH, "unique_id": UNIQUE_ID},
|
user_input=CONFIG,
|
||||||
data=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"}
|
assert result["errors"] == {CONF_ACCESS_TOKEN: "invalid_access_token"}
|
||||||
|
|
||||||
with patch("python_awair.AwairClient.query", side_effect=AwairError()):
|
with patch(
|
||||||
result = await hass.config_entries.flow.async_init(
|
"python_awair.AwairClient.query", side_effect=[USER_FIXTURE, DEVICES_FIXTURE]
|
||||||
DOMAIN,
|
), patch("homeassistant.components.awair.async_setup_entry", return_value=True):
|
||||||
context={"source": SOURCE_REAUTH, "unique_id": UNIQUE_ID},
|
result = await hass.config_entries.flow.async_configure(
|
||||||
data=CONFIG,
|
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"
|
assert result["reason"] == "unknown"
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue