1
mirror of https://github.com/home-assistant/core synced 2024-10-04 07:58:43 +02:00
ha-core/homeassistant/components/vallox/config_flow.py
Sebastian Lövdahl e680ec6247
Remove deprecated Vallox YAML configuration (#91096)
Co-authored-by: G Johansson <goran.johansson@shiftit.se>
2023-05-06 10:23:14 +02:00

86 lines
2.3 KiB
Python

"""Config flow for the Vallox integration."""
from __future__ import annotations
import logging
from typing import Any
from vallox_websocket_api import Vallox, ValloxApiException
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.const import CONF_HOST, CONF_NAME
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResult
from homeassistant.exceptions import HomeAssistantError
from homeassistant.util.network import is_ip_address
from .const import DEFAULT_NAME, DOMAIN
_LOGGER = logging.getLogger(__name__)
STEP_USER_DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_HOST): str,
}
)
async def validate_host(hass: HomeAssistant, host: str) -> None:
"""Validate that the user input allows us to connect."""
if not is_ip_address(host):
raise InvalidHost(f"Invalid IP address: {host}")
client = Vallox(host)
await client.get_info()
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for the Vallox integration."""
VERSION = 1
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle the initial step."""
if user_input is None:
return self.async_show_form(
step_id="user",
data_schema=STEP_USER_DATA_SCHEMA,
)
errors = {}
host = user_input[CONF_HOST]
self._async_abort_entries_match({CONF_HOST: host})
try:
await validate_host(self.hass, host)
except InvalidHost:
errors[CONF_HOST] = "invalid_host"
except ValloxApiException:
errors[CONF_HOST] = "cannot_connect"
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Unexpected exception")
errors[CONF_HOST] = "unknown"
else:
return self.async_create_entry(
title=DEFAULT_NAME,
data={
**user_input,
CONF_NAME: DEFAULT_NAME,
},
)
return self.async_show_form(
step_id="user",
data_schema=STEP_USER_DATA_SCHEMA,
errors=errors,
)
class InvalidHost(HomeAssistantError):
"""Error to indicate an invalid host was input."""