diff --git a/homeassistant/components/radio_browser/media_source.py b/homeassistant/components/radio_browser/media_source.py index 0f551230ef9b..6d8545314c7c 100644 --- a/homeassistant/components/radio_browser/media_source.py +++ b/homeassistant/components/radio_browser/media_source.py @@ -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) diff --git a/homeassistant/components/radio_browser/strings.json b/homeassistant/components/radio_browser/strings.json index c1e99128ee17..c83c4c86e57a 100644 --- a/homeassistant/components/radio_browser/strings.json +++ b/homeassistant/components/radio_browser/strings.json @@ -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" } } } diff --git a/tests/components/radio_browser/test_media_source.py b/tests/components/radio_browser/test_media_source.py index ec01d0f3b4cb..f5b738ad98b3 100644 --- a/tests/components/radio_browser/test_media_source.py +++ b/tests/components/radio_browser/test_media_source.py @@ -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()