Skip to content

Commit 96e26e6

Browse files
committed
add sound mode support for devices that support it
1 parent d5f4dfd commit 96e26e6

File tree

4 files changed

+71
-8
lines changed

4 files changed

+71
-8
lines changed

homeassistant/components/vizio/media_player.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from homeassistant.components.media_player import (
1111
DEVICE_CLASS_SPEAKER,
12+
SUPPORT_SELECT_SOUND_MODE,
1213
MediaPlayerDevice,
1314
)
1415
from homeassistant.config_entries import ConfigEntry
@@ -133,6 +134,8 @@ def __init__(
133134
self._current_input = None
134135
self._current_app = None
135136
self._current_app_config = None
137+
self._current_sound_mode = None
138+
self._available_sound_modes = None
136139
self._available_inputs = []
137140
self._available_apps = []
138141
self._conf_apps = config_entry.options.get(CONF_APPS, {})
@@ -191,17 +194,31 @@ async def async_update(self) -> None:
191194
self._current_app = None
192195
self._current_app_config = None
193196
self._available_apps = None
197+
self._current_sound_mode = None
198+
self._available_sound_modes = None
194199
return
195200

196201
self._state = STATE_ON
197202

198-
audio_settings = await self._device.get_all_audio_settings(
199-
log_api_exception=False
203+
audio_settings = await self._device.get_all_settings(
204+
"audio", log_api_exception=False
200205
)
201206
if audio_settings is not None:
202207
self._volume_level = float(audio_settings["volume"]) / self._max_volume
203208
self._is_muted = audio_settings["mute"].lower() == "on"
204209

210+
if "eq" in audio_settings:
211+
self._supported_commands = (
212+
SUPPORTED_COMMANDS[self._device_class] | SUPPORT_SELECT_SOUND_MODE
213+
)
214+
self._current_sound_mode = audio_settings["eq"]
215+
if self._available_sound_modes is None:
216+
self._available_sound_modes = await self._device.get_setting_options(
217+
"audio", "eq"
218+
)
219+
else:
220+
self._supported_commands = SUPPORTED_COMMANDS[self._device_class]
221+
205222
input_ = await self._device.get_current_input(log_api_exception=False)
206223
if input_ is not None:
207224
self._current_input = input_
@@ -367,7 +384,7 @@ def unique_id(self) -> str:
367384
return self._config_entry.unique_id
368385

369386
@property
370-
def device_info(self):
387+
def device_info(self) -> Dict[str, Any]:
371388
"""Return device registry information."""
372389
return {
373390
"identifiers": {(DOMAIN, self._config_entry.unique_id)},
@@ -378,10 +395,25 @@ def device_info(self):
378395
}
379396

380397
@property
381-
def device_class(self):
398+
def device_class(self) -> str:
382399
"""Return device class for entity."""
383400
return self._device_class
384401

402+
@property
403+
def sound_mode(self) -> Optional[str]:
404+
"""Name of the current sound mode."""
405+
return self._current_sound_mode
406+
407+
@property
408+
def sound_mode_list(self) -> Optional[List[str]]:
409+
"""List of available sound modes."""
410+
return self._available_sound_modes
411+
412+
async def async_select_sound_mode(self, sound_mode):
413+
"""Select sound mode."""
414+
if sound_mode in self._available_sound_modes:
415+
await self._device.set_setting("audio", "eq", sound_mode)
416+
385417
async def async_turn_on(self) -> None:
386418
"""Turn the device on."""
387419
await self._device.pow_on()

tests/components/vizio/conftest.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
APP_LIST,
99
CH_TYPE,
1010
CURRENT_APP_CONFIG,
11+
CURRENT_EQ,
1112
CURRENT_INPUT,
13+
EQ_LIST,
1214
INPUT_LIST,
1315
INPUT_LIST_WITH_APPS,
1416
MODEL,
@@ -135,11 +137,15 @@ def vizio_update_fixture():
135137
"homeassistant.components.vizio.media_player.VizioAsync.can_connect_with_auth_check",
136138
return_value=True,
137139
), patch(
138-
"homeassistant.components.vizio.media_player.VizioAsync.get_all_audio_settings",
140+
"homeassistant.components.vizio.media_player.VizioAsync.get_all_settings",
139141
return_value={
140142
"volume": int(MAX_VOLUME[DEVICE_CLASS_SPEAKER] / 2),
143+
"eq": CURRENT_EQ,
141144
"mute": "Off",
142145
},
146+
), patch(
147+
"homeassistant.components.vizio.media_player.VizioAsync.get_setting_options",
148+
return_value=EQ_LIST,
143149
), patch(
144150
"homeassistant.components.vizio.media_player.VizioAsync.get_current_input",
145151
return_value=CURRENT_INPUT,

tests/components/vizio/const.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ def __init__(self, auth_token: str) -> None:
6464
self.auth_token = auth_token
6565

6666

67+
CURRENT_EQ = "Music"
68+
EQ_LIST = ["Music", "Movie"]
69+
6770
CURRENT_INPUT = "HDMI"
6871
INPUT_LIST = ["HDMI", "USB", "Bluetooth", "AUX"]
6972

tests/components/vizio/test_media_player.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
ATTR_INPUT_SOURCE,
2222
ATTR_MEDIA_VOLUME_LEVEL,
2323
ATTR_MEDIA_VOLUME_MUTED,
24+
ATTR_SOUND_MODE,
2425
DEVICE_CLASS_SPEAKER,
2526
DEVICE_CLASS_TV,
2627
DOMAIN as MP_DOMAIN,
2728
SERVICE_MEDIA_NEXT_TRACK,
2829
SERVICE_MEDIA_PREVIOUS_TRACK,
30+
SERVICE_SELECT_SOUND_MODE,
2931
SERVICE_SELECT_SOURCE,
3032
SERVICE_TURN_OFF,
3133
SERVICE_TURN_ON,
@@ -58,9 +60,11 @@
5860
APP_LIST,
5961
CURRENT_APP,
6062
CURRENT_APP_CONFIG,
63+
CURRENT_EQ,
6164
CURRENT_INPUT,
6265
CUSTOM_CONFIG,
6366
ENTITY_ID,
67+
EQ_LIST,
6468
INPUT_LIST,
6569
INPUT_LIST_WITH_APPS,
6670
MOCK_SPEAKER_APPS_FAILURE,
@@ -99,17 +103,29 @@ async def _test_setup(
99103
data=vol.Schema(VIZIO_SCHEMA)(MOCK_SPEAKER_CONFIG),
100104
unique_id=UNIQUE_ID,
101105
)
106+
dict_to_return = {
107+
"volume": int(MAX_VOLUME[vizio_device_class] / 2),
108+
"mute": "Off",
109+
"eq": CURRENT_EQ,
110+
}
102111
else:
103112
vizio_device_class = VIZIO_DEVICE_CLASS_TV
104113
config_entry = MockConfigEntry(
105114
domain=DOMAIN,
106115
data=vol.Schema(VIZIO_SCHEMA)(MOCK_USER_VALID_TV_CONFIG),
107116
unique_id=UNIQUE_ID,
108117
)
118+
dict_to_return = {
119+
"volume": int(MAX_VOLUME[vizio_device_class] / 2),
120+
"mute": "Off",
121+
}
109122

110123
with patch(
111-
"homeassistant.components.vizio.media_player.VizioAsync.get_all_audio_settings",
112-
return_value={"volume": int(MAX_VOLUME[vizio_device_class] / 2), "mute": "Off"},
124+
"homeassistant.components.vizio.media_player.VizioAsync.get_all_settings",
125+
return_value=dict_to_return,
126+
), patch(
127+
"homeassistant.components.vizio.media_player.VizioAsync.get_setting_options",
128+
return_value=EQ_LIST,
113129
), patch(
114130
"homeassistant.components.vizio.media_player.VizioAsync.get_power_state",
115131
return_value=vizio_power_state,
@@ -130,6 +146,9 @@ async def _test_setup(
130146
assert attr["source"] == CURRENT_INPUT
131147
if ha_device_class == DEVICE_CLASS_SPEAKER:
132148
assert not service_call.called
149+
assert "sound_mode" in attr
150+
else:
151+
assert "sound_mode" not in attr
133152
assert (
134153
attr["volume_level"]
135154
== float(int(MAX_VOLUME[vizio_device_class] / 2))
@@ -149,7 +168,7 @@ async def _test_setup_with_apps(
149168
)
150169

151170
with patch(
152-
"homeassistant.components.vizio.media_player.VizioAsync.get_all_audio_settings",
171+
"homeassistant.components.vizio.media_player.VizioAsync.get_all_settings",
153172
return_value={
154173
"volume": int(MAX_VOLUME[VIZIO_DEVICE_CLASS_TV] / 2),
155174
"mute": "Off",
@@ -351,6 +370,9 @@ async def test_services(
351370
)
352371
await _test_service(hass, "ch_up", SERVICE_MEDIA_NEXT_TRACK, None)
353372
await _test_service(hass, "ch_down", SERVICE_MEDIA_PREVIOUS_TRACK, None)
373+
await _test_service(
374+
hass, "set_setting", SERVICE_SELECT_SOUND_MODE, {ATTR_SOUND_MODE: "Music"}
375+
)
354376

355377

356378
async def test_options_update(

0 commit comments

Comments
 (0)