Consolidate SimpliSafe config flow forms into one (#61402)

This commit is contained in:
Aaron Bach 2021-12-09 15:11:41 -07:00 committed by GitHub
parent 2b7c2d902e
commit b556bd1d58
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 82 deletions

View file

@ -24,7 +24,7 @@ from .const import CONF_USER_ID, DOMAIN, LOGGER
CONF_AUTH_CODE = "auth_code"
STEP_INPUT_AUTH_CODE_SCHEMA = vol.Schema(
STEP_USER_SCHEMA = vol.Schema(
{
vol.Required(CONF_AUTH_CODE): cv.string,
}
@ -54,8 +54,7 @@ class SimpliSafeFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
def __init__(self) -> None:
"""Initialize the config flow."""
self._errors: dict[str, Any] = {}
self._oauth_values: SimpliSafeOAuthValues = async_get_simplisafe_oauth_values()
self._oauth_values: SimpliSafeOAuthValues | None = None
self._reauth: bool = False
self._username: str | None = None
@ -67,19 +66,34 @@ class SimpliSafeFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Define the config flow to handle options."""
return SimpliSafeOptionsFlowHandler(config_entry)
async def async_step_input_auth_code(
def _async_show_form(self, *, errors: dict[str, Any] | None = None) -> FlowResult:
"""Show the form."""
self._oauth_values = async_get_simplisafe_oauth_values()
return self.async_show_form(
step_id="user",
data_schema=STEP_USER_SCHEMA,
errors=errors or {},
description_placeholders={CONF_URL: self._oauth_values.auth_url},
)
async def async_step_reauth(self, config: ConfigType) -> FlowResult:
"""Handle configuration by re-auth."""
self._username = config.get(CONF_USERNAME)
self._reauth = True
return await self.async_step_user()
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle the input of a SimpliSafe OAuth authorization code."""
"""Handle the start of the config flow."""
if user_input is None:
return self.async_show_form(
step_id="input_auth_code", data_schema=STEP_INPUT_AUTH_CODE_SCHEMA
)
return self._async_show_form()
if TYPE_CHECKING:
assert self._oauth_values
self._errors = {}
errors = {}
session = aiohttp_client.async_get_clientsession(self.hass)
try:
@ -89,13 +103,13 @@ class SimpliSafeFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
session=session,
)
except InvalidCredentialsError:
self._errors = {"base": "invalid_auth"}
errors = {"base": "invalid_auth"}
except SimplipyError as err:
LOGGER.error("Unknown error while logging into SimpliSafe: %s", err)
self._errors = {"base": "unknown"}
errors = {"base": "unknown"}
if self._errors:
return await self.async_step_user()
if errors:
return self._async_show_form(errors=errors)
data = {CONF_USER_ID: simplisafe.user_id, CONF_TOKEN: simplisafe.refresh_token}
unique_id = str(simplisafe.user_id)
@ -122,25 +136,6 @@ class SimpliSafeFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
self._abort_if_unique_id_configured()
return self.async_create_entry(title=unique_id, data=data)
async def async_step_reauth(self, config: ConfigType) -> FlowResult:
"""Handle configuration by re-auth."""
self._username = config.get(CONF_USERNAME)
self._reauth = True
return await self.async_step_user()
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle the start of the config flow."""
if user_input is None:
return self.async_show_form(
step_id="user",
errors=self._errors,
description_placeholders={CONF_URL: self._oauth_values.auth_url},
)
return await self.async_step_input_auth_code()
class SimpliSafeOptionsFlowHandler(config_entries.OptionsFlow):
"""Handle a SimpliSafe options flow."""

View file

@ -1,15 +1,11 @@
{
"config": {
"step": {
"input_auth_code": {
"title": "Finish Authorization",
"description": "Input the authorization code from the SimpliSafe web app URL:",
"user": {
"description": "Starting in 2021, SimpliSafe has moved to a new authentication mechanism via its web app. Due to technical limitations, there is a manual step at the end of this process; please ensure that you read the [documentation](http://home-assistant.io/integrations/simplisafe#getting-an-authorization-code) before starting.\n\n1. Click [here]({url}) to open the SimpliSafe web app and input your credentials.\n\n2. When the login process is complete, return here and input the authorization code below.",
"data": {
"auth_code": "Authorization Code"
}
},
"user": {
"description": "Starting in 2021, SimpliSafe has moved to a new authentication mechanism via its web app. Due to technical limitations, there is a manual step at the end of this process; please ensure that you read the [documentation](http://home-assistant.io/integrations/simplisafe#getting-an-authorization-code) before starting.\n\nWhen you are ready, click [here]({url}) to open the SimpliSafe web app and input your credentials. When the process is complete, return here and click Submit."
}
},
"error": {

View file

@ -12,32 +12,11 @@
"unknown": "Unexpected error"
},
"step": {
"input_auth_code": {
"user": {
"data": {
"auth_code": "Authorization Code"
},
"description": "Input the authorization code from the SimpliSafe web app URL:",
"title": "Finish Authorization"
},
"mfa": {
"description": "Check your email for a link from SimpliSafe. After verifying the link, return here to complete the installation of the integration.",
"title": "SimpliSafe Multi-Factor Authentication"
},
"reauth_confirm": {
"data": {
"password": "Password"
},
"description": "Your access has expired or been revoked. Enter your password to re-link your account.",
"title": "Reauthenticate Integration"
},
"user": {
"data": {
"code": "Code (used in Home Assistant UI)",
"password": "Password",
"username": "Email"
},
"description": "Starting in 2021, SimpliSafe has moved to a new authentication mechanism via its web app. Due to technical limitations, there is a manual step at the end of this process; please ensure that you read the [documentation](http://home-assistant.io/integrations/simplisafe#getting-an-authorization-code) before starting.\n\nWhen you are ready, click [here]({url}) to open the SimpliSafe web app and input your credentials. When the process is complete, return here and click Submit.",
"title": "Fill in your information."
"description": "Starting in 2021, SimpliSafe has moved to a new authentication mechanism via its web app. Due to technical limitations, there is a manual step at the end of this process; please ensure that you read the [documentation](http://home-assistant.io/integrations/simplisafe#getting-an-authorization-code) before starting.\n\n1. Click [here]({url}) to open the SimpliSafe web app and input your credentials.\n\n2. When the login process is complete, return here and input the authorization code below."
}
}
},

View file

@ -53,9 +53,6 @@ async def test_duplicate_error(hass, mock_async_from_auth):
assert result["step_id"] == "user"
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_AUTH_CODE: "code123"}
)
@ -73,9 +70,6 @@ async def test_invalid_credentials(hass, mock_async_from_auth):
assert result["step_id"] == "user"
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_AUTH_CODE: "code123"}
)
@ -131,9 +125,6 @@ async def test_step_reauth_old_format(hass, mock_async_from_auth):
with patch(
"homeassistant.components.simplisafe.async_setup_entry", return_value=True
), patch("homeassistant.config_entries.ConfigEntries.async_reload"):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_AUTH_CODE: "code123"}
)
@ -166,9 +157,6 @@ async def test_step_reauth_new_format(hass, mock_async_from_auth):
with patch(
"homeassistant.components.simplisafe.async_setup_entry", return_value=True
), patch("homeassistant.config_entries.ConfigEntries.async_reload"):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_AUTH_CODE: "code123"}
)
@ -205,9 +193,6 @@ async def test_step_reauth_wrong_account(hass, api, mock_async_from_auth):
with patch(
"homeassistant.components.simplisafe.async_setup_entry", return_value=True
), patch("homeassistant.config_entries.ConfigEntries.async_reload"):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_AUTH_CODE: "code123"}
)
@ -229,9 +214,6 @@ async def test_step_user(hass, mock_async_from_auth):
with patch(
"homeassistant.components.simplisafe.async_setup_entry", return_value=True
), patch("homeassistant.config_entries.ConfigEntries.async_reload"):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_AUTH_CODE: "code123"}
)
@ -252,9 +234,6 @@ async def test_unknown_error(hass, mock_async_from_auth):
assert result["step_id"] == "user"
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_AUTH_CODE: "code123"}
)