diff --git a/homeassistant/components/tts/__init__.py b/homeassistant/components/tts/__init__.py index 71adc8a98752..7a876c31d20d 100644 --- a/homeassistant/components/tts/__init__.py +++ b/homeassistant/components/tts/__init__.py @@ -1212,6 +1212,7 @@ class SpeechManager: tts_file["artist"] = artist tts_file["album"] = album tts_file["title"] = message + data_bytes.seek(0) tts_file.save(data_bytes) except mutagen.MutagenError as err: _LOGGER.error("ID3 tag error: %s", err) diff --git a/tests/components/tts/fixtures/tagged.mp3 b/tests/components/tts/fixtures/tagged.mp3 new file mode 100644 index 000000000000..b29b59e5add7 Binary files /dev/null and b/tests/components/tts/fixtures/tagged.mp3 differ diff --git a/tests/components/tts/test_init.py b/tests/components/tts/test_init.py index fba4caa26e65..e6402ce2e786 100644 --- a/tests/components/tts/test_init.py +++ b/tests/components/tts/test_init.py @@ -44,7 +44,13 @@ from .common import ( retrieve_media, ) -from tests.common import MockModule, async_mock_service, mock_integration, mock_platform +from tests.common import ( + MockModule, + async_mock_service, + load_fixture_bytes, + mock_integration, + mock_platform, +) from tests.typing import ClientSessionGenerator, WebSocketGenerator ORIG_WRITE_TAGS = tts.SpeechManager.write_tags @@ -2302,3 +2308,22 @@ async def test_stream_override_with_conversion( assert wav_reader.readframes(wav_reader.getnframes()) == bytes( 22050 * 2 * 2 ) # 1 second @ 22.5Khz/stereo + + +def test_write_tags_keeps_single_id3_tag() -> None: + """Test tagging audio that already carries an ID3 tag does not add a second one.""" + data = load_fixture_bytes("tagged.mp3", DOMAIN) + assert data.startswith(b"ID3") + assert data.count(b"ID3") == 1 + + tagged = ORIG_WRITE_TAGS( + "42f18378fd4393d18c8dd11d03fa9563c1e54491_en-us_-_test.mp3", + data, + "Test", + "There is someone at the door.", + "en", + None, + ) + + assert tagged.startswith(b"ID3") + assert tagged.count(b"ID3") == 1