2024-03-22 23:27:57 +00:00
|
|
|
"""Config flow for Lupusec integration."""
|
2024-01-25 17:52:30 +00:00
|
|
|
|
2024-02-06 00:20:14 +00:00
|
|
|
from json import JSONDecodeError
|
2024-01-25 17:52:30 +00:00
|
|
|
import logging
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
import lupupy
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2024-04-05 19:38:11 +00:00
|
|
|
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
2024-01-25 17:52:30 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_HOST,
|
|
|
|
CONF_IP_ADDRESS,
|
|
|
|
CONF_NAME,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_USERNAME,
|
|
|
|
)
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
|
|
|
|
|
|
|
from .const import DOMAIN
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DATA_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_HOST): str,
|
|
|
|
vol.Required(CONF_USERNAME): str,
|
|
|
|
vol.Required(CONF_PASSWORD): str,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-04-05 19:38:11 +00:00
|
|
|
class LupusecConfigFlowHandler(ConfigFlow, domain=DOMAIN):
|
2024-01-25 17:52:30 +00:00
|
|
|
"""Lupusec config flow."""
|
|
|
|
|
|
|
|
async def async_step_user(
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
2024-04-05 19:38:11 +00:00
|
|
|
) -> ConfigFlowResult:
|
2024-01-25 17:52:30 +00:00
|
|
|
"""Handle a flow initiated by the user."""
|
|
|
|
errors = {}
|
|
|
|
|
|
|
|
if user_input is not None:
|
|
|
|
self._async_abort_entries_match(user_input)
|
|
|
|
host = user_input[CONF_HOST]
|
|
|
|
username = user_input[CONF_USERNAME]
|
|
|
|
password = user_input[CONF_PASSWORD]
|
|
|
|
|
|
|
|
try:
|
|
|
|
await test_host_connection(self.hass, host, username, password)
|
|
|
|
except CannotConnect:
|
|
|
|
errors["base"] = "cannot_connect"
|
2024-02-06 00:20:14 +00:00
|
|
|
except JSONDecodeError:
|
|
|
|
errors["base"] = "cannot_connect"
|
2024-05-07 12:00:27 +00:00
|
|
|
except Exception:
|
2024-01-25 17:52:30 +00:00
|
|
|
_LOGGER.exception("Unexpected exception")
|
|
|
|
errors["base"] = "unknown"
|
|
|
|
|
|
|
|
else:
|
|
|
|
return self.async_create_entry(
|
|
|
|
title=host,
|
|
|
|
data=user_input,
|
|
|
|
)
|
|
|
|
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="user", data_schema=DATA_SCHEMA, errors=errors
|
|
|
|
)
|
|
|
|
|
2024-04-05 19:38:11 +00:00
|
|
|
async def async_step_import(self, user_input: dict[str, Any]) -> ConfigFlowResult:
|
2024-01-25 17:52:30 +00:00
|
|
|
"""Import the yaml config."""
|
|
|
|
self._async_abort_entries_match(
|
|
|
|
{
|
|
|
|
CONF_HOST: user_input[CONF_IP_ADDRESS],
|
|
|
|
CONF_USERNAME: user_input[CONF_USERNAME],
|
|
|
|
CONF_PASSWORD: user_input[CONF_PASSWORD],
|
|
|
|
}
|
|
|
|
)
|
|
|
|
host = user_input[CONF_IP_ADDRESS]
|
|
|
|
username = user_input[CONF_USERNAME]
|
|
|
|
password = user_input[CONF_PASSWORD]
|
|
|
|
try:
|
|
|
|
await test_host_connection(self.hass, host, username, password)
|
|
|
|
except CannotConnect:
|
|
|
|
return self.async_abort(reason="cannot_connect")
|
2024-02-06 00:20:14 +00:00
|
|
|
except JSONDecodeError:
|
|
|
|
return self.async_abort(reason="cannot_connect")
|
2024-05-07 12:00:27 +00:00
|
|
|
except Exception:
|
2024-01-25 17:52:30 +00:00
|
|
|
_LOGGER.exception("Unexpected exception")
|
|
|
|
return self.async_abort(reason="unknown")
|
|
|
|
|
|
|
|
return self.async_create_entry(
|
|
|
|
title=user_input.get(CONF_NAME, host),
|
|
|
|
data={
|
|
|
|
CONF_HOST: host,
|
|
|
|
CONF_USERNAME: username,
|
|
|
|
CONF_PASSWORD: password,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_host_connection(
|
|
|
|
hass: HomeAssistant, host: str, username: str, password: str
|
|
|
|
):
|
|
|
|
"""Test if the host is reachable and is actually a Lupusec device."""
|
|
|
|
|
|
|
|
try:
|
|
|
|
await hass.async_add_executor_job(lupupy.Lupusec, username, password, host)
|
2024-02-06 11:17:39 +00:00
|
|
|
except lupupy.LupusecException as ex:
|
2024-01-25 17:52:30 +00:00
|
|
|
_LOGGER.error("Failed to connect to Lupusec device at %s", host)
|
2024-02-06 11:17:39 +00:00
|
|
|
raise CannotConnect from ex
|
2024-01-25 17:52:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CannotConnect(HomeAssistantError):
|
|
|
|
"""Error to indicate we cannot connect."""
|