mirror of
https://github.com/home-assistant/core.git
synced 2025-08-31 10:21:30 +02:00
Add tests and improve error handling for Sonos update_alarm service call (#150715)
This commit is contained in:
@@ -793,8 +793,13 @@ class SonosMediaPlayerEntity(SonosEntity, MediaPlayerEntity):
|
|||||||
if one_alarm.alarm_id == str(alarm_id):
|
if one_alarm.alarm_id == str(alarm_id):
|
||||||
alarm = one_alarm
|
alarm = one_alarm
|
||||||
if alarm is None:
|
if alarm is None:
|
||||||
_LOGGER.warning("Did not find alarm with id %s", alarm_id)
|
raise ServiceValidationError(
|
||||||
return
|
translation_domain=DOMAIN,
|
||||||
|
translation_key="invalid_alarm_id",
|
||||||
|
translation_placeholders={
|
||||||
|
"alarm_id": str(alarm_id),
|
||||||
|
},
|
||||||
|
)
|
||||||
if time is not None:
|
if time is not None:
|
||||||
alarm.start_time = time
|
alarm.start_time = time
|
||||||
if volume is not None:
|
if volume is not None:
|
||||||
|
@@ -223,6 +223,9 @@
|
|||||||
},
|
},
|
||||||
"timeout_join": {
|
"timeout_join": {
|
||||||
"message": "Timeout while waiting for Sonos player to join the group {group_description}"
|
"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."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -33,14 +33,20 @@ from homeassistant.components.sonos.const import (
|
|||||||
SOURCE_TV,
|
SOURCE_TV,
|
||||||
)
|
)
|
||||||
from homeassistant.components.sonos.media_player import (
|
from homeassistant.components.sonos.media_player import (
|
||||||
|
ATTR_ALARM_ID,
|
||||||
|
ATTR_ENABLED,
|
||||||
|
ATTR_INCLUDE_LINKED_ZONES,
|
||||||
|
ATTR_VOLUME,
|
||||||
LONG_SERVICE_TIMEOUT,
|
LONG_SERVICE_TIMEOUT,
|
||||||
SERVICE_GET_QUEUE,
|
SERVICE_GET_QUEUE,
|
||||||
SERVICE_RESTORE,
|
SERVICE_RESTORE,
|
||||||
SERVICE_SNAPSHOT,
|
SERVICE_SNAPSHOT,
|
||||||
|
SERVICE_UPDATE_ALARM,
|
||||||
VOLUME_INCREMENT,
|
VOLUME_INCREMENT,
|
||||||
)
|
)
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_ENTITY_ID,
|
ATTR_ENTITY_ID,
|
||||||
|
ATTR_TIME,
|
||||||
SERVICE_MEDIA_NEXT_TRACK,
|
SERVICE_MEDIA_NEXT_TRACK,
|
||||||
SERVICE_MEDIA_PAUSE,
|
SERVICE_MEDIA_PAUSE,
|
||||||
SERVICE_MEDIA_PLAY,
|
SERVICE_MEDIA_PLAY,
|
||||||
@@ -1265,3 +1271,67 @@ async def test_media_source_list(
|
|||||||
"""Test the mapping between the speaker model name and source_list."""
|
"""Test the mapping between the speaker model name and source_list."""
|
||||||
state = hass.states.get("media_player.zone_a")
|
state = hass.states.get("media_player.zone_a")
|
||||||
assert state.attributes.get(ATTR_INPUT_SOURCE_LIST) == source_list
|
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
|
||||||
|
Reference in New Issue
Block a user