diff --git a/homeassistant/components/cambridge_audio/icons.json b/homeassistant/components/cambridge_audio/icons.json index cb43d36779f..b4346a7fe8e 100644 --- a/homeassistant/components/cambridge_audio/icons.json +++ b/homeassistant/components/cambridge_audio/icons.json @@ -8,6 +8,9 @@ "dim": "mdi:brightness-6", "off": "mdi:brightness-3" } + }, + "audio_output": { + "default": "mdi:audio-input-stereo-minijack" } }, "switch": { diff --git a/homeassistant/components/cambridge_audio/select.py b/homeassistant/components/cambridge_audio/select.py index d2d44ecfb92..ca6eebdec6b 100644 --- a/homeassistant/components/cambridge_audio/select.py +++ b/homeassistant/components/cambridge_audio/select.py @@ -1,7 +1,7 @@ """Support for Cambridge Audio select entities.""" from collections.abc import Awaitable, Callable -from dataclasses import dataclass +from dataclasses import dataclass, field from aiostreammagic import StreamMagicClient from aiostreammagic.models import DisplayBrightness @@ -19,10 +19,34 @@ from .entity import CambridgeAudioEntity class CambridgeAudioSelectEntityDescription(SelectEntityDescription): """Describes Cambridge Audio select entity.""" + options_fn: Callable[[StreamMagicClient], list[str]] = field(default=lambda _: []) + load_fn: Callable[[StreamMagicClient], bool] = field(default=lambda _: True) value_fn: Callable[[StreamMagicClient], str | None] set_value_fn: Callable[[StreamMagicClient, str], Awaitable[None]] +async def _audio_output_set_value_fn(client: StreamMagicClient, value: str) -> None: + """Set the audio output using the display name.""" + audio_output_id = next( + (output.id for output in client.audio_output.outputs if value == output.name), + None, + ) + assert audio_output_id is not None + await client.set_audio_output(audio_output_id) + + +def _audio_output_value_fn(client: StreamMagicClient) -> str | None: + """Convert the current audio output id to name.""" + return next( + ( + output.name + for output in client.audio_output.outputs + if client.state.audio_output == output.id + ), + None, + ) + + CONTROL_ENTITIES: tuple[CambridgeAudioSelectEntityDescription, ...] = ( CambridgeAudioSelectEntityDescription( key="display_brightness", @@ -34,6 +58,17 @@ CONTROL_ENTITIES: tuple[CambridgeAudioSelectEntityDescription, ...] = ( DisplayBrightness(value) ), ), + CambridgeAudioSelectEntityDescription( + key="audio_output", + translation_key="audio_output", + entity_category=EntityCategory.CONFIG, + options_fn=lambda client: [ + output.name for output in client.audio_output.outputs + ], + load_fn=lambda client: len(client.audio_output.outputs) > 0, + value_fn=_audio_output_value_fn, + set_value_fn=_audio_output_set_value_fn, + ), ) @@ -46,7 +81,9 @@ async def async_setup_entry( client: StreamMagicClient = entry.runtime_data entities: list[CambridgeAudioSelect] = [ - CambridgeAudioSelect(client, description) for description in CONTROL_ENTITIES + CambridgeAudioSelect(client, description) + for description in CONTROL_ENTITIES + if description.load_fn(client) ] async_add_entities(entities) @@ -65,6 +102,9 @@ class CambridgeAudioSelect(CambridgeAudioEntity, SelectEntity): super().__init__(client) self.entity_description = description self._attr_unique_id = f"{client.info.unit_id}-{description.key}" + options_fn = description.options_fn(client) + if options_fn: + self._attr_options = options_fn @property def current_option(self) -> str | None: diff --git a/homeassistant/components/cambridge_audio/strings.json b/homeassistant/components/cambridge_audio/strings.json index 8c33a5d142b..c368ba060a7 100644 --- a/homeassistant/components/cambridge_audio/strings.json +++ b/homeassistant/components/cambridge_audio/strings.json @@ -32,6 +32,9 @@ "dim": "Dim", "off": "[%key:common::state::off%]" } + }, + "audio_output": { + "name": "Audio output" } }, "switch": { diff --git a/tests/components/cambridge_audio/conftest.py b/tests/components/cambridge_audio/conftest.py index 86339e59b98..33a9ded70e3 100644 --- a/tests/components/cambridge_audio/conftest.py +++ b/tests/components/cambridge_audio/conftest.py @@ -4,6 +4,7 @@ from collections.abc import Generator from unittest.mock import AsyncMock, Mock, patch from aiostreammagic.models import ( + AudioOutput, Display, Info, NowPlaying, @@ -63,6 +64,9 @@ def mock_stream_magic_client() -> Generator[AsyncMock]: client.preset_list = PresetList.from_json( load_fixture("get_presets_list.json", DOMAIN) ) + client.audio_output = AudioOutput.from_json( + load_fixture("get_audio_output.json", DOMAIN) + ) client.is_connected = Mock(return_value=True) client.position_last_updated = client.play_state.position client.unregister_state_update_callbacks.return_value = True diff --git a/tests/components/cambridge_audio/fixtures/get_audio_output.json b/tests/components/cambridge_audio/fixtures/get_audio_output.json new file mode 100644 index 00000000000..e38ae037307 --- /dev/null +++ b/tests/components/cambridge_audio/fixtures/get_audio_output.json @@ -0,0 +1,16 @@ +{ + "outputs": [ + { + "id": "speaker_a", + "name": "Speaker A" + }, + { + "id": "speaker_b", + "name": "Speaker B" + }, + { + "id": "headphones", + "name": "Headphones" + } + ] +} diff --git a/tests/components/cambridge_audio/snapshots/test_select.ambr b/tests/components/cambridge_audio/snapshots/test_select.ambr index 39e1ea8f173..b40c8a8d5c4 100644 --- a/tests/components/cambridge_audio/snapshots/test_select.ambr +++ b/tests/components/cambridge_audio/snapshots/test_select.ambr @@ -1,4 +1,61 @@ # serializer version: 1 +# name: test_all_entities[select.cambridge_audio_cxnv2_audio_output-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'options': list([ + 'Speaker A', + 'Speaker B', + 'Headphones', + ]), + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'select', + 'entity_category': , + 'entity_id': 'select.cambridge_audio_cxnv2_audio_output', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Audio output', + 'platform': 'cambridge_audio', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'audio_output', + 'unique_id': '0020c2d8-audio_output', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_entities[select.cambridge_audio_cxnv2_audio_output-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Cambridge Audio CXNv2 Audio output', + 'options': list([ + 'Speaker A', + 'Speaker B', + 'Headphones', + ]), + }), + 'context': , + 'entity_id': 'select.cambridge_audio_cxnv2_audio_output', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'unknown', + }) +# --- # name: test_all_entities[select.cambridge_audio_cxnv2_display_brightness-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ diff --git a/tests/components/cambridge_audio/test_select.py b/tests/components/cambridge_audio/test_select.py index e1185be45c0..473c4027163 100644 --- a/tests/components/cambridge_audio/test_select.py +++ b/tests/components/cambridge_audio/test_select.py @@ -51,3 +51,14 @@ async def test_setting_value( blocking=True, ) mock_stream_magic_client.set_display_brightness.assert_called_once_with("dim") + + await hass.services.async_call( + SELECT_DOMAIN, + SERVICE_SELECT_OPTION, + { + ATTR_ENTITY_ID: "select.cambridge_audio_cxnv2_audio_output", + ATTR_OPTION: "Speaker A", + }, + blocking=True, + ) + mock_stream_magic_client.set_audio_output.assert_called_once_with("speaker_a")