core/tests/components/demo/test_media_player.py

270 lines
9.5 KiB
Python
Raw Normal View History

2016-03-09 09:25:50 +00:00
"""The tests for the Demo Media player platform."""
2016-02-02 08:31:36 +00:00
import unittest
from unittest.mock import patch
import asyncio
import pytest
import voluptuous as vol
from homeassistant.setup import setup_component, async_setup_component
2016-02-02 08:31:36 +00:00
import homeassistant.components.media_player as mp
from homeassistant.helpers.aiohttp_client import DATA_CLIENTSESSION
2016-02-02 08:31:36 +00:00
from tests.common import get_test_home_assistant
from tests.components.media_player import common
2019-07-31 19:25:30 +00:00
entity_id = "media_player.walkman"
2016-02-02 08:31:36 +00:00
class TestDemoMediaPlayer(unittest.TestCase):
2016-03-09 09:25:50 +00:00
"""Test the media_player module."""
2016-02-02 08:31:36 +00:00
def setUp(self): # pylint: disable=invalid-name
2018-08-19 20:29:08 +00:00
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
def tearDown(self):
"""Shut down test instance."""
self.hass.stop()
2016-02-02 08:31:36 +00:00
def test_source_select(self):
"""Test the input source service."""
2019-07-31 19:25:30 +00:00
entity_id = "media_player.lounge_room"
assert setup_component(
2019-07-31 19:25:30 +00:00
self.hass, mp.DOMAIN, {"media_player": {"platform": "demo"}}
)
state = self.hass.states.get(entity_id)
2019-07-31 19:25:30 +00:00
assert "dvd" == state.attributes.get("source")
with pytest.raises(vol.Invalid):
common.select_source(self.hass, None, entity_id)
self.hass.block_till_done()
state = self.hass.states.get(entity_id)
2019-07-31 19:25:30 +00:00
assert "dvd" == state.attributes.get("source")
2019-07-31 19:25:30 +00:00
common.select_source(self.hass, "xbox", entity_id)
self.hass.block_till_done()
state = self.hass.states.get(entity_id)
2019-07-31 19:25:30 +00:00
assert "xbox" == state.attributes.get("source")
def test_clear_playlist(self):
"""Test clear playlist."""
assert setup_component(
2019-07-31 19:25:30 +00:00
self.hass, mp.DOMAIN, {"media_player": {"platform": "demo"}}
)
assert self.hass.states.is_state(entity_id, "playing")
common.clear_playlist(self.hass, entity_id)
self.hass.block_till_done()
2019-07-31 19:25:30 +00:00
assert self.hass.states.is_state(entity_id, "off")
2016-02-02 08:31:36 +00:00
def test_volume_services(self):
2016-03-09 09:25:50 +00:00
"""Test the volume service."""
assert setup_component(
2019-07-31 19:25:30 +00:00
self.hass, mp.DOMAIN, {"media_player": {"platform": "demo"}}
)
2016-02-02 08:31:36 +00:00
state = self.hass.states.get(entity_id)
2019-07-31 19:25:30 +00:00
assert 1.0 == state.attributes.get("volume_level")
2016-02-02 08:31:36 +00:00
with pytest.raises(vol.Invalid):
common.set_volume_level(self.hass, None, entity_id)
self.hass.block_till_done()
state = self.hass.states.get(entity_id)
2019-07-31 19:25:30 +00:00
assert 1.0 == state.attributes.get("volume_level")
common.set_volume_level(self.hass, 0.5, entity_id)
self.hass.block_till_done()
2016-02-02 08:31:36 +00:00
state = self.hass.states.get(entity_id)
2019-07-31 19:25:30 +00:00
assert 0.5 == state.attributes.get("volume_level")
2016-02-02 08:31:36 +00:00
common.volume_down(self.hass, entity_id)
self.hass.block_till_done()
2016-02-02 08:31:36 +00:00
state = self.hass.states.get(entity_id)
2019-07-31 19:25:30 +00:00
assert 0.4 == state.attributes.get("volume_level")
2016-02-02 08:31:36 +00:00
common.volume_up(self.hass, entity_id)
self.hass.block_till_done()
2016-02-02 08:31:36 +00:00
state = self.hass.states.get(entity_id)
2019-07-31 19:25:30 +00:00
assert 0.5 == state.attributes.get("volume_level")
2016-02-02 08:31:36 +00:00
2019-07-31 19:25:30 +00:00
assert False is state.attributes.get("is_volume_muted")
with pytest.raises(vol.Invalid):
common.mute_volume(self.hass, None, entity_id)
self.hass.block_till_done()
state = self.hass.states.get(entity_id)
2019-07-31 19:25:30 +00:00
assert False is state.attributes.get("is_volume_muted")
common.mute_volume(self.hass, True, entity_id)
self.hass.block_till_done()
2016-02-02 08:31:36 +00:00
state = self.hass.states.get(entity_id)
2019-07-31 19:25:30 +00:00
assert True is state.attributes.get("is_volume_muted")
2016-02-02 08:31:36 +00:00
def test_turning_off_and_on(self):
2016-03-09 09:25:50 +00:00
"""Test turn_on and turn_off."""
assert setup_component(
2019-07-31 19:25:30 +00:00
self.hass, mp.DOMAIN, {"media_player": {"platform": "demo"}}
)
assert self.hass.states.is_state(entity_id, "playing")
2016-02-02 08:31:36 +00:00
common.turn_off(self.hass, entity_id)
self.hass.block_till_done()
2019-07-31 19:25:30 +00:00
assert self.hass.states.is_state(entity_id, "off")
2016-02-02 08:31:36 +00:00
assert not mp.is_on(self.hass, entity_id)
common.turn_on(self.hass, entity_id)
self.hass.block_till_done()
2019-07-31 19:25:30 +00:00
assert self.hass.states.is_state(entity_id, "playing")
2016-02-02 08:31:36 +00:00
common.toggle(self.hass, entity_id)
self.hass.block_till_done()
2019-07-31 19:25:30 +00:00
assert self.hass.states.is_state(entity_id, "off")
2016-02-02 08:31:36 +00:00
assert not mp.is_on(self.hass, entity_id)
def test_playing_pausing(self):
2016-03-09 09:25:50 +00:00
"""Test media_pause."""
assert setup_component(
2019-07-31 19:25:30 +00:00
self.hass, mp.DOMAIN, {"media_player": {"platform": "demo"}}
)
assert self.hass.states.is_state(entity_id, "playing")
2016-02-02 08:31:36 +00:00
common.media_pause(self.hass, entity_id)
self.hass.block_till_done()
2019-07-31 19:25:30 +00:00
assert self.hass.states.is_state(entity_id, "paused")
2016-02-02 08:31:36 +00:00
common.media_play_pause(self.hass, entity_id)
self.hass.block_till_done()
2019-07-31 19:25:30 +00:00
assert self.hass.states.is_state(entity_id, "playing")
2016-02-02 08:31:36 +00:00
common.media_play_pause(self.hass, entity_id)
self.hass.block_till_done()
2019-07-31 19:25:30 +00:00
assert self.hass.states.is_state(entity_id, "paused")
2016-02-02 08:31:36 +00:00
common.media_play(self.hass, entity_id)
self.hass.block_till_done()
2019-07-31 19:25:30 +00:00
assert self.hass.states.is_state(entity_id, "playing")
2016-02-02 08:31:36 +00:00
def test_prev_next_track(self):
"""Test media_next_track and media_previous_track ."""
assert setup_component(
2019-07-31 19:25:30 +00:00
self.hass, mp.DOMAIN, {"media_player": {"platform": "demo"}}
)
2016-02-02 08:31:36 +00:00
state = self.hass.states.get(entity_id)
2019-07-31 19:25:30 +00:00
assert 1 == state.attributes.get("media_track")
2016-02-02 08:31:36 +00:00
common.media_next_track(self.hass, entity_id)
self.hass.block_till_done()
2016-02-02 08:31:36 +00:00
state = self.hass.states.get(entity_id)
2019-07-31 19:25:30 +00:00
assert 2 == state.attributes.get("media_track")
2016-02-02 08:31:36 +00:00
common.media_next_track(self.hass, entity_id)
self.hass.block_till_done()
2016-02-02 08:31:36 +00:00
state = self.hass.states.get(entity_id)
2019-07-31 19:25:30 +00:00
assert 3 == state.attributes.get("media_track")
2016-02-02 08:31:36 +00:00
common.media_previous_track(self.hass, entity_id)
self.hass.block_till_done()
2016-02-02 08:31:36 +00:00
state = self.hass.states.get(entity_id)
2019-07-31 19:25:30 +00:00
assert 2 == state.attributes.get("media_track")
2016-02-02 08:31:36 +00:00
assert setup_component(
2019-07-31 19:25:30 +00:00
self.hass, mp.DOMAIN, {"media_player": {"platform": "demo"}}
)
ent_id = "media_player.lounge_room"
state = self.hass.states.get(ent_id)
2019-07-31 19:25:30 +00:00
assert 1 == state.attributes.get("media_episode")
common.media_next_track(self.hass, ent_id)
self.hass.block_till_done()
state = self.hass.states.get(ent_id)
2019-07-31 19:25:30 +00:00
assert 2 == state.attributes.get("media_episode")
common.media_previous_track(self.hass, ent_id)
self.hass.block_till_done()
state = self.hass.states.get(ent_id)
2019-07-31 19:25:30 +00:00
assert 1 == state.attributes.get("media_episode")
def test_play_media(self):
2016-03-09 09:25:50 +00:00
"""Test play_media ."""
assert setup_component(
2019-07-31 19:25:30 +00:00
self.hass, mp.DOMAIN, {"media_player": {"platform": "demo"}}
)
ent_id = "media_player.living_room"
2016-02-02 08:31:36 +00:00
state = self.hass.states.get(ent_id)
2019-07-31 19:25:30 +00:00
assert 0 < (mp.SUPPORT_PLAY_MEDIA & state.attributes.get("supported_features"))
assert state.attributes.get("media_content_id") is not None
2016-02-02 08:31:36 +00:00
with pytest.raises(vol.Invalid):
2019-07-31 19:25:30 +00:00
common.play_media(self.hass, None, "some_id", ent_id)
self.hass.block_till_done()
state = self.hass.states.get(ent_id)
2019-07-31 19:25:30 +00:00
assert 0 < (mp.SUPPORT_PLAY_MEDIA & state.attributes.get("supported_features"))
assert not "some_id" == state.attributes.get("media_content_id")
2019-07-31 19:25:30 +00:00
common.play_media(self.hass, "youtube", "some_id", ent_id)
self.hass.block_till_done()
2016-02-02 08:31:36 +00:00
state = self.hass.states.get(ent_id)
2019-07-31 19:25:30 +00:00
assert 0 < (mp.SUPPORT_PLAY_MEDIA & state.attributes.get("supported_features"))
assert "some_id" == state.attributes.get("media_content_id")
2016-02-02 08:31:36 +00:00
2019-07-31 19:25:30 +00:00
@patch(
"homeassistant.components.demo.media_player.DemoYoutubePlayer." "media_seek",
autospec=True,
)
def test_seek(self, mock_seek):
"""Test seek."""
assert setup_component(
2019-07-31 19:25:30 +00:00
self.hass, mp.DOMAIN, {"media_player": {"platform": "demo"}}
)
ent_id = "media_player.living_room"
state = self.hass.states.get(ent_id)
2019-07-31 19:25:30 +00:00
assert state.attributes["supported_features"] & mp.SUPPORT_SEEK
assert not mock_seek.called
with pytest.raises(vol.Invalid):
common.media_seek(self.hass, None, ent_id)
self.hass.block_till_done()
2016-02-02 08:31:36 +00:00
assert not mock_seek.called
common.media_seek(self.hass, 100, ent_id)
self.hass.block_till_done()
2016-02-02 08:31:36 +00:00
assert mock_seek.called
async def test_media_image_proxy(hass, hass_client):
"""Test the media server image proxy server ."""
assert await async_setup_component(
2019-07-31 19:25:30 +00:00
hass, mp.DOMAIN, {"media_player": {"platform": "demo"}}
)
2019-07-31 19:25:30 +00:00
fake_picture_data = "test.test"
2019-07-31 19:25:30 +00:00
class MockResponse:
def __init__(self):
self.status = 200
2019-07-31 19:25:30 +00:00
self.headers = {"Content-Type": "sometype"}
@asyncio.coroutine
def read(self):
2019-07-31 19:25:30 +00:00
return fake_picture_data.encode("ascii")
@asyncio.coroutine
def release(self):
pass
2019-07-31 19:25:30 +00:00
class MockWebsession:
@asyncio.coroutine
def get(self, url):
return MockResponse()
def detach(self):
pass
hass.data[DATA_CLIENTSESSION] = MockWebsession()
2019-07-31 19:25:30 +00:00
assert hass.states.is_state(entity_id, "playing")
state = hass.states.get(entity_id)
client = await hass_client()
2019-07-31 19:25:30 +00:00
req = await client.get(state.attributes.get("entity_picture"))
assert req.status == 200
assert await req.text() == fake_picture_data