Skip to content

Commit

Permalink
Use ConfigFlowResult type in config flow
Browse files Browse the repository at this point in the history
  • Loading branch information
ollo69 committed Oct 12, 2024
1 parent 1115498 commit b54eb40
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
//"editor.formatOnSave": true
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.formatOnSave": true
},
// Added --no-cov to work around TypeError: message must be set
// https://github.com/microsoft/vscode-python/issues/14067
"python.testing.pytestArgs": ["--no-cov"],
Expand Down
41 changes: 25 additions & 16 deletions custom_components/smartthinq_sensors/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
from pycountry import countries as py_countries, languages as py_languages
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.config_entries import (
CONN_CLASS_CLOUD_POLL,
SOURCE_REAUTH,
ConfigEntryState,
ConfigFlow,
ConfigFlowResult,
)
from homeassistant.const import (
CONF_BASE,
CONF_CLIENT_ID,
Expand All @@ -21,7 +27,6 @@
__version__,
)
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.selector import (
SelectOptionDict,
SelectSelector,
Expand Down Expand Up @@ -67,11 +72,11 @@
}


class SmartThinQFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
class SmartThinQFlowHandler(ConfigFlow, domain=DOMAIN):
"""Handle SmartThinQ config flow."""

VERSION = 1
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL
CONNECTION_CLASS = CONN_CLASS_CLOUD_POLL

def __init__(self) -> None:
"""Initialize flow."""
Expand Down Expand Up @@ -103,7 +108,7 @@ def _validate_region_language(region: str, language: str) -> str | None:

return None

def _get_hass_region_lang(self):
def _get_hass_region_lang(self) -> None:
"""Get the hass configured region and language."""
if self._region and self._user_lang:
return
Expand All @@ -120,7 +125,7 @@ def _get_hass_region_lang(self):

async def async_step_import(
self, import_config: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Import a config entry."""
self._is_import = True
self._region = import_config.get(CONF_REGION)
Expand All @@ -130,7 +135,7 @@ async def async_step_import(

async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Handle a flow initialized by the user interface"""

if not is_valid_ha_version():
Expand All @@ -146,7 +151,7 @@ async def async_step_user(
self._error = "invalid_config"
elif entries := self._async_current_entries():
entry = entries[0]
if entry.state == config_entries.ConfigEntryState.LOADED:
if entry.state == ConfigEntryState.LOADED:
return self.async_abort(reason="single_instance_allowed")
if not self._region:
self._region = entry.data.get(CONF_REGION)
Expand Down Expand Up @@ -174,7 +179,7 @@ async def async_step_user(
self._language += f"-{region}"

if not use_redirect and not (username and password):
if self.source == config_entries.SOURCE_REAUTH and not (username or password):
if self.source == SOURCE_REAUTH and not (username or password):
return await self.async_step_reauth_confirm()
return self._show_form(errors="no_user_info")

Expand All @@ -201,7 +206,7 @@ async def async_step_user(

async def async_step_url(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Parse the response url for oauth data and submit for save."""
if not user_input:
return self._show_form(step_id="url")
Expand Down Expand Up @@ -251,7 +256,9 @@ async def _check_connection(self, lge_auth: LGEAuthentication) -> int:
self._client_id = client.client_id
return RESULT_SUCCESS

async def _manage_error(self, error_code: int, is_user_step=False) -> FlowResult:
async def _manage_error(
self, error_code: int, is_user_step=False
) -> ConfigFlowResult:
"""Manage the error result."""
if error_code == RESULT_NO_DEV:
return self.async_abort(reason="no_smartthinq_devices")
Expand All @@ -267,7 +274,7 @@ async def _manage_error(self, error_code: int, is_user_step=False) -> FlowResult
return await self.async_step_user()

@callback
def _save_config_entry(self) -> FlowResult:
def _save_config_entry(self) -> ConfigFlowResult:
"""Save the entry."""

data = {
Expand Down Expand Up @@ -329,7 +336,7 @@ def _prepare_form_schema(self, step_id="user") -> vol.Schema:
return schema

@callback
def _show_form(self, errors: str | None = None, step_id="user") -> FlowResult:
def _show_form(self, errors: str | None = None, step_id="user") -> ConfigFlowResult:
"""Show the form to the user."""
base_err = errors or self._error
self._error = None
Expand All @@ -341,20 +348,22 @@ def _show_form(self, errors: str | None = None, step_id="user") -> FlowResult:
errors={CONF_BASE: base_err} if base_err else None,
)

async def async_step_reauth(self, entry_data: Mapping[str, Any]) -> FlowResult:
async def async_step_reauth(
self, entry_data: Mapping[str, Any]
) -> ConfigFlowResult:
"""Perform reauth upon an API authentication error."""
return await self.async_step_reauth_confirm()

async def async_step_reauth_confirm(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Dialog that informs the user that reauth is required."""
if user_input is None:
return self.async_show_form(
step_id="reauth_confirm",
data_schema=vol.Schema(
{vol.Required(CONF_REAUTH_CRED, default=False): bool}
)
),
)

if user_input[CONF_REAUTH_CRED] is True:
Expand Down

0 comments on commit b54eb40

Please sign in to comment.