From f84651b14e1d51999e5919f745a553a94a87eada Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Mon, 27 Mar 2023 20:00:54 +0200 Subject: [PATCH] Improve tts test coverage (#90370) --- tests/components/tts/test_init.py | 64 +++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/tests/components/tts/test_init.py b/tests/components/tts/test_init.py index e7b3a818f63..251ed9b30c0 100644 --- a/tests/components/tts/test_init.py +++ b/tests/components/tts/test_init.py @@ -14,6 +14,7 @@ from homeassistant.components.media_player import ( SERVICE_PLAY_MEDIA, MediaType, ) +from homeassistant.components.media_source import Unresolvable from homeassistant.config import async_process_ha_core_config from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError @@ -398,6 +399,54 @@ async def test_setup_component_and_test_with_service_options_def( ).is_file() +async def test_setup_component_and_test_with_service_options_def_2( + hass: HomeAssistant, empty_cache_dir +) -> None: + """Set up a TTS platform and call service with default options. + + This tests merging default and user provided options. + """ + calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) + + config = {tts.DOMAIN: {"platform": "test"}} + + class MockProviderWithDefaults(MockProvider): + @property + def default_options(self): + return {"voice": "alex"} + + mock_integration(hass, MockModule(domain="test")) + mock_platform(hass, "test.tts", MockTTS(MockProviderWithDefaults)) + + with assert_setup_component(1, tts.DOMAIN): + assert await async_setup_component(hass, tts.DOMAIN, config) + + await hass.services.async_call( + tts.DOMAIN, + "test_say", + { + "entity_id": "media_player.something", + tts.ATTR_MESSAGE: "There is someone at the door.", + tts.ATTR_LANGUAGE: "de", + tts.ATTR_OPTIONS: {"age": 5}, + }, + blocking=True, + ) + opt_hash = tts._hash_options({"voice": "alex", "age": 5}) + + assert len(calls) == 1 + assert calls[0].data[ATTR_MEDIA_CONTENT_TYPE] == MediaType.MUSIC + assert ( + await get_media_source_url(hass, calls[0].data[ATTR_MEDIA_CONTENT_ID]) + == f"/api/tts_proxy/42f18378fd4393d18c8dd11d03fa9563c1e54491_de_{opt_hash}_test.mp3" + ) + await hass.async_block_till_done() + assert ( + empty_cache_dir + / f"42f18378fd4393d18c8dd11d03fa9563c1e54491_de_{opt_hash}_test.mp3" + ).is_file() + + async def test_setup_component_and_test_service_with_service_options_wrong( hass: HomeAssistant, empty_cache_dir, mock_tts ) -> None: @@ -718,6 +767,8 @@ async def test_setup_component_test_with_cache_dir( async def test_setup_component_test_with_error_on_get_tts(hass: HomeAssistant) -> None: """Set up a TTS platform with wrong get_tts_audio.""" + calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) + config = {tts.DOMAIN: {"platform": "test"}} class MockProviderEmpty(MockProvider): @@ -733,6 +784,19 @@ async def test_setup_component_test_with_error_on_get_tts(hass: HomeAssistant) - with assert_setup_component(1, tts.DOMAIN): assert await async_setup_component(hass, tts.DOMAIN, config) + await hass.services.async_call( + tts.DOMAIN, + "test_say", + { + "entity_id": "media_player.something", + tts.ATTR_MESSAGE: "There is someone at the door.", + }, + blocking=True, + ) + assert len(calls) == 1 + with pytest.raises(Unresolvable): + await get_media_source_url(hass, calls[0].data[ATTR_MEDIA_CONTENT_ID]) + async def test_setup_component_load_cache_retrieve_without_mem_cache( hass: HomeAssistant,