Convert remaining TTS tests to async (#64505)

* Convert remaining TTS tests to async

* Add block till done after setting up component
This commit is contained in:
Paulus Schoutsen 2022-01-20 08:58:19 -08:00 committed by GitHub
parent 8fda3ae4cb
commit ddf548cd27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 611 additions and 639 deletions

View File

@ -1,83 +1,62 @@
"""The tests for the MaryTTS speech platform.""" """The tests for the MaryTTS speech platform."""
import asyncio
import os import os
import shutil import shutil
from unittest.mock import patch from unittest.mock import patch
import pytest
from homeassistant.components.media_player.const import ( from homeassistant.components.media_player.const import (
ATTR_MEDIA_CONTENT_ID, ATTR_MEDIA_CONTENT_ID,
DOMAIN as DOMAIN_MP, DOMAIN as DOMAIN_MP,
SERVICE_PLAY_MEDIA, SERVICE_PLAY_MEDIA,
) )
import homeassistant.components.tts as tts import homeassistant.components.tts as tts
from homeassistant.config import async_process_ha_core_config from homeassistant.setup import async_setup_component
from homeassistant.setup import setup_component
from tests.common import assert_setup_component, get_test_home_assistant, mock_service from tests.common import assert_setup_component, async_mock_service
class TestTTSMaryTTSPlatform: @pytest.fixture(autouse=True)
"""Test the speech component.""" def cleanup_cache(hass):
"""Prevent TTS writing."""
def setup_method(self): yield
"""Set up things to be run when tests are started.""" default_tts = hass.config.path(tts.DEFAULT_CACHE_DIR)
self.hass = get_test_home_assistant()
asyncio.run_coroutine_threadsafe(
async_process_ha_core_config(
self.hass, {"internal_url": "http://example.local:8123"}
),
self.hass.loop,
)
self.host = "localhost"
self.port = 59125
self.params = {
"INPUT_TEXT": "HomeAssistant",
"INPUT_TYPE": "TEXT",
"OUTPUT_TYPE": "AUDIO",
"LOCALE": "en_US",
"AUDIO": "WAVE_FILE",
"VOICE": "cmu-slt-hsmm",
}
def teardown_method(self):
"""Stop everything that was started."""
default_tts = self.hass.config.path(tts.DEFAULT_CACHE_DIR)
if os.path.isdir(default_tts): if os.path.isdir(default_tts):
shutil.rmtree(default_tts) shutil.rmtree(default_tts)
self.hass.stop()
def test_setup_component(self): async def test_setup_component(hass):
"""Test setup component.""" """Test setup component."""
config = {tts.DOMAIN: {"platform": "marytts"}} config = {tts.DOMAIN: {"platform": "marytts"}}
with assert_setup_component(1, tts.DOMAIN): with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config) await async_setup_component(hass, tts.DOMAIN, config)
await hass.async_block_till_done()
def test_service_say(self):
async def test_service_say(hass):
"""Test service call say.""" """Test service call say."""
calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
config = {tts.DOMAIN: {"platform": "marytts"}} config = {tts.DOMAIN: {"platform": "marytts"}}
with assert_setup_component(1, tts.DOMAIN): with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config) await async_setup_component(hass, tts.DOMAIN, config)
await hass.async_block_till_done()
with patch( with patch(
"homeassistant.components.marytts.tts.MaryTTS.speak", "homeassistant.components.marytts.tts.MaryTTS.speak",
return_value=b"audio", return_value=b"audio",
) as mock_speak: ) as mock_speak:
self.hass.services.call( await hass.services.async_call(
tts.DOMAIN, tts.DOMAIN,
"marytts_say", "marytts_say",
{ {
"entity_id": "media_player.something", "entity_id": "media_player.something",
tts.ATTR_MESSAGE: "HomeAssistant", tts.ATTR_MESSAGE: "HomeAssistant",
}, },
blocking=True,
) )
self.hass.block_till_done()
mock_speak.assert_called_once() mock_speak.assert_called_once()
mock_speak.assert_called_with("HomeAssistant", {}) mock_speak.assert_called_with("HomeAssistant", {})
@ -85,30 +64,30 @@ class TestTTSMaryTTSPlatform:
assert len(calls) == 1 assert len(calls) == 1
assert calls[0].data[ATTR_MEDIA_CONTENT_ID].find(".wav") != -1 assert calls[0].data[ATTR_MEDIA_CONTENT_ID].find(".wav") != -1
def test_service_say_with_effect(self):
"""Test service call say with effects."""
calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
config = { async def test_service_say_with_effect(hass):
tts.DOMAIN: {"platform": "marytts", "effect": {"Volume": "amount:2.0;"}} """Test service call say with effects."""
} calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
config = {tts.DOMAIN: {"platform": "marytts", "effect": {"Volume": "amount:2.0;"}}}
with assert_setup_component(1, tts.DOMAIN): with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config) await async_setup_component(hass, tts.DOMAIN, config)
await hass.async_block_till_done()
with patch( with patch(
"homeassistant.components.marytts.tts.MaryTTS.speak", "homeassistant.components.marytts.tts.MaryTTS.speak",
return_value=b"audio", return_value=b"audio",
) as mock_speak: ) as mock_speak:
self.hass.services.call( await hass.services.async_call(
tts.DOMAIN, tts.DOMAIN,
"marytts_say", "marytts_say",
{ {
"entity_id": "media_player.something", "entity_id": "media_player.something",
tts.ATTR_MESSAGE: "HomeAssistant", tts.ATTR_MESSAGE: "HomeAssistant",
}, },
blocking=True,
) )
self.hass.block_till_done()
mock_speak.assert_called_once() mock_speak.assert_called_once()
mock_speak.assert_called_with("HomeAssistant", {"Volume": "amount:2.0;"}) mock_speak.assert_called_with("HomeAssistant", {"Volume": "amount:2.0;"})
@ -116,20 +95,22 @@ class TestTTSMaryTTSPlatform:
assert len(calls) == 1 assert len(calls) == 1
assert calls[0].data[ATTR_MEDIA_CONTENT_ID].find(".wav") != -1 assert calls[0].data[ATTR_MEDIA_CONTENT_ID].find(".wav") != -1
def test_service_say_http_error(self):
async def test_service_say_http_error(hass):
"""Test service call say.""" """Test service call say."""
calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
config = {tts.DOMAIN: {"platform": "marytts"}} config = {tts.DOMAIN: {"platform": "marytts"}}
with assert_setup_component(1, tts.DOMAIN): with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config) await async_setup_component(hass, tts.DOMAIN, config)
await hass.async_block_till_done()
with patch( with patch(
"homeassistant.components.marytts.tts.MaryTTS.speak", "homeassistant.components.marytts.tts.MaryTTS.speak",
side_effect=Exception(), side_effect=Exception(),
) as mock_speak: ) as mock_speak:
self.hass.services.call( await hass.services.async_call(
tts.DOMAIN, tts.DOMAIN,
"marytts_say", "marytts_say",
{ {
@ -137,7 +118,7 @@ class TestTTSMaryTTSPlatform:
tts.ATTR_MESSAGE: "HomeAssistant", tts.ATTR_MESSAGE: "HomeAssistant",
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
mock_speak.assert_called_once() mock_speak.assert_called_once()
assert len(calls) == 0 assert len(calls) == 0

View File

@ -4,35 +4,21 @@ from http import HTTPStatus
import os import os
import shutil import shutil
import pytest
from homeassistant.components.media_player.const import ( from homeassistant.components.media_player.const import (
ATTR_MEDIA_CONTENT_ID, ATTR_MEDIA_CONTENT_ID,
DOMAIN as DOMAIN_MP, DOMAIN as DOMAIN_MP,
SERVICE_PLAY_MEDIA, SERVICE_PLAY_MEDIA,
) )
import homeassistant.components.tts as tts import homeassistant.components.tts as tts
from homeassistant.config import async_process_ha_core_config from homeassistant.setup import async_setup_component
from homeassistant.setup import setup_component
from tests.common import assert_setup_component, get_test_home_assistant, mock_service from tests.common import assert_setup_component, async_mock_service
from tests.components.tts.test_init import mutagen_mock # noqa: F401 from tests.components.tts.test_init import mutagen_mock # noqa: F401
URL = "https://api.voicerss.org/"
class TestTTSVoiceRSSPlatform: FORM_DATA = {
"""Test the voicerss speech component."""
def setup_method(self):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
asyncio.run_coroutine_threadsafe(
async_process_ha_core_config(
self.hass, {"internal_url": "http://example.local:8123"}
),
self.hass.loop,
)
self.url = "https://api.voicerss.org/"
self.form_data = {
"key": "1234567xx", "key": "1234567xx",
"hl": "en-us", "hl": "en-us",
"c": "MP3", "c": "MP3",
@ -40,42 +26,47 @@ class TestTTSVoiceRSSPlatform:
"src": "I person is on front of your door.", "src": "I person is on front of your door.",
} }
def teardown_method(self):
"""Stop everything that was started.""" @pytest.fixture(autouse=True)
default_tts = self.hass.config.path(tts.DEFAULT_CACHE_DIR) def cleanup_cache(hass):
"""Prevent TTS writing."""
yield
default_tts = hass.config.path(tts.DEFAULT_CACHE_DIR)
if os.path.isdir(default_tts): if os.path.isdir(default_tts):
shutil.rmtree(default_tts) shutil.rmtree(default_tts)
self.hass.stop()
def test_setup_component(self): async def test_setup_component(hass):
"""Test setup component.""" """Test setup component."""
config = {tts.DOMAIN: {"platform": "voicerss", "api_key": "1234567xx"}} config = {tts.DOMAIN: {"platform": "voicerss", "api_key": "1234567xx"}}
with assert_setup_component(1, tts.DOMAIN): with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config) await async_setup_component(hass, tts.DOMAIN, config)
await hass.async_block_till_done()
def test_setup_component_without_api_key(self):
async def test_setup_component_without_api_key(hass):
"""Test setup component without api key.""" """Test setup component without api key."""
config = {tts.DOMAIN: {"platform": "voicerss"}} config = {tts.DOMAIN: {"platform": "voicerss"}}
with assert_setup_component(0, tts.DOMAIN): with assert_setup_component(0, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config) await async_setup_component(hass, tts.DOMAIN, config)
await hass.async_block_till_done()
def test_service_say(self, aioclient_mock):
async def test_service_say(hass, aioclient_mock):
"""Test service call say.""" """Test service call say."""
calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
aioclient_mock.post( aioclient_mock.post(URL, data=FORM_DATA, status=HTTPStatus.OK, content=b"test")
self.url, data=self.form_data, status=HTTPStatus.OK, content=b"test"
)
config = {tts.DOMAIN: {"platform": "voicerss", "api_key": "1234567xx"}} config = {tts.DOMAIN: {"platform": "voicerss", "api_key": "1234567xx"}}
with assert_setup_component(1, tts.DOMAIN): with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config) await async_setup_component(hass, tts.DOMAIN, config)
await hass.async_block_till_done()
self.hass.services.call( await hass.services.async_call(
tts.DOMAIN, tts.DOMAIN,
"voicerss_say", "voicerss_say",
{ {
@ -83,21 +74,20 @@ class TestTTSVoiceRSSPlatform:
tts.ATTR_MESSAGE: "I person is on front of your door.", tts.ATTR_MESSAGE: "I person is on front of your door.",
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(calls) == 1
assert len(aioclient_mock.mock_calls) == 1 assert len(aioclient_mock.mock_calls) == 1
assert aioclient_mock.mock_calls[0][2] == self.form_data assert aioclient_mock.mock_calls[0][2] == FORM_DATA
assert calls[0].data[ATTR_MEDIA_CONTENT_ID].find(".mp3") != -1 assert calls[0].data[ATTR_MEDIA_CONTENT_ID].find(".mp3") != -1
def test_service_say_german_config(self, aioclient_mock):
"""Test service call say with german code in the config."""
calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
self.form_data["hl"] = "de-de" async def test_service_say_german_config(hass, aioclient_mock):
aioclient_mock.post( """Test service call say with german code in the config."""
self.url, data=self.form_data, status=HTTPStatus.OK, content=b"test" calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
)
form_data = {**FORM_DATA, "hl": "de-de"}
aioclient_mock.post(URL, data=form_data, status=HTTPStatus.OK, content=b"test")
config = { config = {
tts.DOMAIN: { tts.DOMAIN: {
@ -108,9 +98,10 @@ class TestTTSVoiceRSSPlatform:
} }
with assert_setup_component(1, tts.DOMAIN): with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config) await async_setup_component(hass, tts.DOMAIN, config)
await hass.async_block_till_done()
self.hass.services.call( await hass.services.async_call(
tts.DOMAIN, tts.DOMAIN,
"voicerss_say", "voicerss_say",
{ {
@ -118,27 +109,27 @@ class TestTTSVoiceRSSPlatform:
tts.ATTR_MESSAGE: "I person is on front of your door.", tts.ATTR_MESSAGE: "I person is on front of your door.",
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(calls) == 1
assert len(aioclient_mock.mock_calls) == 1 assert len(aioclient_mock.mock_calls) == 1
assert aioclient_mock.mock_calls[0][2] == self.form_data assert aioclient_mock.mock_calls[0][2] == form_data
def test_service_say_german_service(self, aioclient_mock):
async def test_service_say_german_service(hass, aioclient_mock):
"""Test service call say with german code in the service.""" """Test service call say with german code in the service."""
calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
self.form_data["hl"] = "de-de" form_data = {**FORM_DATA, "hl": "de-de"}
aioclient_mock.post( aioclient_mock.post(URL, data=form_data, status=HTTPStatus.OK, content=b"test")
self.url, data=self.form_data, status=HTTPStatus.OK, content=b"test"
)
config = {tts.DOMAIN: {"platform": "voicerss", "api_key": "1234567xx"}} config = {tts.DOMAIN: {"platform": "voicerss", "api_key": "1234567xx"}}
with assert_setup_component(1, tts.DOMAIN): with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config) await async_setup_component(hass, tts.DOMAIN, config)
await hass.async_block_till_done()
self.hass.services.call( await hass.services.async_call(
tts.DOMAIN, tts.DOMAIN,
"voicerss_say", "voicerss_say",
{ {
@ -147,24 +138,26 @@ class TestTTSVoiceRSSPlatform:
tts.ATTR_LANGUAGE: "de-de", tts.ATTR_LANGUAGE: "de-de",
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(calls) == 1
assert len(aioclient_mock.mock_calls) == 1 assert len(aioclient_mock.mock_calls) == 1
assert aioclient_mock.mock_calls[0][2] == self.form_data assert aioclient_mock.mock_calls[0][2] == form_data
def test_service_say_error(self, aioclient_mock):
async def test_service_say_error(hass, aioclient_mock):
"""Test service call say with http response 400.""" """Test service call say with http response 400."""
calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
aioclient_mock.post(self.url, data=self.form_data, status=400, content=b"test") aioclient_mock.post(URL, data=FORM_DATA, status=400, content=b"test")
config = {tts.DOMAIN: {"platform": "voicerss", "api_key": "1234567xx"}} config = {tts.DOMAIN: {"platform": "voicerss", "api_key": "1234567xx"}}
with assert_setup_component(1, tts.DOMAIN): with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config) await async_setup_component(hass, tts.DOMAIN, config)
await hass.async_block_till_done()
self.hass.services.call( await hass.services.async_call(
tts.DOMAIN, tts.DOMAIN,
"voicerss_say", "voicerss_say",
{ {
@ -172,24 +165,26 @@ class TestTTSVoiceRSSPlatform:
tts.ATTR_MESSAGE: "I person is on front of your door.", tts.ATTR_MESSAGE: "I person is on front of your door.",
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(calls) == 0
assert len(aioclient_mock.mock_calls) == 1 assert len(aioclient_mock.mock_calls) == 1
assert aioclient_mock.mock_calls[0][2] == self.form_data assert aioclient_mock.mock_calls[0][2] == FORM_DATA
def test_service_say_timeout(self, aioclient_mock):
async def test_service_say_timeout(hass, aioclient_mock):
"""Test service call say with http timeout.""" """Test service call say with http timeout."""
calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
aioclient_mock.post(self.url, data=self.form_data, exc=asyncio.TimeoutError()) aioclient_mock.post(URL, data=FORM_DATA, exc=asyncio.TimeoutError())
config = {tts.DOMAIN: {"platform": "voicerss", "api_key": "1234567xx"}} config = {tts.DOMAIN: {"platform": "voicerss", "api_key": "1234567xx"}}
with assert_setup_component(1, tts.DOMAIN): with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config) await async_setup_component(hass, tts.DOMAIN, config)
await hass.async_block_till_done()
self.hass.services.call( await hass.services.async_call(
tts.DOMAIN, tts.DOMAIN,
"voicerss_say", "voicerss_say",
{ {
@ -197,19 +192,20 @@ class TestTTSVoiceRSSPlatform:
tts.ATTR_MESSAGE: "I person is on front of your door.", tts.ATTR_MESSAGE: "I person is on front of your door.",
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(calls) == 0
assert len(aioclient_mock.mock_calls) == 1 assert len(aioclient_mock.mock_calls) == 1
assert aioclient_mock.mock_calls[0][2] == self.form_data assert aioclient_mock.mock_calls[0][2] == FORM_DATA
def test_service_say_error_msg(self, aioclient_mock):
async def test_service_say_error_msg(hass, aioclient_mock):
"""Test service call say with http error api message.""" """Test service call say with http error api message."""
calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
aioclient_mock.post( aioclient_mock.post(
self.url, URL,
data=self.form_data, data=FORM_DATA,
status=HTTPStatus.OK, status=HTTPStatus.OK,
content=b"The subscription does not support SSML!", content=b"The subscription does not support SSML!",
) )
@ -217,9 +213,10 @@ class TestTTSVoiceRSSPlatform:
config = {tts.DOMAIN: {"platform": "voicerss", "api_key": "1234567xx"}} config = {tts.DOMAIN: {"platform": "voicerss", "api_key": "1234567xx"}}
with assert_setup_component(1, tts.DOMAIN): with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config) await async_setup_component(hass, tts.DOMAIN, config)
await hass.async_block_till_done()
self.hass.services.call( await hass.services.async_call(
tts.DOMAIN, tts.DOMAIN,
"voicerss_say", "voicerss_say",
{ {
@ -227,8 +224,8 @@ class TestTTSVoiceRSSPlatform:
tts.ATTR_MESSAGE: "I person is on front of your door.", tts.ATTR_MESSAGE: "I person is on front of your door.",
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(calls) == 0
assert len(aioclient_mock.mock_calls) == 1 assert len(aioclient_mock.mock_calls) == 1
assert aioclient_mock.mock_calls[0][2] == self.form_data assert aioclient_mock.mock_calls[0][2] == FORM_DATA

View File

@ -4,60 +4,53 @@ from http import HTTPStatus
import os import os
import shutil import shutil
import pytest
from homeassistant.components.media_player.const import ( from homeassistant.components.media_player.const import (
DOMAIN as DOMAIN_MP, DOMAIN as DOMAIN_MP,
SERVICE_PLAY_MEDIA, SERVICE_PLAY_MEDIA,
) )
import homeassistant.components.tts as tts import homeassistant.components.tts as tts
from homeassistant.config import async_process_ha_core_config from homeassistant.setup import async_setup_component
from homeassistant.setup import setup_component
from tests.common import assert_setup_component, get_test_home_assistant, mock_service from tests.common import assert_setup_component, async_mock_service
from tests.components.tts.test_init import ( # noqa: F401, pylint: disable=unused-import from tests.components.tts.test_init import ( # noqa: F401, pylint: disable=unused-import
mutagen_mock, mutagen_mock,
) )
URL = "https://tts.voicetech.yandex.net/generate?"
class TestTTSYandexPlatform:
"""Test the speech component."""
def setup_method(self): @pytest.fixture(autouse=True)
"""Set up things to be run when tests are started.""" def cleanup_cache(hass):
self.hass = get_test_home_assistant() """Prevent TTS writing."""
self._base_url = "https://tts.voicetech.yandex.net/generate?" yield
default_tts = hass.config.path(tts.DEFAULT_CACHE_DIR)
asyncio.run_coroutine_threadsafe(
async_process_ha_core_config(
self.hass, {"internal_url": "http://example.local:8123"}
),
self.hass.loop,
)
def teardown_method(self):
"""Stop everything that was started."""
default_tts = self.hass.config.path(tts.DEFAULT_CACHE_DIR)
if os.path.isdir(default_tts): if os.path.isdir(default_tts):
shutil.rmtree(default_tts) shutil.rmtree(default_tts)
self.hass.stop()
def test_setup_component(self): async def test_setup_component(hass):
"""Test setup component.""" """Test setup component."""
config = {tts.DOMAIN: {"platform": "yandextts", "api_key": "1234567xx"}} config = {tts.DOMAIN: {"platform": "yandextts", "api_key": "1234567xx"}}
with assert_setup_component(1, tts.DOMAIN): with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config) await async_setup_component(hass, tts.DOMAIN, config)
await hass.async_block_till_done()
def test_setup_component_without_api_key(self):
async def test_setup_component_without_api_key(hass):
"""Test setup component without api key.""" """Test setup component without api key."""
config = {tts.DOMAIN: {"platform": "yandextts"}} config = {tts.DOMAIN: {"platform": "yandextts"}}
with assert_setup_component(0, tts.DOMAIN): with assert_setup_component(0, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config) await async_setup_component(hass, tts.DOMAIN, config)
await hass.async_block_till_done()
def test_service_say(self, aioclient_mock):
async def test_service_say(hass, aioclient_mock):
"""Test service call say.""" """Test service call say."""
calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
url_param = { url_param = {
"text": "HomeAssistant", "text": "HomeAssistant",
@ -68,28 +61,28 @@ class TestTTSYandexPlatform:
"emotion": "neutral", "emotion": "neutral",
"speed": 1, "speed": 1,
} }
aioclient_mock.get( aioclient_mock.get(URL, status=HTTPStatus.OK, content=b"test", params=url_param)
self._base_url, status=HTTPStatus.OK, content=b"test", params=url_param
)
config = {tts.DOMAIN: {"platform": "yandextts", "api_key": "1234567xx"}} config = {tts.DOMAIN: {"platform": "yandextts", "api_key": "1234567xx"}}
with assert_setup_component(1, tts.DOMAIN): with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config) await async_setup_component(hass, tts.DOMAIN, config)
await hass.async_block_till_done()
self.hass.services.call( await hass.services.async_call(
tts.DOMAIN, tts.DOMAIN,
"yandextts_say", "yandextts_say",
{"entity_id": "media_player.something", tts.ATTR_MESSAGE: "HomeAssistant"}, {"entity_id": "media_player.something", tts.ATTR_MESSAGE: "HomeAssistant"},
) )
self.hass.block_till_done() await hass.async_block_till_done()
assert len(aioclient_mock.mock_calls) == 1 assert len(aioclient_mock.mock_calls) == 1
assert len(calls) == 1 assert len(calls) == 1
def test_service_say_russian_config(self, aioclient_mock):
async def test_service_say_russian_config(hass, aioclient_mock):
"""Test service call say.""" """Test service call say."""
calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
url_param = { url_param = {
"text": "HomeAssistant", "text": "HomeAssistant",
@ -100,9 +93,7 @@ class TestTTSYandexPlatform:
"emotion": "neutral", "emotion": "neutral",
"speed": 1, "speed": 1,
} }
aioclient_mock.get( aioclient_mock.get(URL, status=HTTPStatus.OK, content=b"test", params=url_param)
self._base_url, status=HTTPStatus.OK, content=b"test", params=url_param
)
config = { config = {
tts.DOMAIN: { tts.DOMAIN: {
@ -113,21 +104,23 @@ class TestTTSYandexPlatform:
} }
with assert_setup_component(1, tts.DOMAIN): with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config) await async_setup_component(hass, tts.DOMAIN, config)
await hass.async_block_till_done()
self.hass.services.call( await hass.services.async_call(
tts.DOMAIN, tts.DOMAIN,
"yandextts_say", "yandextts_say",
{"entity_id": "media_player.something", tts.ATTR_MESSAGE: "HomeAssistant"}, {"entity_id": "media_player.something", tts.ATTR_MESSAGE: "HomeAssistant"},
) )
self.hass.block_till_done() await hass.async_block_till_done()
assert len(aioclient_mock.mock_calls) == 1 assert len(aioclient_mock.mock_calls) == 1
assert len(calls) == 1 assert len(calls) == 1
def test_service_say_russian_service(self, aioclient_mock):
async def test_service_say_russian_service(hass, aioclient_mock):
"""Test service call say.""" """Test service call say."""
calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
url_param = { url_param = {
"text": "HomeAssistant", "text": "HomeAssistant",
@ -138,16 +131,15 @@ class TestTTSYandexPlatform:
"emotion": "neutral", "emotion": "neutral",
"speed": 1, "speed": 1,
} }
aioclient_mock.get( aioclient_mock.get(URL, status=HTTPStatus.OK, content=b"test", params=url_param)
self._base_url, status=HTTPStatus.OK, content=b"test", params=url_param
)
config = {tts.DOMAIN: {"platform": "yandextts", "api_key": "1234567xx"}} config = {tts.DOMAIN: {"platform": "yandextts", "api_key": "1234567xx"}}
with assert_setup_component(1, tts.DOMAIN): with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config) await async_setup_component(hass, tts.DOMAIN, config)
await hass.async_block_till_done()
self.hass.services.call( await hass.services.async_call(
tts.DOMAIN, tts.DOMAIN,
"yandextts_say", "yandextts_say",
{ {
@ -156,14 +148,15 @@ class TestTTSYandexPlatform:
tts.ATTR_LANGUAGE: "ru-RU", tts.ATTR_LANGUAGE: "ru-RU",
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
assert len(aioclient_mock.mock_calls) == 1 assert len(aioclient_mock.mock_calls) == 1
assert len(calls) == 1 assert len(calls) == 1
def test_service_say_timeout(self, aioclient_mock):
async def test_service_say_timeout(hass, aioclient_mock):
"""Test service call say.""" """Test service call say."""
calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
url_param = { url_param = {
"text": "HomeAssistant", "text": "HomeAssistant",
@ -175,7 +168,7 @@ class TestTTSYandexPlatform:
"speed": 1, "speed": 1,
} }
aioclient_mock.get( aioclient_mock.get(
self._base_url, URL,
status=HTTPStatus.OK, status=HTTPStatus.OK,
exc=asyncio.TimeoutError(), exc=asyncio.TimeoutError(),
params=url_param, params=url_param,
@ -184,21 +177,23 @@ class TestTTSYandexPlatform:
config = {tts.DOMAIN: {"platform": "yandextts", "api_key": "1234567xx"}} config = {tts.DOMAIN: {"platform": "yandextts", "api_key": "1234567xx"}}
with assert_setup_component(1, tts.DOMAIN): with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config) await async_setup_component(hass, tts.DOMAIN, config)
await hass.async_block_till_done()
self.hass.services.call( await hass.services.async_call(
tts.DOMAIN, tts.DOMAIN,
"yandextts_say", "yandextts_say",
{"entity_id": "media_player.something", tts.ATTR_MESSAGE: "HomeAssistant"}, {"entity_id": "media_player.something", tts.ATTR_MESSAGE: "HomeAssistant"},
) )
self.hass.block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(calls) == 0
assert len(aioclient_mock.mock_calls) == 1 assert len(aioclient_mock.mock_calls) == 1
def test_service_say_http_error(self, aioclient_mock):
async def test_service_say_http_error(hass, aioclient_mock):
"""Test service call say.""" """Test service call say."""
calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
url_param = { url_param = {
"text": "HomeAssistant", "text": "HomeAssistant",
@ -210,7 +205,7 @@ class TestTTSYandexPlatform:
"speed": 1, "speed": 1,
} }
aioclient_mock.get( aioclient_mock.get(
self._base_url, URL,
status=HTTPStatus.FORBIDDEN, status=HTTPStatus.FORBIDDEN,
content=b"test", content=b"test",
params=url_param, params=url_param,
@ -219,20 +214,22 @@ class TestTTSYandexPlatform:
config = {tts.DOMAIN: {"platform": "yandextts", "api_key": "1234567xx"}} config = {tts.DOMAIN: {"platform": "yandextts", "api_key": "1234567xx"}}
with assert_setup_component(1, tts.DOMAIN): with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config) await async_setup_component(hass, tts.DOMAIN, config)
await hass.async_block_till_done()
self.hass.services.call( await hass.services.async_call(
tts.DOMAIN, tts.DOMAIN,
"yandextts_say", "yandextts_say",
{"entity_id": "media_player.something", tts.ATTR_MESSAGE: "HomeAssistant"}, {"entity_id": "media_player.something", tts.ATTR_MESSAGE: "HomeAssistant"},
) )
self.hass.block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(calls) == 0
def test_service_say_specified_speaker(self, aioclient_mock):
async def test_service_say_specified_speaker(hass, aioclient_mock):
"""Test service call say.""" """Test service call say."""
calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
url_param = { url_param = {
"text": "HomeAssistant", "text": "HomeAssistant",
@ -243,9 +240,7 @@ class TestTTSYandexPlatform:
"emotion": "neutral", "emotion": "neutral",
"speed": 1, "speed": 1,
} }
aioclient_mock.get( aioclient_mock.get(URL, status=HTTPStatus.OK, content=b"test", params=url_param)
self._base_url, status=HTTPStatus.OK, content=b"test", params=url_param
)
config = { config = {
tts.DOMAIN: { tts.DOMAIN: {
@ -256,21 +251,23 @@ class TestTTSYandexPlatform:
} }
with assert_setup_component(1, tts.DOMAIN): with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config) await async_setup_component(hass, tts.DOMAIN, config)
await hass.async_block_till_done()
self.hass.services.call( await hass.services.async_call(
tts.DOMAIN, tts.DOMAIN,
"yandextts_say", "yandextts_say",
{"entity_id": "media_player.something", tts.ATTR_MESSAGE: "HomeAssistant"}, {"entity_id": "media_player.something", tts.ATTR_MESSAGE: "HomeAssistant"},
) )
self.hass.block_till_done() await hass.async_block_till_done()
assert len(aioclient_mock.mock_calls) == 1 assert len(aioclient_mock.mock_calls) == 1
assert len(calls) == 1 assert len(calls) == 1
def test_service_say_specified_emotion(self, aioclient_mock):
async def test_service_say_specified_emotion(hass, aioclient_mock):
"""Test service call say.""" """Test service call say."""
calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
url_param = { url_param = {
"text": "HomeAssistant", "text": "HomeAssistant",
@ -281,9 +278,7 @@ class TestTTSYandexPlatform:
"emotion": "evil", "emotion": "evil",
"speed": 1, "speed": 1,
} }
aioclient_mock.get( aioclient_mock.get(URL, status=HTTPStatus.OK, content=b"test", params=url_param)
self._base_url, status=HTTPStatus.OK, content=b"test", params=url_param
)
config = { config = {
tts.DOMAIN: { tts.DOMAIN: {
@ -294,21 +289,23 @@ class TestTTSYandexPlatform:
} }
with assert_setup_component(1, tts.DOMAIN): with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config) await async_setup_component(hass, tts.DOMAIN, config)
await hass.async_block_till_done()
self.hass.services.call( await hass.services.async_call(
tts.DOMAIN, tts.DOMAIN,
"yandextts_say", "yandextts_say",
{"entity_id": "media_player.something", tts.ATTR_MESSAGE: "HomeAssistant"}, {"entity_id": "media_player.something", tts.ATTR_MESSAGE: "HomeAssistant"},
) )
self.hass.block_till_done() await hass.async_block_till_done()
assert len(aioclient_mock.mock_calls) == 1 assert len(aioclient_mock.mock_calls) == 1
assert len(calls) == 1 assert len(calls) == 1
def test_service_say_specified_low_speed(self, aioclient_mock):
async def test_service_say_specified_low_speed(hass, aioclient_mock):
"""Test service call say.""" """Test service call say."""
calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
url_param = { url_param = {
"text": "HomeAssistant", "text": "HomeAssistant",
@ -319,30 +316,30 @@ class TestTTSYandexPlatform:
"emotion": "neutral", "emotion": "neutral",
"speed": "0.1", "speed": "0.1",
} }
aioclient_mock.get( aioclient_mock.get(URL, status=HTTPStatus.OK, content=b"test", params=url_param)
self._base_url, status=HTTPStatus.OK, content=b"test", params=url_param
)
config = { config = {
tts.DOMAIN: {"platform": "yandextts", "api_key": "1234567xx", "speed": 0.1} tts.DOMAIN: {"platform": "yandextts", "api_key": "1234567xx", "speed": 0.1}
} }
with assert_setup_component(1, tts.DOMAIN): with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config) await async_setup_component(hass, tts.DOMAIN, config)
await hass.async_block_till_done()
self.hass.services.call( await hass.services.async_call(
tts.DOMAIN, tts.DOMAIN,
"yandextts_say", "yandextts_say",
{"entity_id": "media_player.something", tts.ATTR_MESSAGE: "HomeAssistant"}, {"entity_id": "media_player.something", tts.ATTR_MESSAGE: "HomeAssistant"},
) )
self.hass.block_till_done() await hass.async_block_till_done()
assert len(aioclient_mock.mock_calls) == 1 assert len(aioclient_mock.mock_calls) == 1
assert len(calls) == 1 assert len(calls) == 1
def test_service_say_specified_speed(self, aioclient_mock):
async def test_service_say_specified_speed(hass, aioclient_mock):
"""Test service call say.""" """Test service call say."""
calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
url_param = { url_param = {
"text": "HomeAssistant", "text": "HomeAssistant",
@ -353,30 +350,28 @@ class TestTTSYandexPlatform:
"emotion": "neutral", "emotion": "neutral",
"speed": 2, "speed": 2,
} }
aioclient_mock.get( aioclient_mock.get(URL, status=HTTPStatus.OK, content=b"test", params=url_param)
self._base_url, status=HTTPStatus.OK, content=b"test", params=url_param
)
config = { config = {tts.DOMAIN: {"platform": "yandextts", "api_key": "1234567xx", "speed": 2}}
tts.DOMAIN: {"platform": "yandextts", "api_key": "1234567xx", "speed": 2}
}
with assert_setup_component(1, tts.DOMAIN): with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config) await async_setup_component(hass, tts.DOMAIN, config)
await hass.async_block_till_done()
self.hass.services.call( await hass.services.async_call(
tts.DOMAIN, tts.DOMAIN,
"yandextts_say", "yandextts_say",
{"entity_id": "media_player.something", tts.ATTR_MESSAGE: "HomeAssistant"}, {"entity_id": "media_player.something", tts.ATTR_MESSAGE: "HomeAssistant"},
) )
self.hass.block_till_done() await hass.async_block_till_done()
assert len(aioclient_mock.mock_calls) == 1 assert len(aioclient_mock.mock_calls) == 1
assert len(calls) == 1 assert len(calls) == 1
def test_service_say_specified_options(self, aioclient_mock):
async def test_service_say_specified_options(hass, aioclient_mock):
"""Test service call say with options.""" """Test service call say with options."""
calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
url_param = { url_param = {
"text": "HomeAssistant", "text": "HomeAssistant",
@ -387,15 +382,14 @@ class TestTTSYandexPlatform:
"emotion": "evil", "emotion": "evil",
"speed": 2, "speed": 2,
} }
aioclient_mock.get( aioclient_mock.get(URL, status=HTTPStatus.OK, content=b"test", params=url_param)
self._base_url, status=HTTPStatus.OK, content=b"test", params=url_param
)
config = {tts.DOMAIN: {"platform": "yandextts", "api_key": "1234567xx"}} config = {tts.DOMAIN: {"platform": "yandextts", "api_key": "1234567xx"}}
with assert_setup_component(1, tts.DOMAIN): with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config) await async_setup_component(hass, tts.DOMAIN, config)
await hass.async_block_till_done()
self.hass.services.call( await hass.services.async_call(
tts.DOMAIN, tts.DOMAIN,
"yandextts_say", "yandextts_say",
{ {
@ -404,7 +398,7 @@ class TestTTSYandexPlatform:
"options": {"emotion": "evil", "speed": 2}, "options": {"emotion": "evil", "speed": 2},
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
assert len(aioclient_mock.mock_calls) == 1 assert len(aioclient_mock.mock_calls) == 1
assert len(calls) == 1 assert len(calls) == 1