From 2aa9d22350b28c3a47cca20a16179eab9cd6b80e Mon Sep 17 00:00:00 2001 From: Noah Husby <32528627+noahhusby@users.noreply.github.com> Date: Thu, 12 Feb 2026 17:10:13 -0600 Subject: [PATCH] Add room correction setting to Cambridge Audio (#162743) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: AbĂ­lio Costa --- .../components/cambridge_audio/icons.json | 3 ++ .../components/cambridge_audio/strings.json | 3 ++ .../components/cambridge_audio/switch.py | 22 ++++++++- tests/components/cambridge_audio/conftest.py | 2 + .../cambridge_audio/fixtures/get_audio.json | 6 +++ .../snapshots/test_switch.ambr | 49 +++++++++++++++++++ 6 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 tests/components/cambridge_audio/fixtures/get_audio.json diff --git a/homeassistant/components/cambridge_audio/icons.json b/homeassistant/components/cambridge_audio/icons.json index a889a85a17b..dbeb52a73ff 100644 --- a/homeassistant/components/cambridge_audio/icons.json +++ b/homeassistant/components/cambridge_audio/icons.json @@ -29,6 +29,9 @@ "state": { "off": "mdi:volume-low" } + }, + "room_correction": { + "default": "mdi:arrow-oscillating" } } } diff --git a/homeassistant/components/cambridge_audio/strings.json b/homeassistant/components/cambridge_audio/strings.json index 09d95b16e46..c386df6bf47 100644 --- a/homeassistant/components/cambridge_audio/strings.json +++ b/homeassistant/components/cambridge_audio/strings.json @@ -62,6 +62,9 @@ }, "pre_amp": { "name": "Pre-Amp" + }, + "room_correction": { + "name": "Room correction" } } }, diff --git a/homeassistant/components/cambridge_audio/switch.py b/homeassistant/components/cambridge_audio/switch.py index 0cebe8266c4..be521aad4f3 100644 --- a/homeassistant/components/cambridge_audio/switch.py +++ b/homeassistant/components/cambridge_audio/switch.py @@ -1,8 +1,8 @@ """Support for Cambridge Audio switch entities.""" from collections.abc import Awaitable, Callable -from dataclasses import dataclass -from typing import Any +from dataclasses import dataclass, field +from typing import TYPE_CHECKING, Any from aiostreammagic import StreamMagicClient @@ -21,10 +21,18 @@ PARALLEL_UPDATES = 0 class CambridgeAudioSwitchEntityDescription(SwitchEntityDescription): """Describes Cambridge Audio switch entity.""" + load_fn: Callable[[StreamMagicClient], bool] = field(default=lambda _: True) value_fn: Callable[[StreamMagicClient], bool] set_value_fn: Callable[[StreamMagicClient, bool], Awaitable[None]] +def room_correction_enabled(client: StreamMagicClient) -> bool: + """Check if room correction is enabled.""" + if TYPE_CHECKING: + assert client.audio.tilt_eq is not None + return client.audio.tilt_eq.enabled + + CONTROL_ENTITIES: tuple[CambridgeAudioSwitchEntityDescription, ...] = ( CambridgeAudioSwitchEntityDescription( key="pre_amp", @@ -40,6 +48,14 @@ CONTROL_ENTITIES: tuple[CambridgeAudioSwitchEntityDescription, ...] = ( value_fn=lambda client: client.update.early_update, set_value_fn=lambda client, value: client.set_early_update(value), ), + CambridgeAudioSwitchEntityDescription( + key="room_correction", + translation_key="room_correction", + entity_category=EntityCategory.CONFIG, + load_fn=lambda client: client.audio.tilt_eq is not None, + value_fn=room_correction_enabled, + set_value_fn=lambda client, value: client.set_room_correction_mode(value), + ), ) @@ -49,9 +65,11 @@ async def async_setup_entry( async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Set up Cambridge Audio switch entities based on a config entry.""" + client: StreamMagicClient = entry.runtime_data async_add_entities( CambridgeAudioSwitch(entry.runtime_data, description) for description in CONTROL_ENTITIES + if description.load_fn(client) ) diff --git a/tests/components/cambridge_audio/conftest.py b/tests/components/cambridge_audio/conftest.py index 33a9ded70e3..b3a48320591 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 ( + Audio, AudioOutput, Display, Info, @@ -67,6 +68,7 @@ def mock_stream_magic_client() -> Generator[AsyncMock]: client.audio_output = AudioOutput.from_json( load_fixture("get_audio_output.json", DOMAIN) ) + client.audio = Audio.from_json(load_fixture("get_audio.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.json b/tests/components/cambridge_audio/fixtures/get_audio.json new file mode 100644 index 00000000000..68bd8d9ebcc --- /dev/null +++ b/tests/components/cambridge_audio/fixtures/get_audio.json @@ -0,0 +1,6 @@ +{ + "tilt_eq": { + "enabled": true, + "intensity": 100 + } +} diff --git a/tests/components/cambridge_audio/snapshots/test_switch.ambr b/tests/components/cambridge_audio/snapshots/test_switch.ambr index 3f00ac0b7a5..eb8fa68809d 100644 --- a/tests/components/cambridge_audio/snapshots/test_switch.ambr +++ b/tests/components/cambridge_audio/snapshots/test_switch.ambr @@ -97,3 +97,52 @@ 'state': 'off', }) # --- +# name: test_all_entities[switch.cambridge_audio_cxnv2_room_correction-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': , + 'entity_id': 'switch.cambridge_audio_cxnv2_room_correction', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Room correction', + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Room correction', + 'platform': 'cambridge_audio', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'room_correction', + 'unique_id': '0020c2d8-room_correction', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_entities[switch.cambridge_audio_cxnv2_room_correction-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Cambridge Audio CXNv2 Room correction', + }), + 'context': , + 'entity_id': 'switch.cambridge_audio_cxnv2_room_correction', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# ---