mirror of
https://github.com/home-assistant/core.git
synced 2025-11-08 18:39:30 +00:00
193 lines
6.4 KiB
Python
193 lines
6.4 KiB
Python
"""Config flow for the portainer integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Mapping
|
|
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_TOKEN, CONF_URL, CONF_VERIFY_SSL
|
|
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_URL): str,
|
|
vol.Required(CONF_API_TOKEN): str,
|
|
vol.Optional(CONF_VERIFY_SSL, default=True): bool,
|
|
}
|
|
)
|
|
|
|
|
|
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_URL],
|
|
api_key=data[CONF_API_TOKEN],
|
|
session=async_get_clientsession(
|
|
hass=hass, verify_ssl=data.get(CONF_VERIFY_SSL, True)
|
|
),
|
|
)
|
|
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_URL])
|
|
|
|
|
|
class PortainerConfigFlow(ConfigFlow, domain=DOMAIN):
|
|
"""Handle a config flow for Portainer."""
|
|
|
|
VERSION = 2
|
|
|
|
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_URL: user_input[CONF_URL]})
|
|
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_TOKEN])
|
|
self._abort_if_unique_id_configured()
|
|
return self.async_create_entry(
|
|
title=user_input[CONF_URL], data=user_input
|
|
)
|
|
|
|
return self.async_show_form(
|
|
step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
|
|
)
|
|
|
|
async def async_step_reauth(
|
|
self, entry_data: Mapping[str, Any]
|
|
) -> ConfigFlowResult:
|
|
"""Perform reauth when Portainer API authentication fails."""
|
|
return await self.async_step_reauth_confirm()
|
|
|
|
async def async_step_reauth_confirm(
|
|
self, user_input: dict[str, Any] | None = None
|
|
) -> ConfigFlowResult:
|
|
"""Handle reauth: ask for new API token and validate."""
|
|
errors: dict[str, str] = {}
|
|
reauth_entry = self._get_reauth_entry()
|
|
if user_input is not None:
|
|
try:
|
|
await _validate_input(
|
|
self.hass,
|
|
data={
|
|
**reauth_entry.data,
|
|
CONF_API_TOKEN: user_input[CONF_API_TOKEN],
|
|
},
|
|
)
|
|
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:
|
|
return self.async_update_reload_and_abort(
|
|
reauth_entry,
|
|
data_updates={CONF_API_TOKEN: user_input[CONF_API_TOKEN]},
|
|
)
|
|
|
|
return self.async_show_form(
|
|
step_id="reauth_confirm",
|
|
data_schema=vol.Schema({vol.Required(CONF_API_TOKEN): str}),
|
|
errors=errors,
|
|
)
|
|
|
|
async def async_step_reconfigure(
|
|
self, user_input: dict[str, Any] | None = None
|
|
) -> ConfigFlowResult:
|
|
"""Handle reconfiguration of the integration."""
|
|
errors: dict[str, str] = {}
|
|
reconf_entry = self._get_reconfigure_entry()
|
|
suggested_values = {
|
|
CONF_URL: reconf_entry.data[CONF_URL],
|
|
CONF_API_TOKEN: reconf_entry.data[CONF_API_TOKEN],
|
|
CONF_VERIFY_SSL: reconf_entry.data[CONF_VERIFY_SSL],
|
|
}
|
|
|
|
if user_input:
|
|
try:
|
|
await _validate_input(
|
|
self.hass,
|
|
data={
|
|
**reconf_entry.data,
|
|
**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:
|
|
return self.async_update_reload_and_abort(
|
|
reconf_entry,
|
|
data_updates={
|
|
CONF_URL: user_input[CONF_URL],
|
|
CONF_API_TOKEN: user_input[CONF_API_TOKEN],
|
|
CONF_VERIFY_SSL: user_input[CONF_VERIFY_SSL],
|
|
},
|
|
)
|
|
|
|
return self.async_show_form(
|
|
step_id="reconfigure",
|
|
data_schema=self.add_suggested_values_to_schema(
|
|
data_schema=STEP_USER_DATA_SCHEMA,
|
|
suggested_values=user_input or suggested_values,
|
|
),
|
|
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."""
|