From 95dd8b9dc2bdaf2f3d4da6f8fa2e03571b17e977 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Bachmann?= Date: Tue, 20 Jan 2026 05:44:32 +0000 Subject: [PATCH] 0.7.0 --- .../bahmcloud_store/config_flow.py | 92 ++++++++----------- 1 file changed, 38 insertions(+), 54 deletions(-) diff --git a/custom_components/bahmcloud_store/config_flow.py b/custom_components/bahmcloud_store/config_flow.py index 0ed36f5..297e617 100644 --- a/custom_components/bahmcloud_store/config_flow.py +++ b/custom_components/bahmcloud_store/config_flow.py @@ -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) - if token: - opts[CONF_GITHUB_TOKEN] = token - else: - opts.pop(CONF_GITHUB_TOKEN, None) - return self.async_create_entry(title="", data=opts) + 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))) - cur = (self.config_entry.options.get(CONF_GITHUB_TOKEN) or "").strip() - schema = vol.Schema({vol.Optional(CONF_GITHUB_TOKEN, default=cur): str}) + token = str(user_input.get(CONF_GITHUB_TOKEN, "")).strip() or None + options = dict(self._config_entry.options) - return self.async_show_form( - step_id="init", - data_schema=schema, - description_placeholders={ - "rate_limit": "Optional. Adds GitHub authorization to avoid rate limits.", - }, - ) + # Allow clearing the token. + if token: + options[CONF_GITHUB_TOKEN] = token + else: + options.pop(CONF_GITHUB_TOKEN, None) + + return self.async_create_entry(title="", data=options)