Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to pass YandexTTS options via service call #10578

Merged
merged 1 commit into from Nov 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions homeassistant/components/tts/__init__.py
Expand Up @@ -286,10 +286,11 @@ def async_get_url(self, engine, message, cache=None, language=None,
options = options or provider.default_options
if options is not None:
invalid_opts = [opt_name for opt_name in options.keys()
if opt_name not in provider.supported_options]
if opt_name not in (provider.supported_options or
[])]
if invalid_opts:
raise HomeAssistantError(
"Invalid options found: %s", invalid_opts)
"Invalid options found: {}".format(invalid_opts))
options_key = ctypes.c_size_t(hash(frozenset(options))).value
else:
options_key = '-'
Expand Down
21 changes: 17 additions & 4 deletions homeassistant/components/tts/yandextts.py
Expand Up @@ -63,6 +63,13 @@
vol.Range(min=MIN_SPEED, max=MAX_SPEED)
})

SUPPORTED_OPTIONS = [
CONF_CODEC,
CONF_VOICE,
CONF_EMOTION,
CONF_SPEED,
]


@asyncio.coroutine
def async_get_engine(hass, config):
Expand Down Expand Up @@ -94,22 +101,28 @@ def supported_languages(self):
"""Return list of supported languages."""
return SUPPORT_LANGUAGES

@property
def supported_options(self):
"""Return list of supported options."""
return SUPPORTED_OPTIONS

@asyncio.coroutine
def async_get_tts_audio(self, message, language, options=None):
"""Load TTS from yandex."""
websession = async_get_clientsession(self.hass)
actual_language = language
options = options or {}

try:
with async_timeout.timeout(10, loop=self.hass.loop):
url_param = {
'text': message,
'lang': actual_language,
'key': self._key,
'speaker': self._speaker,
'format': self._codec,
'emotion': self._emotion,
'speed': self._speed
'speaker': options.get(CONF_VOICE, self._speaker),
'format': options.get(CONF_CODEC, self._codec),
'emotion': options.get(CONF_EMOTION, self._emotion),
'speed': options.get(CONF_SPEED, self._speed)
}

request = yield from websession.get(
Expand Down
37 changes: 37 additions & 0 deletions tests/components/tts/test_yandextts.py
Expand Up @@ -363,3 +363,40 @@ def test_service_say_specified_speed(self, aioclient_mock):

assert len(aioclient_mock.mock_calls) == 1
assert len(calls) == 1

def test_service_say_specified_options(self, aioclient_mock):
"""Test service call say with options."""
calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)

url_param = {
'text': 'HomeAssistant',
'lang': 'en-US',
'key': '1234567xx',
'speaker': 'zahar',
'format': 'mp3',
'emotion': 'evil',
'speed': 2
}
aioclient_mock.get(
self._base_url, status=200, content=b'test', params=url_param)
config = {
tts.DOMAIN: {
'platform': 'yandextts',
'api_key': '1234567xx',
}
}

with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config)

self.hass.services.call(tts.DOMAIN, 'yandextts_say', {
tts.ATTR_MESSAGE: "HomeAssistant",
'options': {
'emotion': 'evil',
'speed': 2,
}
})
self.hass.block_till_done()

assert len(aioclient_mock.mock_calls) == 1
assert len(calls) == 1