2022-03-08 21:28:39 +00:00
|
|
|
"""Config flow for Airzone."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from aioairzone.common import ConnectionOptions
|
2022-04-13 17:12:21 +00:00
|
|
|
from aioairzone.exceptions import AirzoneError, InvalidSystem
|
2022-03-28 23:05:47 +00:00
|
|
|
from aioairzone.localapi import AirzoneLocalApi
|
2022-03-08 21:28:39 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant import config_entries
|
2022-04-13 17:12:21 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_ID, CONF_PORT
|
2022-03-08 21:28:39 +00:00
|
|
|
from homeassistant.data_entry_flow import FlowResult
|
|
|
|
from homeassistant.helpers import aiohttp_client
|
|
|
|
|
2022-04-13 17:12:21 +00:00
|
|
|
from .const import DEFAULT_LOCAL_API_PORT, DEFAULT_SYSTEM_ID, DOMAIN
|
|
|
|
|
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_HOST): str,
|
|
|
|
vol.Required(CONF_PORT, default=DEFAULT_LOCAL_API_PORT): int,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
SYSTEM_ID_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_HOST): str,
|
|
|
|
vol.Required(CONF_PORT, default=DEFAULT_LOCAL_API_PORT): int,
|
|
|
|
vol.Required(CONF_ID, default=1): int,
|
|
|
|
}
|
|
|
|
)
|
2022-03-08 21:28:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|
|
|
"""Handle config flow for an Airzone device."""
|
|
|
|
|
|
|
|
async def async_step_user(
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
) -> FlowResult:
|
|
|
|
"""Handle the initial step."""
|
2022-04-13 17:12:21 +00:00
|
|
|
data_schema = CONFIG_SCHEMA
|
2022-03-08 21:28:39 +00:00
|
|
|
errors = {}
|
|
|
|
|
|
|
|
if user_input is not None:
|
2022-04-13 17:12:21 +00:00
|
|
|
system_id = user_input.get(CONF_ID, DEFAULT_SYSTEM_ID)
|
|
|
|
|
2022-03-08 21:28:39 +00:00
|
|
|
self._async_abort_entries_match(
|
|
|
|
{
|
|
|
|
CONF_HOST: user_input[CONF_HOST],
|
|
|
|
CONF_PORT: user_input[CONF_PORT],
|
2022-04-13 17:12:21 +00:00
|
|
|
CONF_ID: system_id,
|
2022-03-08 21:28:39 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-03-09 19:51:39 +00:00
|
|
|
airzone = AirzoneLocalApi(
|
|
|
|
aiohttp_client.async_get_clientsession(self.hass),
|
|
|
|
ConnectionOptions(
|
|
|
|
user_input[CONF_HOST],
|
|
|
|
user_input[CONF_PORT],
|
2022-04-13 17:12:21 +00:00
|
|
|
system_id,
|
2022-03-09 19:51:39 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2022-03-08 21:28:39 +00:00
|
|
|
try:
|
|
|
|
await airzone.validate_airzone()
|
2022-04-13 17:12:21 +00:00
|
|
|
except InvalidSystem:
|
|
|
|
data_schema = SYSTEM_ID_SCHEMA
|
|
|
|
errors["base"] = "invalid_system_id"
|
|
|
|
except AirzoneError:
|
2022-03-08 21:28:39 +00:00
|
|
|
errors["base"] = "cannot_connect"
|
|
|
|
else:
|
|
|
|
title = f"Airzone {user_input[CONF_HOST]}:{user_input[CONF_PORT]}"
|
|
|
|
return self.async_create_entry(title=title, data=user_input)
|
|
|
|
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="user",
|
2022-04-13 17:12:21 +00:00
|
|
|
data_schema=data_schema,
|
2022-03-08 21:28:39 +00:00
|
|
|
errors=errors,
|
|
|
|
)
|