mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Convert media_player unittest tests to pytest style (#41950)
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
5bc4db5ef9
commit
211ef60d96
@ -1,6 +1,5 @@
|
|||||||
"""The tests for the Async Media player helper functions."""
|
"""The tests for the Async Media player helper functions."""
|
||||||
import asyncio
|
import pytest
|
||||||
import unittest
|
|
||||||
|
|
||||||
import homeassistant.components.media_player as mp
|
import homeassistant.components.media_player as mp
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
@ -11,62 +10,9 @@ from homeassistant.const import (
|
|||||||
STATE_PLAYING,
|
STATE_PLAYING,
|
||||||
)
|
)
|
||||||
|
|
||||||
from tests.common import get_test_home_assistant
|
|
||||||
|
|
||||||
|
class ExtendedMediaPlayer(mp.MediaPlayerEntity):
|
||||||
class AsyncMediaPlayer(mp.MediaPlayerEntity):
|
"""Media player test class."""
|
||||||
"""Async media player test class."""
|
|
||||||
|
|
||||||
def __init__(self, hass):
|
|
||||||
"""Initialize the test media player."""
|
|
||||||
self.hass = hass
|
|
||||||
self._volume = 0
|
|
||||||
self._state = STATE_OFF
|
|
||||||
|
|
||||||
@property
|
|
||||||
def state(self):
|
|
||||||
"""State of the player."""
|
|
||||||
return self._state
|
|
||||||
|
|
||||||
@property
|
|
||||||
def volume_level(self):
|
|
||||||
"""Volume level of the media player (0..1)."""
|
|
||||||
return self._volume
|
|
||||||
|
|
||||||
@property
|
|
||||||
def supported_features(self):
|
|
||||||
"""Flag media player features that are supported."""
|
|
||||||
return (
|
|
||||||
mp.const.SUPPORT_VOLUME_SET
|
|
||||||
| mp.const.SUPPORT_PLAY
|
|
||||||
| mp.const.SUPPORT_PAUSE
|
|
||||||
| mp.const.SUPPORT_TURN_OFF
|
|
||||||
| mp.const.SUPPORT_TURN_ON
|
|
||||||
)
|
|
||||||
|
|
||||||
async def async_set_volume_level(self, volume):
|
|
||||||
"""Set volume level, range 0..1."""
|
|
||||||
self._volume = volume
|
|
||||||
|
|
||||||
async def async_media_play(self):
|
|
||||||
"""Send play command."""
|
|
||||||
self._state = STATE_PLAYING
|
|
||||||
|
|
||||||
async def async_media_pause(self):
|
|
||||||
"""Send pause command."""
|
|
||||||
self._state = STATE_PAUSED
|
|
||||||
|
|
||||||
async def async_turn_on(self):
|
|
||||||
"""Turn the media player on."""
|
|
||||||
self._state = STATE_ON
|
|
||||||
|
|
||||||
async def async_turn_off(self):
|
|
||||||
"""Turn the media player off."""
|
|
||||||
self._state = STATE_OFF
|
|
||||||
|
|
||||||
|
|
||||||
class SyncMediaPlayer(mp.MediaPlayerEntity):
|
|
||||||
"""Sync media player test class."""
|
|
||||||
|
|
||||||
def __init__(self, hass):
|
def __init__(self, hass):
|
||||||
"""Initialize the test media player."""
|
"""Initialize the test media player."""
|
||||||
@ -103,12 +49,20 @@ class SyncMediaPlayer(mp.MediaPlayerEntity):
|
|||||||
def volume_up(self):
|
def volume_up(self):
|
||||||
"""Turn volume up for media player."""
|
"""Turn volume up for media player."""
|
||||||
if self.volume_level < 1:
|
if self.volume_level < 1:
|
||||||
self.set_volume_level(min(1, self.volume_level + 0.2))
|
self.set_volume_level(min(1, self.volume_level + 0.1))
|
||||||
|
|
||||||
def volume_down(self):
|
def volume_down(self):
|
||||||
"""Turn volume down for media player."""
|
"""Turn volume down for media player."""
|
||||||
if self.volume_level > 0:
|
if self.volume_level > 0:
|
||||||
self.set_volume_level(max(0, self.volume_level - 0.2))
|
self.set_volume_level(max(0, self.volume_level - 0.1))
|
||||||
|
|
||||||
|
def media_play(self):
|
||||||
|
"""Play the media player."""
|
||||||
|
self._state = STATE_PLAYING
|
||||||
|
|
||||||
|
def media_pause(self):
|
||||||
|
"""Plause the media player."""
|
||||||
|
self._state = STATE_PAUSED
|
||||||
|
|
||||||
def media_play_pause(self):
|
def media_play_pause(self):
|
||||||
"""Play or pause the media player."""
|
"""Play or pause the media player."""
|
||||||
@ -117,6 +71,14 @@ class SyncMediaPlayer(mp.MediaPlayerEntity):
|
|||||||
else:
|
else:
|
||||||
self._state = STATE_PLAYING
|
self._state = STATE_PLAYING
|
||||||
|
|
||||||
|
def turn_on(self):
|
||||||
|
"""Turn on state."""
|
||||||
|
self._state = STATE_ON
|
||||||
|
|
||||||
|
def turn_off(self):
|
||||||
|
"""Turn off state."""
|
||||||
|
self._state = STATE_OFF
|
||||||
|
|
||||||
def toggle(self):
|
def toggle(self):
|
||||||
"""Toggle the power on the media player."""
|
"""Toggle the power on the media player."""
|
||||||
if self._state in [STATE_OFF, STATE_IDLE]:
|
if self._state in [STATE_OFF, STATE_IDLE]:
|
||||||
@ -124,136 +86,105 @@ class SyncMediaPlayer(mp.MediaPlayerEntity):
|
|||||||
else:
|
else:
|
||||||
self._state = STATE_OFF
|
self._state = STATE_OFF
|
||||||
|
|
||||||
async 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.
|
class SimpleMediaPlayer(mp.MediaPlayerEntity):
|
||||||
"""
|
"""Media player test class."""
|
||||||
await super().async_media_play_pause()
|
|
||||||
|
|
||||||
async def async_toggle(self):
|
def __init__(self, hass):
|
||||||
"""Create a coroutine to wrap the future returned by ABC.
|
"""Initialize the test media player."""
|
||||||
|
self.hass = hass
|
||||||
|
self._volume = 0
|
||||||
|
self._state = STATE_OFF
|
||||||
|
|
||||||
This allows the run_coroutine_threadsafe helper to be used.
|
@property
|
||||||
"""
|
def state(self):
|
||||||
await super().async_toggle()
|
"""State of the player."""
|
||||||
|
return self._state
|
||||||
|
|
||||||
|
@property
|
||||||
|
def volume_level(self):
|
||||||
|
"""Volume level of the media player (0..1)."""
|
||||||
|
return self._volume
|
||||||
|
|
||||||
|
@property
|
||||||
|
def supported_features(self):
|
||||||
|
"""Flag media player features that are supported."""
|
||||||
|
return (
|
||||||
|
mp.const.SUPPORT_VOLUME_SET
|
||||||
|
| mp.const.SUPPORT_VOLUME_STEP
|
||||||
|
| mp.const.SUPPORT_PLAY
|
||||||
|
| mp.const.SUPPORT_PAUSE
|
||||||
|
| mp.const.SUPPORT_TURN_OFF
|
||||||
|
| mp.const.SUPPORT_TURN_ON
|
||||||
|
)
|
||||||
|
|
||||||
|
def set_volume_level(self, volume):
|
||||||
|
"""Set volume level, range 0..1."""
|
||||||
|
self._volume = volume
|
||||||
|
|
||||||
|
def media_play(self):
|
||||||
|
"""Play the media player."""
|
||||||
|
self._state = STATE_PLAYING
|
||||||
|
|
||||||
|
def media_pause(self):
|
||||||
|
"""Plause the media player."""
|
||||||
|
self._state = STATE_PAUSED
|
||||||
|
|
||||||
|
def turn_on(self):
|
||||||
|
"""Turn on state."""
|
||||||
|
self._state = STATE_ON
|
||||||
|
|
||||||
|
def turn_off(self):
|
||||||
|
"""Turn off state."""
|
||||||
|
self._state = STATE_OFF
|
||||||
|
|
||||||
|
|
||||||
class TestAsyncMediaPlayer(unittest.TestCase):
|
@pytest.fixture(params=[ExtendedMediaPlayer, SimpleMediaPlayer])
|
||||||
"""Test the media_player module."""
|
def player(hass, request):
|
||||||
|
"""Return a media player."""
|
||||||
def setUp(self): # pylint: disable=invalid-name
|
return request.param(hass)
|
||||||
"""Set up things to be run when tests are started."""
|
|
||||||
self.hass = get_test_home_assistant()
|
|
||||||
self.player = AsyncMediaPlayer(self.hass)
|
|
||||||
self.addCleanup(self.tear_down_cleanup)
|
|
||||||
|
|
||||||
def tear_down_cleanup(self):
|
|
||||||
"""Shut down test instance."""
|
|
||||||
self.hass.stop()
|
|
||||||
|
|
||||||
def test_volume_up(self):
|
|
||||||
"""Test the volume_up helper function."""
|
|
||||||
assert self.player.volume_level == 0
|
|
||||||
asyncio.run_coroutine_threadsafe(
|
|
||||||
self.player.async_set_volume_level(0.5), self.hass.loop
|
|
||||||
).result()
|
|
||||||
assert self.player.volume_level == 0.5
|
|
||||||
asyncio.run_coroutine_threadsafe(
|
|
||||||
self.player.async_volume_up(), self.hass.loop
|
|
||||||
).result()
|
|
||||||
assert self.player.volume_level == 0.6
|
|
||||||
|
|
||||||
def test_volume_down(self):
|
|
||||||
"""Test the volume_down helper function."""
|
|
||||||
assert self.player.volume_level == 0
|
|
||||||
asyncio.run_coroutine_threadsafe(
|
|
||||||
self.player.async_set_volume_level(0.5), self.hass.loop
|
|
||||||
).result()
|
|
||||||
assert self.player.volume_level == 0.5
|
|
||||||
asyncio.run_coroutine_threadsafe(
|
|
||||||
self.player.async_volume_down(), self.hass.loop
|
|
||||||
).result()
|
|
||||||
assert self.player.volume_level == 0.4
|
|
||||||
|
|
||||||
def test_media_play_pause(self):
|
|
||||||
"""Test the media_play_pause helper function."""
|
|
||||||
assert self.player.state == STATE_OFF
|
|
||||||
asyncio.run_coroutine_threadsafe(
|
|
||||||
self.player.async_media_play_pause(), self.hass.loop
|
|
||||||
).result()
|
|
||||||
assert self.player.state == STATE_PLAYING
|
|
||||||
asyncio.run_coroutine_threadsafe(
|
|
||||||
self.player.async_media_play_pause(), self.hass.loop
|
|
||||||
).result()
|
|
||||||
assert self.player.state == STATE_PAUSED
|
|
||||||
|
|
||||||
def test_toggle(self):
|
|
||||||
"""Test the toggle helper function."""
|
|
||||||
assert self.player.state == STATE_OFF
|
|
||||||
asyncio.run_coroutine_threadsafe(
|
|
||||||
self.player.async_toggle(), self.hass.loop
|
|
||||||
).result()
|
|
||||||
assert self.player.state == STATE_ON
|
|
||||||
asyncio.run_coroutine_threadsafe(
|
|
||||||
self.player.async_toggle(), self.hass.loop
|
|
||||||
).result()
|
|
||||||
assert self.player.state == STATE_OFF
|
|
||||||
|
|
||||||
|
|
||||||
class TestSyncMediaPlayer(unittest.TestCase):
|
async def test_volume_up(player):
|
||||||
"""Test the media_player module."""
|
"""Test the volume_up and set volume methods."""
|
||||||
|
assert player.volume_level == 0
|
||||||
|
await player.async_set_volume_level(0.5)
|
||||||
|
assert player.volume_level == 0.5
|
||||||
|
await player.async_volume_up()
|
||||||
|
assert player.volume_level == 0.6
|
||||||
|
|
||||||
def setUp(self): # pylint: disable=invalid-name
|
|
||||||
"""Set up things to be run when tests are started."""
|
|
||||||
self.hass = get_test_home_assistant()
|
|
||||||
self.player = SyncMediaPlayer(self.hass)
|
|
||||||
self.addCleanup(self.tear_down_cleanup)
|
|
||||||
|
|
||||||
def tear_down_cleanup(self):
|
async def test_volume_down(player):
|
||||||
"""Shut down test instance."""
|
"""Test the volume_down and set volume methods."""
|
||||||
self.hass.stop()
|
assert player.volume_level == 0
|
||||||
|
await player.async_set_volume_level(0.5)
|
||||||
|
assert player.volume_level == 0.5
|
||||||
|
await player.async_volume_down()
|
||||||
|
assert player.volume_level == 0.4
|
||||||
|
|
||||||
def test_volume_up(self):
|
|
||||||
"""Test the volume_up helper function."""
|
|
||||||
assert self.player.volume_level == 0
|
|
||||||
self.player.set_volume_level(0.5)
|
|
||||||
assert self.player.volume_level == 0.5
|
|
||||||
asyncio.run_coroutine_threadsafe(
|
|
||||||
self.player.async_volume_up(), self.hass.loop
|
|
||||||
).result()
|
|
||||||
assert self.player.volume_level == 0.7
|
|
||||||
|
|
||||||
def test_volume_down(self):
|
async def test_media_play_pause(player):
|
||||||
"""Test the volume_down helper function."""
|
"""Test the media_play_pause method."""
|
||||||
assert self.player.volume_level == 0
|
assert player.state == STATE_OFF
|
||||||
self.player.set_volume_level(0.5)
|
await player.async_media_play_pause()
|
||||||
assert self.player.volume_level == 0.5
|
assert player.state == STATE_PLAYING
|
||||||
asyncio.run_coroutine_threadsafe(
|
await player.async_media_play_pause()
|
||||||
self.player.async_volume_down(), self.hass.loop
|
assert player.state == STATE_PAUSED
|
||||||
).result()
|
|
||||||
assert self.player.volume_level == 0.3
|
|
||||||
|
|
||||||
def test_media_play_pause(self):
|
|
||||||
"""Test the media_play_pause helper function."""
|
|
||||||
assert self.player.state == STATE_OFF
|
|
||||||
asyncio.run_coroutine_threadsafe(
|
|
||||||
self.player.async_media_play_pause(), self.hass.loop
|
|
||||||
).result()
|
|
||||||
assert self.player.state == STATE_PLAYING
|
|
||||||
asyncio.run_coroutine_threadsafe(
|
|
||||||
self.player.async_media_play_pause(), self.hass.loop
|
|
||||||
).result()
|
|
||||||
assert self.player.state == STATE_PAUSED
|
|
||||||
|
|
||||||
def test_toggle(self):
|
async def test_turn_on_off(player):
|
||||||
"""Test the toggle helper function."""
|
"""Test the turn on and turn off methods."""
|
||||||
assert self.player.state == STATE_OFF
|
assert player.state == STATE_OFF
|
||||||
asyncio.run_coroutine_threadsafe(
|
await player.async_turn_on()
|
||||||
self.player.async_toggle(), self.hass.loop
|
assert player.state == STATE_ON
|
||||||
).result()
|
await player.async_turn_off()
|
||||||
assert self.player.state == STATE_ON
|
assert player.state == STATE_OFF
|
||||||
asyncio.run_coroutine_threadsafe(
|
|
||||||
self.player.async_toggle(), self.hass.loop
|
|
||||||
).result()
|
async def test_toggle(player):
|
||||||
assert self.player.state == STATE_OFF
|
"""Test the toggle method."""
|
||||||
|
assert player.state == STATE_OFF
|
||||||
|
await player.async_toggle()
|
||||||
|
assert player.state == STATE_ON
|
||||||
|
await player.async_toggle()
|
||||||
|
assert player.state == STATE_OFF
|
||||||
|
Loading…
x
Reference in New Issue
Block a user