From 51e2b0a23a2478a521b842d743acf1dba3f55549 Mon Sep 17 00:00:00 2001 From: chliny Date: Sat, 17 May 2025 21:29:26 +0800 Subject: [PATCH] feat: support reconfig model --- .../tencentcloud_asr/__init__.py | 15 ++++++++ .../tencentcloud_asr/config_flow.py | 36 +++++++++++++++---- custom_components/tencentcloud_asr/stt.py | 10 +++--- 3 files changed, 50 insertions(+), 11 deletions(-) diff --git a/custom_components/tencentcloud_asr/__init__.py b/custom_components/tencentcloud_asr/__init__.py index 1db77a7..b1e0fc6 100644 --- a/custom_components/tencentcloud_asr/__init__.py +++ b/custom_components/tencentcloud_asr/__init__.py @@ -1,10 +1,13 @@ +import logging from homeassistant.config_entries import ConfigEntry from homeassistant.const import Platform from homeassistant.core import HomeAssistant +from .tencentcloud_api import DefaultModel DOMAIN = "tencentcloud_asr" PLATFORMS = (Platform.STT,) +_LOGGER = logging.getLogger(__name__) # const key SecretIdKey = "secretid" @@ -13,9 +16,21 @@ ModelKey = "model" async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry): + if not config_entry.update_listeners: + config_entry.add_update_listener(async_update_options) + config_data = {**config_entry.data, **config_entry.options} + model = config_data.get(ModelKey, DefaultModel) + hass.data[ModelKey] = model await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS) return True +async def async_update_options(hass: HomeAssistant, config_entry: ConfigEntry): + config_data = {**config_entry.data, **config_entry.options} + model = config_data.get(ModelKey, DefaultModel) + _LOGGER.debug("update model:%s", model) + hass.data[ModelKey] = model + + async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry): return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS) diff --git a/custom_components/tencentcloud_asr/config_flow.py b/custom_components/tencentcloud_asr/config_flow.py index 74d9a3d..2eebfef 100644 --- a/custom_components/tencentcloud_asr/config_flow.py +++ b/custom_components/tencentcloud_asr/config_flow.py @@ -1,12 +1,12 @@ -import voluptuous as vol -from homeassistant.config_entries import ConfigFlow, ConfigFlowResult -from typing import Any, Dict import logging +import voluptuous as vol +from typing import Any, Dict +from homeassistant.core import callback +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult, OptionsFlow, ConfigEntry + from .tencentcloud_api import Modules, DefaultModel from .tencentcloud_api import TencentCloudAsrAPi from . import SecretIdKey, SecretKeyKey, ModelKey - - from . import DOMAIN _LOGGER = logging.getLogger(__name__) @@ -33,8 +33,32 @@ class ConfigFlowHandler(ConfigFlow, domain=DOMAIN): { vol.Required(SecretIdKey): str, vol.Required(SecretKeyKey): str, - vol.Optional(ModelKey, default=DefaultModel): vol.In(Modules.keys()), + vol.Required(ModelKey, default=DefaultModel): vol.In(Modules.keys()), }, ), errors=errors ) + + @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({ + vol.Optional(ModelKey, default=model): vol.In(Modules.keys()), + }) + return self.async_show_form(step_id="user", data_schema=config_schema, errors={}) diff --git a/custom_components/tencentcloud_asr/stt.py b/custom_components/tencentcloud_asr/stt.py index 39a7287..fd28ff3 100644 --- a/custom_components/tencentcloud_asr/stt.py +++ b/custom_components/tencentcloud_asr/stt.py @@ -29,15 +29,14 @@ class ASRSTT(stt.SpeechToTextEntity): secretKey = config_entry.data.get(SecretKeyKey, "") self.tencentCloudApi = TencentCloudAsrAPi(secretId, secretKey) - self.model: str = config_entry.data.get(ModelKey, DefaultModel) - # self._attr_name = f"TencentCloud Asr id: ({secretId})" self._attr_unique_id = f"tencentcloud_asr_stt" self._attr_name = f"TencentCloud Asr STT" self.hass = hass @property def supported_languages(self) -> list[str]: - return ModuleSupportLanguage.get(self.model, ["zh"]) + model: str = self.hass.data.get(ModelKey, DefaultModel) + return ModuleSupportLanguage.get(model, ["zh"]) @property def supported_formats(self) -> list[stt.AudioFormats]: @@ -69,8 +68,9 @@ class ASRSTT(stt.SpeechToTextEntity): audio += chunk data = base64.b64encode(audio).decode("utf8", "ignore").strip() - _LOGGER.debug(f"process_audio_stream transcribe: {data}") - ret, result = await self.hass.async_add_executor_job(partial(self.tencentCloudApi.SentenceRecognition, self.model, data)) + model: str = self.hass.data.get(ModelKey, DefaultModel) + _LOGGER.debug(f"process_audio_stream transcribe: {data} {model}") + ret, result = await self.hass.async_add_executor_job(partial(self.tencentCloudApi.SentenceRecognition, model, data)) if not ret: return stt.SpeechResult(result, stt.SpeechResultState.ERROR) if not result: