diff --git a/homeassistant/components/bang_olufsen/media_player.py b/homeassistant/components/bang_olufsen/media_player.py index d23c75046ff..0eff9f2bb85 100644 --- a/homeassistant/components/bang_olufsen/media_player.py +++ b/homeassistant/components/bang_olufsen/media_player.py @@ -45,7 +45,7 @@ from homeassistant.components.media_player import ( from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_MODEL from homeassistant.core import HomeAssistant, callback -from homeassistant.exceptions import HomeAssistantError +from homeassistant.exceptions import HomeAssistantError, ServiceValidationError from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity_platform import AddEntitiesCallback @@ -316,7 +316,7 @@ class BangOlufsenMediaPlayer(BangOlufsenEntity, MediaPlayerEntity): @callback def _async_update_playback_error(self, data: PlaybackError) -> None: """Show playback error.""" - _LOGGER.error(data.error) + raise HomeAssistantError(data.error) @callback def _async_update_playback_progress(self, data: PlaybackProgress) -> None: @@ -516,7 +516,9 @@ class BangOlufsenMediaPlayer(BangOlufsenEntity, MediaPlayerEntity): self.async_write_ha_state() else: - _LOGGER.error("Seeking is currently only supported when using Deezer") + raise HomeAssistantError( + translation_domain=DOMAIN, translation_key="non_deezer_seeking" + ) async def async_media_previous_track(self) -> None: """Send the previous track command.""" @@ -529,12 +531,14 @@ class BangOlufsenMediaPlayer(BangOlufsenEntity, MediaPlayerEntity): async def async_select_source(self, source: str) -> None: """Select an input source.""" if source not in self._sources.values(): - _LOGGER.error( - "Invalid source: %s. Valid sources are: %s", - source, - list(self._sources.values()), + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="invalid_source", + translation_placeholders={ + "invalid_source": source, + "valid_sources": ",".join(list(self._sources.values())), + }, ) - return key = [x for x in self._sources if self._sources[x] == source][0] @@ -559,12 +563,14 @@ class BangOlufsenMediaPlayer(BangOlufsenEntity, MediaPlayerEntity): media_type = MediaType.MUSIC if media_type not in VALID_MEDIA_TYPES: - _LOGGER.error( - "%s is an invalid type. Valid values are: %s", - media_type, - VALID_MEDIA_TYPES, + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="invalid_media_type", + translation_placeholders={ + "invalid_media_type": media_type, + "valid_media_types": ",".join(VALID_MEDIA_TYPES), + }, ) - return if media_source.is_media_source_id(media_id): sourced_media = await media_source.async_resolve_media( @@ -681,7 +687,14 @@ class BangOlufsenMediaPlayer(BangOlufsenEntity, MediaPlayerEntity): ) except ApiException as error: - _LOGGER.error(json.loads(error.body)["message"]) + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="play_media_error", + translation_placeholders={ + "media_type": media_type, + "error_message": json.loads(error.body)["message"], + }, + ) from error async def async_browse_media( self, diff --git a/homeassistant/components/bang_olufsen/strings.json b/homeassistant/components/bang_olufsen/strings.json index 93b55cf0db2..cf5b212d424 100644 --- a/homeassistant/components/bang_olufsen/strings.json +++ b/homeassistant/components/bang_olufsen/strings.json @@ -28,6 +28,18 @@ "exceptions": { "m3u_invalid_format": { "message": "Media sources with the .m3u extension are not supported." + }, + "non_deezer_seeking": { + "message": "Seeking is currently only supported when using Deezer" + }, + "invalid_source": { + "message": "Invalid source: {invalid_source}. Valid sources are: {valid_sources}" + }, + "invalid_media_type": { + "message": "{invalid_media_type} is an invalid type. Valid values are: {valid_media_types}." + }, + "play_media_error": { + "message": "An error occurred while attempting to play {media_type}: {error_message}." } } }