[TTS] options support for service calls (#5436)
* [TTS] Support now options like voice and age on service. * Add unittest
This commit is contained in:
1 parent
074f9315d7
commit
2fff8a5a11
7 files changed
+181
-40
No files matched your search
@@ -1,7 +1,8 @@
|
||||
"""The tests for the TTS component."""
|
||||
import ctypes
|
||||
import os
|
||||
import shutil
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import patch, PropertyMock
|
||||
|
||||
import requests
|
||||
|
||||
@@ -82,11 +83,11 @@ class TestTTS(object):
|
||||
assert calls[0].data[ATTR_MEDIA_CONTENT_TYPE] == MEDIA_TYPE_MUSIC
|
||||
assert calls[0].data[ATTR_MEDIA_CONTENT_ID].find(
|
||||
"/api/tts_proxy/265944c108cbb00b2a621be5930513e03a0bb2cd"
|
||||
"_en_demo.mp3") \
|
||||
"_en_-_demo.mp3") \
|
||||
!= -1
|
||||
assert os.path.isfile(os.path.join(
|
||||
self.default_tts_cache,
|
||||
"265944c108cbb00b2a621be5930513e03a0bb2cd_en_demo.mp3"))
|
||||
"265944c108cbb00b2a621be5930513e03a0bb2cd_en_-_demo.mp3"))
|
||||
|
||||
def test_setup_component_and_test_service_with_config_language(self):
|
||||
"""Setup the demo platform and call service."""
|
||||
@@ -111,11 +112,11 @@ class TestTTS(object):
|
||||
assert calls[0].data[ATTR_MEDIA_CONTENT_TYPE] == MEDIA_TYPE_MUSIC
|
||||
assert calls[0].data[ATTR_MEDIA_CONTENT_ID].find(
|
||||
"/api/tts_proxy/265944c108cbb00b2a621be5930513e03a0bb2cd"
|
||||
"_de_demo.mp3") \
|
||||
"_de_-_demo.mp3") \
|
||||
!= -1
|
||||
assert os.path.isfile(os.path.join(
|
||||
self.default_tts_cache,
|
||||
"265944c108cbb00b2a621be5930513e03a0bb2cd_de_demo.mp3"))
|
||||
"265944c108cbb00b2a621be5930513e03a0bb2cd_de_-_demo.mp3"))
|
||||
|
||||
def test_setup_component_and_test_service_with_wrong_conf_language(self):
|
||||
"""Setup the demo platform and call service with wrong config."""
|
||||
@@ -152,11 +153,11 @@ class TestTTS(object):
|
||||
assert calls[0].data[ATTR_MEDIA_CONTENT_TYPE] == MEDIA_TYPE_MUSIC
|
||||
assert calls[0].data[ATTR_MEDIA_CONTENT_ID].find(
|
||||
"/api/tts_proxy/265944c108cbb00b2a621be5930513e03a0bb2cd"
|
||||
"_de_demo.mp3") \
|
||||
"_de_-_demo.mp3") \
|
||||
!= -1
|
||||
assert os.path.isfile(os.path.join(
|
||||
self.default_tts_cache,
|
||||
"265944c108cbb00b2a621be5930513e03a0bb2cd_de_demo.mp3"))
|
||||
"265944c108cbb00b2a621be5930513e03a0bb2cd_de_-_demo.mp3"))
|
||||
|
||||
def test_setup_component_test_service_with_wrong_service_language(self):
|
||||
"""Setup the demo platform and call service."""
|
||||
@@ -180,7 +181,106 @@ class TestTTS(object):
|
||||
assert len(calls) == 0
|
||||
assert not os.path.isfile(os.path.join(
|
||||
self.default_tts_cache,
|
||||
"265944c108cbb00b2a621be5930513e03a0bb2cd_lang_demo.mp3"))
|
||||
"265944c108cbb00b2a621be5930513e03a0bb2cd_lang_-_demo.mp3"))
|
||||
|
||||
def test_setup_component_and_test_service_with_service_options(self):
|
||||
"""Setup the demo platform and call service with options."""
|
||||
calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
|
||||
|
||||
config = {
|
||||
tts.DOMAIN: {
|
||||
'platform': 'demo',
|
||||
}
|
||||
}
|
||||
|
||||
with assert_setup_component(1, tts.DOMAIN):
|
||||
setup_component(self.hass, tts.DOMAIN, config)
|
||||
|
||||
self.hass.services.call(tts.DOMAIN, 'demo_say', {
|
||||
tts.ATTR_MESSAGE: "I person is on front of your door.",
|
||||
tts.ATTR_LANGUAGE: "de",
|
||||
tts.ATTR_OPTIONS: {
|
||||
'voice': 'alex'
|
||||
}
|
||||
})
|
||||
self.hass.block_till_done()
|
||||
|
||||
opt_hash = ctypes.c_size_t(hash(frozenset({'voice': 'alex'}))).value
|
||||
|
||||
assert len(calls) == 1
|
||||
assert calls[0].data[ATTR_MEDIA_CONTENT_TYPE] == MEDIA_TYPE_MUSIC
|
||||
assert calls[0].data[ATTR_MEDIA_CONTENT_ID].find(
|
||||
"/api/tts_proxy/265944c108cbb00b2a621be5930513e03a0bb2cd"
|
||||
"_de_{0}_demo.mp3".format(opt_hash)) \
|
||||
!= -1
|
||||
assert os.path.isfile(os.path.join(
|
||||
self.default_tts_cache,
|
||||
"265944c108cbb00b2a621be5930513e03a0bb2cd_de_{0}_demo.mp3".format(
|
||||
opt_hash)))
|
||||
|
||||
@patch('homeassistant.components.tts.demo.DemoProvider.default_options',
|
||||
new_callable=PropertyMock(return_value={'voice': 'alex'}))
|
||||
def test_setup_component_and_test_with_service_options_def(self, def_mock):
|
||||
"""Setup the demo platform and call service with default options."""
|
||||
calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
|
||||
|
||||
config = {
|
||||
tts.DOMAIN: {
|
||||
'platform': 'demo',
|
||||
}
|
||||
}
|
||||
|
||||
with assert_setup_component(1, tts.DOMAIN):
|
||||
setup_component(self.hass, tts.DOMAIN, config)
|
||||
|
||||
self.hass.services.call(tts.DOMAIN, 'demo_say', {
|
||||
tts.ATTR_MESSAGE: "I person is on front of your door.",
|
||||
tts.ATTR_LANGUAGE: "de",
|
||||
})
|
||||
self.hass.block_till_done()
|
||||
|
||||
opt_hash = ctypes.c_size_t(hash(frozenset({'voice': 'alex'}))).value
|
||||
|
||||
assert len(calls) == 1
|
||||
assert calls[0].data[ATTR_MEDIA_CONTENT_TYPE] == MEDIA_TYPE_MUSIC
|
||||
assert calls[0].data[ATTR_MEDIA_CONTENT_ID].find(
|
||||
"/api/tts_proxy/265944c108cbb00b2a621be5930513e03a0bb2cd"
|
||||
"_de_{0}_demo.mp3".format(opt_hash)) \
|
||||
!= -1
|
||||
assert os.path.isfile(os.path.join(
|
||||
self.default_tts_cache,
|
||||
"265944c108cbb00b2a621be5930513e03a0bb2cd_de_{0}_demo.mp3".format(
|
||||
opt_hash)))
|
||||
|
||||
def test_setup_component_and_test_service_with_service_options_wrong(self):
|
||||
"""Setup the demo platform and call service with wrong options."""
|
||||
calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
|
||||
|
||||
config = {
|
||||
tts.DOMAIN: {
|
||||
'platform': 'demo',
|
||||
}
|
||||
}
|
||||
|
||||
with assert_setup_component(1, tts.DOMAIN):
|
||||
setup_component(self.hass, tts.DOMAIN, config)
|
||||
|
||||
self.hass.services.call(tts.DOMAIN, 'demo_say', {
|
||||
tts.ATTR_MESSAGE: "I person is on front of your door.",
|
||||
tts.ATTR_LANGUAGE: "de",
|
||||
tts.ATTR_OPTIONS: {
|
||||
'speed': 1
|
||||
}
|
||||
})
|
||||
self.hass.block_till_done()
|
||||
|
||||
opt_hash = ctypes.c_size_t(hash(frozenset({'speed': 1}))).value
|
||||
|
||||
assert len(calls) == 0
|
||||
assert not os.path.isfile(os.path.join(
|
||||
self.default_tts_cache,
|
||||
"265944c108cbb00b2a621be5930513e03a0bb2cd_de_{0}_demo.mp3".format(
|
||||
opt_hash)))
|
||||
|
||||
def test_setup_component_and_test_service_clear_cache(self):
|
||||
"""Setup the demo platform and call service clear cache."""
|
||||
@@ -203,14 +303,14 @@ class TestTTS(object):
|
||||
assert len(calls) == 1
|
||||
assert os.path.isfile(os.path.join(
|
||||
self.default_tts_cache,
|
||||
"265944c108cbb00b2a621be5930513e03a0bb2cd_en_demo.mp3"))
|
||||
"265944c108cbb00b2a621be5930513e03a0bb2cd_en_-_demo.mp3"))
|
||||
|
||||
self.hass.services.call(tts.DOMAIN, tts.SERVICE_CLEAR_CACHE, {})
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert not os.path.isfile(os.path.join(
|
||||
self.default_tts_cache,
|
||||
"265944c108cbb00b2a621be5930513e03a0bb2cd_en_demo.mp3"))
|
||||
"265944c108cbb00b2a621be5930513e03a0bb2cd_en_-_demo.mp3"))
|
||||
|
||||
def test_setup_component_and_test_service_with_receive_voice(self):
|
||||
"""Setup the demo platform and call service and receive voice."""
|
||||
@@ -278,7 +378,7 @@ class TestTTS(object):
|
||||
self.hass.start()
|
||||
|
||||
url = ("{}/api/tts_proxy/265944c108cbb00b2a621be5930513e03a0bb2cd"
|
||||
"_en_demo.mp3").format(self.hass.config.api.base_url)
|
||||
"_en_-_demo.mp3").format(self.hass.config.api.base_url)
|
||||
|
||||
req = requests.get(url)
|
||||
assert req.status_code == 404
|
||||
@@ -297,7 +397,7 @@ class TestTTS(object):
|
||||
self.hass.start()
|
||||
|
||||
url = ("{}/api/tts_proxy/265944dsk32c1b2a621be5930510bb2cd"
|
||||
"_en_demo.mp3").format(self.hass.config.api.base_url)
|
||||
"_en_-_demo.mp3").format(self.hass.config.api.base_url)
|
||||
|
||||
req = requests.get(url)
|
||||
assert req.status_code == 404
|
||||
@@ -324,7 +424,7 @@ class TestTTS(object):
|
||||
assert len(calls) == 1
|
||||
assert not os.path.isfile(os.path.join(
|
||||
self.default_tts_cache,
|
||||
"265944c108cbb00b2a621be5930513e03a0bb2cd_en_demo.mp3"))
|
||||
"265944c108cbb00b2a621be5930513e03a0bb2cd_en_-_demo.mp3"))
|
||||
|
||||
def test_setup_component_test_with_cache_call_service_without_cache(self):
|
||||
"""Setup demo platform with cache and call service without cache."""
|
||||
@@ -349,7 +449,7 @@ class TestTTS(object):
|
||||
assert len(calls) == 1
|
||||
assert not os.path.isfile(os.path.join(
|
||||
self.default_tts_cache,
|
||||
"265944c108cbb00b2a621be5930513e03a0bb2cd_en_demo.mp3"))
|
||||
"265944c108cbb00b2a621be5930513e03a0bb2cd_en_-_demo.mp3"))
|
||||
|
||||
def test_setup_component_test_with_cache_dir(self):
|
||||
"""Setup demo platform with cache and call service without cache."""
|
||||
@@ -358,7 +458,7 @@ class TestTTS(object):
|
||||
_, demo_data = self.demo_provider.get_tts_audio("bla", 'en')
|
||||
cache_file = os.path.join(
|
||||
self.default_tts_cache,
|
||||
"265944c108cbb00b2a621be5930513e03a0bb2cd_en_demo.mp3")
|
||||
"265944c108cbb00b2a621be5930513e03a0bb2cd_en_-_demo.mp3")
|
||||
|
||||
os.mkdir(self.default_tts_cache)
|
||||
with open(cache_file, "wb") as voice_file:
|
||||
@@ -384,7 +484,7 @@ class TestTTS(object):
|
||||
assert len(calls) == 1
|
||||
assert calls[0].data[ATTR_MEDIA_CONTENT_ID].find(
|
||||
"/api/tts_proxy/265944c108cbb00b2a621be5930513e03a0bb2cd"
|
||||
"_en_demo.mp3") \
|
||||
"_en_-_demo.mp3") \
|
||||
!= -1
|
||||
|
||||
@patch('homeassistant.components.tts.demo.DemoProvider.get_tts_audio',
|
||||
@@ -414,7 +514,7 @@ class TestTTS(object):
|
||||
_, demo_data = self.demo_provider.get_tts_audio("bla", 'en')
|
||||
cache_file = os.path.join(
|
||||
self.default_tts_cache,
|
||||
"265944c108cbb00b2a621be5930513e03a0bb2cd_en_demo.mp3")
|
||||
"265944c108cbb00b2a621be5930513e03a0bb2cd_en_-_demo.mp3")
|
||||
|
||||
os.mkdir(self.default_tts_cache)
|
||||
with open(cache_file, "wb") as voice_file:
|
||||
@@ -433,7 +533,7 @@ class TestTTS(object):
|
||||
self.hass.start()
|
||||
|
||||
url = ("{}/api/tts_proxy/265944c108cbb00b2a621be5930513e03a0bb2cd"
|
||||
"_en_demo.mp3").format(self.hass.config.api.base_url)
|
||||
"_en_-_demo.mp3").format(self.hass.config.api.base_url)
|
||||
|
||||
req = requests.get(url)
|
||||
assert req.status_code == 200
|
||||
|
||||
Reference in new issue
Block a user