mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Fix toggle and media_play_pause post async (#6291)
This commit is contained in:
parent
0fa259089d
commit
faf8bbcf13
@ -757,18 +757,15 @@ class MediaPlayerDevice(Entity):
|
|||||||
"""Boolean if clear playlist command supported."""
|
"""Boolean if clear playlist command supported."""
|
||||||
return bool(self.supported_features & SUPPORT_CLEAR_PLAYLIST)
|
return bool(self.supported_features & SUPPORT_CLEAR_PLAYLIST)
|
||||||
|
|
||||||
def toggle(self):
|
|
||||||
"""Toggle the power on the media player."""
|
|
||||||
if self.state in [STATE_OFF, STATE_IDLE]:
|
|
||||||
self.turn_on()
|
|
||||||
else:
|
|
||||||
self.turn_off()
|
|
||||||
|
|
||||||
def async_toggle(self):
|
def async_toggle(self):
|
||||||
"""Toggle the power on the media player.
|
"""Toggle the power on the media player.
|
||||||
|
|
||||||
This method must be run in the event loop and returns a coroutine.
|
This method must be run in the event loop and returns a coroutine.
|
||||||
"""
|
"""
|
||||||
|
if hasattr(self, 'toggle'):
|
||||||
|
# pylint: disable=no-member
|
||||||
|
return self.hass.loop.run_in_executor(None, self.toggle)
|
||||||
|
|
||||||
if self.state in [STATE_OFF, STATE_IDLE]:
|
if self.state in [STATE_OFF, STATE_IDLE]:
|
||||||
return self.async_turn_on()
|
return self.async_turn_on()
|
||||||
else:
|
else:
|
||||||
@ -804,18 +801,15 @@ class MediaPlayerDevice(Entity):
|
|||||||
yield from self.async_set_volume_level(
|
yield from self.async_set_volume_level(
|
||||||
max(0, self.volume_level - .1))
|
max(0, self.volume_level - .1))
|
||||||
|
|
||||||
def media_play_pause(self):
|
|
||||||
"""Play or pause the media player."""
|
|
||||||
if self.state == STATE_PLAYING:
|
|
||||||
self.media_pause()
|
|
||||||
else:
|
|
||||||
self.media_play()
|
|
||||||
|
|
||||||
def async_media_play_pause(self):
|
def async_media_play_pause(self):
|
||||||
"""Play or pause the media player.
|
"""Play or pause the media player.
|
||||||
|
|
||||||
This method must be run in the event loop and returns a coroutine.
|
This method must be run in the event loop and returns a coroutine.
|
||||||
"""
|
"""
|
||||||
|
if hasattr(self, 'media_play_pause'):
|
||||||
|
# pylint: disable=no-member
|
||||||
|
return self.hass.loop.run_in_executor(None, self.media_play_pause)
|
||||||
|
|
||||||
if self.state == STATE_PLAYING:
|
if self.state == STATE_PLAYING:
|
||||||
return self.async_media_pause()
|
return self.async_media_pause()
|
||||||
else:
|
else:
|
||||||
|
@ -3,6 +3,8 @@ import unittest
|
|||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
import homeassistant.components.media_player as mp
|
import homeassistant.components.media_player as mp
|
||||||
|
from homeassistant.const import (
|
||||||
|
STATE_PLAYING, STATE_PAUSED, STATE_ON, STATE_OFF, STATE_IDLE)
|
||||||
from homeassistant.util.async import run_coroutine_threadsafe
|
from homeassistant.util.async import run_coroutine_threadsafe
|
||||||
|
|
||||||
from tests.common import get_test_home_assistant
|
from tests.common import get_test_home_assistant
|
||||||
@ -15,6 +17,12 @@ class AsyncMediaPlayer(mp.MediaPlayerDevice):
|
|||||||
"""Initialize the test media player."""
|
"""Initialize the test media player."""
|
||||||
self.hass = hass
|
self.hass = hass
|
||||||
self._volume = 0
|
self._volume = 0
|
||||||
|
self._state = STATE_OFF
|
||||||
|
|
||||||
|
@property
|
||||||
|
def state(self):
|
||||||
|
"""State of the player."""
|
||||||
|
return self._state
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def volume_level(self):
|
def volume_level(self):
|
||||||
@ -26,6 +34,26 @@ class AsyncMediaPlayer(mp.MediaPlayerDevice):
|
|||||||
"""Set volume level, range 0..1."""
|
"""Set volume level, range 0..1."""
|
||||||
self._volume = volume
|
self._volume = volume
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def async_media_play(self):
|
||||||
|
"""Send play command."""
|
||||||
|
self._state = STATE_PLAYING
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def async_media_pause(self):
|
||||||
|
"""Send pause command."""
|
||||||
|
self._state = STATE_PAUSED
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def async_turn_on(self):
|
||||||
|
"""Turn the media player on."""
|
||||||
|
self._state = STATE_ON
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def async_turn_off(self):
|
||||||
|
"""Turn the media player off."""
|
||||||
|
self._state = STATE_OFF
|
||||||
|
|
||||||
|
|
||||||
class SyncMediaPlayer(mp.MediaPlayerDevice):
|
class SyncMediaPlayer(mp.MediaPlayerDevice):
|
||||||
"""Sync media player test class."""
|
"""Sync media player test class."""
|
||||||
@ -34,6 +62,12 @@ class SyncMediaPlayer(mp.MediaPlayerDevice):
|
|||||||
"""Initialize the test media player."""
|
"""Initialize the test media player."""
|
||||||
self.hass = hass
|
self.hass = hass
|
||||||
self._volume = 0
|
self._volume = 0
|
||||||
|
self._state = STATE_OFF
|
||||||
|
|
||||||
|
@property
|
||||||
|
def state(self):
|
||||||
|
"""State of the player."""
|
||||||
|
return self._state
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def volume_level(self):
|
def volume_level(self):
|
||||||
@ -54,6 +88,36 @@ class SyncMediaPlayer(mp.MediaPlayerDevice):
|
|||||||
if self.volume_level > 0:
|
if self.volume_level > 0:
|
||||||
self.set_volume_level(max(0, self.volume_level - .2))
|
self.set_volume_level(max(0, self.volume_level - .2))
|
||||||
|
|
||||||
|
def media_play_pause(self):
|
||||||
|
"""Play or pause the media player."""
|
||||||
|
if self._state == STATE_PLAYING:
|
||||||
|
self._state = STATE_PAUSED
|
||||||
|
else:
|
||||||
|
self._state = STATE_PLAYING
|
||||||
|
|
||||||
|
def toggle(self):
|
||||||
|
"""Toggle the power on the media player."""
|
||||||
|
if self._state in [STATE_OFF, STATE_IDLE]:
|
||||||
|
self._state = STATE_ON
|
||||||
|
else:
|
||||||
|
self._state = STATE_OFF
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def async_media_play_pause(self):
|
||||||
|
"""Create a coroutine to wrap the future returned by ABC.
|
||||||
|
|
||||||
|
This allows the run_coroutine_threadsafe helper to be used.
|
||||||
|
"""
|
||||||
|
yield from super().async_media_play_pause()
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def async_toggle(self):
|
||||||
|
"""Create a coroutine to wrap the future returned by ABC.
|
||||||
|
|
||||||
|
This allows the run_coroutine_threadsafe helper to be used.
|
||||||
|
"""
|
||||||
|
yield from super().async_toggle()
|
||||||
|
|
||||||
|
|
||||||
class TestAsyncMediaPlayer(unittest.TestCase):
|
class TestAsyncMediaPlayer(unittest.TestCase):
|
||||||
"""Test the media_player module."""
|
"""Test the media_player module."""
|
||||||
@ -87,6 +151,26 @@ class TestAsyncMediaPlayer(unittest.TestCase):
|
|||||||
self.player.async_volume_down(), self.hass.loop).result()
|
self.player.async_volume_down(), self.hass.loop).result()
|
||||||
self.assertEqual(self.player.volume_level, 0.4)
|
self.assertEqual(self.player.volume_level, 0.4)
|
||||||
|
|
||||||
|
def test_media_play_pause(self):
|
||||||
|
"""Test the media_play_pause helper function."""
|
||||||
|
self.assertEqual(self.player.state, STATE_OFF)
|
||||||
|
run_coroutine_threadsafe(
|
||||||
|
self.player.async_media_play_pause(), self.hass.loop).result()
|
||||||
|
self.assertEqual(self.player.state, STATE_PLAYING)
|
||||||
|
run_coroutine_threadsafe(
|
||||||
|
self.player.async_media_play_pause(), self.hass.loop).result()
|
||||||
|
self.assertEqual(self.player.state, STATE_PAUSED)
|
||||||
|
|
||||||
|
def test_toggle(self):
|
||||||
|
"""Test the toggle helper function."""
|
||||||
|
self.assertEqual(self.player.state, STATE_OFF)
|
||||||
|
run_coroutine_threadsafe(
|
||||||
|
self.player.async_toggle(), self.hass.loop).result()
|
||||||
|
self.assertEqual(self.player.state, STATE_ON)
|
||||||
|
run_coroutine_threadsafe(
|
||||||
|
self.player.async_toggle(), self.hass.loop).result()
|
||||||
|
self.assertEqual(self.player.state, STATE_OFF)
|
||||||
|
|
||||||
|
|
||||||
class TestSyncMediaPlayer(unittest.TestCase):
|
class TestSyncMediaPlayer(unittest.TestCase):
|
||||||
"""Test the media_player module."""
|
"""Test the media_player module."""
|
||||||
@ -117,3 +201,23 @@ class TestSyncMediaPlayer(unittest.TestCase):
|
|||||||
run_coroutine_threadsafe(
|
run_coroutine_threadsafe(
|
||||||
self.player.async_volume_down(), self.hass.loop).result()
|
self.player.async_volume_down(), self.hass.loop).result()
|
||||||
self.assertEqual(self.player.volume_level, 0.3)
|
self.assertEqual(self.player.volume_level, 0.3)
|
||||||
|
|
||||||
|
def test_media_play_pause(self):
|
||||||
|
"""Test the media_play_pause helper function."""
|
||||||
|
self.assertEqual(self.player.state, STATE_OFF)
|
||||||
|
run_coroutine_threadsafe(
|
||||||
|
self.player.async_media_play_pause(), self.hass.loop).result()
|
||||||
|
self.assertEqual(self.player.state, STATE_PLAYING)
|
||||||
|
run_coroutine_threadsafe(
|
||||||
|
self.player.async_media_play_pause(), self.hass.loop).result()
|
||||||
|
self.assertEqual(self.player.state, STATE_PAUSED)
|
||||||
|
|
||||||
|
def test_toggle(self):
|
||||||
|
"""Test the toggle helper function."""
|
||||||
|
self.assertEqual(self.player.state, STATE_OFF)
|
||||||
|
run_coroutine_threadsafe(
|
||||||
|
self.player.async_toggle(), self.hass.loop).result()
|
||||||
|
self.assertEqual(self.player.state, STATE_ON)
|
||||||
|
run_coroutine_threadsafe(
|
||||||
|
self.player.async_toggle(), self.hass.loop).result()
|
||||||
|
self.assertEqual(self.player.state, STATE_OFF)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user