mirror of https://github.com/home-assistant/core
64 lines
1.9 KiB
Python
64 lines
1.9 KiB
Python
"""Config flow for Ecoforest integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from typing import Any
|
|
|
|
from httpx import BasicAuth
|
|
from pyecoforest.api import EcoforestApi
|
|
from pyecoforest.exceptions import EcoforestAuthenticationRequired
|
|
import voluptuous as vol
|
|
|
|
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
|
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
|
|
|
|
from .const import DOMAIN, MANUFACTURER
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
STEP_USER_DATA_SCHEMA = vol.Schema(
|
|
{
|
|
vol.Required(CONF_HOST): str,
|
|
vol.Required(CONF_USERNAME): str,
|
|
vol.Required(CONF_PASSWORD): str,
|
|
}
|
|
)
|
|
|
|
|
|
class EcoForestConfigFlow(ConfigFlow, domain=DOMAIN):
|
|
"""Handle a config flow for Ecoforest."""
|
|
|
|
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:
|
|
try:
|
|
api = EcoforestApi(
|
|
user_input[CONF_HOST],
|
|
BasicAuth(user_input[CONF_USERNAME], user_input[CONF_PASSWORD]),
|
|
)
|
|
device = await api.get()
|
|
except EcoforestAuthenticationRequired:
|
|
errors["base"] = "invalid_auth"
|
|
except Exception:
|
|
_LOGGER.exception("Unexpected exception")
|
|
errors["base"] = "cannot_connect"
|
|
else:
|
|
await self.async_set_unique_id(device.serial_number)
|
|
self._abort_if_unique_id_configured()
|
|
return self.async_create_entry(
|
|
title=f"{MANUFACTURER} {device.serial_number}", data=user_input
|
|
)
|
|
|
|
return self.async_show_form(
|
|
step_id="user",
|
|
data_schema=STEP_USER_DATA_SCHEMA,
|
|
errors=errors,
|
|
)
|