Add is_volume_muted property to vizio integration (#32332)

* add is_muted property and update tests

* black

* manually set is_muted on async_mute_volume calls to set state early

* combine two lines into one

* set is_muted to None when device is not on
This commit is contained in:
Raman Gupta 2020-03-04 04:04:52 -05:00 committed by GitHub
parent 6a6bf517fe
commit 4f619691df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 9 deletions

View File

@ -230,7 +230,7 @@ class VizioConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
new_options.update(updated_options) new_options.update(updated_options)
self.hass.config_entries.async_update_entry( self.hass.config_entries.async_update_entry(
entry=entry, data=new_data, options=new_options, entry=entry, data=new_data, options=new_options
) )
return self.async_abort(reason="updated_entry") return self.async_abort(reason="updated_entry")

View File

@ -54,7 +54,7 @@ async def async_setup_entry(
# If config entry options not set up, set them up, otherwise assign values managed in options # If config entry options not set up, set them up, otherwise assign values managed in options
volume_step = config_entry.options.get( volume_step = config_entry.options.get(
CONF_VOLUME_STEP, config_entry.data.get(CONF_VOLUME_STEP, DEFAULT_VOLUME_STEP), CONF_VOLUME_STEP, config_entry.data.get(CONF_VOLUME_STEP, DEFAULT_VOLUME_STEP)
) )
params = {} params = {}
@ -107,6 +107,7 @@ class VizioDevice(MediaPlayerDevice):
self._state = None self._state = None
self._volume_level = None self._volume_level = None
self._volume_step = volume_step self._volume_step = volume_step
self._is_muted = None
self._current_input = None self._current_input = None
self._available_inputs = None self._available_inputs = None
self._device_class = device_class self._device_class = device_class
@ -145,15 +146,19 @@ class VizioDevice(MediaPlayerDevice):
if not is_on: if not is_on:
self._state = STATE_OFF self._state = STATE_OFF
self._volume_level = None self._volume_level = None
self._is_muted = None
self._current_input = None self._current_input = None
self._available_inputs = None self._available_inputs = None
return return
self._state = STATE_ON self._state = STATE_ON
volume = await self._device.get_current_volume(log_api_exception=False) audio_settings = await self._device.get_all_audio_settings(
if volume is not None: log_api_exception=False
self._volume_level = float(volume) / self._max_volume )
if audio_settings is not None:
self._volume_level = float(audio_settings["volume"]) / self._max_volume
self._is_muted = audio_settings["mute"].lower() == "on"
input_ = await self._device.get_current_input(log_api_exception=False) input_ = await self._device.get_current_input(log_api_exception=False)
if input_ is not None: if input_ is not None:
@ -224,6 +229,11 @@ class VizioDevice(MediaPlayerDevice):
"""Return the volume level of the device.""" """Return the volume level of the device."""
return self._volume_level return self._volume_level
@property
def is_volume_muted(self):
"""Boolean if volume is currently muted."""
return self._is_muted
@property @property
def source(self) -> str: def source(self) -> str:
"""Return current input of the device.""" """Return current input of the device."""
@ -272,8 +282,10 @@ class VizioDevice(MediaPlayerDevice):
"""Mute the volume.""" """Mute the volume."""
if mute: if mute:
await self._device.mute_on() await self._device.mute_on()
self._is_muted = True
else: else:
await self._device.mute_off() await self._device.mute_off()
self._is_muted = False
async def async_media_previous_track(self) -> None: async def async_media_previous_track(self) -> None:
"""Send previous channel command.""" """Send previous channel command."""

View File

@ -132,8 +132,11 @@ def vizio_update_fixture():
"homeassistant.components.vizio.media_player.VizioAsync.can_connect_with_auth_check", "homeassistant.components.vizio.media_player.VizioAsync.can_connect_with_auth_check",
return_value=True, return_value=True,
), patch( ), patch(
"homeassistant.components.vizio.media_player.VizioAsync.get_current_volume", "homeassistant.components.vizio.media_player.VizioAsync.get_all_audio_settings",
return_value=int(MAX_VOLUME[DEVICE_CLASS_SPEAKER] / 2), return_value={
"volume": int(MAX_VOLUME[DEVICE_CLASS_SPEAKER] / 2),
"mute": "Off",
},
), patch( ), patch(
"homeassistant.components.vizio.media_player.VizioAsync.get_current_input", "homeassistant.components.vizio.media_player.VizioAsync.get_current_input",
return_value=CURRENT_INPUT, return_value=CURRENT_INPUT,

View File

@ -69,8 +69,8 @@ async def _test_setup(
) )
with patch( with patch(
"homeassistant.components.vizio.media_player.VizioAsync.get_current_volume", "homeassistant.components.vizio.media_player.VizioAsync.get_all_audio_settings",
return_value=int(MAX_VOLUME[vizio_device_class] / 2), return_value={"volume": int(MAX_VOLUME[vizio_device_class] / 2), "mute": "Off"},
), patch( ), patch(
"homeassistant.components.vizio.media_player.VizioAsync.get_power_state", "homeassistant.components.vizio.media_player.VizioAsync.get_power_state",
return_value=vizio_power_state, return_value=vizio_power_state,