TTS Cleanup and expose get audio (#79065)

This commit is contained in:
Paulus Schoutsen 2022-09-25 20:53:20 -04:00 committed by GitHub
parent 39ddc37d76
commit 697e7b3a20
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 250 additions and 85 deletions

View file

@ -49,13 +49,18 @@ async def internal_url_mock(hass):
)
async def test_setup_component_demo(hass):
@pytest.fixture
async def setup_tts(hass):
"""Mock TTS."""
with patch("homeassistant.components.demo.async_setup", return_value=True):
assert await async_setup_component(
hass, tts.DOMAIN, {"tts": {"platform": "demo"}}
)
await hass.async_block_till_done()
async def test_setup_component_demo(hass, setup_tts):
"""Set up the demo platform with defaults."""
config = {tts.DOMAIN: {"platform": "demo"}}
with assert_setup_component(1, tts.DOMAIN):
assert await async_setup_component(hass, tts.DOMAIN, config)
assert hass.services.has_service(tts.DOMAIN, "demo_say")
assert hass.services.has_service(tts.DOMAIN, "clear_cache")
assert f"{tts.DOMAIN}.demo" in hass.config.components
@ -421,12 +426,14 @@ async def test_setup_component_and_test_service_with_receive_voice(
with assert_setup_component(1, tts.DOMAIN):
assert await async_setup_component(hass, tts.DOMAIN, config)
message = "There is someone at the door."
await hass.services.async_call(
tts.DOMAIN,
"demo_say",
{
"entity_id": "media_player.something",
tts.ATTR_MESSAGE: "There is someone at the door.",
tts.ATTR_MESSAGE: message,
},
blocking=True,
)
@ -440,13 +447,19 @@ async def test_setup_component_and_test_service_with_receive_voice(
"42f18378fd4393d18c8dd11d03fa9563c1e54491_en_-_demo.mp3",
demo_data,
demo_provider,
"There is someone at the door.",
message,
"en",
None,
)
assert req.status == HTTPStatus.OK
assert await req.read() == demo_data
extension, data = await tts.async_get_media_source_audio(
hass, calls[0].data[ATTR_MEDIA_CONTENT_ID]
)
assert extension == "mp3"
assert demo_data == data
async def test_setup_component_and_test_service_with_receive_voice_german(
hass, demo_provider, hass_client
@ -736,3 +749,44 @@ def test_invalid_base_url(value):
"""Test we catch bad base urls."""
with pytest.raises(vol.Invalid):
tts.valid_base_url(value)
@pytest.mark.parametrize(
"engine,language,options,cache,result_engine,result_query",
(
(None, None, None, None, "demo", ""),
(None, "de", None, None, "demo", "language=de"),
(None, "de", {"voice": "henk"}, None, "demo", "language=de&voice=henk"),
(None, "de", None, True, "demo", "cache=true&language=de"),
),
)
async def test_generate_media_source_id(
hass, setup_tts, engine, language, options, cache, result_engine, result_query
):
"""Test generating a media source ID."""
media_source_id = tts.generate_media_source_id(
hass, "msg", engine, language, options, cache
)
assert media_source_id.startswith("media-source://tts/")
_, _, engine_query = media_source_id.rpartition("/")
engine, _, query = engine_query.partition("?")
assert engine == result_engine
assert query.startswith("message=msg")
assert query[12:] == result_query
@pytest.mark.parametrize(
"engine,language,options",
(
("not-loaded-engine", None, None),
(None, "unsupported-language", None),
(None, None, {"option": "not-supported"}),
),
)
async def test_generate_media_source_id_invalid_options(
hass, setup_tts, engine, language, options
):
"""Test generating a media source ID."""
with pytest.raises(HomeAssistantError):
tts.generate_media_source_id(hass, "msg", engine, language, options, None)