From 4f96f05a755166b2d59b93135fdd4b540cd22f52 Mon Sep 17 00:00:00 2001 From: Graham Rogers Date: Mon, 2 Aug 2021 14:10:56 +0100 Subject: [PATCH] Improve Universal media player toggle default behavior (#49395) Before it could not be overridden and the default behavior meant nothing was called when all children were off, so it could not be used to turn on the media player. The new default behavior is to delegate to `turn_on` and `turn_off` instead, which is more likely to be the expected behavior. --- homeassistant/components/universal/media_player.py | 6 +++++- tests/components/universal/test_media_player.py | 3 ++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/universal/media_player.py b/homeassistant/components/universal/media_player.py index a2981852dc1..0ebd9eaf890 100644 --- a/homeassistant/components/universal/media_player.py +++ b/homeassistant/components/universal/media_player.py @@ -612,7 +612,11 @@ class UniversalMediaPlayer(MediaPlayerEntity): async def async_toggle(self): """Toggle the power on the media player.""" - await self._async_call_service(SERVICE_TOGGLE, allow_override=True) + if SERVICE_TOGGLE in self._cmds: + await self._async_call_service(SERVICE_TOGGLE, allow_override=True) + else: + # Delegate to turn_on or turn_off by default + await super().async_toggle() async def async_update(self): """Update state in HA.""" diff --git a/tests/components/universal/test_media_player.py b/tests/components/universal/test_media_player.py index 54617a61348..737d37052c2 100644 --- a/tests/components/universal/test_media_player.py +++ b/tests/components/universal/test_media_player.py @@ -998,7 +998,8 @@ class TestMediaPlayer(unittest.TestCase): assert len(self.mock_mp_2.service_calls["shuffle_set"]) == 1 asyncio.run_coroutine_threadsafe(ump.async_toggle(), self.hass.loop).result() - assert len(self.mock_mp_2.service_calls["toggle"]) == 1 + # Delegate to turn_off + assert len(self.mock_mp_2.service_calls["turn_off"]) == 2 def test_service_call_to_command(self): """Test service call to command."""