Translate raised exceptions for Squeezebox (#144842)

* initial

* tweak

* review updates
This commit is contained in:
peteS-UK 2025-05-15 00:39:00 +01:00 committed by GitHub
parent 2050b0b375
commit 9a0fed89bd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 86 additions and 11 deletions

View File

@ -19,7 +19,7 @@ from homeassistant.components.media_player import (
from homeassistant.core import HomeAssistant
from homeassistant.helpers.network import is_internal_request
from .const import UNPLAYABLE_TYPES
from .const import DOMAIN, UNPLAYABLE_TYPES
LIBRARY = [
"favorites",
@ -315,7 +315,14 @@ async def build_item_response(
children.append(child_media)
if children is None:
raise BrowseError(f"Media not found: {search_type} / {search_id}")
raise BrowseError(
translation_domain=DOMAIN,
translation_key="browse_media_not_found",
translation_placeholders={
"type": str(search_type),
"id": str(search_id),
},
)
assert media_class["item"] is not None
if not search_id:
@ -398,7 +405,13 @@ async def generate_playlist(
media_id = payload["search_id"]
if media_type not in browse_media.squeezebox_id_by_type:
raise BrowseError(f"Media type not supported: {media_type}")
raise BrowseError(
translation_domain=DOMAIN,
translation_key="browse_media_type_not_supported",
translation_placeholders={
"media_type": str(media_type),
},
)
browse_id = (browse_media.squeezebox_id_by_type[media_type], media_id)
if media_type.startswith("app-"):
@ -412,4 +425,11 @@ async def generate_playlist(
if result and "items" in result:
items: list = result["items"]
return items
raise BrowseError(f"Media not found: {media_type} / {media_id}")
raise BrowseError(
translation_domain=DOMAIN,
translation_key="browse_media_not_found",
translation_placeholders={
"type": str(media_type),
"id": str(media_id),
},
)

View File

@ -18,6 +18,7 @@ if TYPE_CHECKING:
from . import SqueezeboxConfigEntry
from .const import (
DOMAIN,
PLAYER_UPDATE_INTERVAL,
SENSOR_UPDATE_INTERVAL,
SIGNAL_PLAYER_REDISCOVERED,
@ -65,7 +66,10 @@ class LMSStatusDataUpdateCoordinator(DataUpdateCoordinator):
data: dict | None = await self.lms.async_prepared_status()
if not data:
raise UpdateFailed("No data from status poll")
raise UpdateFailed(
translation_domain=DOMAIN,
translation_key="coordinator_no_data",
)
_LOGGER.debug("Raw serverstatus %s=%s", self.lms.name, data)
return data

View File

@ -471,7 +471,11 @@ class SqueezeBoxMediaPlayerEntity(SqueezeboxEntity, MediaPlayerEntity):
if announce:
if media_type not in MediaType.MUSIC:
raise ServiceValidationError(
"Announcements must have media type of 'music'. Playlists are not supported"
translation_domain=DOMAIN,
translation_key="invalid_announce_media_type",
translation_placeholders={
"media_type": str(media_type),
},
)
extra = kwargs.get(ATTR_MEDIA_EXTRA, {})
@ -480,7 +484,11 @@ class SqueezeBoxMediaPlayerEntity(SqueezeboxEntity, MediaPlayerEntity):
announce_volume = get_announce_volume(extra)
except ValueError:
raise ServiceValidationError(
f"{ATTR_ANNOUNCE_VOLUME} must be a number greater than 0 and less than or equal to 1"
translation_domain=DOMAIN,
translation_key="invalid_announce_volume",
translation_placeholders={
"announce_volume": ATTR_ANNOUNCE_VOLUME,
},
) from None
else:
self._player.set_announce_volume(announce_volume)
@ -489,7 +497,11 @@ class SqueezeBoxMediaPlayerEntity(SqueezeboxEntity, MediaPlayerEntity):
announce_timeout = get_announce_timeout(extra)
except ValueError:
raise ServiceValidationError(
f"{ATTR_ANNOUNCE_TIMEOUT} must be a whole number greater than 0"
translation_domain=DOMAIN,
translation_key="invalid_announce_timeout",
translation_placeholders={
"announce_timeout": ATTR_ANNOUNCE_TIMEOUT,
},
) from None
else:
self._player.set_announce_timeout(announce_timeout)
@ -595,13 +607,21 @@ class SqueezeBoxMediaPlayerEntity(SqueezeboxEntity, MediaPlayerEntity):
other_player = ent_reg.async_get(other_player_entity_id)
if other_player is None:
raise ServiceValidationError(
f"Could not find player with entity_id {other_player_entity_id}"
translation_domain=DOMAIN,
translation_key="join_cannot_find_other_player",
translation_placeholders={
"other_player_entity_id": str(other_player_entity_id)
},
)
if other_player_id := other_player.unique_id:
await self._player.async_sync(other_player_id)
else:
raise ServiceValidationError(
f"Could not join unknown player {other_player_entity_id}"
translation_domain=DOMAIN,
translation_key="join_cannot_join_unknown_player",
translation_placeholders={
"other_player_entity_id": str(other_player_entity_id)
},
)
async def async_unjoin_player(self) -> None:

View File

@ -156,5 +156,34 @@
}
}
}
},
"exceptions": {
"invalid_announce_media_type": {
"message": "Only type 'music' can be played as announcement (received type {media_type})."
},
"invalid_announce_volume": {
"message": "{announce_volume} must be a number greater than 0 and less than or equal to 1."
},
"invalid_announce_timeout": {
"message": "{announce_timeout} must be a number greater than 0."
},
"join_cannot_find_other_player": {
"message": "Could not find player with entity_id {other_player_entity_id}."
},
"join_cannot_join_unknown_player": {
"message": "Could not join unknown player {other_player_entity_id}."
},
"coordinator_no_data": {
"message": "No data from status poll."
},
"browse_media_not_found": {
"message": "Media not found: {type} / {id}."
},
"browse_media_type_not_supported": {
"message": "Media type not supported: {media_type}."
},
"update_restart_failed": {
"message": "Error trying to update LMS Plugins: Restart failed."
}
}
}

View File

@ -19,6 +19,7 @@ from homeassistant.helpers.event import async_call_later
from . import SqueezeboxConfigEntry
from .const import (
DOMAIN,
SERVER_MODEL,
STATUS_QUERY_VERSION,
STATUS_UPDATE_NEWPLUGINS,
@ -161,7 +162,8 @@ class ServerStatusUpdatePlugins(ServerStatusUpdate):
self.restart_triggered = False
self.async_write_ha_state()
raise HomeAssistantError(
"Error trying to update LMS Plugins: Restart failed"
translation_domain=DOMAIN,
translation_key="update_restart_failed",
)
async def _async_update_catchall(self, now: datetime | None = None) -> None: