diff --git a/tests/components/marytts/test_tts.py b/tests/components/marytts/test_tts.py index 6a09ff405c8..29b2bc29f4a 100644 --- a/tests/components/marytts/test_tts.py +++ b/tests/components/marytts/test_tts.py @@ -1,143 +1,124 @@ """The tests for the MaryTTS speech platform.""" -import asyncio import os import shutil from unittest.mock import patch +import pytest + from homeassistant.components.media_player.const import ( ATTR_MEDIA_CONTENT_ID, DOMAIN as DOMAIN_MP, SERVICE_PLAY_MEDIA, ) import homeassistant.components.tts as tts -from homeassistant.config import async_process_ha_core_config -from homeassistant.setup import setup_component +from homeassistant.setup import async_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: - """Test the speech component.""" +@pytest.fixture(autouse=True) +def cleanup_cache(hass): + """Prevent TTS writing.""" + yield + default_tts = hass.config.path(tts.DEFAULT_CACHE_DIR) + if os.path.isdir(default_tts): + shutil.rmtree(default_tts) - 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, +async def test_setup_component(hass): + """Test setup component.""" + config = {tts.DOMAIN: {"platform": "marytts"}} + + with assert_setup_component(1, tts.DOMAIN): + await async_setup_component(hass, tts.DOMAIN, config) + await hass.async_block_till_done() + + +async def test_service_say(hass): + """Test service call say.""" + calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) + + config = {tts.DOMAIN: {"platform": "marytts"}} + + with assert_setup_component(1, tts.DOMAIN): + await async_setup_component(hass, tts.DOMAIN, config) + await hass.async_block_till_done() + + with patch( + "homeassistant.components.marytts.tts.MaryTTS.speak", + return_value=b"audio", + ) as mock_speak: + await hass.services.async_call( + tts.DOMAIN, + "marytts_say", + { + "entity_id": "media_player.something", + tts.ATTR_MESSAGE: "HomeAssistant", + }, + blocking=True, ) - 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", - } + mock_speak.assert_called_once() + mock_speak.assert_called_with("HomeAssistant", {}) - 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): - shutil.rmtree(default_tts) + assert len(calls) == 1 + assert calls[0].data[ATTR_MEDIA_CONTENT_ID].find(".wav") != -1 - self.hass.stop() - def test_setup_component(self): - """Test setup component.""" - config = {tts.DOMAIN: {"platform": "marytts"}} +async def test_service_say_with_effect(hass): + """Test service call say with effects.""" + calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) - with assert_setup_component(1, tts.DOMAIN): - setup_component(self.hass, tts.DOMAIN, config) + config = {tts.DOMAIN: {"platform": "marytts", "effect": {"Volume": "amount:2.0;"}}} - def test_service_say(self): - """Test service call say.""" - calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) + with assert_setup_component(1, tts.DOMAIN): + await async_setup_component(hass, tts.DOMAIN, config) + await hass.async_block_till_done() - config = {tts.DOMAIN: {"platform": "marytts"}} + with patch( + "homeassistant.components.marytts.tts.MaryTTS.speak", + return_value=b"audio", + ) as mock_speak: + await hass.services.async_call( + tts.DOMAIN, + "marytts_say", + { + "entity_id": "media_player.something", + tts.ATTR_MESSAGE: "HomeAssistant", + }, + blocking=True, + ) - with assert_setup_component(1, tts.DOMAIN): - setup_component(self.hass, tts.DOMAIN, config) + mock_speak.assert_called_once() + mock_speak.assert_called_with("HomeAssistant", {"Volume": "amount:2.0;"}) - with patch( - "homeassistant.components.marytts.tts.MaryTTS.speak", - return_value=b"audio", - ) as mock_speak: - self.hass.services.call( - tts.DOMAIN, - "marytts_say", - { - "entity_id": "media_player.something", - tts.ATTR_MESSAGE: "HomeAssistant", - }, - ) - self.hass.block_till_done() + assert len(calls) == 1 + assert calls[0].data[ATTR_MEDIA_CONTENT_ID].find(".wav") != -1 - mock_speak.assert_called_once() - mock_speak.assert_called_with("HomeAssistant", {}) - assert len(calls) == 1 - assert calls[0].data[ATTR_MEDIA_CONTENT_ID].find(".wav") != -1 +async def test_service_say_http_error(hass): + """Test service call say.""" + calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) - def test_service_say_with_effect(self): - """Test service call say with effects.""" - calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) + config = {tts.DOMAIN: {"platform": "marytts"}} - config = { - tts.DOMAIN: {"platform": "marytts", "effect": {"Volume": "amount:2.0;"}} - } + with assert_setup_component(1, tts.DOMAIN): + await async_setup_component(hass, tts.DOMAIN, config) + await hass.async_block_till_done() - with assert_setup_component(1, tts.DOMAIN): - setup_component(self.hass, tts.DOMAIN, config) + with patch( + "homeassistant.components.marytts.tts.MaryTTS.speak", + side_effect=Exception(), + ) as mock_speak: + await hass.services.async_call( + tts.DOMAIN, + "marytts_say", + { + "entity_id": "media_player.something", + tts.ATTR_MESSAGE: "HomeAssistant", + }, + ) + await hass.async_block_till_done() - with patch( - "homeassistant.components.marytts.tts.MaryTTS.speak", - return_value=b"audio", - ) as mock_speak: - self.hass.services.call( - tts.DOMAIN, - "marytts_say", - { - "entity_id": "media_player.something", - tts.ATTR_MESSAGE: "HomeAssistant", - }, - ) - self.hass.block_till_done() - - mock_speak.assert_called_once() - mock_speak.assert_called_with("HomeAssistant", {"Volume": "amount:2.0;"}) - - assert len(calls) == 1 - assert calls[0].data[ATTR_MEDIA_CONTENT_ID].find(".wav") != -1 - - def test_service_say_http_error(self): - """Test service call say.""" - calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) - - config = {tts.DOMAIN: {"platform": "marytts"}} - - with assert_setup_component(1, tts.DOMAIN): - setup_component(self.hass, tts.DOMAIN, config) - - with patch( - "homeassistant.components.marytts.tts.MaryTTS.speak", - side_effect=Exception(), - ) as mock_speak: - self.hass.services.call( - tts.DOMAIN, - "marytts_say", - { - "entity_id": "media_player.something", - tts.ATTR_MESSAGE: "HomeAssistant", - }, - ) - self.hass.block_till_done() - - mock_speak.assert_called_once() - assert len(calls) == 0 + mock_speak.assert_called_once() + assert len(calls) == 0 diff --git a/tests/components/voicerss/test_tts.py b/tests/components/voicerss/test_tts.py index 222ed90f67a..424bbe5e065 100644 --- a/tests/components/voicerss/test_tts.py +++ b/tests/components/voicerss/test_tts.py @@ -4,231 +4,228 @@ from http import HTTPStatus import os import shutil +import pytest + from homeassistant.components.media_player.const import ( ATTR_MEDIA_CONTENT_ID, DOMAIN as DOMAIN_MP, SERVICE_PLAY_MEDIA, ) import homeassistant.components.tts as tts -from homeassistant.config import async_process_ha_core_config -from homeassistant.setup import setup_component +from homeassistant.setup import async_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 +URL = "https://api.voicerss.org/" +FORM_DATA = { + "key": "1234567xx", + "hl": "en-us", + "c": "MP3", + "f": "8khz_8bit_mono", + "src": "I person is on front of your door.", +} -class TestTTSVoiceRSSPlatform: - """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() +@pytest.fixture(autouse=True) +def cleanup_cache(hass): + """Prevent TTS writing.""" + yield + default_tts = hass.config.path(tts.DEFAULT_CACHE_DIR) + if os.path.isdir(default_tts): + shutil.rmtree(default_tts) - 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", - "hl": "en-us", - "c": "MP3", - "f": "8khz_8bit_mono", - "src": "I person is on front of your door.", +async def test_setup_component(hass): + """Test setup component.""" + config = {tts.DOMAIN: {"platform": "voicerss", "api_key": "1234567xx"}} + + with assert_setup_component(1, tts.DOMAIN): + await async_setup_component(hass, tts.DOMAIN, config) + await hass.async_block_till_done() + + +async def test_setup_component_without_api_key(hass): + """Test setup component without api key.""" + config = {tts.DOMAIN: {"platform": "voicerss"}} + + with assert_setup_component(0, tts.DOMAIN): + await async_setup_component(hass, tts.DOMAIN, config) + await hass.async_block_till_done() + + +async def test_service_say(hass, aioclient_mock): + """Test service call say.""" + calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) + + aioclient_mock.post(URL, data=FORM_DATA, status=HTTPStatus.OK, content=b"test") + + config = {tts.DOMAIN: {"platform": "voicerss", "api_key": "1234567xx"}} + + with assert_setup_component(1, tts.DOMAIN): + await async_setup_component(hass, tts.DOMAIN, config) + await hass.async_block_till_done() + + await hass.services.async_call( + tts.DOMAIN, + "voicerss_say", + { + "entity_id": "media_player.something", + tts.ATTR_MESSAGE: "I person is on front of your door.", + }, + ) + await hass.async_block_till_done() + + assert len(calls) == 1 + assert len(aioclient_mock.mock_calls) == 1 + assert aioclient_mock.mock_calls[0][2] == FORM_DATA + assert calls[0].data[ATTR_MEDIA_CONTENT_ID].find(".mp3") != -1 + + +async def test_service_say_german_config(hass, aioclient_mock): + """Test service call say with german code in the config.""" + 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 = { + tts.DOMAIN: { + "platform": "voicerss", + "api_key": "1234567xx", + "language": "de-de", } + } - 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): - shutil.rmtree(default_tts) + with assert_setup_component(1, tts.DOMAIN): + await async_setup_component(hass, tts.DOMAIN, config) + await hass.async_block_till_done() - self.hass.stop() + await hass.services.async_call( + tts.DOMAIN, + "voicerss_say", + { + "entity_id": "media_player.something", + tts.ATTR_MESSAGE: "I person is on front of your door.", + }, + ) + await hass.async_block_till_done() - def test_setup_component(self): - """Test setup component.""" - config = {tts.DOMAIN: {"platform": "voicerss", "api_key": "1234567xx"}} + assert len(calls) == 1 + assert len(aioclient_mock.mock_calls) == 1 + assert aioclient_mock.mock_calls[0][2] == form_data - with assert_setup_component(1, tts.DOMAIN): - setup_component(self.hass, tts.DOMAIN, config) - def test_setup_component_without_api_key(self): - """Test setup component without api key.""" - config = {tts.DOMAIN: {"platform": "voicerss"}} +async def test_service_say_german_service(hass, aioclient_mock): + """Test service call say with german code in the service.""" + calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) - with assert_setup_component(0, tts.DOMAIN): - setup_component(self.hass, tts.DOMAIN, config) + form_data = {**FORM_DATA, "hl": "de-de"} + aioclient_mock.post(URL, data=form_data, status=HTTPStatus.OK, content=b"test") - def test_service_say(self, aioclient_mock): - """Test service call say.""" - calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) + config = {tts.DOMAIN: {"platform": "voicerss", "api_key": "1234567xx"}} - aioclient_mock.post( - self.url, data=self.form_data, status=HTTPStatus.OK, content=b"test" - ) + with assert_setup_component(1, tts.DOMAIN): + await async_setup_component(hass, tts.DOMAIN, config) + await hass.async_block_till_done() - config = {tts.DOMAIN: {"platform": "voicerss", "api_key": "1234567xx"}} + await hass.services.async_call( + tts.DOMAIN, + "voicerss_say", + { + "entity_id": "media_player.something", + tts.ATTR_MESSAGE: "I person is on front of your door.", + tts.ATTR_LANGUAGE: "de-de", + }, + ) + await hass.async_block_till_done() - with assert_setup_component(1, tts.DOMAIN): - setup_component(self.hass, tts.DOMAIN, config) + assert len(calls) == 1 + assert len(aioclient_mock.mock_calls) == 1 + assert aioclient_mock.mock_calls[0][2] == form_data - self.hass.services.call( - tts.DOMAIN, - "voicerss_say", - { - "entity_id": "media_player.something", - tts.ATTR_MESSAGE: "I person is on front of your door.", - }, - ) - self.hass.block_till_done() - assert len(calls) == 1 - assert len(aioclient_mock.mock_calls) == 1 - assert aioclient_mock.mock_calls[0][2] == self.form_data - assert calls[0].data[ATTR_MEDIA_CONTENT_ID].find(".mp3") != -1 +async def test_service_say_error(hass, aioclient_mock): + """Test service call say with http response 400.""" + calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) - 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) + aioclient_mock.post(URL, data=FORM_DATA, status=400, content=b"test") - self.form_data["hl"] = "de-de" - aioclient_mock.post( - 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", - "language": "de-de", - } - } + with assert_setup_component(1, tts.DOMAIN): + await async_setup_component(hass, tts.DOMAIN, config) + await hass.async_block_till_done() - with assert_setup_component(1, tts.DOMAIN): - setup_component(self.hass, tts.DOMAIN, config) + await hass.services.async_call( + tts.DOMAIN, + "voicerss_say", + { + "entity_id": "media_player.something", + tts.ATTR_MESSAGE: "I person is on front of your door.", + }, + ) + await hass.async_block_till_done() - self.hass.services.call( - tts.DOMAIN, - "voicerss_say", - { - "entity_id": "media_player.something", - tts.ATTR_MESSAGE: "I person is on front of your door.", - }, - ) - self.hass.block_till_done() + assert len(calls) == 0 + assert len(aioclient_mock.mock_calls) == 1 + assert aioclient_mock.mock_calls[0][2] == FORM_DATA - assert len(calls) == 1 - assert len(aioclient_mock.mock_calls) == 1 - assert aioclient_mock.mock_calls[0][2] == self.form_data - def test_service_say_german_service(self, aioclient_mock): - """Test service call say with german code in the service.""" - calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) +async def test_service_say_timeout(hass, aioclient_mock): + """Test service call say with http timeout.""" + calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) - self.form_data["hl"] = "de-de" - aioclient_mock.post( - self.url, data=self.form_data, status=HTTPStatus.OK, content=b"test" - ) + 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): - setup_component(self.hass, tts.DOMAIN, config) + with assert_setup_component(1, tts.DOMAIN): + await async_setup_component(hass, tts.DOMAIN, config) + await hass.async_block_till_done() - self.hass.services.call( - tts.DOMAIN, - "voicerss_say", - { - "entity_id": "media_player.something", - tts.ATTR_MESSAGE: "I person is on front of your door.", - tts.ATTR_LANGUAGE: "de-de", - }, - ) - self.hass.block_till_done() + await hass.services.async_call( + tts.DOMAIN, + "voicerss_say", + { + "entity_id": "media_player.something", + tts.ATTR_MESSAGE: "I person is on front of your door.", + }, + ) + await hass.async_block_till_done() - assert len(calls) == 1 - assert len(aioclient_mock.mock_calls) == 1 - assert aioclient_mock.mock_calls[0][2] == self.form_data + assert len(calls) == 0 + assert len(aioclient_mock.mock_calls) == 1 + assert aioclient_mock.mock_calls[0][2] == FORM_DATA - def test_service_say_error(self, aioclient_mock): - """Test service call say with http response 400.""" - calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) - aioclient_mock.post(self.url, data=self.form_data, status=400, content=b"test") +async def test_service_say_error_msg(hass, aioclient_mock): + """Test service call say with http error api message.""" + calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) - config = {tts.DOMAIN: {"platform": "voicerss", "api_key": "1234567xx"}} + aioclient_mock.post( + URL, + data=FORM_DATA, + status=HTTPStatus.OK, + content=b"The subscription does not support SSML!", + ) - with assert_setup_component(1, tts.DOMAIN): - setup_component(self.hass, tts.DOMAIN, config) + config = {tts.DOMAIN: {"platform": "voicerss", "api_key": "1234567xx"}} - self.hass.services.call( - tts.DOMAIN, - "voicerss_say", - { - "entity_id": "media_player.something", - tts.ATTR_MESSAGE: "I person is on front of your door.", - }, - ) - self.hass.block_till_done() + with assert_setup_component(1, tts.DOMAIN): + await async_setup_component(hass, tts.DOMAIN, config) + await hass.async_block_till_done() - assert len(calls) == 0 - assert len(aioclient_mock.mock_calls) == 1 - assert aioclient_mock.mock_calls[0][2] == self.form_data + await hass.services.async_call( + tts.DOMAIN, + "voicerss_say", + { + "entity_id": "media_player.something", + tts.ATTR_MESSAGE: "I person is on front of your door.", + }, + ) + await hass.async_block_till_done() - def test_service_say_timeout(self, aioclient_mock): - """Test service call say with http timeout.""" - calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) - - aioclient_mock.post(self.url, data=self.form_data, exc=asyncio.TimeoutError()) - - config = {tts.DOMAIN: {"platform": "voicerss", "api_key": "1234567xx"}} - - with assert_setup_component(1, tts.DOMAIN): - setup_component(self.hass, tts.DOMAIN, config) - - self.hass.services.call( - tts.DOMAIN, - "voicerss_say", - { - "entity_id": "media_player.something", - tts.ATTR_MESSAGE: "I person is on front of your door.", - }, - ) - self.hass.block_till_done() - - assert len(calls) == 0 - assert len(aioclient_mock.mock_calls) == 1 - assert aioclient_mock.mock_calls[0][2] == self.form_data - - def test_service_say_error_msg(self, aioclient_mock): - """Test service call say with http error api message.""" - calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) - - aioclient_mock.post( - self.url, - data=self.form_data, - status=HTTPStatus.OK, - content=b"The subscription does not support SSML!", - ) - - config = {tts.DOMAIN: {"platform": "voicerss", "api_key": "1234567xx"}} - - with assert_setup_component(1, tts.DOMAIN): - setup_component(self.hass, tts.DOMAIN, config) - - self.hass.services.call( - tts.DOMAIN, - "voicerss_say", - { - "entity_id": "media_player.something", - tts.ATTR_MESSAGE: "I person is on front of your door.", - }, - ) - self.hass.block_till_done() - - assert len(calls) == 0 - assert len(aioclient_mock.mock_calls) == 1 - assert aioclient_mock.mock_calls[0][2] == self.form_data + assert len(calls) == 0 + assert len(aioclient_mock.mock_calls) == 1 + assert aioclient_mock.mock_calls[0][2] == FORM_DATA diff --git a/tests/components/yandextts/test_tts.py b/tests/components/yandextts/test_tts.py index beab0b396fd..db4dbce4b8b 100644 --- a/tests/components/yandextts/test_tts.py +++ b/tests/components/yandextts/test_tts.py @@ -4,407 +4,401 @@ from http import HTTPStatus import os import shutil +import pytest + from homeassistant.components.media_player.const import ( DOMAIN as DOMAIN_MP, SERVICE_PLAY_MEDIA, ) import homeassistant.components.tts as tts -from homeassistant.config import async_process_ha_core_config -from homeassistant.setup import setup_component +from homeassistant.setup import async_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 mutagen_mock, ) +URL = "https://tts.voicetech.yandex.net/generate?" -class TestTTSYandexPlatform: - """Test the speech component.""" - def setup_method(self): - """Set up things to be run when tests are started.""" - self.hass = get_test_home_assistant() - self._base_url = "https://tts.voicetech.yandex.net/generate?" +@pytest.fixture(autouse=True) +def cleanup_cache(hass): + """Prevent TTS writing.""" + yield + default_tts = hass.config.path(tts.DEFAULT_CACHE_DIR) + if os.path.isdir(default_tts): + shutil.rmtree(default_tts) - 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): - shutil.rmtree(default_tts) +async def test_setup_component(hass): + """Test setup component.""" + config = {tts.DOMAIN: {"platform": "yandextts", "api_key": "1234567xx"}} - self.hass.stop() + with assert_setup_component(1, tts.DOMAIN): + await async_setup_component(hass, tts.DOMAIN, config) + await hass.async_block_till_done() - def test_setup_component(self): - """Test setup component.""" - config = {tts.DOMAIN: {"platform": "yandextts", "api_key": "1234567xx"}} - with assert_setup_component(1, tts.DOMAIN): - setup_component(self.hass, tts.DOMAIN, config) +async def test_setup_component_without_api_key(hass): + """Test setup component without api key.""" + config = {tts.DOMAIN: {"platform": "yandextts"}} - def test_setup_component_without_api_key(self): - """Test setup component without api key.""" - config = {tts.DOMAIN: {"platform": "yandextts"}} + with assert_setup_component(0, tts.DOMAIN): + await async_setup_component(hass, tts.DOMAIN, config) + await hass.async_block_till_done() - with assert_setup_component(0, tts.DOMAIN): - setup_component(self.hass, tts.DOMAIN, config) - def test_service_say(self, aioclient_mock): - """Test service call say.""" - calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) +async def test_service_say(hass, aioclient_mock): + """Test service call say.""" + calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) - url_param = { - "text": "HomeAssistant", - "lang": "en-US", - "key": "1234567xx", - "speaker": "zahar", - "format": "mp3", - "emotion": "neutral", - "speed": 1, + url_param = { + "text": "HomeAssistant", + "lang": "en-US", + "key": "1234567xx", + "speaker": "zahar", + "format": "mp3", + "emotion": "neutral", + "speed": 1, + } + aioclient_mock.get(URL, status=HTTPStatus.OK, content=b"test", params=url_param) + + config = {tts.DOMAIN: {"platform": "yandextts", "api_key": "1234567xx"}} + + with assert_setup_component(1, tts.DOMAIN): + await async_setup_component(hass, tts.DOMAIN, config) + await hass.async_block_till_done() + + await hass.services.async_call( + tts.DOMAIN, + "yandextts_say", + {"entity_id": "media_player.something", tts.ATTR_MESSAGE: "HomeAssistant"}, + ) + await hass.async_block_till_done() + + assert len(aioclient_mock.mock_calls) == 1 + assert len(calls) == 1 + + +async def test_service_say_russian_config(hass, aioclient_mock): + """Test service call say.""" + calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) + + url_param = { + "text": "HomeAssistant", + "lang": "ru-RU", + "key": "1234567xx", + "speaker": "zahar", + "format": "mp3", + "emotion": "neutral", + "speed": 1, + } + aioclient_mock.get(URL, status=HTTPStatus.OK, content=b"test", params=url_param) + + config = { + tts.DOMAIN: { + "platform": "yandextts", + "api_key": "1234567xx", + "language": "ru-RU", } - aioclient_mock.get( - self._base_url, status=HTTPStatus.OK, content=b"test", params=url_param - ) + } - config = {tts.DOMAIN: {"platform": "yandextts", "api_key": "1234567xx"}} + with assert_setup_component(1, tts.DOMAIN): + await async_setup_component(hass, tts.DOMAIN, config) + await hass.async_block_till_done() - with assert_setup_component(1, tts.DOMAIN): - setup_component(self.hass, tts.DOMAIN, config) + await hass.services.async_call( + tts.DOMAIN, + "yandextts_say", + {"entity_id": "media_player.something", tts.ATTR_MESSAGE: "HomeAssistant"}, + ) + await hass.async_block_till_done() - self.hass.services.call( - tts.DOMAIN, - "yandextts_say", - {"entity_id": "media_player.something", tts.ATTR_MESSAGE: "HomeAssistant"}, - ) - self.hass.block_till_done() + assert len(aioclient_mock.mock_calls) == 1 + assert len(calls) == 1 - assert len(aioclient_mock.mock_calls) == 1 - assert len(calls) == 1 - def test_service_say_russian_config(self, aioclient_mock): - """Test service call say.""" - calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) +async def test_service_say_russian_service(hass, aioclient_mock): + """Test service call say.""" + calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) - url_param = { - "text": "HomeAssistant", - "lang": "ru-RU", - "key": "1234567xx", - "speaker": "zahar", - "format": "mp3", - "emotion": "neutral", - "speed": 1, + url_param = { + "text": "HomeAssistant", + "lang": "ru-RU", + "key": "1234567xx", + "speaker": "zahar", + "format": "mp3", + "emotion": "neutral", + "speed": 1, + } + aioclient_mock.get(URL, status=HTTPStatus.OK, content=b"test", params=url_param) + + config = {tts.DOMAIN: {"platform": "yandextts", "api_key": "1234567xx"}} + + with assert_setup_component(1, tts.DOMAIN): + await async_setup_component(hass, tts.DOMAIN, config) + await hass.async_block_till_done() + + await hass.services.async_call( + tts.DOMAIN, + "yandextts_say", + { + "entity_id": "media_player.something", + tts.ATTR_MESSAGE: "HomeAssistant", + tts.ATTR_LANGUAGE: "ru-RU", + }, + ) + await hass.async_block_till_done() + + assert len(aioclient_mock.mock_calls) == 1 + assert len(calls) == 1 + + +async def test_service_say_timeout(hass, aioclient_mock): + """Test service call say.""" + calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) + + url_param = { + "text": "HomeAssistant", + "lang": "en-US", + "key": "1234567xx", + "speaker": "zahar", + "format": "mp3", + "emotion": "neutral", + "speed": 1, + } + aioclient_mock.get( + URL, + status=HTTPStatus.OK, + exc=asyncio.TimeoutError(), + params=url_param, + ) + + config = {tts.DOMAIN: {"platform": "yandextts", "api_key": "1234567xx"}} + + with assert_setup_component(1, tts.DOMAIN): + await async_setup_component(hass, tts.DOMAIN, config) + await hass.async_block_till_done() + + await hass.services.async_call( + tts.DOMAIN, + "yandextts_say", + {"entity_id": "media_player.something", tts.ATTR_MESSAGE: "HomeAssistant"}, + ) + await hass.async_block_till_done() + + assert len(calls) == 0 + assert len(aioclient_mock.mock_calls) == 1 + + +async def test_service_say_http_error(hass, aioclient_mock): + """Test service call say.""" + calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) + + url_param = { + "text": "HomeAssistant", + "lang": "en-US", + "key": "1234567xx", + "speaker": "zahar", + "format": "mp3", + "emotion": "neutral", + "speed": 1, + } + aioclient_mock.get( + URL, + status=HTTPStatus.FORBIDDEN, + content=b"test", + params=url_param, + ) + + config = {tts.DOMAIN: {"platform": "yandextts", "api_key": "1234567xx"}} + + with assert_setup_component(1, tts.DOMAIN): + await async_setup_component(hass, tts.DOMAIN, config) + await hass.async_block_till_done() + + await hass.services.async_call( + tts.DOMAIN, + "yandextts_say", + {"entity_id": "media_player.something", tts.ATTR_MESSAGE: "HomeAssistant"}, + ) + await hass.async_block_till_done() + + assert len(calls) == 0 + + +async def test_service_say_specified_speaker(hass, aioclient_mock): + """Test service call say.""" + calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) + + url_param = { + "text": "HomeAssistant", + "lang": "en-US", + "key": "1234567xx", + "speaker": "alyss", + "format": "mp3", + "emotion": "neutral", + "speed": 1, + } + aioclient_mock.get(URL, status=HTTPStatus.OK, content=b"test", params=url_param) + + config = { + tts.DOMAIN: { + "platform": "yandextts", + "api_key": "1234567xx", + "voice": "alyss", } - aioclient_mock.get( - self._base_url, status=HTTPStatus.OK, content=b"test", params=url_param - ) + } - config = { - tts.DOMAIN: { - "platform": "yandextts", - "api_key": "1234567xx", - "language": "ru-RU", - } - } + with assert_setup_component(1, tts.DOMAIN): + await async_setup_component(hass, tts.DOMAIN, config) + await hass.async_block_till_done() - with assert_setup_component(1, tts.DOMAIN): - setup_component(self.hass, tts.DOMAIN, config) + await hass.services.async_call( + tts.DOMAIN, + "yandextts_say", + {"entity_id": "media_player.something", tts.ATTR_MESSAGE: "HomeAssistant"}, + ) + await hass.async_block_till_done() - self.hass.services.call( - tts.DOMAIN, - "yandextts_say", - {"entity_id": "media_player.something", tts.ATTR_MESSAGE: "HomeAssistant"}, - ) - self.hass.block_till_done() + assert len(aioclient_mock.mock_calls) == 1 + assert len(calls) == 1 - assert len(aioclient_mock.mock_calls) == 1 - assert len(calls) == 1 - def test_service_say_russian_service(self, aioclient_mock): - """Test service call say.""" - calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) +async def test_service_say_specified_emotion(hass, aioclient_mock): + """Test service call say.""" + calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) - url_param = { - "text": "HomeAssistant", - "lang": "ru-RU", - "key": "1234567xx", - "speaker": "zahar", - "format": "mp3", - "emotion": "neutral", - "speed": 1, - } - aioclient_mock.get( - self._base_url, status=HTTPStatus.OK, content=b"test", params=url_param - ) + url_param = { + "text": "HomeAssistant", + "lang": "en-US", + "key": "1234567xx", + "speaker": "zahar", + "format": "mp3", + "emotion": "evil", + "speed": 1, + } + aioclient_mock.get(URL, status=HTTPStatus.OK, 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", - { - "entity_id": "media_player.something", - tts.ATTR_MESSAGE: "HomeAssistant", - tts.ATTR_LANGUAGE: "ru-RU", - }, - ) - self.hass.block_till_done() - - assert len(aioclient_mock.mock_calls) == 1 - assert len(calls) == 1 - - def test_service_say_timeout(self, aioclient_mock): - """Test service call say.""" - calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) - - url_param = { - "text": "HomeAssistant", - "lang": "en-US", - "key": "1234567xx", - "speaker": "zahar", - "format": "mp3", - "emotion": "neutral", - "speed": 1, - } - aioclient_mock.get( - self._base_url, - status=HTTPStatus.OK, - exc=asyncio.TimeoutError(), - 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", - {"entity_id": "media_player.something", tts.ATTR_MESSAGE: "HomeAssistant"}, - ) - self.hass.block_till_done() - - assert len(calls) == 0 - assert len(aioclient_mock.mock_calls) == 1 - - def test_service_say_http_error(self, aioclient_mock): - """Test service call say.""" - calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) - - url_param = { - "text": "HomeAssistant", - "lang": "en-US", - "key": "1234567xx", - "speaker": "zahar", - "format": "mp3", - "emotion": "neutral", - "speed": 1, - } - aioclient_mock.get( - self._base_url, - status=HTTPStatus.FORBIDDEN, - 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", - {"entity_id": "media_player.something", tts.ATTR_MESSAGE: "HomeAssistant"}, - ) - self.hass.block_till_done() - - assert len(calls) == 0 - - def test_service_say_specified_speaker(self, aioclient_mock): - """Test service call say.""" - calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) - - url_param = { - "text": "HomeAssistant", - "lang": "en-US", - "key": "1234567xx", - "speaker": "alyss", - "format": "mp3", - "emotion": "neutral", - "speed": 1, - } - aioclient_mock.get( - self._base_url, status=HTTPStatus.OK, content=b"test", params=url_param - ) - - config = { - tts.DOMAIN: { - "platform": "yandextts", - "api_key": "1234567xx", - "voice": "alyss", - } - } - - with assert_setup_component(1, tts.DOMAIN): - setup_component(self.hass, tts.DOMAIN, config) - - self.hass.services.call( - tts.DOMAIN, - "yandextts_say", - {"entity_id": "media_player.something", tts.ATTR_MESSAGE: "HomeAssistant"}, - ) - self.hass.block_till_done() - - assert len(aioclient_mock.mock_calls) == 1 - assert len(calls) == 1 - - def test_service_say_specified_emotion(self, aioclient_mock): - """Test service call say.""" - calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) - - url_param = { - "text": "HomeAssistant", - "lang": "en-US", - "key": "1234567xx", - "speaker": "zahar", - "format": "mp3", + config = { + tts.DOMAIN: { + "platform": "yandextts", + "api_key": "1234567xx", "emotion": "evil", - "speed": 1, } - aioclient_mock.get( - self._base_url, status=HTTPStatus.OK, content=b"test", params=url_param - ) + } - config = { - tts.DOMAIN: { - "platform": "yandextts", - "api_key": "1234567xx", - "emotion": "evil", - } - } + with assert_setup_component(1, tts.DOMAIN): + await async_setup_component(hass, tts.DOMAIN, config) + await hass.async_block_till_done() - with assert_setup_component(1, tts.DOMAIN): - setup_component(self.hass, tts.DOMAIN, config) + await hass.services.async_call( + tts.DOMAIN, + "yandextts_say", + {"entity_id": "media_player.something", tts.ATTR_MESSAGE: "HomeAssistant"}, + ) + await hass.async_block_till_done() - self.hass.services.call( - tts.DOMAIN, - "yandextts_say", - {"entity_id": "media_player.something", tts.ATTR_MESSAGE: "HomeAssistant"}, - ) - self.hass.block_till_done() + assert len(aioclient_mock.mock_calls) == 1 + assert len(calls) == 1 - assert len(aioclient_mock.mock_calls) == 1 - assert len(calls) == 1 - def test_service_say_specified_low_speed(self, aioclient_mock): - """Test service call say.""" - calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) +async def test_service_say_specified_low_speed(hass, aioclient_mock): + """Test service call say.""" + calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) - url_param = { - "text": "HomeAssistant", - "lang": "en-US", - "key": "1234567xx", - "speaker": "zahar", - "format": "mp3", - "emotion": "neutral", - "speed": "0.1", - } - aioclient_mock.get( - self._base_url, status=HTTPStatus.OK, content=b"test", params=url_param - ) + url_param = { + "text": "HomeAssistant", + "lang": "en-US", + "key": "1234567xx", + "speaker": "zahar", + "format": "mp3", + "emotion": "neutral", + "speed": "0.1", + } + aioclient_mock.get(URL, status=HTTPStatus.OK, content=b"test", params=url_param) - config = { - tts.DOMAIN: {"platform": "yandextts", "api_key": "1234567xx", "speed": 0.1} - } + config = { + tts.DOMAIN: {"platform": "yandextts", "api_key": "1234567xx", "speed": 0.1} + } - with assert_setup_component(1, tts.DOMAIN): - setup_component(self.hass, tts.DOMAIN, config) + with assert_setup_component(1, tts.DOMAIN): + await async_setup_component(hass, tts.DOMAIN, config) + await hass.async_block_till_done() - self.hass.services.call( - tts.DOMAIN, - "yandextts_say", - {"entity_id": "media_player.something", tts.ATTR_MESSAGE: "HomeAssistant"}, - ) - self.hass.block_till_done() + await hass.services.async_call( + tts.DOMAIN, + "yandextts_say", + {"entity_id": "media_player.something", tts.ATTR_MESSAGE: "HomeAssistant"}, + ) + await hass.async_block_till_done() - assert len(aioclient_mock.mock_calls) == 1 - assert len(calls) == 1 + assert len(aioclient_mock.mock_calls) == 1 + assert len(calls) == 1 - def test_service_say_specified_speed(self, aioclient_mock): - """Test service call say.""" - calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) - url_param = { - "text": "HomeAssistant", - "lang": "en-US", - "key": "1234567xx", - "speaker": "zahar", - "format": "mp3", - "emotion": "neutral", - "speed": 2, - } - aioclient_mock.get( - self._base_url, status=HTTPStatus.OK, content=b"test", params=url_param - ) +async def test_service_say_specified_speed(hass, aioclient_mock): + """Test service call say.""" + calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) - config = { - tts.DOMAIN: {"platform": "yandextts", "api_key": "1234567xx", "speed": 2} - } + url_param = { + "text": "HomeAssistant", + "lang": "en-US", + "key": "1234567xx", + "speaker": "zahar", + "format": "mp3", + "emotion": "neutral", + "speed": 2, + } + aioclient_mock.get(URL, status=HTTPStatus.OK, content=b"test", params=url_param) - with assert_setup_component(1, tts.DOMAIN): - setup_component(self.hass, tts.DOMAIN, config) + config = {tts.DOMAIN: {"platform": "yandextts", "api_key": "1234567xx", "speed": 2}} - self.hass.services.call( - tts.DOMAIN, - "yandextts_say", - {"entity_id": "media_player.something", tts.ATTR_MESSAGE: "HomeAssistant"}, - ) - self.hass.block_till_done() + with assert_setup_component(1, tts.DOMAIN): + await async_setup_component(hass, tts.DOMAIN, config) + await hass.async_block_till_done() - assert len(aioclient_mock.mock_calls) == 1 - assert len(calls) == 1 + await hass.services.async_call( + tts.DOMAIN, + "yandextts_say", + {"entity_id": "media_player.something", tts.ATTR_MESSAGE: "HomeAssistant"}, + ) + await hass.async_block_till_done() - 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) + assert len(aioclient_mock.mock_calls) == 1 + assert len(calls) == 1 - url_param = { - "text": "HomeAssistant", - "lang": "en-US", - "key": "1234567xx", - "speaker": "zahar", - "format": "mp3", - "emotion": "evil", - "speed": 2, - } - aioclient_mock.get( - self._base_url, status=HTTPStatus.OK, 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) +async def test_service_say_specified_options(hass, aioclient_mock): + """Test service call say with options.""" + calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) - self.hass.services.call( - tts.DOMAIN, - "yandextts_say", - { - "entity_id": "media_player.something", - tts.ATTR_MESSAGE: "HomeAssistant", - "options": {"emotion": "evil", "speed": 2}, - }, - ) - self.hass.block_till_done() + url_param = { + "text": "HomeAssistant", + "lang": "en-US", + "key": "1234567xx", + "speaker": "zahar", + "format": "mp3", + "emotion": "evil", + "speed": 2, + } + aioclient_mock.get(URL, status=HTTPStatus.OK, content=b"test", params=url_param) + config = {tts.DOMAIN: {"platform": "yandextts", "api_key": "1234567xx"}} - assert len(aioclient_mock.mock_calls) == 1 - assert len(calls) == 1 + with assert_setup_component(1, tts.DOMAIN): + await async_setup_component(hass, tts.DOMAIN, config) + await hass.async_block_till_done() + + await hass.services.async_call( + tts.DOMAIN, + "yandextts_say", + { + "entity_id": "media_player.something", + tts.ATTR_MESSAGE: "HomeAssistant", + "options": {"emotion": "evil", "speed": 2}, + }, + ) + await hass.async_block_till_done() + + assert len(aioclient_mock.mock_calls) == 1 + assert len(calls) == 1