Translate the Radio Browser media source errors (#184079)

This commit is contained in:
Franck Nijhof
2026-10-02 19:29:11 +01:00
committed by GitHub
parent 1cc70af18b
commit bdf6fa2d7a
3 changed files with 63 additions and 3 deletions
@@ -80,10 +80,16 @@ class RadioMediaSource(MediaSource):
translation_key="radio_browser_error",
) from e
if not station:
raise Unresolvable("Radio station is no longer available")
raise Unresolvable(
translation_domain=DOMAIN,
translation_key="station_not_found",
)
if not (mime_type := self._async_get_station_mime_type(station)):
raise Unresolvable("Could not determine stream type of radio station")
raise Unresolvable(
translation_domain=DOMAIN,
translation_key="unknown_stream_type",
)
# Register "click" with Radio Browser
await radios.station_click(uuid=station.uuid)
@@ -12,6 +12,12 @@
},
"radio_browser_error": {
"message": "Error occurred while communicating with Radio Browser"
},
"station_not_found": {
"message": "The radio station is no longer available"
},
"unknown_stream_type": {
"message": "Could not determine the stream type of the radio station"
}
}
}
@@ -1,6 +1,6 @@
"""Tests for radio_browser media_source."""
from unittest.mock import AsyncMock, patch
from unittest.mock import AsyncMock, MagicMock, patch
from aiodns.error import DNSError
import pytest
@@ -263,3 +263,51 @@ async def test_resolve_media_not_ready(
hass, f"{media_source.URI_SCHEME}{DOMAIN}/123456", None
)
assert exc_info.value.translation_key == "config_entry_not_ready"
async def test_resolve_media_station_not_found(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test resolving a station that no longer exists."""
with patch(
"homeassistant.components.radio_browser.RadioBrowser",
autospec=True,
) as mock_browser:
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
mock_browser.return_value.station.return_value = None
with pytest.raises(media_source.Unresolvable) as exc_info:
await media_source.async_resolve_media(
hass, f"{media_source.URI_SCHEME}{DOMAIN}/123456", None
)
assert exc_info.value.translation_key == "station_not_found"
async def test_resolve_media_unknown_stream_type(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test resolving a station whose stream type cannot be determined."""
with patch(
"homeassistant.components.radio_browser.RadioBrowser",
autospec=True,
) as mock_browser:
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
station = MagicMock(codec="UNKNOWN", url="https://example.com/stream")
mock_browser.return_value.station.return_value = station
with pytest.raises(media_source.Unresolvable) as exc_info:
await media_source.async_resolve_media(
hass, f"{media_source.URI_SCHEME}{DOMAIN}/123456", None
)
assert exc_info.value.translation_key == "unknown_stream_type"
mock_browser.return_value.station_click.assert_not_called()