mirror of
https://github.com/howelljiang/baidu-voice.git
synced 2026-07-22 07:03:54 +08:00
按照ha推荐的插件初始化方式进行重构
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"""Baidu Speech-to-Text integration."""
|
||||
import logging
|
||||
from typing import Any, Optional
|
||||
from typing import Any
|
||||
|
||||
from aip import AipSpeech
|
||||
from homeassistant.components.stt import (
|
||||
@@ -13,8 +13,6 @@ from homeassistant.components.stt import (
|
||||
SpeechMetadata,
|
||||
SpeechResult,
|
||||
SpeechResultState,
|
||||
Provider,
|
||||
async_get_provider,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
@@ -24,8 +22,31 @@ from .const import DOMAIN
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
class BaiduSTTBase:
|
||||
"""Base class for Baidu STT functionality."""
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> bool:
|
||||
"""Set up Baidu STT from a config entry."""
|
||||
async_add_entities([BaiduSTTEntity(hass, config_entry)])
|
||||
return True
|
||||
|
||||
class BaiduSTTEntity(SpeechToTextEntity):
|
||||
"""Baidu Speech-to-Text entity."""
|
||||
|
||||
def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None:
|
||||
"""Initialize Baidu STT entity."""
|
||||
self.hass = hass
|
||||
self._config_entry = config_entry
|
||||
self._attr_name = "Baidu STT"
|
||||
self._attr_unique_id = f"{DOMAIN}_stt"
|
||||
|
||||
# 初始化百度语音识别客户端
|
||||
self._client = AipSpeech(
|
||||
config_entry.data["app_id"],
|
||||
config_entry.data["api_key"],
|
||||
config_entry.data["secret_key"]
|
||||
)
|
||||
|
||||
@property
|
||||
def supported_languages(self) -> list[str]:
|
||||
@@ -57,18 +78,26 @@ class BaiduSTTBase:
|
||||
"""Return a list of supported channels."""
|
||||
return [AudioChannels.CHANNEL_MONO]
|
||||
|
||||
async def _process_audio(self, audio_data: bytes, client: AipSpeech) -> SpeechResult:
|
||||
"""Process audio data using Baidu STT API."""
|
||||
async def async_process_audio_stream(
|
||||
self, metadata: SpeechMetadata, stream: Any
|
||||
) -> SpeechResult:
|
||||
"""Process an audio stream to STT service."""
|
||||
try:
|
||||
# 读取音频数据
|
||||
audio_data = b""
|
||||
async for chunk in stream:
|
||||
audio_data += chunk
|
||||
|
||||
if not audio_data:
|
||||
_LOGGER.warning("No audio data received")
|
||||
return SpeechResult("", SpeechResultState.ERROR)
|
||||
|
||||
_LOGGER.debug("Processing audio stream with metadata: %s", metadata)
|
||||
_LOGGER.debug("Total audio data size: %d bytes", len(audio_data))
|
||||
|
||||
# 使用百度语音识别API
|
||||
result = await self.hass.async_add_executor_job(
|
||||
client.asr,
|
||||
self._client.asr,
|
||||
audio_data,
|
||||
'pcm',
|
||||
16000,
|
||||
@@ -88,86 +117,4 @@ class BaiduSTTBase:
|
||||
|
||||
except Exception as ex:
|
||||
_LOGGER.error("Error during Baidu STT: %s", ex, exc_info=True)
|
||||
return SpeechResult("", SpeechResultState.ERROR)
|
||||
|
||||
async def async_get_engine(
|
||||
hass: HomeAssistant,
|
||||
config: dict,
|
||||
discovery_info: Optional[dict] = None
|
||||
) -> Provider:
|
||||
"""Set up Baidu STT component."""
|
||||
_LOGGER.debug("Setting up Baidu STT engine")
|
||||
return BaiduSTTProvider(hass)
|
||||
|
||||
class BaiduSTTProvider(BaiduSTTBase, Provider):
|
||||
"""Baidu STT api provider."""
|
||||
|
||||
def __init__(self, hass: HomeAssistant) -> None:
|
||||
"""Initialize Baidu STT provider."""
|
||||
self.hass = hass
|
||||
self._app_id = hass.data[DOMAIN]["app_id"]
|
||||
self._api_key = hass.data[DOMAIN]["api_key"]
|
||||
self._secret_key = hass.data[DOMAIN]["secret_key"]
|
||||
self._client = AipSpeech(self._app_id, self._api_key, self._secret_key)
|
||||
self.name = "Baidu STT"
|
||||
self._language = "zh-CN"
|
||||
_LOGGER.debug("Initialized Baidu STT provider with language: %s", self._language)
|
||||
|
||||
@property
|
||||
def default_language(self) -> str:
|
||||
"""Return the default language."""
|
||||
return self._language
|
||||
|
||||
async def async_process_audio_stream(
|
||||
self, metadata: SpeechMetadata, stream: Any
|
||||
) -> SpeechResult:
|
||||
"""Process an audio stream to STT service."""
|
||||
_LOGGER.debug("Processing audio stream with metadata: %s", metadata)
|
||||
|
||||
# 读取音频数据
|
||||
audio_data = b""
|
||||
async for chunk in stream:
|
||||
audio_data += chunk
|
||||
|
||||
return await self._process_audio(audio_data, self._client)
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up Baidu STT from a config entry."""
|
||||
provider = await async_get_provider(hass, DOMAIN)
|
||||
if provider:
|
||||
async_add_entities([BaiduSTTEntity(provider, config_entry)])
|
||||
|
||||
class BaiduSTTEntity(BaiduSTTBase, SpeechToTextEntity):
|
||||
"""Baidu Speech-to-Text entity."""
|
||||
|
||||
def __init__(self, provider: Provider, config_entry: ConfigEntry) -> None:
|
||||
"""Initialize Baidu STT entity."""
|
||||
self._provider = provider
|
||||
self._config_entry = config_entry
|
||||
self._attr_name = "Baidu Speech-to-Text"
|
||||
self._attr_unique_id = f"{DOMAIN}_stt"
|
||||
self.hass = provider.hass
|
||||
|
||||
# 初始化百度语音识别客户端
|
||||
self._client = AipSpeech(
|
||||
config_entry.data["app_id"],
|
||||
config_entry.data["api_key"],
|
||||
config_entry.data["secret_key"]
|
||||
)
|
||||
|
||||
async def async_process_audio_stream(
|
||||
self, metadata: SpeechMetadata, stream: Any
|
||||
) -> SpeechResult:
|
||||
"""Process an audio stream to STT service."""
|
||||
_LOGGER.debug("Processing audio stream with metadata: %s", metadata)
|
||||
|
||||
# 读取音频数据
|
||||
audio_data = b""
|
||||
async for chunk in stream:
|
||||
audio_data += chunk
|
||||
|
||||
return await self._process_audio(audio_data, self._client)
|
||||
return SpeechResult("", SpeechResultState.ERROR)
|
||||
Reference in New Issue
Block a user