From 2c48f9aa4cad0719999cebb625fc862d788dcc2e Mon Sep 17 00:00:00 2001 From: Pete Sage <76050312+PeteRager@users.noreply.github.com> Date: Sun, 8 Sep 2024 11:34:27 -0400 Subject: [PATCH] FIx Sonos announce regression issue (#125515) * initial commit * initial commit --- .../components/sonos/media_player.py | 24 +++++++++++++++---- tests/components/sonos/test_media_player.py | 21 ++++++++++++++++ 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/sonos/media_player.py b/homeassistant/components/sonos/media_player.py index c4d417b0394..7711a1e88ea 100644 --- a/homeassistant/components/sonos/media_player.py +++ b/homeassistant/components/sonos/media_player.py @@ -84,6 +84,7 @@ REPEAT_TO_SONOS = { SONOS_TO_REPEAT = {meaning: mode for mode, meaning in REPEAT_TO_SONOS.items()} UPNP_ERRORS_TO_IGNORE = ["701", "711", "712"] +ANNOUNCE_NOT_SUPPORTED_ERRORS: list[str] = ["globalError"] SERVICE_SNAPSHOT = "snapshot" SERVICE_RESTORE = "restore" @@ -556,11 +557,24 @@ class SonosMediaPlayerEntity(SonosEntity, MediaPlayerEntity): ) from exc if response.get("success"): return - raise HomeAssistantError( - translation_domain=SONOS_DOMAIN, - translation_key="announce_media_error", - translation_placeholders={"media_id": media_id, "response": response}, - ) + if response.get("type") in ANNOUNCE_NOT_SUPPORTED_ERRORS: + # If the speaker does not support announce do not raise and + # fall through to_play_media to play the clip directly. + _LOGGER.debug( + "Speaker %s does not support announce, media_id %s response %s", + self.speaker.zone_name, + media_id, + response, + ) + else: + raise HomeAssistantError( + translation_domain=SONOS_DOMAIN, + translation_key="announce_media_error", + translation_placeholders={ + "media_id": media_id, + "response": response, + }, + ) if spotify.is_spotify_media_type(media_type): media_type = spotify.resolve_spotify_media_type(media_type) diff --git a/tests/components/sonos/test_media_player.py b/tests/components/sonos/test_media_player.py index 9887601a0a3..63b2c8889ec 100644 --- a/tests/components/sonos/test_media_player.py +++ b/tests/components/sonos/test_media_player.py @@ -1163,6 +1163,27 @@ async def test_play_media_announce( ) assert sonos_websocket.play_clip.call_count == 1 + # Test speakers that do not support announce. This + # will result in playing the clip directly via play_uri + sonos_websocket.play_clip.reset_mock() + sonos_websocket.play_clip.side_effect = None + retval = {"success": 0, "type": "globalError"} + sonos_websocket.play_clip.return_value = [retval, {}] + + await hass.services.async_call( + MP_DOMAIN, + SERVICE_PLAY_MEDIA, + { + ATTR_ENTITY_ID: "media_player.zone_a", + ATTR_MEDIA_CONTENT_TYPE: "music", + ATTR_MEDIA_CONTENT_ID: content_id, + ATTR_MEDIA_ANNOUNCE: True, + }, + blocking=True, + ) + assert sonos_websocket.play_clip.call_count == 1 + soco.play_uri.assert_called_with(content_id, force_radio=False) + async def test_media_get_queue( hass: HomeAssistant,