Add tests and improve error handling for Sonos update_alarm service call (#150715)

This commit is contained in:
Pete Sage
2025-08-17 16:47:45 -04:00
committed by GitHub
parent b44c47cd80
commit 9f17a8a943
3 changed files with 80 additions and 2 deletions

View File

@@ -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:

View File

@@ -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."
}
}
}

View File

@@ -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