This commit is contained in:
2026-01-20 05:44:32 +00:00
parent 8b01c04a4c
commit 95dd8b9dc2

View File

@@ -1,87 +1,71 @@
from __future__ import annotations
import logging
from typing import Any
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.core import callback
DOMAIN = "bahmcloud_store"
from .const import CONF_GITHUB_TOKEN, DOMAIN
_LOGGER = logging.getLogger(__name__)
CONF_GITHUB_TOKEN = "github_token"
def _schema(default_token: str | None = None) -> vol.Schema:
default_token = (default_token or "").strip()
return vol.Schema({vol.Optional(CONF_GITHUB_TOKEN, default=default_token): str})
class BCSConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
class BahmcloudStoreConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Config flow for Bahmcloud Store.
Design goals:
- GUI setup only (no YAML config)
- store index URL is fixed (not configurable)
- optional GitHub token to raise API rate limits
The store index URL is fixed and not user-configurable.
The only optional setting is a GitHub token to increase API rate limits.
"""
VERSION = 1
async def async_step_user(self, user_input: dict[str, Any] | None = None):
"""Handle the initial step."""
# Single instance only
if self._async_current_entries():
return self.async_abort(reason="single_instance_allowed")
async def async_step_user(self, user_input: dict | None = None):
# Allow only one instance.
await self.async_set_unique_id(DOMAIN)
self._abort_if_unique_id_configured()
if user_input is not None:
token = (user_input.get(CONF_GITHUB_TOKEN) or "").strip() or None
data: dict[str, Any] = {}
options: dict[str, Any] = {}
if token:
options[CONF_GITHUB_TOKEN] = token
if user_input is None:
return self.async_show_form(step_id="user", data_schema=_schema(None))
return self.async_create_entry(title="Bahmcloud Store", data=data, options=options)
token = str(user_input.get(CONF_GITHUB_TOKEN, "")).strip() or None
schema = vol.Schema(
{
vol.Optional(CONF_GITHUB_TOKEN, default=""): str,
}
)
return self.async_show_form(
step_id="user",
data_schema=schema,
description_placeholders={
"rate_limit": "GitHub API is rate-limited without a token (recommended for large indexes / HACS).",
},
return self.async_create_entry(
title="Bahmcloud Store",
data={},
options={CONF_GITHUB_TOKEN: token} if token else {},
)
@staticmethod
@callback
def async_get_options_flow(config_entry: config_entries.ConfigEntry):
return BCSOptionsFlowHandler(config_entry)
return BahmcloudStoreOptionsFlowHandler(config_entry)
class BCSOptionsFlowHandler(config_entries.OptionsFlow):
"""Handle options for Bahmcloud Store."""
class BahmcloudStoreOptionsFlowHandler(config_entries.OptionsFlow):
"""Options flow to manage optional GitHub token."""
def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
self.config_entry = config_entry
self._config_entry = config_entry
async def async_step_init(self, user_input: dict[str, Any] | None = None):
if user_input is not None:
token = (user_input.get(CONF_GITHUB_TOKEN) or "").strip() or None
opts: dict[str, Any] = dict(self.config_entry.options)
async def async_step_init(self, user_input: dict | None = None):
if user_input is None:
current = self._config_entry.options.get(CONF_GITHUB_TOKEN) or ""
return self.async_show_form(step_id="init", data_schema=_schema(str(current)))
token = str(user_input.get(CONF_GITHUB_TOKEN, "")).strip() or None
options = dict(self._config_entry.options)
# Allow clearing the token.
if token:
opts[CONF_GITHUB_TOKEN] = token
options[CONF_GITHUB_TOKEN] = token
else:
opts.pop(CONF_GITHUB_TOKEN, None)
return self.async_create_entry(title="", data=opts)
options.pop(CONF_GITHUB_TOKEN, None)
cur = (self.config_entry.options.get(CONF_GITHUB_TOKEN) or "").strip()
schema = vol.Schema({vol.Optional(CONF_GITHUB_TOKEN, default=cur): str})
return self.async_show_form(
step_id="init",
data_schema=schema,
description_placeholders={
"rate_limit": "Optional. Adds GitHub authorization to avoid rate limits.",
},
)
return self.async_create_entry(title="", data=options)