mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 08:17:08 +00:00
Cleanup Discovergy config flow (#108381)
* Cleanup Discovergy config flow * Make use of the helper function
This commit is contained in:
parent
988d72b8b6
commit
4d85f78b32
@ -15,26 +15,37 @@ from homeassistant.config_entries import ConfigEntry
|
|||||||
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
|
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
|
||||||
from homeassistant.data_entry_flow import FlowResult
|
from homeassistant.data_entry_flow import FlowResult
|
||||||
from homeassistant.helpers.httpx_client import get_async_client
|
from homeassistant.helpers.httpx_client import get_async_client
|
||||||
|
from homeassistant.helpers.selector import (
|
||||||
|
TextSelector,
|
||||||
|
TextSelectorConfig,
|
||||||
|
TextSelectorType,
|
||||||
|
)
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def make_schema(email: str = "", password: str = "") -> vol.Schema:
|
CONFIG_SCHEMA = vol.Schema(
|
||||||
"""Create schema for config flow."""
|
{
|
||||||
return vol.Schema(
|
vol.Required(
|
||||||
{
|
CONF_EMAIL,
|
||||||
vol.Required(
|
): TextSelector(
|
||||||
CONF_EMAIL,
|
TextSelectorConfig(
|
||||||
default=email,
|
type=TextSelectorType.EMAIL,
|
||||||
): str,
|
autocomplete="email",
|
||||||
vol.Required(
|
)
|
||||||
CONF_PASSWORD,
|
),
|
||||||
default=password,
|
vol.Required(
|
||||||
): str,
|
CONF_PASSWORD,
|
||||||
}
|
): TextSelector(
|
||||||
)
|
TextSelectorConfig(
|
||||||
|
type=TextSelectorType.PASSWORD,
|
||||||
|
autocomplete="current-password",
|
||||||
|
)
|
||||||
|
),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
@ -42,7 +53,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
|
|
||||||
VERSION = 1
|
VERSION = 1
|
||||||
|
|
||||||
existing_entry: ConfigEntry | None = None
|
_existing_entry: ConfigEntry | None = None
|
||||||
|
|
||||||
async def async_step_user(
|
async def async_step_user(
|
||||||
self, user_input: dict[str, Any] | None = None
|
self, user_input: dict[str, Any] | None = None
|
||||||
@ -51,15 +62,14 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
if user_input is None:
|
if user_input is None:
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id="user",
|
step_id="user",
|
||||||
data_schema=make_schema(),
|
data_schema=CONFIG_SCHEMA,
|
||||||
)
|
)
|
||||||
|
|
||||||
return await self._validate_and_save(user_input)
|
return await self._validate_and_save(user_input)
|
||||||
|
|
||||||
async def async_step_reauth(self, entry_data: Mapping[str, Any]) -> FlowResult:
|
async def async_step_reauth(self, entry_data: Mapping[str, Any]) -> FlowResult:
|
||||||
"""Handle the initial step."""
|
"""Handle the initial step."""
|
||||||
self.existing_entry = await self.async_set_unique_id(self.context["unique_id"])
|
self._existing_entry = await self.async_set_unique_id(self.context["unique_id"])
|
||||||
|
|
||||||
return await self._validate_and_save(entry_data, step_id="reauth")
|
return await self._validate_and_save(entry_data, step_id="reauth")
|
||||||
|
|
||||||
async def _validate_and_save(
|
async def _validate_and_save(
|
||||||
@ -84,18 +94,14 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
_LOGGER.exception("Unexpected error occurred while getting meters")
|
_LOGGER.exception("Unexpected error occurred while getting meters")
|
||||||
errors["base"] = "unknown"
|
errors["base"] = "unknown"
|
||||||
else:
|
else:
|
||||||
if self.existing_entry:
|
if self._existing_entry:
|
||||||
self.hass.config_entries.async_update_entry(
|
return self.async_update_reload_and_abort(
|
||||||
self.existing_entry,
|
entry=self._existing_entry,
|
||||||
data={
|
data={
|
||||||
CONF_EMAIL: user_input[CONF_EMAIL],
|
CONF_EMAIL: user_input[CONF_EMAIL],
|
||||||
CONF_PASSWORD: user_input[CONF_PASSWORD],
|
CONF_PASSWORD: user_input[CONF_PASSWORD],
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
await self.hass.config_entries.async_reload(
|
|
||||||
self.existing_entry.entry_id
|
|
||||||
)
|
|
||||||
return self.async_abort(reason="reauth_successful")
|
|
||||||
|
|
||||||
# set unique id to title which is the account email
|
# set unique id to title which is the account email
|
||||||
await self.async_set_unique_id(user_input[CONF_EMAIL].lower())
|
await self.async_set_unique_id(user_input[CONF_EMAIL].lower())
|
||||||
@ -107,6 +113,9 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
|
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id=step_id,
|
step_id=step_id,
|
||||||
data_schema=make_schema(),
|
data_schema=self.add_suggested_values_to_schema(
|
||||||
|
CONFIG_SCHEMA,
|
||||||
|
self._existing_entry.data if self._existing_entry else user_input,
|
||||||
|
),
|
||||||
errors=errors,
|
errors=errors,
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user