mirror of
https://github.com/chliny/hass-tencentcloud-asr.git
synced 2026-07-21 22:53:58 +08:00
feat: support reconfig model
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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={})
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user