Skip to content

Commit

Permalink
Improve integration tests for Cambridge Audio (home-assistant#125906)
Browse files Browse the repository at this point in the history
  • Loading branch information
noahhusby authored Sep 13, 2024
1 parent d507953 commit 2d9c970
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 2 deletions.
2 changes: 1 addition & 1 deletion homeassistant/components/cambridge_audio/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,6 @@ async def async_set_shuffle(self, shuffle: bool) -> None:
async def async_set_repeat(self, repeat: RepeatMode) -> None:
"""Set the repeat mode for the current queue."""
repeat_mode = CambridgeRepeatMode.OFF
if repeat:
if repeat in {RepeatMode.ALL, RepeatMode.ONE}:
repeat_mode = CambridgeRepeatMode.ALL
await self.client.set_repeat(repeat_mode)
105 changes: 104 additions & 1 deletion tests/components/cambridge_audio/test_media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@

from unittest.mock import AsyncMock

from aiostreammagic import TransportControl
from aiostreammagic import (
RepeatMode as CambridgeRepeatMode,
ShuffleMode,
TransportControl,
)
from aiostreammagic.models import CallbackType
import pytest

from homeassistant.components.media_player import (
ATTR_MEDIA_REPEAT,
ATTR_MEDIA_SEEK_POSITION,
ATTR_MEDIA_SHUFFLE,
DOMAIN as MP_DOMAIN,
MediaPlayerEntityFeature,
RepeatMode,
)
from homeassistant.const import (
ATTR_ENTITY_ID,
Expand All @@ -17,9 +25,15 @@
SERVICE_MEDIA_PAUSE,
SERVICE_MEDIA_PLAY,
SERVICE_MEDIA_PREVIOUS_TRACK,
SERVICE_MEDIA_SEEK,
SERVICE_REPEAT_SET,
SERVICE_SHUFFLE_SET,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
STATE_BUFFERING,
STATE_IDLE,
STATE_OFF,
STATE_ON,
STATE_PAUSED,
STATE_PLAYING,
STATE_STANDBY,
Expand Down Expand Up @@ -124,6 +138,7 @@ async def test_entity_supported_features(
(True, "connecting", STATE_BUFFERING),
(True, "stop", STATE_IDLE),
(True, "ready", STATE_IDLE),
(True, "other", STATE_ON),
],
)
async def test_entity_state(
Expand Down Expand Up @@ -194,3 +209,91 @@ async def test_media_next_previous_track(
await hass.services.async_call(MP_DOMAIN, SERVICE_MEDIA_PREVIOUS_TRACK, data, True)

mock_stream_magic_client.previous_track.assert_called_once()


async def test_shuffle_repeat(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_stream_magic_client: AsyncMock,
) -> None:
"""Test shuffle and repeat service."""
await setup_integration(hass, mock_config_entry)

mock_stream_magic_client.now_playing.controls = [
TransportControl.TOGGLE_SHUFFLE,
TransportControl.TOGGLE_REPEAT,
]

# Test shuffle
await hass.services.async_call(
MP_DOMAIN,
SERVICE_SHUFFLE_SET,
{ATTR_ENTITY_ID: ENTITY_ID, ATTR_MEDIA_SHUFFLE: False},
)

mock_stream_magic_client.set_shuffle.assert_called_with(ShuffleMode.OFF)

await hass.services.async_call(
MP_DOMAIN,
SERVICE_SHUFFLE_SET,
{ATTR_ENTITY_ID: ENTITY_ID, ATTR_MEDIA_SHUFFLE: True},
)

mock_stream_magic_client.set_shuffle.assert_called_with(ShuffleMode.ALL)

# Test repeat
await hass.services.async_call(
MP_DOMAIN,
SERVICE_REPEAT_SET,
{ATTR_ENTITY_ID: ENTITY_ID, ATTR_MEDIA_REPEAT: RepeatMode.OFF},
)

mock_stream_magic_client.set_repeat.assert_called_with(CambridgeRepeatMode.OFF)

await hass.services.async_call(
MP_DOMAIN,
SERVICE_REPEAT_SET,
{ATTR_ENTITY_ID: ENTITY_ID, ATTR_MEDIA_REPEAT: RepeatMode.ALL},
)

mock_stream_magic_client.set_repeat.assert_called_with(CambridgeRepeatMode.ALL)


async def test_power_service(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_stream_magic_client: AsyncMock,
) -> None:
"""Test power service."""
await setup_integration(hass, mock_config_entry)

data = {ATTR_ENTITY_ID: ENTITY_ID}

await hass.services.async_call(MP_DOMAIN, SERVICE_TURN_ON, data, True)

mock_stream_magic_client.power_on.assert_called_once()

await hass.services.async_call(MP_DOMAIN, SERVICE_TURN_OFF, data, True)

mock_stream_magic_client.power_off.assert_called_once()


async def test_media_seek(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_stream_magic_client: AsyncMock,
) -> None:
"""Test media seek service."""
await setup_integration(hass, mock_config_entry)

mock_stream_magic_client.now_playing.controls = [
TransportControl.SEEK,
]

await hass.services.async_call(
MP_DOMAIN,
SERVICE_MEDIA_SEEK,
{ATTR_ENTITY_ID: ENTITY_ID, ATTR_MEDIA_SEEK_POSITION: 100},
)

mock_stream_magic_client.media_seek.assert_called_once_with(100)

0 comments on commit 2d9c970

Please sign in to comment.