From dbb79e2937381dc4284f8ab1cc3c0f720ba762b6 Mon Sep 17 00:00:00 2001 From: jjlawren Date: Fri, 18 Mar 2022 12:12:10 -0500 Subject: [PATCH] Add support for Sonos subwoofer gain controls (#68334) --- homeassistant/components/sonos/number.py | 1 + homeassistant/components/sonos/speaker.py | 3 ++- tests/components/sonos/conftest.py | 1 + tests/components/sonos/test_number.py | 17 +++++++++++++++-- 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/sonos/number.py b/homeassistant/components/sonos/number.py index c9b8ec47583..b0a10690ce6 100644 --- a/homeassistant/components/sonos/number.py +++ b/homeassistant/components/sonos/number.py @@ -19,6 +19,7 @@ LEVEL_TYPES = { "audio_delay": (0, 5), "bass": (-10, 10), "treble": (-10, 10), + "sub_gain": (-15, 15), } _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/sonos/speaker.py b/homeassistant/components/sonos/speaker.py index 3a2bac51684..98c6d3bba26 100644 --- a/homeassistant/components/sonos/speaker.py +++ b/homeassistant/components/sonos/speaker.py @@ -150,6 +150,7 @@ class SonosSpeaker: self.dialog_level: bool | None = None self.night_mode: bool | None = None self.sub_enabled: bool | None = None + self.sub_gain: int | None = None self.surround_enabled: bool | None = None # Misc features @@ -490,7 +491,7 @@ class SonosSpeaker: if bool_var in variables: setattr(self, bool_var, variables[bool_var] == "1") - for int_var in ("audio_delay", "bass", "treble"): + for int_var in ("audio_delay", "bass", "treble", "sub_gain"): if int_var in variables: setattr(self, int_var, variables[int_var]) diff --git a/tests/components/sonos/conftest.py b/tests/components/sonos/conftest.py index 8e133f76ac1..ebff338b39a 100644 --- a/tests/components/sonos/conftest.py +++ b/tests/components/sonos/conftest.py @@ -114,6 +114,7 @@ def soco_fixture( mock_soco.treble = -1 mock_soco.mic_enabled = False mock_soco.sub_enabled = False + mock_soco.sub_gain = 5 mock_soco.surround_enabled = True mock_soco.soundbar_audio_input_format = "Dolby 5.1" mock_soco.get_battery_info.return_value = battery_info diff --git a/tests/components/sonos/test_number.py b/tests/components/sonos/test_number.py index 91c00e05390..5829a7a6724 100644 --- a/tests/components/sonos/test_number.py +++ b/tests/components/sonos/test_number.py @@ -6,8 +6,8 @@ from homeassistant.const import ATTR_ENTITY_ID from homeassistant.helpers import entity_registry as ent_reg -async def test_audio_input_sensor(hass, async_autosetup_sonos, soco): - """Test audio input sensor.""" +async def test_number_entities(hass, async_autosetup_sonos, soco): + """Test number entities.""" entity_registry = ent_reg.async_get(hass) bass_number = entity_registry.entities["number.zone_a_bass"] @@ -30,3 +30,16 @@ async def test_audio_input_sensor(hass, async_autosetup_sonos, soco): blocking=True, ) assert mock_audio_delay.called_with(3) + + sub_gain_number = entity_registry.entities["number.zone_a_sub_gain"] + sub_gain_state = hass.states.get(sub_gain_number.entity_id) + assert sub_gain_state.state == "5" + + with patch("soco.SoCo.sub_gain") as mock_sub_gain: + await hass.services.async_call( + NUMBER_DOMAIN, + SERVICE_SET_VALUE, + {ATTR_ENTITY_ID: sub_gain_number.entity_id, "value": -8}, + blocking=True, + ) + assert mock_sub_gain.called_with(-8)