Add name to tts voices (#91814)

* Add name to tts voices

* Add new file
pull/91844/head
Erik Montnemery 2023-04-22 02:41:14 +02:00 committed by GitHub
parent c6d846453d
commit 9a0de43f98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 35 additions and 8 deletions

View File

@ -9,6 +9,7 @@ from homeassistant.components.tts import (
CONF_LANG,
PLATFORM_SCHEMA,
Provider,
Voice,
)
from homeassistant.core import callback
@ -97,9 +98,11 @@ class CloudProvider(Provider):
return [ATTR_GENDER, ATTR_VOICE, ATTR_AUDIO_OUTPUT]
@callback
def async_get_supported_voices(self, language: str) -> list[str] | None:
def async_get_supported_voices(self, language: str) -> list[Voice] | None:
"""Return a list of supported voices for a language."""
return TTS_VOICES.get(language)
if not (voices := TTS_VOICES.get(language)):
return None
return [Voice(voice, voice) for voice in voices]
@property
def default_options(self):

View File

@ -66,6 +66,7 @@ from .const import (
from .helper import get_engine_instance
from .legacy import PLATFORM_SCHEMA, PLATFORM_SCHEMA_BASE, Provider, async_setup_legacy
from .media_source import generate_media_source_id, media_source_id_to_kwargs
from .models import Voice
__all__ = [
"async_get_media_source_audio",
@ -80,6 +81,7 @@ __all__ = [
"PLATFORM_SCHEMA",
"Provider",
"TtsAudioType",
"Voice",
]
_LOGGER = logging.getLogger(__name__)
@ -302,7 +304,7 @@ class TextToSpeechEntity(RestoreEntity):
return None
@callback
def async_get_supported_voices(self, language: str) -> list[str] | None:
def async_get_supported_voices(self, language: str) -> list[Voice] | None:
"""Return a list of supported voices for a language."""
return None

View File

@ -52,6 +52,7 @@ from .const import (
TtsAudioType,
)
from .media_source import generate_media_source_id
from .models import Voice
if TYPE_CHECKING:
from . import SpeechManager
@ -229,7 +230,7 @@ class Provider:
return None
@callback
def async_get_supported_voices(self, language: str) -> list[str] | None:
def async_get_supported_voices(self, language: str) -> list[Voice] | None:
"""Return a list of supported voices for a language."""
return None

View File

@ -0,0 +1,10 @@
"""Text-to-speech data models."""
from dataclasses import dataclass
@dataclass(frozen=True)
class Voice:
"""A TTS voice."""
voice_id: str
name: str

View File

@ -78,7 +78,9 @@ async def test_provider_properties(cloud_with_prefs) -> None:
)
assert provider.supported_options == ["gender", "voice", "audio_output"]
assert "nl-NL" in provider.supported_languages
assert "ColetteNeural" in provider.async_get_supported_voices("nl-NL")
assert tts.Voice(
"ColetteNeural", "ColetteNeural"
) in provider.async_get_supported_voices("nl-NL")
async def test_get_tts_audio(cloud_with_prefs) -> None:

View File

@ -13,6 +13,7 @@ from homeassistant.components.tts import (
Provider,
TextToSpeechEntity,
TtsAudioType,
Voice,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
@ -61,10 +62,13 @@ class BaseProvider:
return SUPPORT_LANGUAGES
@callback
def async_get_supported_voices(self, language: str) -> list[str] | None:
def async_get_supported_voices(self, language: str) -> list[Voice] | None:
"""Return list of supported languages."""
if language == "en-US":
return ["James Earl Jones", "Fran Drescher"]
return [
Voice("james_earl_jones", "James Earl Jones"),
Voice("fran_drescher", "Fran Drescher"),
]
return None
@property

View File

@ -1743,4 +1743,9 @@ async def test_ws_list_voices(
msg = await client.receive_json()
assert msg["success"]
assert msg["result"] == {"voices": ["James Earl Jones", "Fran Drescher"]}
assert msg["result"] == {
"voices": [
{"voice_id": "james_earl_jones", "name": "James Earl Jones"},
{"voice_id": "fran_drescher", "name": "Fran Drescher"},
]
}