2025-05-11 21:56:55 +08:00
|
|
|
import logging
|
2025-05-17 21:29:26 +08:00
|
|
|
import voluptuous as vol
|
|
|
|
|
from typing import Any, Dict
|
|
|
|
|
from homeassistant.core import callback
|
|
|
|
|
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult, OptionsFlow, ConfigEntry
|
2026-07-19 22:50:02 +08:00
|
|
|
from homeassistant.helpers.selector import (
|
|
|
|
|
BooleanSelector,
|
|
|
|
|
SelectOptionDict,
|
|
|
|
|
SelectSelector,
|
|
|
|
|
SelectSelectorConfig,
|
|
|
|
|
)
|
2025-05-17 21:29:26 +08:00
|
|
|
|
2025-05-18 21:19:58 +08:00
|
|
|
from .tencentcloud_api import ModuleSupportLanguage, DefaultModel
|
2025-05-11 23:18:06 +08:00
|
|
|
from .tencentcloud_api import TencentCloudAsrAPi
|
2026-07-19 22:50:02 +08:00
|
|
|
from . import (
|
|
|
|
|
ConvertNumModeKey,
|
|
|
|
|
FilterDirtyKey,
|
|
|
|
|
FilterModalKey,
|
|
|
|
|
FilterPuncKey,
|
|
|
|
|
ModelKey,
|
|
|
|
|
SecretIdKey,
|
|
|
|
|
SecretKeyKey,
|
|
|
|
|
)
|
2025-05-11 21:56:55 +08:00
|
|
|
from . import DOMAIN
|
|
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
2025-05-18 21:19:58 +08:00
|
|
|
modelSelector = SelectSelector(
|
|
|
|
|
SelectSelectorConfig(
|
|
|
|
|
options=[
|
|
|
|
|
SelectOptionDict(value=model_id, label=model_id) for model_id in ModuleSupportLanguage
|
|
|
|
|
],
|
|
|
|
|
translation_key="model_descriptions",
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
2026-07-19 22:50:02 +08:00
|
|
|
filterDirtySelector = SelectSelector(
|
|
|
|
|
SelectSelectorConfig(
|
|
|
|
|
options=[
|
|
|
|
|
SelectOptionDict(value="0", label="0"),
|
|
|
|
|
SelectOptionDict(value="1", label="1"),
|
|
|
|
|
SelectOptionDict(value="2", label="2"),
|
|
|
|
|
],
|
|
|
|
|
translation_key="filter_dirty_descriptions",
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
2025-05-18 21:19:58 +08:00
|
|
|
|
2025-05-11 21:56:55 +08:00
|
|
|
class ConfigFlowHandler(ConfigFlow, domain=DOMAIN):
|
|
|
|
|
async def async_step_user(self, user_input: dict[str, Any] | None = None) -> ConfigFlowResult:
|
|
|
|
|
errors: Dict[str, str] = {}
|
|
|
|
|
if user_input:
|
2025-05-11 23:18:06 +08:00
|
|
|
try:
|
|
|
|
|
secretId = user_input.get(SecretIdKey)
|
|
|
|
|
secretKey = user_input.get(SecretKeyKey)
|
|
|
|
|
TencentCloudAsrAPi(secretId, secretKey)
|
|
|
|
|
except Exception as err:
|
|
|
|
|
errors["base"] = str(err)
|
|
|
|
|
_LOGGER.error("check secret failed:%s", err)
|
|
|
|
|
|
|
|
|
|
if not errors:
|
|
|
|
|
return self.async_create_entry(title=DOMAIN, data=user_input)
|
2025-05-11 21:56:55 +08:00
|
|
|
|
2025-05-18 21:19:58 +08:00
|
|
|
config_schema = vol.Schema(
|
|
|
|
|
{
|
|
|
|
|
vol.Required(SecretIdKey): str,
|
|
|
|
|
vol.Required(SecretKeyKey): str,
|
|
|
|
|
vol.Required(ModelKey, default=DefaultModel): modelSelector,
|
2026-07-19 22:50:02 +08:00
|
|
|
vol.Required(FilterDirtyKey, default="0"): filterDirtySelector,
|
|
|
|
|
vol.Required(FilterModalKey, default=False): BooleanSelector(),
|
|
|
|
|
vol.Required(FilterPuncKey, default=False): BooleanSelector(),
|
|
|
|
|
vol.Required(ConvertNumModeKey, default=True): BooleanSelector(),
|
2025-05-18 21:19:58 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return self.async_show_form(step_id="user", data_schema=config_schema, errors=errors)
|
2025-05-17 21:29:26 +08:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
@callback
|
|
|
|
|
def async_get_options_flow(config_entry: ConfigEntry):
|
|
|
|
|
return TencentCloudAsrOptionFlow(config_entry)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TencentCloudAsrOptionFlow(OptionsFlow):
|
|
|
|
|
def __init__(self, config_entry: ConfigEntry):
|
|
|
|
|
self._config_entry = config_entry
|
|
|
|
|
|
|
|
|
|
async def async_step_init(self, user_input=None):
|
|
|
|
|
return await self.async_step_user()
|
|
|
|
|
|
|
|
|
|
async def async_step_user(self, user_input=None):
|
|
|
|
|
if not user_input:
|
|
|
|
|
user_input = {**self._config_entry.data, **self._config_entry.options}
|
|
|
|
|
else:
|
|
|
|
|
return self.async_create_entry(title=DOMAIN, data=user_input)
|
|
|
|
|
model = user_input.get(ModelKey, DefaultModel)
|
|
|
|
|
config_schema = vol.Schema({
|
2025-05-18 21:19:58 +08:00
|
|
|
vol.Optional(ModelKey, default=model): modelSelector,
|
2026-07-19 22:50:02 +08:00
|
|
|
vol.Required(
|
|
|
|
|
FilterDirtyKey,
|
|
|
|
|
default=user_input.get(FilterDirtyKey, "0"),
|
|
|
|
|
): filterDirtySelector,
|
|
|
|
|
vol.Required(
|
|
|
|
|
FilterModalKey,
|
|
|
|
|
default=user_input.get(FilterModalKey, False),
|
|
|
|
|
): BooleanSelector(),
|
|
|
|
|
vol.Required(
|
|
|
|
|
FilterPuncKey,
|
|
|
|
|
default=user_input.get(FilterPuncKey, False),
|
|
|
|
|
): BooleanSelector(),
|
|
|
|
|
vol.Required(
|
|
|
|
|
ConvertNumModeKey,
|
|
|
|
|
default=user_input.get(ConvertNumModeKey, True),
|
|
|
|
|
): BooleanSelector(),
|
2025-05-17 21:29:26 +08:00
|
|
|
})
|
|
|
|
|
return self.async_show_form(step_id="user", data_schema=config_schema, errors={})
|