From 9f17a8a943141281dbde6071b92514c6f27b1403 Mon Sep 17 00:00:00 2001 From: Pete Sage <76050312+PeteRager@users.noreply.github.com> Date: Sun, 17 Aug 2025 16:47:45 -0400 Subject: [PATCH] Add tests and improve error handling for Sonos update_alarm service call (#150715) --- .../components/sonos/media_player.py | 9 ++- homeassistant/components/sonos/strings.json | 3 + tests/components/sonos/test_media_player.py | 70 +++++++++++++++++++ 3 files changed, 80 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/sonos/media_player.py b/homeassistant/components/sonos/media_player.py index 6fb7bf00589..0b30c820da3 100644 --- a/homeassistant/components/sonos/media_player.py +++ b/homeassistant/components/sonos/media_player.py @@ -793,8 +793,13 @@ class SonosMediaPlayerEntity(SonosEntity, MediaPlayerEntity): if one_alarm.alarm_id == str(alarm_id): alarm = one_alarm if alarm is None: - _LOGGER.warning("Did not find alarm with id %s", alarm_id) - return + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="invalid_alarm_id", + translation_placeholders={ + "alarm_id": str(alarm_id), + }, + ) if time is not None: alarm.start_time = time if volume is not None: diff --git a/homeassistant/components/sonos/strings.json b/homeassistant/components/sonos/strings.json index 068290066b7..f8aee08f6b7 100644 --- a/homeassistant/components/sonos/strings.json +++ b/homeassistant/components/sonos/strings.json @@ -223,6 +223,9 @@ }, "timeout_join": { "message": "Timeout while waiting for Sonos player to join the group {group_description}" + }, + "invalid_alarm_id": { + "message": "Alarm {alarm_id} does not exist and cannot be updated." } } } diff --git a/tests/components/sonos/test_media_player.py b/tests/components/sonos/test_media_player.py index 84ad624cdc8..41b18750fd4 100644 --- a/tests/components/sonos/test_media_player.py +++ b/tests/components/sonos/test_media_player.py @@ -33,14 +33,20 @@ from homeassistant.components.sonos.const import ( SOURCE_TV, ) from homeassistant.components.sonos.media_player import ( + ATTR_ALARM_ID, + ATTR_ENABLED, + ATTR_INCLUDE_LINKED_ZONES, + ATTR_VOLUME, LONG_SERVICE_TIMEOUT, SERVICE_GET_QUEUE, SERVICE_RESTORE, SERVICE_SNAPSHOT, + SERVICE_UPDATE_ALARM, VOLUME_INCREMENT, ) from homeassistant.const import ( ATTR_ENTITY_ID, + ATTR_TIME, SERVICE_MEDIA_NEXT_TRACK, SERVICE_MEDIA_PAUSE, SERVICE_MEDIA_PLAY, @@ -1265,3 +1271,67 @@ async def test_media_source_list( """Test the mapping between the speaker model name and source_list.""" state = hass.states.get("media_player.zone_a") assert state.attributes.get(ATTR_INPUT_SOURCE_LIST) == source_list + + +async def test_service_update_alarm( + hass: HomeAssistant, + soco: MockSoCo, + async_autosetup_sonos, +) -> None: + """Test updating an alarm.""" + + await hass.services.async_call( + DOMAIN, + SERVICE_UPDATE_ALARM, + { + ATTR_ENTITY_ID: "media_player.zone_a", + ATTR_ALARM_ID: 14, + ATTR_TIME: "07:15:00", + ATTR_VOLUME: 0.25, + ATTR_INCLUDE_LINKED_ZONES: True, + ATTR_ENABLED: True, + }, + blocking=True, + ) + + assert soco.alarmClock.UpdateAlarm.call_count == 1 + assert soco.alarmClock.UpdateAlarm.call_args.args[0] == [ + ("ID", "14"), + ("StartLocalTime", "07:15:00"), + ("Duration", "02:00:00"), + ("Recurrence", "DAILY"), + ("Enabled", "1"), + ("RoomUUID", "RINCON_test"), + ("ProgramURI", "x-rincon-buzzer:0"), + ("ProgramMetaData", ""), + ("PlayMode", "SHUFFLE_NOREPEAT"), + ("Volume", 25), + ("IncludeLinkedZones", "1"), + ] + + +async def test_service_update_alarm_dne( + hass: HomeAssistant, + soco: MockSoCo, + async_autosetup_sonos, +) -> None: + """Test updating an alarm that does not exist.""" + + with pytest.raises( + ServiceValidationError, + match="Alarm 99 does not exist and cannot be updated", + ): + await hass.services.async_call( + DOMAIN, + SERVICE_UPDATE_ALARM, + { + ATTR_ENTITY_ID: "media_player.zone_a", + ATTR_ALARM_ID: 99, + ATTR_TIME: "07:15:00", + ATTR_VOLUME: 0.25, + ATTR_INCLUDE_LINKED_ZONES: True, + ATTR_ENABLED: True, + }, + blocking=True, + ) + assert soco.alarmClock.UpdateAlarm.call_count == 0