0.7.0
This commit is contained in:
@@ -1,87 +1,71 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
|
from homeassistant.core import callback
|
||||||
|
|
||||||
DOMAIN = "bahmcloud_store"
|
from .const import CONF_GITHUB_TOKEN, DOMAIN
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_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.
|
"""Config flow for Bahmcloud Store.
|
||||||
|
|
||||||
Design goals:
|
The store index URL is fixed and not user-configurable.
|
||||||
- GUI setup only (no YAML config)
|
The only optional setting is a GitHub token to increase API rate limits.
|
||||||
- store index URL is fixed (not configurable)
|
|
||||||
- optional GitHub token to raise API rate limits
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
VERSION = 1
|
VERSION = 1
|
||||||
|
|
||||||
async def async_step_user(self, user_input: dict[str, Any] | None = None):
|
async def async_step_user(self, user_input: dict | None = None):
|
||||||
"""Handle the initial step."""
|
# Allow only one instance.
|
||||||
# Single instance only
|
await self.async_set_unique_id(DOMAIN)
|
||||||
if self._async_current_entries():
|
self._abort_if_unique_id_configured()
|
||||||
return self.async_abort(reason="single_instance_allowed")
|
|
||||||
|
|
||||||
if user_input is not None:
|
if user_input is None:
|
||||||
token = (user_input.get(CONF_GITHUB_TOKEN) or "").strip() or None
|
return self.async_show_form(step_id="user", data_schema=_schema(None))
|
||||||
data: dict[str, Any] = {}
|
|
||||||
options: dict[str, Any] = {}
|
|
||||||
if token:
|
|
||||||
options[CONF_GITHUB_TOKEN] = token
|
|
||||||
|
|
||||||
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(
|
return self.async_create_entry(
|
||||||
{
|
title="Bahmcloud Store",
|
||||||
vol.Optional(CONF_GITHUB_TOKEN, default=""): str,
|
data={},
|
||||||
}
|
options={CONF_GITHUB_TOKEN: token} if token else {},
|
||||||
)
|
|
||||||
|
|
||||||
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).",
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@callback
|
||||||
def async_get_options_flow(config_entry: config_entries.ConfigEntry):
|
def async_get_options_flow(config_entry: config_entries.ConfigEntry):
|
||||||
return BCSOptionsFlowHandler(config_entry)
|
return BahmcloudStoreOptionsFlowHandler(config_entry)
|
||||||
|
|
||||||
|
|
||||||
class BCSOptionsFlowHandler(config_entries.OptionsFlow):
|
class BahmcloudStoreOptionsFlowHandler(config_entries.OptionsFlow):
|
||||||
"""Handle options for Bahmcloud Store."""
|
"""Options flow to manage optional GitHub token."""
|
||||||
|
|
||||||
def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
|
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):
|
async def async_step_init(self, user_input: dict | None = None):
|
||||||
if user_input is not None:
|
if user_input is None:
|
||||||
token = (user_input.get(CONF_GITHUB_TOKEN) or "").strip() or None
|
current = self._config_entry.options.get(CONF_GITHUB_TOKEN) or ""
|
||||||
opts: dict[str, Any] = dict(self.config_entry.options)
|
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:
|
if token:
|
||||||
opts[CONF_GITHUB_TOKEN] = token
|
options[CONF_GITHUB_TOKEN] = token
|
||||||
else:
|
else:
|
||||||
opts.pop(CONF_GITHUB_TOKEN, None)
|
options.pop(CONF_GITHUB_TOKEN, None)
|
||||||
return self.async_create_entry(title="", data=opts)
|
|
||||||
|
|
||||||
cur = (self.config_entry.options.get(CONF_GITHUB_TOKEN) or "").strip()
|
return self.async_create_entry(title="", data=options)
|
||||||
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.",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user