mirror of
https://github.com/home-assistant/core.git
synced 2026-04-02 08:26:41 +01:00
129 lines
4.3 KiB
Python
129 lines
4.3 KiB
Python
"""Config flow for the Fresh-r integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Mapping
|
|
from typing import Any
|
|
|
|
from aiohttp import ClientError
|
|
from pyfreshr import FreshrClient
|
|
from pyfreshr.exceptions import LoginError
|
|
import voluptuous as vol
|
|
|
|
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
from .const import DOMAIN, LOGGER
|
|
|
|
STEP_USER_DATA_SCHEMA = vol.Schema(
|
|
{
|
|
vol.Required(CONF_USERNAME): str,
|
|
vol.Required(CONF_PASSWORD): str,
|
|
}
|
|
)
|
|
|
|
|
|
class FreshrFlowHandler(ConfigFlow, domain=DOMAIN):
|
|
"""Handle a config flow for Fresh-r."""
|
|
|
|
VERSION = 1
|
|
MINOR_VERSION = 1
|
|
|
|
async def _validate_input(self, username: str, password: str) -> str | None:
|
|
"""Validate credentials, returning an error key or None on success."""
|
|
client = FreshrClient(session=async_get_clientsession(self.hass))
|
|
try:
|
|
await client.login(username, password)
|
|
except LoginError:
|
|
return "invalid_auth"
|
|
except ClientError:
|
|
return "cannot_connect"
|
|
except Exception: # noqa: BLE001
|
|
LOGGER.exception("Unexpected exception")
|
|
return "unknown"
|
|
return None
|
|
|
|
async def async_step_user(
|
|
self, user_input: dict[str, Any] | None = None
|
|
) -> ConfigFlowResult:
|
|
"""Handle the initial step."""
|
|
errors: dict[str, str] = {}
|
|
if user_input is not None:
|
|
error = await self._validate_input(
|
|
user_input[CONF_USERNAME], user_input[CONF_PASSWORD]
|
|
)
|
|
if error:
|
|
errors["base"] = error
|
|
else:
|
|
await self.async_set_unique_id(user_input[CONF_USERNAME].lower())
|
|
self._abort_if_unique_id_configured()
|
|
return self.async_create_entry(
|
|
title=f"Fresh-r ({user_input[CONF_USERNAME]})",
|
|
data=user_input,
|
|
)
|
|
|
|
return self.async_show_form(
|
|
step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
|
|
)
|
|
|
|
async def async_step_reconfigure(
|
|
self, user_input: dict[str, Any] | None = None
|
|
) -> ConfigFlowResult:
|
|
"""Handle reconfiguration."""
|
|
reconfigure_entry = self._get_reconfigure_entry()
|
|
errors: dict[str, str] = {}
|
|
|
|
if user_input is not None:
|
|
error = await self._validate_input(
|
|
reconfigure_entry.data[CONF_USERNAME], user_input[CONF_PASSWORD]
|
|
)
|
|
if error:
|
|
errors["base"] = error
|
|
else:
|
|
return self.async_update_reload_and_abort(
|
|
reconfigure_entry,
|
|
data_updates={CONF_PASSWORD: user_input[CONF_PASSWORD]},
|
|
)
|
|
|
|
return self.async_show_form(
|
|
step_id="reconfigure",
|
|
data_schema=vol.Schema({vol.Required(CONF_PASSWORD): str}),
|
|
description_placeholders={
|
|
CONF_USERNAME: reconfigure_entry.data[CONF_USERNAME]
|
|
},
|
|
errors=errors,
|
|
)
|
|
|
|
async def async_step_reauth(
|
|
self, _user_input: Mapping[str, Any]
|
|
) -> ConfigFlowResult:
|
|
"""Handle reauthentication."""
|
|
return await self.async_step_reauth_confirm()
|
|
|
|
async def async_step_reauth_confirm(
|
|
self, user_input: dict[str, Any] | None = None
|
|
) -> ConfigFlowResult:
|
|
"""Handle reauthentication confirmation."""
|
|
errors: dict[str, str] = {}
|
|
reauth_entry = self._get_reauth_entry()
|
|
|
|
if user_input is not None:
|
|
error = await self._validate_input(
|
|
reauth_entry.data[CONF_USERNAME], user_input[CONF_PASSWORD]
|
|
)
|
|
if error:
|
|
errors["base"] = error
|
|
else:
|
|
return self.async_update_reload_and_abort(
|
|
reauth_entry,
|
|
data_updates={CONF_PASSWORD: user_input[CONF_PASSWORD]},
|
|
)
|
|
|
|
return self.async_show_form(
|
|
step_id="reauth_confirm",
|
|
data_schema=vol.Schema({vol.Required(CONF_PASSWORD): str}),
|
|
description_placeholders={CONF_USERNAME: reauth_entry.data[CONF_USERNAME]},
|
|
errors=errors,
|
|
)
|