mirror of
https://github.com/home-assistant/core.git
synced 2025-12-20 02:48:57 +00:00
Co-authored-by: Manu <4445816+tr4nt0r@users.noreply.github.com> Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
96 lines
2.9 KiB
Python
96 lines
2.9 KiB
Python
"""Config flow for the portainer integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from typing import Any
|
|
|
|
from pyportainer import (
|
|
Portainer,
|
|
PortainerAuthenticationError,
|
|
PortainerConnectionError,
|
|
PortainerTimeoutError,
|
|
)
|
|
import voluptuous as vol
|
|
|
|
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
|
from homeassistant.const import CONF_API_KEY, CONF_HOST
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.exceptions import HomeAssistantError
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
from .const import DOMAIN
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
STEP_USER_DATA_SCHEMA = vol.Schema(
|
|
{
|
|
vol.Required(CONF_HOST): str,
|
|
vol.Required(CONF_API_KEY): str,
|
|
}
|
|
)
|
|
|
|
|
|
async def _validate_input(hass: HomeAssistant, data: dict[str, Any]) -> None:
|
|
"""Validate the user input allows us to connect."""
|
|
|
|
client = Portainer(
|
|
api_url=data[CONF_HOST],
|
|
api_key=data[CONF_API_KEY],
|
|
session=async_get_clientsession(hass),
|
|
)
|
|
try:
|
|
await client.get_endpoints()
|
|
except PortainerAuthenticationError:
|
|
raise InvalidAuth from None
|
|
except PortainerConnectionError as err:
|
|
raise CannotConnect from err
|
|
except PortainerTimeoutError as err:
|
|
raise PortainerTimeout from err
|
|
|
|
_LOGGER.debug("Connected to Portainer API: %s", data[CONF_HOST])
|
|
|
|
|
|
class PortainerConfigFlow(ConfigFlow, domain=DOMAIN):
|
|
"""Handle a config flow for Portainer."""
|
|
|
|
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:
|
|
self._async_abort_entries_match({CONF_HOST: user_input[CONF_HOST]})
|
|
try:
|
|
await _validate_input(self.hass, user_input)
|
|
except CannotConnect:
|
|
errors["base"] = "cannot_connect"
|
|
except InvalidAuth:
|
|
errors["base"] = "invalid_auth"
|
|
except PortainerTimeout:
|
|
errors["base"] = "timeout_connect"
|
|
except Exception:
|
|
_LOGGER.exception("Unexpected exception")
|
|
errors["base"] = "unknown"
|
|
else:
|
|
await self.async_set_unique_id(user_input[CONF_API_KEY])
|
|
self._abort_if_unique_id_configured()
|
|
return self.async_create_entry(
|
|
title=user_input[CONF_HOST], data=user_input
|
|
)
|
|
|
|
return self.async_show_form(
|
|
step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
|
|
)
|
|
|
|
|
|
class CannotConnect(HomeAssistantError):
|
|
"""Error to indicate we cannot connect."""
|
|
|
|
|
|
class InvalidAuth(HomeAssistantError):
|
|
"""Error to indicate there is invalid auth."""
|
|
|
|
|
|
class PortainerTimeout(HomeAssistantError):
|
|
"""Error to indicate a timeout occurred."""
|