mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Support setting Amazon Polly engine in service call (#120226)
This commit is contained in:
parent
d6bd4312ab
commit
822660732b
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections import defaultdict
|
||||||
import logging
|
import logging
|
||||||
from typing import Any, Final
|
from typing import Any, Final
|
||||||
|
|
||||||
@ -114,6 +115,8 @@ def get_engine(
|
|||||||
|
|
||||||
all_voices: dict[str, dict[str, str]] = {}
|
all_voices: dict[str, dict[str, str]] = {}
|
||||||
|
|
||||||
|
all_engines: dict[str, set[str]] = defaultdict(set)
|
||||||
|
|
||||||
all_voices_req = polly_client.describe_voices()
|
all_voices_req = polly_client.describe_voices()
|
||||||
|
|
||||||
for voice in all_voices_req.get("Voices", []):
|
for voice in all_voices_req.get("Voices", []):
|
||||||
@ -124,8 +127,12 @@ def get_engine(
|
|||||||
language_code: str | None = voice.get("LanguageCode")
|
language_code: str | None = voice.get("LanguageCode")
|
||||||
if language_code is not None and language_code not in supported_languages:
|
if language_code is not None and language_code not in supported_languages:
|
||||||
supported_languages.append(language_code)
|
supported_languages.append(language_code)
|
||||||
|
for engine in voice.get("SupportedEngines"):
|
||||||
|
all_engines[engine].add(voice_id)
|
||||||
|
|
||||||
return AmazonPollyProvider(polly_client, config, supported_languages, all_voices)
|
return AmazonPollyProvider(
|
||||||
|
polly_client, config, supported_languages, all_voices, all_engines
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class AmazonPollyProvider(Provider):
|
class AmazonPollyProvider(Provider):
|
||||||
@ -137,13 +144,16 @@ class AmazonPollyProvider(Provider):
|
|||||||
config: ConfigType,
|
config: ConfigType,
|
||||||
supported_languages: list[str],
|
supported_languages: list[str],
|
||||||
all_voices: dict[str, dict[str, str]],
|
all_voices: dict[str, dict[str, str]],
|
||||||
|
all_engines: dict[str, set[str]],
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize Amazon Polly provider for TTS."""
|
"""Initialize Amazon Polly provider for TTS."""
|
||||||
self.client = polly_client
|
self.client = polly_client
|
||||||
self.config = config
|
self.config = config
|
||||||
self.supported_langs = supported_languages
|
self.supported_langs = supported_languages
|
||||||
self.all_voices = all_voices
|
self.all_voices = all_voices
|
||||||
|
self.all_engines = all_engines
|
||||||
self.default_voice: str = self.config[CONF_VOICE]
|
self.default_voice: str = self.config[CONF_VOICE]
|
||||||
|
self.default_engine: str = self.config[CONF_ENGINE]
|
||||||
self.name = "Amazon Polly"
|
self.name = "Amazon Polly"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -159,12 +169,12 @@ class AmazonPollyProvider(Provider):
|
|||||||
@property
|
@property
|
||||||
def default_options(self) -> dict[str, str]:
|
def default_options(self) -> dict[str, str]:
|
||||||
"""Return dict include default options."""
|
"""Return dict include default options."""
|
||||||
return {CONF_VOICE: self.default_voice}
|
return {CONF_VOICE: self.default_voice, CONF_ENGINE: self.default_engine}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def supported_options(self) -> list[str]:
|
def supported_options(self) -> list[str]:
|
||||||
"""Return a list of supported options."""
|
"""Return a list of supported options."""
|
||||||
return [CONF_VOICE]
|
return [CONF_VOICE, CONF_ENGINE]
|
||||||
|
|
||||||
def get_tts_audio(
|
def get_tts_audio(
|
||||||
self,
|
self,
|
||||||
@ -179,9 +189,14 @@ class AmazonPollyProvider(Provider):
|
|||||||
_LOGGER.error("%s does not support the %s language", voice_id, language)
|
_LOGGER.error("%s does not support the %s language", voice_id, language)
|
||||||
return None, None
|
return None, None
|
||||||
|
|
||||||
|
engine = options.get(CONF_ENGINE, self.default_engine)
|
||||||
|
if voice_id not in self.all_engines[engine]:
|
||||||
|
_LOGGER.error("%s does not support the %s engine", voice_id, engine)
|
||||||
|
return None, None
|
||||||
|
|
||||||
_LOGGER.debug("Requesting TTS file for text: %s", message)
|
_LOGGER.debug("Requesting TTS file for text: %s", message)
|
||||||
resp = self.client.synthesize_speech(
|
resp = self.client.synthesize_speech(
|
||||||
Engine=self.config[CONF_ENGINE],
|
Engine=engine,
|
||||||
OutputFormat=self.config[CONF_OUTPUT_FORMAT],
|
OutputFormat=self.config[CONF_OUTPUT_FORMAT],
|
||||||
SampleRate=self.config[CONF_SAMPLE_RATE],
|
SampleRate=self.config[CONF_SAMPLE_RATE],
|
||||||
Text=message,
|
Text=message,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user