Fix pico tts errors (#41619)

Enabled the use of mutagen in unit tests.
Added a test for tagging a minimal .wav file.
This commit is contained in:
Kevin Cathcart 2020-10-21 05:58:07 -04:00 committed by GitHub
parent c677489535
commit 0674ae6205
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 4 deletions

View File

@ -11,7 +11,7 @@ from typing import Dict, Optional
from aiohttp import web from aiohttp import web
import mutagen import mutagen
from mutagen.id3 import ID3FileType, TextFrame as ID3Text from mutagen.id3 import ID3, TextFrame as ID3Text
import voluptuous as vol import voluptuous as vol
from homeassistant.components.http import HomeAssistantView from homeassistant.components.http import HomeAssistantView
@ -468,7 +468,9 @@ class SpeechManager:
try: try:
tts_file = mutagen.File(data_bytes) tts_file = mutagen.File(data_bytes)
if tts_file is not None: if tts_file is not None:
if isinstance(tts_file, ID3FileType): if not tts_file.tags:
tts_file.add_tags()
if isinstance(tts_file.tags, ID3):
tts_file["artist"] = ID3Text(encoding=3, text=artist) tts_file["artist"] = ID3Text(encoding=3, text=artist)
tts_file["album"] = ID3Text(encoding=3, text=album) tts_file["album"] = ID3Text(encoding=3, text=album)
tts_file["title"] = ID3Text(encoding=3, text=message) tts_file["title"] = ID3Text(encoding=3, text=message)

View File

@ -73,7 +73,7 @@ def empty_cache_dir(tmp_path, mock_init_cache_dir, mock_get_cache_files, request
assert False assert False
@pytest.fixture(autouse=True) @pytest.fixture()
def mutagen_mock(): def mutagen_mock():
"""Mock writing tags.""" """Mock writing tags."""
with patch( with patch(
@ -480,7 +480,7 @@ async def test_setup_component_and_test_service_with_receive_voice(
"42f18378fd4393d18c8dd11d03fa9563c1e54491_en_-_demo.mp3", "42f18378fd4393d18c8dd11d03fa9563c1e54491_en_-_demo.mp3",
demo_data, demo_data,
demo_provider, demo_provider,
"AI person is in front of your door.", "There is someone at the door.",
"en", "en",
None, None,
) )
@ -716,3 +716,24 @@ async def test_setup_component_and_web_get_url_bad_config(hass, hass_client):
req = await client.post(url, json=data) req = await client.post(url, json=data)
assert req.status == 400 assert req.status == 400
async def test_tags_with_wave(hass, demo_provider):
"""Set up the demo platform and call service and receive voice."""
# below data represents an empty wav file
demo_data = bytes.fromhex(
"52 49 46 46 24 00 00 00 57 41 56 45 66 6d 74 20 10 00 00 00 01 00 02 00"
+ "22 56 00 00 88 58 01 00 04 00 10 00 64 61 74 61 00 00 00 00"
)
tagged_data = tts.SpeechManager.write_tags(
"42f18378fd4393d18c8dd11d03fa9563c1e54491_en_-_demo.wav",
demo_data,
demo_provider,
"AI person is in front of your door.",
"en",
None,
)
assert tagged_data != demo_data