mirror of
https://github.com/home-assistant/core.git
synced 2025-08-12 06:50:00 +00:00
.devcontainer
.github
.vscode
docs
homeassistant
script
tests
auth
components
abode
adguard
air_quality
airly
alarm_control_panel
alert
alexa
almond
ambiclimate
ambient_station
androidtv
api
api_streams
apns
apprise
aprs
arcam_fmj
arlo
asuswrt
aurora
auth
automatic
automation
awair
aws
axis
bayesian
binary_sensor
blackbird
bom
broadlink
buienradar
caldav
calendar
camera
canary
cast
cert_expiry
climate
cloud
coinmarketcap
command_line
config
configurator
conversation
coolmaster
counter
cover
daikin
darksky
datadog
deconz
default_config
demo
__init__.py
test_calendar.py
test_camera.py
test_climate.py
test_cover.py
test_fan.py
test_geo_location.py
test_init.py
test_light.py
test_lock.py
test_media_player.py
test_notify.py
test_remote.py
test_vacuum.py
test_water_heater.py
device_automation
device_sun_light_trigger
device_tracker
dialogflow
directv
discovery
dsmr
dte_energy_bridge
duckdns
dyson
ecobee
ee_brightbox
efergy
emulated_hue
emulated_roku
esphome
everlights
facebook
facebox
fail2ban
fan
feedreader
ffmpeg
fido
file
filesize
filter
flux
folder
folder_watcher
foobot
freedns
fritzbox
frontend
generic
generic_thermostat
geo_json_events
geo_location
geo_rss_events
geofency
geonetnz_quakes
glances
google
google_assistant
google_domains
google_pubsub
google_translate
google_wifi
gpslogger
graphite
group
hangouts
hassio
hddtemp
heos
here_travel_time
history
history_graph
history_stats
homeassistant
homekit
homekit_controller
homematic
homematicip_cloud
honeywell
html5
http
huawei_lte
hue
iaqualink
ifttt
ign_sismologia
image_processing
imap_email_content
influxdb
input_boolean
input_datetime
input_number
input_select
input_text
integration
intent_script
ios
ipma
iqvia
islamic_prayer_times
izone
jewish_calendar
kira
light
linky
litejet
local_file
locative
lock
logbook
logentries
logger
logi_circle
london_air
lovelace
luftdaten
mailbox
mailgun
manual
manual_mqtt
marytts
media_player
melissa
meraki
met
mfi
mhz19
microsoft_face
microsoft_face_detect
microsoft_face_identify
min_max
minio
mobile_app
mochad
modbus
mold_indicator
monoprice
moon
mqtt
mqtt_eventstream
mqtt_json
mqtt_room
mqtt_statestream
mythicbeastsdns
namecheapdns
neato
ness_alarm
nest
nextbus
no_ip
notify
notion
nsw_fuel_station
nsw_rural_fire_service_feed
nuheat
nws
nx584
onboarding
openalpr_cloud
openalpr_local
openhardwaremonitor
opentherm_gw
openuv
owntracks
panel_custom
panel_iframe
persistent_notification
person
pi_hole
pilight
plant
plex
point
prometheus
proximity
ps4
ptvsd
push
pushbullet
python_script
qld_bushfire
qwikswitch
radarr
rainmachine
random
recorder
reddit
remember_the_milk
remote
rest
rest_command
rflink
rfxtrx
ring
rmvtransport
rss_feed_template
samsungtv
scene
script
season
sensor
seventeentrack
shell_command
shopping_list
sigfox
simplisafe
simulated
sleepiq
sma
smartthings
smhi
smtp
snips
solaredge
solarlog
soma
somfy
sonarr
sonos
soundtouch
spaceapi
spc
splunk
sql
ssdp
startca
statistics
statsd
stream
sun
switch
switcher_kis
system_health
system_log
tcp
teksavvy
tellduslive
template
threshold
time_date
timer
tod
tomato
toon
tplink
traccar
tradfri
transmission
transport_nsw
trend
tts
twentemilieu
twilio
uk_transport
unifi
unifi_direct
universal
updater
upnp
uptime
usgs_earthquakes_feed
utility_meter
uvc
vacuum
velbus
verisure
version
vesync
voicerss
vultr
wake_on_lan
water_heater
weather
webhook
weblink
webostv
websocket_api
withings
workday
worldclock
wsdot
wunderground
wwlln
xiaomi
xiaomi_miio
yamaha
yandex_transport
yandextts
yessssms
yr
yweather
zeroconf
zha
zone
zwave
__init__.py
conftest.py
fixtures
helpers
mock
resources
scripts
test_util
testing_config
util
__init__.py
common.py
conftest.py
test_bootstrap.py
test_config.py
test_config_entries.py
test_core.py
test_data_entry_flow.py
test_loader.py
test_main.py
test_requirements.py
test_setup.py
.codecov.yml
.coveragerc
.dockerignore
.gitattributes
.gitignore
.hound.yml
.ignore
.pre-commit-config-all.yaml
.pre-commit-config.yaml
.readthedocs.yml
.travis.yml
CLA.md
CODEOWNERS
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile.dev
LICENSE.md
MANIFEST.in
README.rst
azure-pipelines-ci.yml
azure-pipelines-release.yml
azure-pipelines-translation.yml
azure-pipelines-wheels.yml
pylintrc
pyproject.toml
requirements_all.txt
requirements_docs.txt
requirements_test.txt
requirements_test_all.txt
setup.cfg
setup.py
tox.ini
270 lines
9.5 KiB
Python
270 lines
9.5 KiB
Python
"""The tests for the Demo Media player platform."""
|
|
import unittest
|
|
from unittest.mock import patch
|
|
import asyncio
|
|
|
|
import pytest
|
|
import voluptuous as vol
|
|
|
|
from homeassistant.setup import setup_component, async_setup_component
|
|
import homeassistant.components.media_player as mp
|
|
from homeassistant.helpers.aiohttp_client import DATA_CLIENTSESSION
|
|
|
|
from tests.common import get_test_home_assistant
|
|
from tests.components.media_player import common
|
|
|
|
entity_id = "media_player.walkman"
|
|
|
|
|
|
class TestDemoMediaPlayer(unittest.TestCase):
|
|
"""Test the media_player module."""
|
|
|
|
def setUp(self): # pylint: disable=invalid-name
|
|
"""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()
|
|
|
|
def test_source_select(self):
|
|
"""Test the input source service."""
|
|
entity_id = "media_player.lounge_room"
|
|
|
|
assert setup_component(
|
|
self.hass, mp.DOMAIN, {"media_player": {"platform": "demo"}}
|
|
)
|
|
state = self.hass.states.get(entity_id)
|
|
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)
|
|
assert "dvd" == state.attributes.get("source")
|
|
|
|
common.select_source(self.hass, "xbox", entity_id)
|
|
self.hass.block_till_done()
|
|
state = self.hass.states.get(entity_id)
|
|
assert "xbox" == state.attributes.get("source")
|
|
|
|
def test_clear_playlist(self):
|
|
"""Test clear playlist."""
|
|
assert setup_component(
|
|
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()
|
|
assert self.hass.states.is_state(entity_id, "off")
|
|
|
|
def test_volume_services(self):
|
|
"""Test the volume service."""
|
|
assert setup_component(
|
|
self.hass, mp.DOMAIN, {"media_player": {"platform": "demo"}}
|
|
)
|
|
state = self.hass.states.get(entity_id)
|
|
assert 1.0 == state.attributes.get("volume_level")
|
|
|
|
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)
|
|
assert 1.0 == state.attributes.get("volume_level")
|
|
|
|
common.set_volume_level(self.hass, 0.5, entity_id)
|
|
self.hass.block_till_done()
|
|
state = self.hass.states.get(entity_id)
|
|
assert 0.5 == state.attributes.get("volume_level")
|
|
|
|
common.volume_down(self.hass, entity_id)
|
|
self.hass.block_till_done()
|
|
state = self.hass.states.get(entity_id)
|
|
assert 0.4 == state.attributes.get("volume_level")
|
|
|
|
common.volume_up(self.hass, entity_id)
|
|
self.hass.block_till_done()
|
|
state = self.hass.states.get(entity_id)
|
|
assert 0.5 == state.attributes.get("volume_level")
|
|
|
|
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)
|
|
assert False is state.attributes.get("is_volume_muted")
|
|
|
|
common.mute_volume(self.hass, True, entity_id)
|
|
self.hass.block_till_done()
|
|
state = self.hass.states.get(entity_id)
|
|
assert True is state.attributes.get("is_volume_muted")
|
|
|
|
def test_turning_off_and_on(self):
|
|
"""Test turn_on and turn_off."""
|
|
assert setup_component(
|
|
self.hass, mp.DOMAIN, {"media_player": {"platform": "demo"}}
|
|
)
|
|
assert self.hass.states.is_state(entity_id, "playing")
|
|
|
|
common.turn_off(self.hass, entity_id)
|
|
self.hass.block_till_done()
|
|
assert self.hass.states.is_state(entity_id, "off")
|
|
assert not mp.is_on(self.hass, entity_id)
|
|
|
|
common.turn_on(self.hass, entity_id)
|
|
self.hass.block_till_done()
|
|
assert self.hass.states.is_state(entity_id, "playing")
|
|
|
|
common.toggle(self.hass, entity_id)
|
|
self.hass.block_till_done()
|
|
assert self.hass.states.is_state(entity_id, "off")
|
|
assert not mp.is_on(self.hass, entity_id)
|
|
|
|
def test_playing_pausing(self):
|
|
"""Test media_pause."""
|
|
assert setup_component(
|
|
self.hass, mp.DOMAIN, {"media_player": {"platform": "demo"}}
|
|
)
|
|
assert self.hass.states.is_state(entity_id, "playing")
|
|
|
|
common.media_pause(self.hass, entity_id)
|
|
self.hass.block_till_done()
|
|
assert self.hass.states.is_state(entity_id, "paused")
|
|
|
|
common.media_play_pause(self.hass, entity_id)
|
|
self.hass.block_till_done()
|
|
assert self.hass.states.is_state(entity_id, "playing")
|
|
|
|
common.media_play_pause(self.hass, entity_id)
|
|
self.hass.block_till_done()
|
|
assert self.hass.states.is_state(entity_id, "paused")
|
|
|
|
common.media_play(self.hass, entity_id)
|
|
self.hass.block_till_done()
|
|
assert self.hass.states.is_state(entity_id, "playing")
|
|
|
|
def test_prev_next_track(self):
|
|
"""Test media_next_track and media_previous_track ."""
|
|
assert setup_component(
|
|
self.hass, mp.DOMAIN, {"media_player": {"platform": "demo"}}
|
|
)
|
|
state = self.hass.states.get(entity_id)
|
|
assert 1 == state.attributes.get("media_track")
|
|
|
|
common.media_next_track(self.hass, entity_id)
|
|
self.hass.block_till_done()
|
|
state = self.hass.states.get(entity_id)
|
|
assert 2 == state.attributes.get("media_track")
|
|
|
|
common.media_next_track(self.hass, entity_id)
|
|
self.hass.block_till_done()
|
|
state = self.hass.states.get(entity_id)
|
|
assert 3 == state.attributes.get("media_track")
|
|
|
|
common.media_previous_track(self.hass, entity_id)
|
|
self.hass.block_till_done()
|
|
state = self.hass.states.get(entity_id)
|
|
assert 2 == state.attributes.get("media_track")
|
|
|
|
assert setup_component(
|
|
self.hass, mp.DOMAIN, {"media_player": {"platform": "demo"}}
|
|
)
|
|
ent_id = "media_player.lounge_room"
|
|
state = self.hass.states.get(ent_id)
|
|
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)
|
|
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)
|
|
assert 1 == state.attributes.get("media_episode")
|
|
|
|
def test_play_media(self):
|
|
"""Test play_media ."""
|
|
assert setup_component(
|
|
self.hass, mp.DOMAIN, {"media_player": {"platform": "demo"}}
|
|
)
|
|
ent_id = "media_player.living_room"
|
|
state = self.hass.states.get(ent_id)
|
|
assert 0 < (mp.SUPPORT_PLAY_MEDIA & state.attributes.get("supported_features"))
|
|
assert state.attributes.get("media_content_id") is not None
|
|
|
|
with pytest.raises(vol.Invalid):
|
|
common.play_media(self.hass, None, "some_id", ent_id)
|
|
self.hass.block_till_done()
|
|
state = self.hass.states.get(ent_id)
|
|
assert 0 < (mp.SUPPORT_PLAY_MEDIA & state.attributes.get("supported_features"))
|
|
assert not "some_id" == state.attributes.get("media_content_id")
|
|
|
|
common.play_media(self.hass, "youtube", "some_id", ent_id)
|
|
self.hass.block_till_done()
|
|
state = self.hass.states.get(ent_id)
|
|
assert 0 < (mp.SUPPORT_PLAY_MEDIA & state.attributes.get("supported_features"))
|
|
assert "some_id" == state.attributes.get("media_content_id")
|
|
|
|
@patch(
|
|
"homeassistant.components.demo.media_player.DemoYoutubePlayer." "media_seek",
|
|
autospec=True,
|
|
)
|
|
def test_seek(self, mock_seek):
|
|
"""Test seek."""
|
|
assert setup_component(
|
|
self.hass, mp.DOMAIN, {"media_player": {"platform": "demo"}}
|
|
)
|
|
ent_id = "media_player.living_room"
|
|
state = self.hass.states.get(ent_id)
|
|
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()
|
|
assert not mock_seek.called
|
|
common.media_seek(self.hass, 100, ent_id)
|
|
self.hass.block_till_done()
|
|
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(
|
|
hass, mp.DOMAIN, {"media_player": {"platform": "demo"}}
|
|
)
|
|
|
|
fake_picture_data = "test.test"
|
|
|
|
class MockResponse:
|
|
def __init__(self):
|
|
self.status = 200
|
|
self.headers = {"Content-Type": "sometype"}
|
|
|
|
@asyncio.coroutine
|
|
def read(self):
|
|
return fake_picture_data.encode("ascii")
|
|
|
|
@asyncio.coroutine
|
|
def release(self):
|
|
pass
|
|
|
|
class MockWebsession:
|
|
@asyncio.coroutine
|
|
def get(self, url):
|
|
return MockResponse()
|
|
|
|
def detach(self):
|
|
pass
|
|
|
|
hass.data[DATA_CLIENTSESSION] = MockWebsession()
|
|
|
|
assert hass.states.is_state(entity_id, "playing")
|
|
state = hass.states.get(entity_id)
|
|
client = await hass_client()
|
|
req = await client.get(state.attributes.get("entity_picture"))
|
|
assert req.status == 200
|
|
assert await req.text() == fake_picture_data
|