2016-12-16 08:10:48 +00:00
|
|
|
"""
|
|
|
|
Support for the voicerss speech service.
|
|
|
|
|
|
|
|
For more details about this component, please refer to the documentation at
|
2017-01-15 13:53:07 +00:00
|
|
|
https://home-assistant.io/components/tts.voicerss/
|
2016-12-16 08:10:48 +00:00
|
|
|
"""
|
|
|
|
import asyncio
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import aiohttp
|
|
|
|
import async_timeout
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-03-29 18:45:02 +00:00
|
|
|
from homeassistant.components.tts import CONF_LANG, PLATFORM_SCHEMA, Provider
|
2016-12-16 08:10:48 +00:00
|
|
|
from homeassistant.const import CONF_API_KEY
|
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
VOICERSS_API_URL = "https://api.voicerss.org/"
|
|
|
|
|
2016-12-21 09:22:12 +00:00
|
|
|
ERROR_MSG = [
|
|
|
|
b'Error description',
|
|
|
|
b'The subscription is expired or requests count limitation is exceeded!',
|
|
|
|
b'The request content length is too large!',
|
|
|
|
b'The language does not support!',
|
|
|
|
b'The language is not specified!',
|
|
|
|
b'The text is not specified!',
|
|
|
|
b'The API key is not available!',
|
|
|
|
b'The API key is not specified!',
|
|
|
|
b'The subscription does not support SSML!',
|
|
|
|
]
|
|
|
|
|
2016-12-16 08:10:48 +00:00
|
|
|
SUPPORT_LANGUAGES = [
|
|
|
|
'ca-es', 'zh-cn', 'zh-hk', 'zh-tw', 'da-dk', 'nl-nl', 'en-au', 'en-ca',
|
|
|
|
'en-gb', 'en-in', 'en-us', 'fi-fi', 'fr-ca', 'fr-fr', 'de-de', 'it-it',
|
|
|
|
'ja-jp', 'ko-kr', 'nb-no', 'pl-pl', 'pt-br', 'pt-pt', 'ru-ru', 'es-mx',
|
|
|
|
'es-es', 'sv-se',
|
|
|
|
]
|
|
|
|
|
|
|
|
SUPPORT_CODECS = [
|
|
|
|
'mp3', 'wav', 'aac', 'ogg', 'caf'
|
|
|
|
]
|
|
|
|
|
|
|
|
SUPPORT_FORMATS = [
|
|
|
|
'8khz_8bit_mono', '8khz_8bit_stereo', '8khz_16bit_mono',
|
|
|
|
'8khz_16bit_stereo', '11khz_8bit_mono', '11khz_8bit_stereo',
|
|
|
|
'11khz_16bit_mono', '11khz_16bit_stereo', '12khz_8bit_mono',
|
|
|
|
'12khz_8bit_stereo', '12khz_16bit_mono', '12khz_16bit_stereo',
|
|
|
|
'16khz_8bit_mono', '16khz_8bit_stereo', '16khz_16bit_mono',
|
|
|
|
'16khz_16bit_stereo', '22khz_8bit_mono', '22khz_8bit_stereo',
|
|
|
|
'22khz_16bit_mono', '22khz_16bit_stereo', '24khz_8bit_mono',
|
|
|
|
'24khz_8bit_stereo', '24khz_16bit_mono', '24khz_16bit_stereo',
|
|
|
|
'32khz_8bit_mono', '32khz_8bit_stereo', '32khz_16bit_mono',
|
|
|
|
'32khz_16bit_stereo', '44khz_8bit_mono', '44khz_8bit_stereo',
|
|
|
|
'44khz_16bit_mono', '44khz_16bit_stereo', '48khz_8bit_mono',
|
|
|
|
'48khz_8bit_stereo', '48khz_16bit_mono', '48khz_16bit_stereo',
|
|
|
|
'alaw_8khz_mono', 'alaw_8khz_stereo', 'alaw_11khz_mono',
|
|
|
|
'alaw_11khz_stereo', 'alaw_22khz_mono', 'alaw_22khz_stereo',
|
|
|
|
'alaw_44khz_mono', 'alaw_44khz_stereo', 'ulaw_8khz_mono',
|
|
|
|
'ulaw_8khz_stereo', 'ulaw_11khz_mono', 'ulaw_11khz_stereo',
|
|
|
|
'ulaw_22khz_mono', 'ulaw_22khz_stereo', 'ulaw_44khz_mono',
|
|
|
|
'ulaw_44khz_stereo',
|
|
|
|
]
|
|
|
|
|
|
|
|
CONF_CODEC = 'codec'
|
|
|
|
CONF_FORMAT = 'format'
|
|
|
|
|
|
|
|
DEFAULT_LANG = 'en-us'
|
|
|
|
DEFAULT_CODEC = 'mp3'
|
|
|
|
DEFAULT_FORMAT = '8khz_8bit_mono'
|
|
|
|
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_API_KEY): cv.string,
|
|
|
|
vol.Optional(CONF_LANG, default=DEFAULT_LANG): vol.In(SUPPORT_LANGUAGES),
|
|
|
|
vol.Optional(CONF_CODEC, default=DEFAULT_CODEC): vol.In(SUPPORT_CODECS),
|
|
|
|
vol.Optional(CONF_FORMAT, default=DEFAULT_FORMAT): vol.In(SUPPORT_FORMATS),
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2018-10-01 06:55:00 +00:00
|
|
|
async def async_get_engine(hass, config):
|
2017-01-15 13:53:07 +00:00
|
|
|
"""Set up VoiceRSS TTS component."""
|
2016-12-16 08:10:48 +00:00
|
|
|
return VoiceRSSProvider(hass, config)
|
|
|
|
|
|
|
|
|
|
|
|
class VoiceRSSProvider(Provider):
|
2017-01-15 13:53:07 +00:00
|
|
|
"""The VoiceRSS speech API provider."""
|
2016-12-16 08:10:48 +00:00
|
|
|
|
|
|
|
def __init__(self, hass, conf):
|
|
|
|
"""Init VoiceRSS TTS service."""
|
|
|
|
self.hass = hass
|
2017-01-11 15:31:16 +00:00
|
|
|
self._extension = conf[CONF_CODEC]
|
|
|
|
self._lang = conf[CONF_LANG]
|
2017-02-07 11:07:11 +00:00
|
|
|
self.name = 'VoiceRSS'
|
2017-01-11 15:31:16 +00:00
|
|
|
|
|
|
|
self._form_data = {
|
|
|
|
'key': conf[CONF_API_KEY],
|
|
|
|
'hl': conf[CONF_LANG],
|
|
|
|
'c': (conf[CONF_CODEC]).upper(),
|
|
|
|
'f': conf[CONF_FORMAT],
|
2016-12-16 08:10:48 +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
|
|
|
|
|
2018-10-01 06:55:00 +00:00
|
|
|
async def async_get_tts_audio(self, message, language, options=None):
|
2017-01-15 13:53:07 +00:00
|
|
|
"""Load TTS from VoiceRSS."""
|
2016-12-16 08:10:48 +00:00
|
|
|
websession = async_get_clientsession(self.hass)
|
2017-01-11 15:31:16 +00:00
|
|
|
form_data = self._form_data.copy()
|
2016-12-21 09:22:12 +00:00
|
|
|
|
|
|
|
form_data['src'] = message
|
2017-01-11 15:31:16 +00:00
|
|
|
form_data['hl'] = language
|
2016-12-27 16:01:22 +00:00
|
|
|
|
2016-12-16 08:10:48 +00:00
|
|
|
try:
|
|
|
|
with async_timeout.timeout(10, loop=self.hass.loop):
|
2018-10-01 06:55:00 +00:00
|
|
|
request = await websession.post(
|
2016-12-21 09:22:12 +00:00
|
|
|
VOICERSS_API_URL, data=form_data
|
2016-12-16 08:10:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if request.status != 200:
|
2016-12-21 09:22:12 +00:00
|
|
|
_LOGGER.error("Error %d on load url %s.",
|
2016-12-16 08:10:48 +00:00
|
|
|
request.status, request.url)
|
|
|
|
return (None, None)
|
2018-10-01 06:55:00 +00:00
|
|
|
data = await request.read()
|
2016-12-16 08:10:48 +00:00
|
|
|
|
2016-12-21 09:22:12 +00:00
|
|
|
if data in ERROR_MSG:
|
|
|
|
_LOGGER.error(
|
2017-01-15 13:53:07 +00:00
|
|
|
"Error receive %s from VoiceRSS", str(data, 'utf-8'))
|
2016-12-21 09:22:12 +00:00
|
|
|
return (None, None)
|
|
|
|
|
2017-03-30 07:50:53 +00:00
|
|
|
except (asyncio.TimeoutError, aiohttp.ClientError):
|
2017-01-15 13:53:07 +00:00
|
|
|
_LOGGER.error("Timeout for VoiceRSS API")
|
2016-12-16 08:10:48 +00:00
|
|
|
return (None, None)
|
|
|
|
|
2017-01-11 15:31:16 +00:00
|
|
|
return (self._extension, data)
|