mirror of
https://github.com/home-assistant/core.git
synced 2026-04-17 23:53:49 +01:00
Co-authored-by: mik-laj <12058428+mik-laj@users.noreply.github.com> Co-authored-by: Claude Haiku 4.5 <noreply@anthropic.com>
117 lines
4.3 KiB
Python
117 lines
4.3 KiB
Python
"""Config flow for the AWS S3 integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
from urllib.parse import urlparse
|
|
|
|
from aiobotocore.session import AioSession
|
|
from botocore.exceptions import ClientError, ConnectionError, ParamValidationError
|
|
import voluptuous as vol
|
|
|
|
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
|
from homeassistant.helpers import config_validation as cv
|
|
from homeassistant.helpers.selector import (
|
|
TextSelector,
|
|
TextSelectorConfig,
|
|
TextSelectorType,
|
|
)
|
|
|
|
from .const import (
|
|
AWS_DOMAIN,
|
|
CONF_ACCESS_KEY_ID,
|
|
CONF_BUCKET,
|
|
CONF_ENDPOINT_URL,
|
|
CONF_PREFIX,
|
|
CONF_SECRET_ACCESS_KEY,
|
|
DEFAULT_ENDPOINT_URL,
|
|
DESCRIPTION_AWS_S3_DOCS_URL,
|
|
DESCRIPTION_BOTO3_DOCS_URL,
|
|
DOMAIN,
|
|
)
|
|
|
|
STEP_USER_DATA_SCHEMA = vol.Schema(
|
|
{
|
|
vol.Required(CONF_ACCESS_KEY_ID): cv.string,
|
|
vol.Required(CONF_SECRET_ACCESS_KEY): TextSelector(
|
|
config=TextSelectorConfig(type=TextSelectorType.PASSWORD)
|
|
),
|
|
vol.Required(CONF_BUCKET): cv.string,
|
|
vol.Required(CONF_ENDPOINT_URL, default=DEFAULT_ENDPOINT_URL): TextSelector(
|
|
config=TextSelectorConfig(type=TextSelectorType.URL)
|
|
),
|
|
vol.Optional(CONF_PREFIX, default=""): cv.string,
|
|
}
|
|
)
|
|
|
|
|
|
class S3ConfigFlow(ConfigFlow, domain=DOMAIN):
|
|
"""Handle a config flow."""
|
|
|
|
async def async_step_user(
|
|
self, user_input: dict[str, Any] | None = None
|
|
) -> ConfigFlowResult:
|
|
"""Handle a flow initiated by the user."""
|
|
errors: dict[str, str] = {}
|
|
|
|
if user_input is not None:
|
|
normalized_prefix = user_input.get(CONF_PREFIX, "").strip("/")
|
|
# Check for existing entries, treating missing prefix as empty
|
|
for entry in self._async_current_entries(include_ignore=False):
|
|
entry_prefix = (entry.data.get(CONF_PREFIX) or "").strip("/")
|
|
if (
|
|
entry.data.get(CONF_BUCKET) == user_input[CONF_BUCKET]
|
|
and entry.data.get(CONF_ENDPOINT_URL)
|
|
== user_input[CONF_ENDPOINT_URL]
|
|
and entry_prefix == normalized_prefix
|
|
):
|
|
return self.async_abort(reason="already_configured")
|
|
|
|
hostname = urlparse(user_input[CONF_ENDPOINT_URL]).hostname
|
|
if not hostname or not hostname.endswith(AWS_DOMAIN):
|
|
errors[CONF_ENDPOINT_URL] = "invalid_endpoint_url"
|
|
else:
|
|
try:
|
|
session = AioSession()
|
|
async with session.create_client(
|
|
"s3",
|
|
endpoint_url=user_input.get(CONF_ENDPOINT_URL),
|
|
aws_secret_access_key=user_input[CONF_SECRET_ACCESS_KEY],
|
|
aws_access_key_id=user_input[CONF_ACCESS_KEY_ID],
|
|
) as client:
|
|
await client.head_bucket(Bucket=user_input[CONF_BUCKET])
|
|
except ClientError:
|
|
errors["base"] = "invalid_credentials"
|
|
except ParamValidationError as err:
|
|
if "Invalid bucket name" in str(err):
|
|
errors[CONF_BUCKET] = "invalid_bucket_name"
|
|
except ValueError:
|
|
errors[CONF_ENDPOINT_URL] = "invalid_endpoint_url"
|
|
except ConnectionError:
|
|
errors[CONF_ENDPOINT_URL] = "cannot_connect"
|
|
else:
|
|
data = dict(user_input)
|
|
if not normalized_prefix:
|
|
# Do not persist empty optional values
|
|
data.pop(CONF_PREFIX, None)
|
|
else:
|
|
data[CONF_PREFIX] = normalized_prefix
|
|
|
|
title = user_input[CONF_BUCKET]
|
|
if normalized_prefix:
|
|
title = f"{title} - {normalized_prefix}"
|
|
|
|
return self.async_create_entry(title=title, data=data)
|
|
|
|
return self.async_show_form(
|
|
step_id="user",
|
|
data_schema=self.add_suggested_values_to_schema(
|
|
STEP_USER_DATA_SCHEMA, user_input
|
|
),
|
|
errors=errors,
|
|
description_placeholders={
|
|
"aws_s3_docs_url": DESCRIPTION_AWS_S3_DOCS_URL,
|
|
"boto3_docs_url": DESCRIPTION_BOTO3_DOCS_URL,
|
|
},
|
|
)
|