2019-02-14 15:01:46 +00:00
|
|
|
"""Support for the Google speech service."""
|
2020-12-02 21:03:31 +00:00
|
|
|
from io import BytesIO
|
2016-12-13 07:23:08 +00:00
|
|
|
import logging
|
|
|
|
|
2020-12-02 21:03:31 +00:00
|
|
|
from gtts import gTTS, gTTSError
|
2016-12-14 21:32:20 +00:00
|
|
|
import voluptuous as vol
|
2016-12-13 07:23:08 +00:00
|
|
|
|
2017-11-04 19:04:05 +00:00
|
|
|
from homeassistant.components.tts import CONF_LANG, PLATFORM_SCHEMA, Provider
|
2016-12-13 07:23:08 +00:00
|
|
|
|
2022-12-01 12:31:19 +00:00
|
|
|
from .const import MAP_LANG_TLD, SUPPORT_LANGUAGES, SUPPORT_TLD
|
2016-12-13 07:23:08 +00:00
|
|
|
|
2022-12-01 12:31:19 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2016-12-14 21:32:20 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_LANG = "en"
|
2016-12-14 21:32:20 +00:00
|
|
|
|
2022-12-01 12:31:19 +00:00
|
|
|
SUPPORT_OPTIONS = ["tld"]
|
|
|
|
|
|
|
|
DEFAULT_TLD = "com"
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
2022-12-01 12:31:19 +00:00
|
|
|
{
|
|
|
|
vol.Optional(CONF_LANG, default=DEFAULT_LANG): vol.In(SUPPORT_LANGUAGES),
|
|
|
|
vol.Optional("tld", default=DEFAULT_TLD): vol.In(SUPPORT_TLD),
|
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-12-14 21:32:20 +00:00
|
|
|
|
2016-12-13 07:23:08 +00:00
|
|
|
|
2019-11-05 21:39:15 +00:00
|
|
|
async def async_get_engine(hass, config, discovery_info=None):
|
2017-01-15 13:53:07 +00:00
|
|
|
"""Set up Google speech component."""
|
2022-12-01 12:31:19 +00:00
|
|
|
return GoogleProvider(hass, config[CONF_LANG], config["tld"])
|
2016-12-13 07:23:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GoogleProvider(Provider):
|
2017-01-15 13:53:07 +00:00
|
|
|
"""The Google speech API provider."""
|
2016-12-13 07:23:08 +00:00
|
|
|
|
2022-12-01 12:31:19 +00:00
|
|
|
def __init__(self, hass, lang, tld):
|
2016-12-13 07:23:08 +00:00
|
|
|
"""Init Google TTS service."""
|
|
|
|
self.hass = hass
|
2022-12-01 12:31:19 +00:00
|
|
|
if lang in MAP_LANG_TLD:
|
|
|
|
self._lang = MAP_LANG_TLD[lang].lang
|
|
|
|
self._tld = MAP_LANG_TLD[lang].tld
|
|
|
|
else:
|
|
|
|
self._lang = lang
|
|
|
|
self._tld = tld
|
2019-07-31 19:25:30 +00:00
|
|
|
self.name = "Google"
|
2016-12-13 07:23:08 +00:00
|
|
|
|
2017-01-11 15:31:16 +00:00
|
|
|
@property
|
|
|
|
def default_language(self):
|
2017-05-02 20:47:20 +00:00
|
|
|
"""Return the default language."""
|
2017-01-11 15:31:16 +00:00
|
|
|
return self._lang
|
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_languages(self):
|
2017-05-02 20:47:20 +00:00
|
|
|
"""Return list of supported languages."""
|
2017-01-11 15:31:16 +00:00
|
|
|
return SUPPORT_LANGUAGES
|
|
|
|
|
2022-12-01 12:31:19 +00:00
|
|
|
@property
|
|
|
|
def supported_options(self):
|
|
|
|
"""Return a list of supported options."""
|
|
|
|
return SUPPORT_OPTIONS
|
|
|
|
|
2023-05-24 19:02:55 +00:00
|
|
|
def get_tts_audio(self, message, language, options):
|
2016-12-13 07:23:08 +00:00
|
|
|
"""Load TTS from google."""
|
2022-12-01 12:31:19 +00:00
|
|
|
tld = self._tld
|
|
|
|
if language in MAP_LANG_TLD:
|
|
|
|
tld = MAP_LANG_TLD[language].tld
|
|
|
|
language = MAP_LANG_TLD[language].lang
|
2023-05-24 19:02:55 +00:00
|
|
|
if "tld" in options:
|
2022-12-01 12:31:19 +00:00
|
|
|
tld = options["tld"]
|
|
|
|
tts = gTTS(text=message, lang=language, tld=tld)
|
2020-12-02 21:03:31 +00:00
|
|
|
mp3_data = BytesIO()
|
|
|
|
|
|
|
|
try:
|
|
|
|
tts.write_to_fp(mp3_data)
|
|
|
|
except gTTSError as exc:
|
|
|
|
_LOGGER.exception("Error during processing of TTS request %s", exc)
|
|
|
|
return None, None
|
2016-12-13 07:23:08 +00:00
|
|
|
|
2020-12-02 21:03:31 +00:00
|
|
|
return "mp3", mp3_data.getvalue()
|