diff --git a/homeassistant/components/tts/__init__.py b/homeassistant/components/tts/__init__.py index 7ee4922677c..719f8c52e7a 100644 --- a/homeassistant/components/tts/__init__.py +++ b/homeassistant/components/tts/__init__.py @@ -11,7 +11,7 @@ from typing import Dict, Optional from aiohttp import web import mutagen -from mutagen.id3 import ID3FileType, TextFrame as ID3Text +from mutagen.id3 import ID3, TextFrame as ID3Text import voluptuous as vol from homeassistant.components.http import HomeAssistantView @@ -468,7 +468,9 @@ class SpeechManager: try: tts_file = mutagen.File(data_bytes) 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["album"] = ID3Text(encoding=3, text=album) tts_file["title"] = ID3Text(encoding=3, text=message) diff --git a/tests/components/tts/test_init.py b/tests/components/tts/test_init.py index d5f099175b1..d66efec8c64 100644 --- a/tests/components/tts/test_init.py +++ b/tests/components/tts/test_init.py @@ -73,7 +73,7 @@ def empty_cache_dir(tmp_path, mock_init_cache_dir, mock_get_cache_files, request assert False -@pytest.fixture(autouse=True) +@pytest.fixture() def mutagen_mock(): """Mock writing tags.""" with patch( @@ -480,7 +480,7 @@ async def test_setup_component_and_test_service_with_receive_voice( "42f18378fd4393d18c8dd11d03fa9563c1e54491_en_-_demo.mp3", demo_data, demo_provider, - "AI person is in front of your door.", + "There is someone at the door.", "en", 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) 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