Use flow result type constants more (#51122)

This commit is contained in:
Ville Skyttä 2021-05-29 15:09:13 +03:00 committed by GitHub
parent b6716ecebd
commit c2f5dcefa5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 25 additions and 21 deletions

View file

@ -52,7 +52,7 @@ flow for details.
Progress the flow. Most flows will be 1 page, but could optionally add extra 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 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 The authorization code associated with an authorized user by default, it will
associate with an credential if "type" set to "link_user" in associate with an credential if "type" set to "link_user" in
"/auth/login_flow" "/auth/login_flow"

View file

@ -9,6 +9,7 @@ import time
from homeassistant.const import CONF_DEVICE, CONF_PLATFORM from homeassistant.const import CONF_DEVICE, CONF_PLATFORM
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import RESULT_TYPE_ABORT
from homeassistant.helpers.dispatcher import ( from homeassistant.helpers.dispatcher import (
async_dispatcher_connect, async_dispatcher_connect,
async_dispatcher_send, async_dispatcher_send,
@ -274,7 +275,7 @@ async def async_start( # noqa: C901
) )
if ( if (
result result
and result["type"] == "abort" and result["type"] == RESULT_TYPE_ABORT
and result["reason"] and result["reason"]
in ["already_configured", "single_instance_allowed"] in ["already_configured", "single_instance_allowed"]
): ):

View file

@ -27,7 +27,7 @@ from homeassistant.components.mysensors import (
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import callback 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 import homeassistant.helpers.config_validation as cv
from . import CONF_RETAIN, CONF_VERSION, DEFAULT_VERSION 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 user_input[CONF_GATEWAY_TYPE] = CONF_GATEWAY_TYPE_SERIAL
result: dict[str, Any] = await self.async_step_user(user_input=user_input) 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 self.async_abort(reason=next(iter(result["errors"].values())))
return result return result

View file

@ -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.config_flow import CannotConnect, InvalidAuth
from homeassistant.components.NEW_DOMAIN.const import DOMAIN from homeassistant.components.NEW_DOMAIN.const import DOMAIN
from homeassistant.core import HomeAssistant 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: 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( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER} DOMAIN, context={"source": config_entries.SOURCE_USER}
) )
assert result["type"] == "form" assert result["type"] == RESULT_TYPE_FORM
assert result["errors"] == {} assert result["errors"] == {}
with patch( with patch(
@ -33,7 +34,7 @@ async def test_form(hass: HomeAssistant) -> None:
) )
await hass.async_block_till_done() 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["title"] == "Name of the device"
assert result2["data"] == { assert result2["data"] == {
"host": "1.1.1.1", "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"} 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"} assert result2["errors"] == {"base": "cannot_connect"}

View file

@ -8,6 +8,7 @@ import voluptuous as vol
from homeassistant import auth from homeassistant import auth
from homeassistant.auth import auth_store from homeassistant.auth import auth_store
from homeassistant.auth.providers import trusted_networks as tn_auth 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 @pytest.fixture
@ -161,7 +162,7 @@ async def test_login_flow(manager, provider):
# not from trusted network # not from trusted network
flow = await provider.async_login_flow({"ip_address": ip_address("127.0.0.1")}) flow = await provider.async_login_flow({"ip_address": ip_address("127.0.0.1")})
step = await flow.async_step_init() step = await flow.async_step_init()
assert step["type"] == "abort" assert step["type"] == RESULT_TYPE_ABORT
assert step["reason"] == "not_allowed" assert step["reason"] == "not_allowed"
# from trusted network, list users # from trusted network, list users
@ -176,7 +177,7 @@ async def test_login_flow(manager, provider):
# login with valid user # login with valid user
step = await flow.async_step_init({"user": user.id}) 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 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")} {"ip_address": ip_address("127.0.0.1")}
) )
step = await flow.async_step_init() step = await flow.async_step_init()
assert step["type"] == "abort" assert step["type"] == RESULT_TYPE_ABORT
assert step["reason"] == "not_allowed" assert step["reason"] == "not_allowed"
# from trusted network, list users intersect trusted_users # 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")} {"ip_address": ip_address("127.0.0.1")}
) )
step = await flow.async_step_init() step = await flow.async_step_init()
assert step["type"] == "abort" assert step["type"] == RESULT_TYPE_ABORT
assert step["reason"] == "not_allowed" assert step["reason"] == "not_allowed"
# from trusted network, list users intersect trusted_users # 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")} {"ip_address": ip_address("127.0.0.1")}
) )
step = await flow.async_step_init() step = await flow.async_step_init()
assert step["type"] == "abort" assert step["type"] == RESULT_TYPE_ABORT
assert step["reason"] == "not_allowed" assert step["reason"] == "not_allowed"
# from trusted network, only one available user, bypass the login flow # 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")} {"ip_address": ip_address("192.168.0.1")}
) )
step = await flow.async_step_init() step = await flow.async_step_init()
assert step["type"] == "create_entry" assert step["type"] == RESULT_TYPE_CREATE_ENTRY
assert step["data"]["user"] == owner.id assert step["data"]["user"] == owner.id
user = await manager_bypass_login.async_create_user("test-user") user = await manager_bypass_login.async_create_user("test-user")

View file

@ -9,6 +9,7 @@ import pytest
from homeassistant import config_entries, data_entry_flow, loader from homeassistant import config_entries, data_entry_flow, loader
from homeassistant.const import EVENT_HOMEASSISTANT_STARTED, EVENT_HOMEASSISTANT_STOP from homeassistant.const import EVENT_HOMEASSISTANT_STARTED, EVENT_HOMEASSISTANT_STOP
from homeassistant.core import CoreState, callback from homeassistant.core import CoreState, callback
from homeassistant.data_entry_flow import RESULT_TYPE_ABORT
from homeassistant.exceptions import ( from homeassistant.exceptions import (
ConfigEntryAuthFailed, ConfigEntryAuthFailed,
ConfigEntryNotReady, ConfigEntryNotReady,
@ -1612,7 +1613,7 @@ async def test_unique_id_update_existing_entry_without_reload(hass, manager):
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert result["type"] == "abort" assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "already_configured" assert result["reason"] == "already_configured"
assert entry.data["host"] == "1.1.1.1" assert entry.data["host"] == "1.1.1.1"
assert entry.data["additional"] == "data" 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() await hass.async_block_till_done()
assert result["type"] == "abort" assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "already_configured" assert result["reason"] == "already_configured"
assert entry.data["host"] == "1.1.1.1" assert entry.data["host"] == "1.1.1.1"
assert entry.data["additional"] == "data" 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() await hass.async_block_till_done()
assert result["type"] == "abort" assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "already_configured" assert result["reason"] == "already_configured"
assert entry.data["host"] == "2.2.2.2" assert entry.data["host"] == "2.2.2.2"
assert entry.data["additional"] == "data" 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() await hass.async_block_till_done()
assert result["type"] == "abort" assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "already_configured" assert result["reason"] == "already_configured"
assert entry.data["host"] == "0.0.0.0" assert entry.data["host"] == "0.0.0.0"
assert entry.data["additional"] == "data" 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() await hass.async_block_till_done()
assert result["type"] == "abort" assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == reason assert result["reason"] == reason

View file

@ -121,7 +121,7 @@ async def test_show_form(manager):
) )
form = await manager.async_init("test") 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["data_schema"] is schema
assert form["errors"] == {"username": "Should be unique."} 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"}) raise data_entry_flow.AbortFlow("mock-reason", {"placeholder": "yo"})
form = await manager.async_init("test") 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["reason"] == "mock-reason"
assert form["description_placeholders"] == {"placeholder": "yo"} assert form["description_placeholders"] == {"placeholder": "yo"}