mirror of
https://github.com/home-assistant/core.git
synced 2026-09-13 12:10:22 +01:00
71 lines
2.2 KiB
Python
71 lines
2.2 KiB
Python
"""Config flow for the Sunsynk integration."""
|
|
|
|
from typing import Any, override
|
|
|
|
from sunsynk.client import SunsynkClient
|
|
from sunsynk.exceptions import SunsynkAuthenticationError, SunsynkConnectionError
|
|
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 homeassistant.helpers.selector import (
|
|
TextSelector,
|
|
TextSelectorConfig,
|
|
TextSelectorType,
|
|
)
|
|
|
|
from .const import DOMAIN
|
|
|
|
STEP_USER_DATA_SCHEMA = vol.Schema(
|
|
{
|
|
vol.Required(CONF_USERNAME): TextSelector(
|
|
TextSelectorConfig(type=TextSelectorType.EMAIL, autocomplete="username")
|
|
),
|
|
vol.Required(CONF_PASSWORD): TextSelector(
|
|
TextSelectorConfig(
|
|
type=TextSelectorType.PASSWORD, autocomplete="current-password"
|
|
)
|
|
),
|
|
}
|
|
)
|
|
|
|
|
|
class SunsynkConfigFlow(ConfigFlow, domain=DOMAIN):
|
|
"""Handle a config flow for Sunsynk."""
|
|
|
|
VERSION = 1
|
|
|
|
@override
|
|
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:
|
|
client = SunsynkClient(
|
|
user_input[CONF_USERNAME],
|
|
user_input[CONF_PASSWORD],
|
|
session=async_get_clientsession(self.hass),
|
|
)
|
|
try:
|
|
user = await client.get_user()
|
|
except SunsynkAuthenticationError:
|
|
errors["base"] = "invalid_auth"
|
|
except SunsynkConnectionError:
|
|
errors["base"] = "cannot_connect"
|
|
else:
|
|
await self.async_set_unique_id(str(user.id))
|
|
self._abort_if_unique_id_configured()
|
|
return self.async_create_entry(
|
|
title=user_input[CONF_USERNAME], data=user_input
|
|
)
|
|
|
|
return self.async_show_form(
|
|
step_id="user",
|
|
data_schema=self.add_suggested_values_to_schema(
|
|
STEP_USER_DATA_SCHEMA, user_input
|
|
),
|
|
errors=errors,
|
|
)
|