From 1e9a0c0609dfe6983fd90327a6ffc2fedfaf1d0f Mon Sep 17 00:00:00 2001 From: Jeff Irion Date: Wed, 19 Jan 2022 08:55:46 -0800 Subject: [PATCH] Fix the Android TV volume mute service (#64403) * Fix the Android TV volume mute service * Update homeassistant/components/androidtv/media_player.py Co-authored-by: Martin Hjelmare * assert mute_volume.called Co-authored-by: Martin Hjelmare --- .../components/androidtv/media_player.py | 6 ++- .../components/androidtv/test_media_player.py | 50 ++++++++++++++++--- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/androidtv/media_player.py b/homeassistant/components/androidtv/media_player.py index 67da914a06c..03b1e679961 100644 --- a/homeassistant/components/androidtv/media_player.py +++ b/homeassistant/components/androidtv/media_player.py @@ -609,7 +609,11 @@ class AndroidTVDevice(ADBDevice): @adb_decorator() async def async_mute_volume(self, mute): """Mute the volume.""" - await self.aftv.mute_volume() + is_muted = await self.aftv.is_volume_muted() + + # `None` indicates that the muted status could not be determined + if is_muted is not None and is_muted != mute: + await self.aftv.mute_volume() @adb_decorator() async def async_set_volume_level(self, volume): diff --git a/tests/components/androidtv/test_media_player.py b/tests/components/androidtv/test_media_player.py index 98f63dd8b4c..5326e48f7b9 100644 --- a/tests/components/androidtv/test_media_player.py +++ b/tests/components/androidtv/test_media_player.py @@ -1111,13 +1111,6 @@ async def test_services_androidtv(hass): await _test_service( hass, entity_id, SERVICE_VOLUME_DOWN, "volume_down", return_value=0.1 ) - await _test_service( - hass, - entity_id, - SERVICE_VOLUME_MUTE, - "mute_volume", - {ATTR_MEDIA_VOLUME_MUTED: False}, - ) await _test_service( hass, entity_id, @@ -1152,6 +1145,49 @@ async def test_services_firetv(hass): await _test_service(hass, entity_id, SERVICE_TURN_ON, "adb_shell") +async def test_volume_mute(hass): + """Test the volume mute service.""" + patch_key, entity_id, config_entry = _setup(CONFIG_ANDROIDTV_ADB_SERVER) + config_entry.add_to_hass(hass) + + with patchers.PATCH_ADB_DEVICE_TCP, patchers.patch_connect(True)[patch_key]: + with patchers.patch_shell(SHELL_RESPONSE_OFF)[patch_key]: + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + with patchers.patch_shell(SHELL_RESPONSE_STANDBY)[patch_key]: + service_data = {ATTR_ENTITY_ID: entity_id, ATTR_MEDIA_VOLUME_MUTED: True} + with patch( + "androidtv.androidtv.androidtv_async.AndroidTVAsync.mute_volume", + return_value=None, + ) as mute_volume: + # Don't send the mute key if the volume is already muted + with patch( + "androidtv.androidtv.androidtv_async.AndroidTVAsync.is_volume_muted", + return_value=True, + ): + await hass.services.async_call( + MP_DOMAIN, + SERVICE_VOLUME_MUTE, + service_data=service_data, + blocking=True, + ) + assert not mute_volume.called + + # Send the mute key because the volume is not already muted + with patch( + "androidtv.androidtv.androidtv_async.AndroidTVAsync.is_volume_muted", + return_value=False, + ): + await hass.services.async_call( + MP_DOMAIN, + SERVICE_VOLUME_MUTE, + service_data=service_data, + blocking=True, + ) + assert mute_volume.called + + async def test_connection_closed_on_ha_stop(hass): """Test that the ADB socket connection is closed when HA stops.""" patch_key, entity_id, config_entry = _setup(CONFIG_ANDROIDTV_ADB_SERVER)