hass-core/homeassistant/components/aquacell/config_flow.py
Jordi 7219a4fa98
Add Aquacell integration (#117117)
* Initial commit

* Support changed API
Change sensor entity descriptions

* Fix sensor not handling coordinator update

* Implement re-authentication flow and handle token expiry

* Bump aioaquacell

* Bump aioaquacell

* Cleanup and initial tests

* Fixes for config flow tests

* Cleanup

* Fixes

* Formatted

* Use config entry runtime
Use icon translations
Removed reauth
Removed last updated sensor
Changed lid in place to binary sensor
Cleanup

* Remove reauth strings

* Removed binary_sensor platform
Fixed sensors not updating properly

* Remove reauth tests
Bump aioaquacell

* Moved softener property to entity class
Inlined validate_input method
Renaming of entities
Do a single async_add_entities call to add all entities
Reduced code in try blocks

* Made tests parameterized and use test fixture for api
Cleaned up unused code
Removed traces of reauth

* Add check if refresh token is expired
Add tests

* Add missing unique_id to config entry mock
Inlined _update_config_entry_refresh_token method
Fix incorrect test method name and comment

* Add snapshot test
Changed WiFi level to WiFi strength

* Bump aioaquacell to 0.1.7

* Move test_coordinator tests to test_init
Add test for duplicate config entry
2024-06-06 22:33:58 +02:00

71 lines
2.3 KiB
Python

"""Config flow for Aquacell integration."""
from __future__ import annotations
from datetime import datetime
import logging
from typing import Any
from aioaquacell import ApiException, AquacellApi, AuthenticationFailed
import voluptuous as vol
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import CONF_REFRESH_TOKEN, CONF_REFRESH_TOKEN_CREATION_TIME, DOMAIN
_LOGGER = logging.getLogger(__name__)
DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_EMAIL): str,
vol.Required(CONF_PASSWORD): str,
}
)
class AquaCellConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Aquacell."""
VERSION = 1
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:
await self.async_set_unique_id(
user_input[CONF_EMAIL].lower(), raise_on_progress=False
)
self._abort_if_unique_id_configured()
session = async_get_clientsession(self.hass)
api = AquacellApi(session)
try:
refresh_token = await api.authenticate(
user_input[CONF_EMAIL], user_input[CONF_PASSWORD]
)
except ApiException:
errors["base"] = "cannot_connect"
except AuthenticationFailed:
errors["base"] = "invalid_auth"
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Unexpected exception")
errors["base"] = "unknown"
else:
return self.async_create_entry(
title=user_input[CONF_EMAIL],
data={
**user_input,
CONF_REFRESH_TOKEN: refresh_token,
CONF_REFRESH_TOKEN_CREATION_TIME: datetime.now().timestamp(),
},
)
return self.async_show_form(
step_id="user",
data_schema=DATA_SCHEMA,
errors=errors,
)