From 6e081eb5aba927a806a723261225b095e766a1dc Mon Sep 17 00:00:00 2001 From: Christopher Roberts Date: Fri, 26 Jan 2024 03:44:58 +0000 Subject: [PATCH 1/2] Add play/pause functionality to vizio integration Leverages existing pyvizio functionality. My impression is that it also works for soundbars based on https://github.com/exiva/Vizio_SmartCast_API/issues/19. --- homeassistant/components/vizio/const.py | 4 +++- homeassistant/components/vizio/media_player.py | 8 ++++++++ tests/components/vizio/test_media_player.py | 4 ++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/vizio/const.py b/homeassistant/components/vizio/const.py index 9615032c0973cc..4ab619730499ec 100644 --- a/homeassistant/components/vizio/const.py +++ b/homeassistant/components/vizio/const.py @@ -55,7 +55,9 @@ } COMMON_SUPPORTED_COMMANDS = ( - MediaPlayerEntityFeature.SELECT_SOURCE + MediaPlayerEntityFeature.PAUSE + | MediaPlayerEntityFeature.PLAY + | MediaPlayerEntityFeature.SELECT_SOURCE | MediaPlayerEntityFeature.TURN_ON | MediaPlayerEntityFeature.TURN_OFF | MediaPlayerEntityFeature.VOLUME_MUTE diff --git a/homeassistant/components/vizio/media_player.py b/homeassistant/components/vizio/media_player.py index e3de3caa99daa0..d8862f87c16ab0 100644 --- a/homeassistant/components/vizio/media_player.py +++ b/homeassistant/components/vizio/media_player.py @@ -470,3 +470,11 @@ async def async_set_volume_level(self, volume: float) -> None: num = int(self._max_volume * (self._attr_volume_level - volume)) await self._device.vol_down(num=num, log_api_exception=False) self._attr_volume_level = volume + + async def async_media_play(self) -> None: + """Play whatever media is currently active.""" + await self._device.play(log_api_exception=False) + + async def async_media_pause(self) -> None: + """Pause whatever media is currently active.""" + await self._device.pause(log_api_exception=False) diff --git a/tests/components/vizio/test_media_player.py b/tests/components/vizio/test_media_player.py index 142c5f74b84065..eaeff69dc115df 100644 --- a/tests/components/vizio/test_media_player.py +++ b/tests/components/vizio/test_media_player.py @@ -27,6 +27,8 @@ ATTR_SOUND_MODE, DOMAIN as MP_DOMAIN, SERVICE_MEDIA_NEXT_TRACK, + SERVICE_MEDIA_PAUSE, + SERVICE_MEDIA_PLAY, SERVICE_MEDIA_PREVIOUS_TRACK, SERVICE_SELECT_SOUND_MODE, SERVICE_SELECT_SOURCE, @@ -438,6 +440,8 @@ async def test_services( "eq", "Music", ) + await _test_service(hass, MP_DOMAIN, "play", SERVICE_MEDIA_PLAY, None) + await _test_service(hass, MP_DOMAIN, "pause", SERVICE_MEDIA_PAUSE, None) async def test_options_update( From 5eb2ec3dc2a5fe01f13897528fe6807640db6013 Mon Sep 17 00:00:00 2001 From: Christopher Roberts Date: Fri, 26 Jan 2024 06:46:17 +0000 Subject: [PATCH 2/2] Set vizio assumed_state to True The Vizio API is only capable of indicating whether the device is on or off and not whether it's playing/paused/idle. Setting assumed_state to True gives us separate Play and Pause buttons versus the (useless) merged Play/Pause button we would get otherwise. --- homeassistant/components/vizio/media_player.py | 1 + 1 file changed, 1 insertion(+) diff --git a/homeassistant/components/vizio/media_player.py b/homeassistant/components/vizio/media_player.py index d8862f87c16ab0..5fa712f451a5af 100644 --- a/homeassistant/components/vizio/media_player.py +++ b/homeassistant/components/vizio/media_player.py @@ -155,6 +155,7 @@ def __init__( ) self._device = device self._max_volume = float(self._device.get_max_volume()) + self._attr_assumed_state = True # Entity class attributes that will change with each update (we only include # the ones that are initialized differently from the defaults)