Make media_player.toggle turn on a standby device (#74221)

This commit is contained in:
Pierre Ståhl 2022-06-30 18:59:46 +02:00 committed by GitHub
parent d38e8e213a
commit de700e7859
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 2 deletions

View file

@ -52,6 +52,7 @@ from homeassistant.const import (
STATE_IDLE,
STATE_OFF,
STATE_PLAYING,
STATE_STANDBY,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
@ -888,7 +889,7 @@ class MediaPlayerEntity(Entity):
await self.hass.async_add_executor_job(self.toggle)
return
if self.state in (STATE_OFF, STATE_IDLE):
if self.state in (STATE_OFF, STATE_IDLE, STATE_STANDBY):
await self.async_turn_on()
else:
await self.async_turn_off()

View file

@ -8,6 +8,7 @@ from homeassistant.const import (
STATE_ON,
STATE_PAUSED,
STATE_PLAYING,
STATE_STANDBY,
)
@ -79,9 +80,13 @@ class ExtendedMediaPlayer(mp.MediaPlayerEntity):
"""Turn off state."""
self._state = STATE_OFF
def standby(self):
"""Put device in standby."""
self._state = STATE_STANDBY
def toggle(self):
"""Toggle the power on the media player."""
if self._state in [STATE_OFF, STATE_IDLE]:
if self._state in [STATE_OFF, STATE_IDLE, STATE_STANDBY]:
self._state = STATE_ON
else:
self._state = STATE_OFF
@ -138,6 +143,10 @@ class SimpleMediaPlayer(mp.MediaPlayerEntity):
"""Turn off state."""
self._state = STATE_OFF
def standby(self):
"""Put device in standby."""
self._state = STATE_STANDBY
@pytest.fixture(params=[ExtendedMediaPlayer, SimpleMediaPlayer])
def player(hass, request):
@ -188,3 +197,7 @@ async def test_toggle(player):
assert player.state == STATE_ON
await player.async_toggle()
assert player.state == STATE_OFF
player.standby()
assert player.state == STATE_STANDBY
await player.async_toggle()
assert player.state == STATE_ON