47 lines
1.7 KiB
Python
47 lines
1.7 KiB
Python
"""UI configuration flow for GREE Controller."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
import voluptuous as vol
|
|
|
|
from homeassistant import config_entries
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
from .api import GreeControllerApiError, GreeControllerClient
|
|
from .const import CONF_TOKEN, CONF_URL, DOMAIN
|
|
|
|
|
|
class GreeControllerConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|
"""Configure a standalone GREE Controller instance."""
|
|
|
|
VERSION = 1
|
|
|
|
async def async_step_user(self, user_input: dict[str, Any] | None = None):
|
|
"""Handle the initial connection form."""
|
|
errors: dict[str, str] = {}
|
|
if user_input is not None:
|
|
url = str(user_input[CONF_URL]).strip().rstrip("/")
|
|
token = str(user_input[CONF_TOKEN]).strip()
|
|
client = GreeControllerClient(async_get_clientsession(self.hass), url, token)
|
|
try:
|
|
await client.devices()
|
|
except GreeControllerApiError as err:
|
|
errors["base"] = "invalid_auth" if "authentication" in str(err).lower() else "cannot_connect"
|
|
else:
|
|
await self.async_set_unique_id("gree-controller")
|
|
self._abort_if_unique_id_configured()
|
|
return self.async_create_entry(
|
|
title="GREE Controller",
|
|
data={CONF_URL: url, CONF_TOKEN: token},
|
|
)
|
|
|
|
schema = vol.Schema(
|
|
{
|
|
vol.Required(CONF_URL, default="http://gree-controller:8787"): str,
|
|
vol.Required(CONF_TOKEN): str,
|
|
}
|
|
)
|
|
return self.async_show_form(step_id="user", data_schema=schema, errors=errors)
|