167 lines
5.9 KiB
Python
167 lines
5.9 KiB
Python
|
"""Config flow for Suez Water integration."""
|
||
|
from __future__ import annotations
|
||
|
|
||
|
import logging
|
||
|
from typing import Any
|
||
|
|
||
|
from pysuez import SuezClient
|
||
|
from pysuez.client import PySuezError
|
||
|
import voluptuous as vol
|
||
|
|
||
|
from homeassistant.config_entries import ConfigFlow
|
||
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||
|
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN
|
||
|
from homeassistant.data_entry_flow import AbortFlow, FlowResult
|
||
|
from homeassistant.exceptions import HomeAssistantError
|
||
|
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
|
||
|
|
||
|
from .const import CONF_COUNTER_ID, DOMAIN
|
||
|
|
||
|
_LOGGER = logging.getLogger(__name__)
|
||
|
|
||
|
ISSUE_PLACEHOLDER = {"url": "/config/integrations/dashboard/add?domain=suez_water"}
|
||
|
STEP_USER_DATA_SCHEMA = vol.Schema(
|
||
|
{
|
||
|
vol.Required(CONF_USERNAME): str,
|
||
|
vol.Required(CONF_PASSWORD): str,
|
||
|
vol.Required(CONF_COUNTER_ID): str,
|
||
|
}
|
||
|
)
|
||
|
|
||
|
|
||
|
def validate_input(data: dict[str, Any]) -> None:
|
||
|
"""Validate the user input allows us to connect.
|
||
|
|
||
|
Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user.
|
||
|
"""
|
||
|
try:
|
||
|
client = SuezClient(
|
||
|
data[CONF_USERNAME],
|
||
|
data[CONF_PASSWORD],
|
||
|
data[CONF_COUNTER_ID],
|
||
|
provider=None,
|
||
|
)
|
||
|
if not client.check_credentials():
|
||
|
raise InvalidAuth
|
||
|
except PySuezError:
|
||
|
raise CannotConnect
|
||
|
|
||
|
|
||
|
class SuezWaterConfigFlow(ConfigFlow, domain=DOMAIN):
|
||
|
"""Handle a config flow for Suez Water."""
|
||
|
|
||
|
VERSION = 1
|
||
|
|
||
|
async def async_step_user(
|
||
|
self, user_input: dict[str, Any] | None = None
|
||
|
) -> FlowResult:
|
||
|
"""Handle the initial step."""
|
||
|
errors: dict[str, str] = {}
|
||
|
if user_input is not None:
|
||
|
await self.async_set_unique_id(user_input[CONF_USERNAME])
|
||
|
self._abort_if_unique_id_configured()
|
||
|
try:
|
||
|
await self.hass.async_add_executor_job(validate_input, user_input)
|
||
|
except CannotConnect:
|
||
|
errors["base"] = "cannot_connect"
|
||
|
except InvalidAuth:
|
||
|
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_USERNAME], data=user_input
|
||
|
)
|
||
|
|
||
|
return self.async_show_form(
|
||
|
step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
|
||
|
)
|
||
|
|
||
|
async def async_step_import(self, user_input: dict[str, Any]) -> FlowResult:
|
||
|
"""Import the yaml config."""
|
||
|
await self.async_set_unique_id(user_input[CONF_USERNAME])
|
||
|
try:
|
||
|
self._abort_if_unique_id_configured()
|
||
|
except AbortFlow as err:
|
||
|
async_create_issue(
|
||
|
self.hass,
|
||
|
HOMEASSISTANT_DOMAIN,
|
||
|
f"deprecated_yaml_{DOMAIN}",
|
||
|
breaks_in_ha_version="2024.7.0",
|
||
|
is_fixable=False,
|
||
|
issue_domain=DOMAIN,
|
||
|
severity=IssueSeverity.WARNING,
|
||
|
translation_key="deprecated_yaml",
|
||
|
translation_placeholders={
|
||
|
"domain": DOMAIN,
|
||
|
"integration_title": "Suez Water",
|
||
|
},
|
||
|
)
|
||
|
raise err
|
||
|
try:
|
||
|
await self.hass.async_add_executor_job(validate_input, user_input)
|
||
|
except CannotConnect:
|
||
|
async_create_issue(
|
||
|
self.hass,
|
||
|
DOMAIN,
|
||
|
"deprecated_yaml_import_issue_cannot_connect",
|
||
|
breaks_in_ha_version="2024.7.0",
|
||
|
is_fixable=False,
|
||
|
issue_domain=DOMAIN,
|
||
|
severity=IssueSeverity.WARNING,
|
||
|
translation_key="deprecated_yaml_import_issue_cannot_connect",
|
||
|
translation_placeholders=ISSUE_PLACEHOLDER,
|
||
|
)
|
||
|
return self.async_abort(reason="cannot_connect")
|
||
|
except InvalidAuth:
|
||
|
async_create_issue(
|
||
|
self.hass,
|
||
|
DOMAIN,
|
||
|
"deprecated_yaml_import_issue_invalid_auth",
|
||
|
breaks_in_ha_version="2024.7.0",
|
||
|
is_fixable=False,
|
||
|
issue_domain=DOMAIN,
|
||
|
severity=IssueSeverity.WARNING,
|
||
|
translation_key="deprecated_yaml_import_issue_invalid_auth",
|
||
|
translation_placeholders=ISSUE_PLACEHOLDER,
|
||
|
)
|
||
|
return self.async_abort(reason="invalid_auth")
|
||
|
except Exception: # pylint: disable=broad-except
|
||
|
_LOGGER.exception("Unexpected exception")
|
||
|
async_create_issue(
|
||
|
self.hass,
|
||
|
DOMAIN,
|
||
|
"deprecated_yaml_import_issue_unknown",
|
||
|
breaks_in_ha_version="2024.7.0",
|
||
|
is_fixable=False,
|
||
|
issue_domain=DOMAIN,
|
||
|
severity=IssueSeverity.WARNING,
|
||
|
translation_key="deprecated_yaml_import_issue_unknown",
|
||
|
translation_placeholders=ISSUE_PLACEHOLDER,
|
||
|
)
|
||
|
return self.async_abort(reason="unknown")
|
||
|
async_create_issue(
|
||
|
self.hass,
|
||
|
HOMEASSISTANT_DOMAIN,
|
||
|
f"deprecated_yaml_{DOMAIN}",
|
||
|
breaks_in_ha_version="2024.7.0",
|
||
|
is_fixable=False,
|
||
|
issue_domain=DOMAIN,
|
||
|
severity=IssueSeverity.WARNING,
|
||
|
translation_key="deprecated_yaml",
|
||
|
translation_placeholders={
|
||
|
"domain": DOMAIN,
|
||
|
"integration_title": "Suez Water",
|
||
|
},
|
||
|
)
|
||
|
return self.async_create_entry(title=user_input[CONF_USERNAME], data=user_input)
|
||
|
|
||
|
|
||
|
class CannotConnect(HomeAssistantError):
|
||
|
"""Error to indicate we cannot connect."""
|
||
|
|
||
|
|
||
|
class InvalidAuth(HomeAssistantError):
|
||
|
"""Error to indicate there is invalid auth."""
|