Add additional tests to Cambridge Audio (#128213)

This commit is contained in:
Noah Husby 2024-10-25 05:13:27 -04:00 committed by GitHub
parent 47bf0ebb47
commit bc0e3b254b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 89 additions and 2 deletions

View File

@ -2,9 +2,11 @@
from unittest.mock import AsyncMock
from aiostreammagic import StreamMagicError
from syrupy import SnapshotAssertion
from homeassistant.components.cambridge_audio.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
@ -13,6 +15,20 @@ from . import setup_integration
from tests.common import MockConfigEntry
async def test_config_entry_not_ready(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_stream_magic_client: AsyncMock,
) -> None:
"""Test the Cambridge Audio configuration entry not ready."""
mock_stream_magic_client.connect = AsyncMock(side_effect=StreamMagicError())
await setup_integration(hass, mock_config_entry)
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY
mock_stream_magic_client.connect = AsyncMock(return_value=True)
async def test_device_info(
hass: HomeAssistant,
snapshot: SnapshotAssertion,

View File

@ -16,6 +16,7 @@ from homeassistant.components.media_player import (
ATTR_MEDIA_REPEAT,
ATTR_MEDIA_SEEK_POSITION,
ATTR_MEDIA_SHUFFLE,
ATTR_MEDIA_VOLUME_LEVEL,
DOMAIN as MP_DOMAIN,
SERVICE_PLAY_MEDIA,
MediaPlayerEntityFeature,
@ -34,6 +35,9 @@ from homeassistant.const import (
SERVICE_SHUFFLE_SET,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
SERVICE_VOLUME_DOWN,
SERVICE_VOLUME_SET,
SERVICE_VOLUME_UP,
STATE_BUFFERING,
STATE_IDLE,
STATE_OFF,
@ -219,12 +223,12 @@ async def test_media_next_previous_track(
mock_stream_magic_client.previous_track.assert_called_once()
async def test_shuffle_repeat(
async def test_shuffle_repeat_set(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_stream_magic_client: AsyncMock,
) -> None:
"""Test shuffle and repeat service."""
"""Test shuffle and repeat set service."""
await setup_integration(hass, mock_config_entry)
mock_stream_magic_client.now_playing.controls = [
@ -267,6 +271,36 @@ async def test_shuffle_repeat(
mock_stream_magic_client.set_repeat.assert_called_with(CambridgeRepeatMode.ALL)
async def test_shuffle_repeat_get(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_stream_magic_client: AsyncMock,
) -> None:
"""Test shuffle and repeat get service."""
await setup_integration(hass, mock_config_entry)
mock_stream_magic_client.play_state.mode_shuffle = None
state = hass.states.get(ENTITY_ID)
assert state.attributes[ATTR_MEDIA_SHUFFLE] is False
mock_stream_magic_client.play_state.mode_shuffle = ShuffleMode.ALL
await mock_state_update(mock_stream_magic_client)
await hass.async_block_till_done()
state = hass.states.get(ENTITY_ID)
assert state.attributes[ATTR_MEDIA_SHUFFLE] is True
mock_stream_magic_client.play_state.mode_repeat = CambridgeRepeatMode.ALL
await mock_state_update(mock_stream_magic_client)
await hass.async_block_till_done()
state = hass.states.get(ENTITY_ID)
assert state.attributes[ATTR_MEDIA_REPEAT] == RepeatMode.ALL
async def test_power_service(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
@ -307,6 +341,43 @@ async def test_media_seek(
mock_stream_magic_client.media_seek.assert_called_once_with(100)
async def test_media_volume(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_stream_magic_client: AsyncMock,
) -> None:
"""Test volume service."""
await setup_integration(hass, mock_config_entry)
mock_stream_magic_client.state.pre_amp_mode = True
# Test volume up
await hass.services.async_call(
MP_DOMAIN,
SERVICE_VOLUME_UP,
{ATTR_ENTITY_ID: ENTITY_ID},
)
mock_stream_magic_client.volume_up.assert_called_once()
# Test volume down
await hass.services.async_call(
MP_DOMAIN,
SERVICE_VOLUME_DOWN,
{ATTR_ENTITY_ID: ENTITY_ID},
)
mock_stream_magic_client.volume_down.assert_called_once()
await hass.services.async_call(
MP_DOMAIN,
SERVICE_VOLUME_SET,
{ATTR_ENTITY_ID: ENTITY_ID, ATTR_MEDIA_VOLUME_LEVEL: 0.30},
)
mock_stream_magic_client.set_volume.assert_called_once_with(30)
async def test_play_media_preset_item_id(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,