From b556bd1d5802e5fdce36bee426c0e7c020e66df8 Mon Sep 17 00:00:00 2001 From: Aaron Bach Date: Thu, 9 Dec 2021 15:11:41 -0700 Subject: [PATCH] Consolidate SimpliSafe config flow forms into one (#61402) --- .../components/simplisafe/config_flow.py | 59 +++++++++---------- .../components/simplisafe/strings.json | 8 +-- .../simplisafe/translations/en.json | 25 +------- .../components/simplisafe/test_config_flow.py | 21 ------- 4 files changed, 31 insertions(+), 82 deletions(-) diff --git a/homeassistant/components/simplisafe/config_flow.py b/homeassistant/components/simplisafe/config_flow.py index 8f8ec6cdc16..53fcb92f71a 100644 --- a/homeassistant/components/simplisafe/config_flow.py +++ b/homeassistant/components/simplisafe/config_flow.py @@ -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.""" diff --git a/homeassistant/components/simplisafe/strings.json b/homeassistant/components/simplisafe/strings.json index 55a916bfe6b..08632cd754a 100644 --- a/homeassistant/components/simplisafe/strings.json +++ b/homeassistant/components/simplisafe/strings.json @@ -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": { diff --git a/homeassistant/components/simplisafe/translations/en.json b/homeassistant/components/simplisafe/translations/en.json index 66843f86d27..3c5dd9261bc 100644 --- a/homeassistant/components/simplisafe/translations/en.json +++ b/homeassistant/components/simplisafe/translations/en.json @@ -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." } } }, diff --git a/tests/components/simplisafe/test_config_flow.py b/tests/components/simplisafe/test_config_flow.py index 99943497556..0597ad377cf 100644 --- a/tests/components/simplisafe/test_config_flow.py +++ b/tests/components/simplisafe/test_config_flow.py @@ -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"} )