mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 10:17:09 +00:00
Add name to tts voices (#91814)
* Add name to tts voices * Add new file
This commit is contained in:
parent
c6d846453d
commit
9a0de43f98
@ -9,6 +9,7 @@ from homeassistant.components.tts import (
|
|||||||
CONF_LANG,
|
CONF_LANG,
|
||||||
PLATFORM_SCHEMA,
|
PLATFORM_SCHEMA,
|
||||||
Provider,
|
Provider,
|
||||||
|
Voice,
|
||||||
)
|
)
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
|
|
||||||
@ -97,9 +98,11 @@ class CloudProvider(Provider):
|
|||||||
return [ATTR_GENDER, ATTR_VOICE, ATTR_AUDIO_OUTPUT]
|
return [ATTR_GENDER, ATTR_VOICE, ATTR_AUDIO_OUTPUT]
|
||||||
|
|
||||||
@callback
|
@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 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
|
@property
|
||||||
def default_options(self):
|
def default_options(self):
|
||||||
|
@ -66,6 +66,7 @@ from .const import (
|
|||||||
from .helper import get_engine_instance
|
from .helper import get_engine_instance
|
||||||
from .legacy import PLATFORM_SCHEMA, PLATFORM_SCHEMA_BASE, Provider, async_setup_legacy
|
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 .media_source import generate_media_source_id, media_source_id_to_kwargs
|
||||||
|
from .models import Voice
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"async_get_media_source_audio",
|
"async_get_media_source_audio",
|
||||||
@ -80,6 +81,7 @@ __all__ = [
|
|||||||
"PLATFORM_SCHEMA",
|
"PLATFORM_SCHEMA",
|
||||||
"Provider",
|
"Provider",
|
||||||
"TtsAudioType",
|
"TtsAudioType",
|
||||||
|
"Voice",
|
||||||
]
|
]
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -302,7 +304,7 @@ class TextToSpeechEntity(RestoreEntity):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
@callback
|
@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 a list of supported voices for a language."""
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@ -52,6 +52,7 @@ from .const import (
|
|||||||
TtsAudioType,
|
TtsAudioType,
|
||||||
)
|
)
|
||||||
from .media_source import generate_media_source_id
|
from .media_source import generate_media_source_id
|
||||||
|
from .models import Voice
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from . import SpeechManager
|
from . import SpeechManager
|
||||||
@ -229,7 +230,7 @@ class Provider:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
@callback
|
@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 a list of supported voices for a language."""
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
10
homeassistant/components/tts/models.py
Normal file
10
homeassistant/components/tts/models.py
Normal 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
|
@ -78,7 +78,9 @@ async def test_provider_properties(cloud_with_prefs) -> None:
|
|||||||
)
|
)
|
||||||
assert provider.supported_options == ["gender", "voice", "audio_output"]
|
assert provider.supported_options == ["gender", "voice", "audio_output"]
|
||||||
assert "nl-NL" in provider.supported_languages
|
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:
|
async def test_get_tts_audio(cloud_with_prefs) -> None:
|
||||||
|
@ -13,6 +13,7 @@ from homeassistant.components.tts import (
|
|||||||
Provider,
|
Provider,
|
||||||
TextToSpeechEntity,
|
TextToSpeechEntity,
|
||||||
TtsAudioType,
|
TtsAudioType,
|
||||||
|
Voice,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
@ -61,10 +62,13 @@ class BaseProvider:
|
|||||||
return SUPPORT_LANGUAGES
|
return SUPPORT_LANGUAGES
|
||||||
|
|
||||||
@callback
|
@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."""
|
"""Return list of supported languages."""
|
||||||
if language == "en-US":
|
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
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -1743,4 +1743,9 @@ async def test_ws_list_voices(
|
|||||||
|
|
||||||
msg = await client.receive_json()
|
msg = await client.receive_json()
|
||||||
assert msg["success"]
|
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"},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user