From c2f5dcefa5249856f147a442b32ff3e16a028be6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Sat, 29 May 2021 15:09:13 +0300 Subject: [PATCH] Use flow result type constants more (#51122) --- homeassistant/components/auth/login_flow.py | 2 +- homeassistant/components/mqtt/discovery.py | 3 ++- homeassistant/components/mysensors/config_flow.py | 4 ++-- .../templates/config_flow/tests/test_config_flow.py | 9 +++++---- tests/auth/providers/test_trusted_networks.py | 13 +++++++------ tests/test_config_entries.py | 11 ++++++----- tests/test_data_entry_flow.py | 4 ++-- 7 files changed, 25 insertions(+), 21 deletions(-) diff --git a/homeassistant/components/auth/login_flow.py b/homeassistant/components/auth/login_flow.py index 725450a0a12..c951e652356 100644 --- a/homeassistant/components/auth/login_flow.py +++ b/homeassistant/components/auth/login_flow.py @@ -52,7 +52,7 @@ flow for details. Progress the flow. Most flows will be 1 page, but could optionally add extra login challenges, like TFA. Once the flow has finished, the returned step will -have type "create_entry" and "result" key will contain an authorization code. +have type RESULT_TYPE_CREATE_ENTRY and "result" key will contain an authorization code. The authorization code associated with an authorized user by default, it will associate with an credential if "type" set to "link_user" in "/auth/login_flow" diff --git a/homeassistant/components/mqtt/discovery.py b/homeassistant/components/mqtt/discovery.py index 3a5a3cb5f87..e68b47abe02 100644 --- a/homeassistant/components/mqtt/discovery.py +++ b/homeassistant/components/mqtt/discovery.py @@ -9,6 +9,7 @@ import time from homeassistant.const import CONF_DEVICE, CONF_PLATFORM from homeassistant.core import HomeAssistant +from homeassistant.data_entry_flow import RESULT_TYPE_ABORT from homeassistant.helpers.dispatcher import ( async_dispatcher_connect, async_dispatcher_send, @@ -274,7 +275,7 @@ async def async_start( # noqa: C901 ) if ( result - and result["type"] == "abort" + and result["type"] == RESULT_TYPE_ABORT and result["reason"] in ["already_configured", "single_instance_allowed"] ): diff --git a/homeassistant/components/mysensors/config_flow.py b/homeassistant/components/mysensors/config_flow.py index 6676e11febf..ad260c3ab58 100644 --- a/homeassistant/components/mysensors/config_flow.py +++ b/homeassistant/components/mysensors/config_flow.py @@ -27,7 +27,7 @@ from homeassistant.components.mysensors import ( ) from homeassistant.config_entries import ConfigEntry from homeassistant.core import callback -from homeassistant.data_entry_flow import FlowResult +from homeassistant.data_entry_flow import RESULT_TYPE_FORM, FlowResult import homeassistant.helpers.config_validation as cv from . import CONF_RETAIN, CONF_VERSION, DEFAULT_VERSION @@ -132,7 +132,7 @@ class MySensorsConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): user_input[CONF_GATEWAY_TYPE] = CONF_GATEWAY_TYPE_SERIAL result: dict[str, Any] = await self.async_step_user(user_input=user_input) - if result["type"] == "form": + if result["type"] == RESULT_TYPE_FORM: return self.async_abort(reason=next(iter(result["errors"].values()))) return result diff --git a/script/scaffold/templates/config_flow/tests/test_config_flow.py b/script/scaffold/templates/config_flow/tests/test_config_flow.py index 674cb921cdd..e72d9eb7679 100644 --- a/script/scaffold/templates/config_flow/tests/test_config_flow.py +++ b/script/scaffold/templates/config_flow/tests/test_config_flow.py @@ -5,6 +5,7 @@ from homeassistant import config_entries, setup from homeassistant.components.NEW_DOMAIN.config_flow import CannotConnect, InvalidAuth from homeassistant.components.NEW_DOMAIN.const import DOMAIN from homeassistant.core import HomeAssistant +from homeassistant.data_entry_flow import RESULT_TYPE_CREATE_ENTRY, RESULT_TYPE_FORM async def test_form(hass: HomeAssistant) -> None: @@ -13,7 +14,7 @@ async def test_form(hass: HomeAssistant) -> None: result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} ) - assert result["type"] == "form" + assert result["type"] == RESULT_TYPE_FORM assert result["errors"] == {} with patch( @@ -33,7 +34,7 @@ async def test_form(hass: HomeAssistant) -> None: ) await hass.async_block_till_done() - assert result2["type"] == "create_entry" + assert result2["type"] == RESULT_TYPE_CREATE_ENTRY assert result2["title"] == "Name of the device" assert result2["data"] == { "host": "1.1.1.1", @@ -62,7 +63,7 @@ async def test_form_invalid_auth(hass: HomeAssistant) -> None: }, ) - assert result2["type"] == "form" + assert result2["type"] == RESULT_TYPE_FORM assert result2["errors"] == {"base": "invalid_auth"} @@ -85,5 +86,5 @@ async def test_form_cannot_connect(hass: HomeAssistant) -> None: }, ) - assert result2["type"] == "form" + assert result2["type"] == RESULT_TYPE_FORM assert result2["errors"] == {"base": "cannot_connect"} diff --git a/tests/auth/providers/test_trusted_networks.py b/tests/auth/providers/test_trusted_networks.py index 39764fa4206..412f660adc3 100644 --- a/tests/auth/providers/test_trusted_networks.py +++ b/tests/auth/providers/test_trusted_networks.py @@ -8,6 +8,7 @@ import voluptuous as vol from homeassistant import auth from homeassistant.auth import auth_store from homeassistant.auth.providers import trusted_networks as tn_auth +from homeassistant.data_entry_flow import RESULT_TYPE_ABORT, RESULT_TYPE_CREATE_ENTRY @pytest.fixture @@ -161,7 +162,7 @@ async def test_login_flow(manager, provider): # not from trusted network flow = await provider.async_login_flow({"ip_address": ip_address("127.0.0.1")}) step = await flow.async_step_init() - assert step["type"] == "abort" + assert step["type"] == RESULT_TYPE_ABORT assert step["reason"] == "not_allowed" # from trusted network, list users @@ -176,7 +177,7 @@ async def test_login_flow(manager, provider): # login with valid user step = await flow.async_step_init({"user": user.id}) - assert step["type"] == "create_entry" + assert step["type"] == RESULT_TYPE_CREATE_ENTRY assert step["data"]["user"] == user.id @@ -200,7 +201,7 @@ async def test_trusted_users_login(manager_with_user, provider_with_user): {"ip_address": ip_address("127.0.0.1")} ) step = await flow.async_step_init() - assert step["type"] == "abort" + assert step["type"] == RESULT_TYPE_ABORT assert step["reason"] == "not_allowed" # from trusted network, list users intersect trusted_users @@ -284,7 +285,7 @@ async def test_trusted_group_login(manager_with_user, provider_with_user): {"ip_address": ip_address("127.0.0.1")} ) step = await flow.async_step_init() - assert step["type"] == "abort" + assert step["type"] == RESULT_TYPE_ABORT assert step["reason"] == "not_allowed" # from trusted network, list users intersect trusted_users @@ -322,7 +323,7 @@ async def test_bypass_login_flow(manager_bypass_login, provider_bypass_login): {"ip_address": ip_address("127.0.0.1")} ) step = await flow.async_step_init() - assert step["type"] == "abort" + assert step["type"] == RESULT_TYPE_ABORT assert step["reason"] == "not_allowed" # from trusted network, only one available user, bypass the login flow @@ -330,7 +331,7 @@ async def test_bypass_login_flow(manager_bypass_login, provider_bypass_login): {"ip_address": ip_address("192.168.0.1")} ) step = await flow.async_step_init() - assert step["type"] == "create_entry" + assert step["type"] == RESULT_TYPE_CREATE_ENTRY assert step["data"]["user"] == owner.id user = await manager_bypass_login.async_create_user("test-user") diff --git a/tests/test_config_entries.py b/tests/test_config_entries.py index e9e864c4491..18791b1eb2d 100644 --- a/tests/test_config_entries.py +++ b/tests/test_config_entries.py @@ -9,6 +9,7 @@ import pytest from homeassistant import config_entries, data_entry_flow, loader from homeassistant.const import EVENT_HOMEASSISTANT_STARTED, EVENT_HOMEASSISTANT_STOP from homeassistant.core import CoreState, callback +from homeassistant.data_entry_flow import RESULT_TYPE_ABORT from homeassistant.exceptions import ( ConfigEntryAuthFailed, ConfigEntryNotReady, @@ -1612,7 +1613,7 @@ async def test_unique_id_update_existing_entry_without_reload(hass, manager): ) await hass.async_block_till_done() - assert result["type"] == "abort" + assert result["type"] == RESULT_TYPE_ABORT assert result["reason"] == "already_configured" assert entry.data["host"] == "1.1.1.1" assert entry.data["additional"] == "data" @@ -1657,7 +1658,7 @@ async def test_unique_id_update_existing_entry_with_reload(hass, manager): ) await hass.async_block_till_done() - assert result["type"] == "abort" + assert result["type"] == RESULT_TYPE_ABORT assert result["reason"] == "already_configured" assert entry.data["host"] == "1.1.1.1" assert entry.data["additional"] == "data" @@ -1674,7 +1675,7 @@ async def test_unique_id_update_existing_entry_with_reload(hass, manager): ) await hass.async_block_till_done() - assert result["type"] == "abort" + assert result["type"] == RESULT_TYPE_ABORT assert result["reason"] == "already_configured" assert entry.data["host"] == "2.2.2.2" assert entry.data["additional"] == "data" @@ -1717,7 +1718,7 @@ async def test_unique_id_not_update_existing_entry(hass, manager): ) await hass.async_block_till_done() - assert result["type"] == "abort" + assert result["type"] == RESULT_TYPE_ABORT assert result["reason"] == "already_configured" assert entry.data["host"] == "0.0.0.0" assert entry.data["additional"] == "data" @@ -2861,5 +2862,5 @@ async def test__async_abort_entries_match(hass, manager, matchers, reason): ) await hass.async_block_till_done() - assert result["type"] == "abort" + assert result["type"] == RESULT_TYPE_ABORT assert result["reason"] == reason diff --git a/tests/test_data_entry_flow.py b/tests/test_data_entry_flow.py index 34b07a2a871..4b5777d86f8 100644 --- a/tests/test_data_entry_flow.py +++ b/tests/test_data_entry_flow.py @@ -121,7 +121,7 @@ async def test_show_form(manager): ) form = await manager.async_init("test") - assert form["type"] == "form" + assert form["type"] == data_entry_flow.RESULT_TYPE_FORM assert form["data_schema"] is schema assert form["errors"] == {"username": "Should be unique."} @@ -369,7 +369,7 @@ async def test_abort_flow_exception(manager): raise data_entry_flow.AbortFlow("mock-reason", {"placeholder": "yo"}) form = await manager.async_init("test") - assert form["type"] == "abort" + assert form["type"] == data_entry_flow.RESULT_TYPE_ABORT assert form["reason"] == "mock-reason" assert form["description_placeholders"] == {"placeholder": "yo"}