diff --git a/tests/components/sonos/test_media_player.py b/tests/components/sonos/test_media_player.py index ab9b598bb04..0a9b1960910 100644 --- a/tests/components/sonos/test_media_player.py +++ b/tests/components/sonos/test_media_player.py @@ -2,6 +2,7 @@ import logging from typing import Any +from unittest.mock import patch import pytest @@ -12,8 +13,16 @@ from homeassistant.components.media_player import ( SERVICE_SELECT_SOURCE, MediaPlayerEnqueue, ) -from homeassistant.components.sonos.const import SOURCE_LINEIN, SOURCE_TV -from homeassistant.components.sonos.media_player import LONG_SERVICE_TIMEOUT +from homeassistant.components.sonos.const import ( + DOMAIN as SONOS_DOMAIN, + SOURCE_LINEIN, + SOURCE_TV, +) +from homeassistant.components.sonos.media_player import ( + LONG_SERVICE_TIMEOUT, + SERVICE_RESTORE, + SERVICE_SNAPSHOT, +) from homeassistant.const import STATE_IDLE from homeassistant.core import HomeAssistant from homeassistant.exceptions import ServiceValidationError @@ -22,8 +31,9 @@ from homeassistant.helpers.device_registry import ( CONNECTION_UPNP, DeviceRegistry, ) +from homeassistant.setup import async_setup_component -from .conftest import MockMusicServiceItem, SoCoMockFactory +from .conftest import MockMusicServiceItem, MockSoCo, SoCoMockFactory async def test_device_registry( @@ -707,3 +717,54 @@ async def test_play_media_favorite_item_id( blocking=True, ) assert "UNKNOWN_ID" in str(sve.value) + + +async def _setup_hass(hass: HomeAssistant): + await async_setup_component( + hass, + SONOS_DOMAIN, + { + "sonos": { + "media_player": { + "interface_addr": "127.0.0.1", + "hosts": ["10.10.10.1", "10.10.10.2"], + } + } + }, + ) + await hass.async_block_till_done() + + +async def test_service_snapshot_restore( + hass: HomeAssistant, + soco_factory: SoCoMockFactory, +) -> None: + """Test the snapshot and restore services.""" + soco_factory.cache_mock(MockSoCo(), "10.10.10.1", "Living Room") + soco_factory.cache_mock(MockSoCo(), "10.10.10.2", "Bedroom") + await _setup_hass(hass) + with patch( + "homeassistant.components.sonos.speaker.Snapshot.snapshot" + ) as mock_snapshot: + await hass.services.async_call( + SONOS_DOMAIN, + SERVICE_SNAPSHOT, + { + "entity_id": ["media_player.living_room", "media_player.bedroom"], + }, + blocking=True, + ) + assert mock_snapshot.call_count == 2 + + with patch( + "homeassistant.components.sonos.speaker.Snapshot.restore" + ) as mock_restore: + await hass.services.async_call( + SONOS_DOMAIN, + SERVICE_RESTORE, + { + "entity_id": ["media_player.living_room", "media_player.bedroom"], + }, + blocking=True, + ) + assert mock_restore.call_count == 2