Add service select scene to Yamaha Hifi media player (#36564)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
StevusPrimusandMartin Hjelmare authored and GitHub committed 2020-06-08 19:31:58 +02:00
1 parent 3adfb86a19
commit 5975ec340b
4 files changed
+219 -103

No files matched your search

+130 -39
View File
@@ -1,12 +1,15 @@
"""The tests for the Yamaha Media player platform."""
import unittest
import pytest
import homeassistant.components.media_player as mp
from homeassistant.components.yamaha import media_player as yamaha
from homeassistant.setup import setup_component
from homeassistant.components.yamaha.const import DOMAIN
from homeassistant.helpers.discovery import async_load_platform
from homeassistant.setup import async_setup_component
from tests.async_mock import MagicMock, patch
from tests.common import get_test_home_assistant
from tests.async_mock import MagicMock, PropertyMock, call, patch
CONFIG = {"media_player": {"platform": "yamaha", "host": "127.0.0.1"}}
def _create_zone_mock(name, url):
@@ -23,54 +26,142 @@ class FakeYamahaDevice:
"""Initialize the fake Yamaha device."""
self.ctrl_url = ctrl_url
self.name = name
self.zones = zones or []
self._zones = zones or []
def zone_controllers(self):
"""Return controllers for all available zones."""
return self.zones
return self._zones
class TestYamahaMediaPlayer(unittest.TestCase):
"""Test the Yamaha media player."""
@pytest.fixture(name="main_zone")
def main_zone_fixture():
"""Mock the main zone."""
return _create_zone_mock("Main zone", "http://main")
def setUp(self):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.main_zone = _create_zone_mock("Main zone", "http://main")
self.device = FakeYamahaDevice(
"http://receiver", "Receiver", zones=[self.main_zone]
@pytest.fixture(name="device")
def device_fixture(main_zone):
"""Mock the yamaha device."""
device = FakeYamahaDevice("http://receiver", "Receiver", zones=[main_zone])
with patch("rxv.RXV", return_value=device):
yield device
async def test_setup_host(hass, device, main_zone):
"""Test set up integration with host."""
assert await async_setup_component(hass, mp.DOMAIN, CONFIG)
await hass.async_block_till_done()
state = hass.states.get("media_player.yamaha_receiver_main_zone")
assert state is not None
assert state.state == "off"
async def test_setup_no_host(hass, device, main_zone):
"""Test set up integration without host."""
with patch("rxv.find", return_value=[device]):
assert await async_setup_component(
hass, mp.DOMAIN, {"media_player": {"platform": "yamaha"}}
)
await hass.async_block_till_done()
def tearDown(self):
"""Stop everything that was started."""
self.hass.stop()
state = hass.states.get("media_player.yamaha_receiver_main_zone")
def enable_output(self, port, enabled):
"""Enable output on a specific port."""
data = {
"entity_id": "media_player.yamaha_receiver_main_zone",
"port": port,
"enabled": enabled,
}
assert state is not None
assert state.state == "off"
self.hass.services.call(yamaha.DOMAIN, yamaha.SERVICE_ENABLE_OUTPUT, data, True)
def create_receiver(self, mock_rxv):
"""Create a mocked receiver."""
mock_rxv.return_value = self.device
async def test_setup_discovery(hass, device, main_zone):
"""Test set up integration via discovery."""
discovery_info = {
"name": "Yamaha Receiver",
"model_name": "Yamaha",
"control_url": "http://receiver",
"description_url": "http://receiver/description",
}
await async_load_platform(
hass, mp.DOMAIN, "yamaha", discovery_info, {mp.DOMAIN: {}}
)
await hass.async_block_till_done()
config = {"media_player": {"platform": "yamaha", "host": "127.0.0.1"}}
state = hass.states.get("media_player.yamaha_receiver_main_zone")
assert setup_component(self.hass, mp.DOMAIN, config)
self.hass.block_till_done()
assert state is not None
assert state.state == "off"
@patch("rxv.RXV")
def test_enable_output(self, mock_rxv):
"""Test enabling and disabling outputs."""
self.create_receiver(mock_rxv)
self.enable_output("hdmi1", True)
self.main_zone.enable_output.assert_called_with("hdmi1", True)
async def test_setup_zone_ignore(hass, device, main_zone):
"""Test set up integration without host."""
assert await async_setup_component(
hass,
mp.DOMAIN,
{
"media_player": {
"platform": "yamaha",
"host": "127.0.0.1",
"zone_ignore": "Main zone",
}
},
)
await hass.async_block_till_done()
self.enable_output("hdmi2", False)
self.main_zone.enable_output.assert_called_with("hdmi2", False)
state = hass.states.get("media_player.yamaha_receiver_main_zone")
assert state is None
async def test_enable_output(hass, device, main_zone):
"""Test enable output service."""
assert await async_setup_component(hass, mp.DOMAIN, CONFIG)
await hass.async_block_till_done()
port = "hdmi1"
enabled = True
data = {
"entity_id": "media_player.yamaha_receiver_main_zone",
"port": port,
"enabled": enabled,
}
await hass.services.async_call(DOMAIN, yamaha.SERVICE_ENABLE_OUTPUT, data, True)
assert main_zone.enable_output.call_count == 1
assert main_zone.enable_output.call_args == call(port, enabled)
async def test_select_scene(hass, device, main_zone, caplog):
"""Test select scene service."""
scene_prop = PropertyMock(return_value=None)
type(main_zone).scene = scene_prop
assert await async_setup_component(hass, mp.DOMAIN, CONFIG)
await hass.async_block_till_done()
scene = "TV Viewing"
data = {
"entity_id": "media_player.yamaha_receiver_main_zone",
"scene": scene,
}
await hass.services.async_call(DOMAIN, yamaha.SERVICE_SELECT_SCENE, data, True)
assert scene_prop.call_count == 1
assert scene_prop.call_args == call(scene)
scene = "BD/DVD Movie Viewing"
data["scene"] = scene
await hass.services.async_call(DOMAIN, yamaha.SERVICE_SELECT_SCENE, data, True)
assert scene_prop.call_count == 2
assert scene_prop.call_args == call(scene)
scene_prop.side_effect = AssertionError()
missing_scene = "Missing scene"
data["scene"] = missing_scene
await hass.services.async_call(DOMAIN, yamaha.SERVICE_SELECT_SCENE, data, True)
assert f"Scene '{missing_scene}' does not exist!" in caplog.text