From cfef72ae5789e2eb840b139de733ba0587470fe8 Mon Sep 17 00:00:00 2001 From: Pete Sage <76050312+PeteRager@users.noreply.github.com> Date: Mon, 29 Jul 2024 06:56:26 -0400 Subject: [PATCH] Add Sonos tests for media_player volume (#122283) --- .../components/sonos/media_player.py | 2 +- tests/components/sonos/test_media_player.py | 46 ++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/sonos/media_player.py b/homeassistant/components/sonos/media_player.py index e9fbb152b7a..4125466bd99 100644 --- a/homeassistant/components/sonos/media_player.py +++ b/homeassistant/components/sonos/media_player.py @@ -388,7 +388,7 @@ class SonosMediaPlayerEntity(SonosEntity, MediaPlayerEntity): @soco_error() def set_volume_level(self, volume: float) -> None: """Set volume level, range 0..1.""" - self.soco.volume = str(int(volume * 100)) + self.soco.volume = int(volume * 100) @soco_error(UPNP_ERRORS_TO_IGNORE) def set_shuffle(self, shuffle: bool) -> None: diff --git a/tests/components/sonos/test_media_player.py b/tests/components/sonos/test_media_player.py index 0a9b1960910..c765ed82ac6 100644 --- a/tests/components/sonos/test_media_player.py +++ b/tests/components/sonos/test_media_player.py @@ -22,8 +22,14 @@ from homeassistant.components.sonos.media_player import ( LONG_SERVICE_TIMEOUT, SERVICE_RESTORE, SERVICE_SNAPSHOT, + VOLUME_INCREMENT, +) +from homeassistant.const import ( + SERVICE_VOLUME_DOWN, + SERVICE_VOLUME_SET, + SERVICE_VOLUME_UP, + STATE_IDLE, ) -from homeassistant.const import STATE_IDLE from homeassistant.core import HomeAssistant from homeassistant.exceptions import ServiceValidationError from homeassistant.helpers.device_registry import ( @@ -768,3 +774,41 @@ async def test_service_snapshot_restore( blocking=True, ) assert mock_restore.call_count == 2 + + +async def test_volume( + hass: HomeAssistant, + soco: MockSoCo, + async_autosetup_sonos, +) -> None: + """Test the media player volume services.""" + initial_volume = soco.volume + + await hass.services.async_call( + MP_DOMAIN, + SERVICE_VOLUME_UP, + { + "entity_id": "media_player.zone_a", + }, + blocking=True, + ) + assert soco.volume == initial_volume + VOLUME_INCREMENT + + await hass.services.async_call( + MP_DOMAIN, + SERVICE_VOLUME_DOWN, + { + "entity_id": "media_player.zone_a", + }, + blocking=True, + ) + assert soco.volume == initial_volume + + await hass.services.async_call( + MP_DOMAIN, + SERVICE_VOLUME_SET, + {"entity_id": "media_player.zone_a", "volume_level": 0.30}, + blocking=True, + ) + # SoCo uses 0..100 for its range. + assert soco.volume == 30