Make roku async (#35104)
* Update manifest.json * work on roku async. * Update config_flow.py * Update __init__.py * Update media_player.py * Update media_player.py * Update __init__.py * Update __init__.py * Update media_player.py * Update media_player.py * Update remote.py * Update test_media_player.py * Update test_media_player.py * Update test_config_flow.py * Update media_player.py * Update remote.py * Update config_flow.py * Update test_media_player.py * Update config_flow.py * Update test_config_flow.py
This commit is contained in:
1 parent
54584e970c
commit
3feb55a8e4
16 files changed
+715
-490
No files matched your search
@@ -1,11 +1,18 @@
|
||||
"""Tests for the Roku component."""
|
||||
from requests_mock import Mocker
|
||||
import re
|
||||
from socket import gaierror as SocketGIAError
|
||||
|
||||
from homeassistant.components.roku.const import DOMAIN
|
||||
from homeassistant.components.ssdp import (
|
||||
ATTR_SSDP_LOCATION,
|
||||
ATTR_UPNP_FRIENDLY_NAME,
|
||||
ATTR_UPNP_SERIAL,
|
||||
)
|
||||
from homeassistant.const import CONF_HOST
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
|
||||
from tests.common import MockConfigEntry, load_fixture
|
||||
from tests.test_util.aiohttp import AiohttpClientMocker
|
||||
|
||||
HOST = "192.168.1.160"
|
||||
NAME = "Roku 3"
|
||||
@@ -13,38 +20,132 @@ SSDP_LOCATION = "http://192.168.1.160/"
|
||||
UPNP_FRIENDLY_NAME = "My Roku 3"
|
||||
UPNP_SERIAL = "1GU48T017973"
|
||||
|
||||
MOCK_SSDP_DISCOVERY_INFO = {
|
||||
ATTR_SSDP_LOCATION: SSDP_LOCATION,
|
||||
ATTR_UPNP_FRIENDLY_NAME: UPNP_FRIENDLY_NAME,
|
||||
ATTR_UPNP_SERIAL: UPNP_SERIAL,
|
||||
}
|
||||
|
||||
|
||||
def mock_connection(
|
||||
requests_mocker: Mocker, device: str = "roku3", app: str = "roku", host: str = HOST,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
device: str = "roku3",
|
||||
app: str = "roku",
|
||||
host: str = HOST,
|
||||
power: bool = True,
|
||||
error: bool = False,
|
||||
server_error: bool = False,
|
||||
) -> None:
|
||||
"""Mock the Roku connection."""
|
||||
roku_url = f"http://{host}:8060"
|
||||
|
||||
requests_mocker.get(
|
||||
if error:
|
||||
mock_connection_error(
|
||||
aioclient_mock=aioclient_mock, device=device, app=app, host=host
|
||||
)
|
||||
return
|
||||
|
||||
if server_error:
|
||||
mock_connection_server_error(
|
||||
aioclient_mock=aioclient_mock, device=device, app=app, host=host
|
||||
)
|
||||
return
|
||||
|
||||
info_fixture = f"roku/{device}-device-info.xml"
|
||||
if not power:
|
||||
info_fixture = f"roku/{device}-device-info-power-off.xml"
|
||||
|
||||
aioclient_mock.get(
|
||||
f"{roku_url}/query/device-info",
|
||||
text=load_fixture(f"roku/{device}-device-info.xml"),
|
||||
text=load_fixture(info_fixture),
|
||||
headers={"Content-Type": "application/xml"},
|
||||
)
|
||||
|
||||
apps_fixture = "roku/apps.xml"
|
||||
if device == "rokutv":
|
||||
apps_fixture = "roku/apps-tv.xml"
|
||||
|
||||
requests_mocker.get(
|
||||
f"{roku_url}/query/apps", text=load_fixture(apps_fixture),
|
||||
aioclient_mock.get(
|
||||
f"{roku_url}/query/apps",
|
||||
text=load_fixture(apps_fixture),
|
||||
headers={"Content-Type": "application/xml"},
|
||||
)
|
||||
|
||||
requests_mocker.get(
|
||||
f"{roku_url}/query/active-app", text=load_fixture(f"roku/active-app-{app}.xml"),
|
||||
aioclient_mock.get(
|
||||
f"{roku_url}/query/active-app",
|
||||
text=load_fixture(f"roku/active-app-{app}.xml"),
|
||||
headers={"Content-Type": "application/xml"},
|
||||
)
|
||||
|
||||
aioclient_mock.get(
|
||||
f"{roku_url}/query/tv-active-channel",
|
||||
text=load_fixture("roku/rokutv-tv-active-channel.xml"),
|
||||
headers={"Content-Type": "application/xml"},
|
||||
)
|
||||
|
||||
aioclient_mock.get(
|
||||
f"{roku_url}/query/tv-channels",
|
||||
text=load_fixture("roku/rokutv-tv-channels.xml"),
|
||||
headers={"Content-Type": "application/xml"},
|
||||
)
|
||||
|
||||
aioclient_mock.post(
|
||||
re.compile(f"{roku_url}/keypress/.*"), text="OK",
|
||||
)
|
||||
|
||||
aioclient_mock.post(
|
||||
re.compile(f"{roku_url}/launch/.*"), text="OK",
|
||||
)
|
||||
|
||||
|
||||
def mock_connection_error(
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
device: str = "roku3",
|
||||
app: str = "roku",
|
||||
host: str = HOST,
|
||||
) -> None:
|
||||
"""Mock the Roku connection error."""
|
||||
roku_url = f"http://{host}:8060"
|
||||
|
||||
aioclient_mock.get(f"{roku_url}/query/device-info", exc=SocketGIAError)
|
||||
aioclient_mock.get(f"{roku_url}/query/apps", exc=SocketGIAError)
|
||||
aioclient_mock.get(f"{roku_url}/query/active-app", exc=SocketGIAError)
|
||||
aioclient_mock.get(f"{roku_url}/query/tv-active-channel", exc=SocketGIAError)
|
||||
aioclient_mock.get(f"{roku_url}/query/tv-channels", exc=SocketGIAError)
|
||||
|
||||
aioclient_mock.post(re.compile(f"{roku_url}/keypress/.*"), exc=SocketGIAError)
|
||||
aioclient_mock.post(re.compile(f"{roku_url}/launch/.*"), exc=SocketGIAError)
|
||||
|
||||
|
||||
def mock_connection_server_error(
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
device: str = "roku3",
|
||||
app: str = "roku",
|
||||
host: str = HOST,
|
||||
) -> None:
|
||||
"""Mock the Roku server error."""
|
||||
roku_url = f"http://{host}:8060"
|
||||
|
||||
aioclient_mock.get(f"{roku_url}/query/device-info", status=500)
|
||||
aioclient_mock.get(f"{roku_url}/query/apps", status=500)
|
||||
aioclient_mock.get(f"{roku_url}/query/active-app", status=500)
|
||||
aioclient_mock.get(f"{roku_url}/query/tv-active-channel", status=500)
|
||||
aioclient_mock.get(f"{roku_url}/query/tv-channels", status=500)
|
||||
|
||||
aioclient_mock.post(re.compile(f"{roku_url}/keypress/.*"), status=500)
|
||||
aioclient_mock.post(re.compile(f"{roku_url}/launch/.*"), status=500)
|
||||
|
||||
|
||||
async def setup_integration(
|
||||
hass: HomeAssistantType,
|
||||
requests_mocker: Mocker,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
device: str = "roku3",
|
||||
app: str = "roku",
|
||||
host: str = HOST,
|
||||
unique_id: str = UPNP_SERIAL,
|
||||
error: bool = False,
|
||||
power: bool = True,
|
||||
server_error: bool = False,
|
||||
skip_entry_setup: bool = False,
|
||||
) -> MockConfigEntry:
|
||||
"""Set up the Roku integration in Home Assistant."""
|
||||
@@ -53,7 +154,15 @@ async def setup_integration(
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
if not skip_entry_setup:
|
||||
mock_connection(requests_mocker, device, app=app, host=host)
|
||||
mock_connection(
|
||||
aioclient_mock,
|
||||
device,
|
||||
app=app,
|
||||
host=host,
|
||||
error=error,
|
||||
power=power,
|
||||
server_error=server_error,
|
||||
)
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
@@ -1,16 +1,5 @@
|
||||
"""Test the Roku config flow."""
|
||||
from socket import gaierror as SocketGIAError
|
||||
|
||||
from requests.exceptions import RequestException
|
||||
from requests_mock import Mocker
|
||||
from roku import RokuException
|
||||
|
||||
from homeassistant.components.roku.const import DOMAIN
|
||||
from homeassistant.components.ssdp import (
|
||||
ATTR_SSDP_LOCATION,
|
||||
ATTR_UPNP_FRIENDLY_NAME,
|
||||
ATTR_UPNP_SERIAL,
|
||||
)
|
||||
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_SSDP, SOURCE_USER
|
||||
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_SOURCE
|
||||
from homeassistant.data_entry_flow import (
|
||||
@@ -24,19 +13,20 @@ from homeassistant.setup import async_setup_component
|
||||
from tests.async_mock import patch
|
||||
from tests.components.roku import (
|
||||
HOST,
|
||||
SSDP_LOCATION,
|
||||
MOCK_SSDP_DISCOVERY_INFO,
|
||||
UPNP_FRIENDLY_NAME,
|
||||
UPNP_SERIAL,
|
||||
mock_connection,
|
||||
setup_integration,
|
||||
)
|
||||
from tests.test_util.aiohttp import AiohttpClientMocker
|
||||
|
||||
|
||||
async def test_duplicate_error(hass: HomeAssistantType, requests_mock: Mocker) -> None:
|
||||
async def test_duplicate_error(
|
||||
hass: HomeAssistantType, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test that errors are shown when duplicates are added."""
|
||||
await setup_integration(hass, requests_mock, skip_entry_setup=True)
|
||||
|
||||
mock_connection(requests_mock)
|
||||
await setup_integration(hass, aioclient_mock, skip_entry_setup=True)
|
||||
mock_connection(aioclient_mock)
|
||||
|
||||
user_input = {CONF_HOST: HOST}
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
@@ -54,11 +44,7 @@ async def test_duplicate_error(hass: HomeAssistantType, requests_mock: Mocker) -
|
||||
assert result["type"] == RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
discovery_info = {
|
||||
ATTR_UPNP_FRIENDLY_NAME: UPNP_FRIENDLY_NAME,
|
||||
ATTR_SSDP_LOCATION: SSDP_LOCATION,
|
||||
ATTR_UPNP_SERIAL: UPNP_SERIAL,
|
||||
}
|
||||
discovery_info = MOCK_SSDP_DISCOVERY_INFO.copy()
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={CONF_SOURCE: SOURCE_SSDP}, data=discovery_info
|
||||
)
|
||||
@@ -67,11 +53,12 @@ async def test_duplicate_error(hass: HomeAssistantType, requests_mock: Mocker) -
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_form(hass: HomeAssistantType, requests_mock: Mocker) -> None:
|
||||
async def test_form(
|
||||
hass: HomeAssistantType, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test the user step."""
|
||||
await async_setup_component(hass, "persistent_notification", {})
|
||||
|
||||
mock_connection(requests_mock)
|
||||
mock_connection(aioclient_mock)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={CONF_SOURCE: SOURCE_USER}
|
||||
@@ -90,7 +77,7 @@ async def test_form(hass: HomeAssistantType, requests_mock: Mocker) -> None:
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["title"] == HOST
|
||||
assert result["title"] == UPNP_FRIENDLY_NAME
|
||||
|
||||
assert result["data"]
|
||||
assert result["data"][CONF_HOST] == HOST
|
||||
@@ -100,70 +87,23 @@ async def test_form(hass: HomeAssistantType, requests_mock: Mocker) -> None:
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_form_cannot_connect(hass: HomeAssistantType) -> None:
|
||||
async def test_form_cannot_connect(
|
||||
hass: HomeAssistantType, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test we handle cannot connect roku error."""
|
||||
mock_connection(aioclient_mock, error=True)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={CONF_SOURCE: SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.roku.config_flow.Roku._call",
|
||||
side_effect=RokuException,
|
||||
) as mock_validate_input:
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
flow_id=result["flow_id"], user_input={CONF_HOST: HOST}
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert len(mock_validate_input.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_form_cannot_connect_request(hass: HomeAssistantType) -> None:
|
||||
"""Test we handle cannot connect request error."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={CONF_SOURCE: SOURCE_USER}
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
flow_id=result["flow_id"], user_input={CONF_HOST: HOST}
|
||||
)
|
||||
|
||||
user_input = {CONF_HOST: HOST}
|
||||
with patch(
|
||||
"homeassistant.components.roku.config_flow.Roku._call",
|
||||
side_effect=RequestException,
|
||||
) as mock_validate_input:
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
flow_id=result["flow_id"], user_input=user_input
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert len(mock_validate_input.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_form_cannot_connect_socket(hass: HomeAssistantType) -> None:
|
||||
"""Test we handle cannot connect socket error."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={CONF_SOURCE: SOURCE_USER}
|
||||
)
|
||||
|
||||
user_input = {CONF_HOST: HOST}
|
||||
with patch(
|
||||
"homeassistant.components.roku.config_flow.Roku._call",
|
||||
side_effect=SocketGIAError,
|
||||
) as mock_validate_input:
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
flow_id=result["flow_id"], user_input=user_input
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert len(mock_validate_input.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_form_unknown_error(hass: HomeAssistantType) -> None:
|
||||
"""Test we handle unknown error."""
|
||||
@@ -173,7 +113,7 @@ async def test_form_unknown_error(hass: HomeAssistantType) -> None:
|
||||
|
||||
user_input = {CONF_HOST: HOST}
|
||||
with patch(
|
||||
"homeassistant.components.roku.config_flow.Roku._call", side_effect=Exception,
|
||||
"homeassistant.components.roku.config_flow.Roku.update", side_effect=Exception,
|
||||
) as mock_validate_input:
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
flow_id=result["flow_id"], user_input=user_input
|
||||
@@ -186,9 +126,11 @@ async def test_form_unknown_error(hass: HomeAssistantType) -> None:
|
||||
assert len(mock_validate_input.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_import(hass: HomeAssistantType, requests_mock: Mocker) -> None:
|
||||
async def test_import(
|
||||
hass: HomeAssistantType, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test the import step."""
|
||||
mock_connection(requests_mock)
|
||||
mock_connection(aioclient_mock)
|
||||
|
||||
user_input = {CONF_HOST: HOST}
|
||||
with patch(
|
||||
@@ -201,7 +143,7 @@ async def test_import(hass: HomeAssistantType, requests_mock: Mocker) -> None:
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["title"] == HOST
|
||||
assert result["title"] == UPNP_FRIENDLY_NAME
|
||||
|
||||
assert result["data"]
|
||||
assert result["data"][CONF_HOST] == HOST
|
||||
@@ -211,15 +153,44 @@ async def test_import(hass: HomeAssistantType, requests_mock: Mocker) -> None:
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_ssdp_discovery(hass: HomeAssistantType, requests_mock: Mocker) -> None:
|
||||
"""Test the ssdp discovery step."""
|
||||
mock_connection(requests_mock)
|
||||
async def test_ssdp_cannot_connect(
|
||||
hass: HomeAssistantType, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test we abort SSDP flow on connection error."""
|
||||
mock_connection(aioclient_mock, error=True)
|
||||
|
||||
discovery_info = {
|
||||
ATTR_SSDP_LOCATION: SSDP_LOCATION,
|
||||
ATTR_UPNP_FRIENDLY_NAME: UPNP_FRIENDLY_NAME,
|
||||
ATTR_UPNP_SERIAL: UPNP_SERIAL,
|
||||
}
|
||||
discovery_info = MOCK_SSDP_DISCOVERY_INFO.copy()
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={CONF_SOURCE: SOURCE_SSDP}, data=discovery_info,
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "cannot_connect"
|
||||
|
||||
|
||||
async def test_ssdp_unknown_error(
|
||||
hass: HomeAssistantType, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test we abort SSDP flow on unknown error."""
|
||||
discovery_info = MOCK_SSDP_DISCOVERY_INFO.copy()
|
||||
with patch(
|
||||
"homeassistant.components.roku.config_flow.Roku.update", side_effect=Exception,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={CONF_SOURCE: SOURCE_SSDP}, data=discovery_info,
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "unknown"
|
||||
|
||||
|
||||
async def test_ssdp_discovery(
|
||||
hass: HomeAssistantType, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test the SSDP discovery flow."""
|
||||
mock_connection(aioclient_mock)
|
||||
|
||||
discovery_info = MOCK_SSDP_DISCOVERY_INFO.copy()
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={CONF_SOURCE: SOURCE_SSDP}, data=discovery_info
|
||||
)
|
||||
|
||||
@@ -1,10 +1,4 @@
|
||||
"""Tests for the Roku integration."""
|
||||
from socket import gaierror as SocketGIAError
|
||||
|
||||
from requests.exceptions import RequestException
|
||||
from requests_mock import Mocker
|
||||
from roku import RokuException
|
||||
|
||||
from homeassistant.components.roku.const import DOMAIN
|
||||
from homeassistant.config_entries import (
|
||||
ENTRY_STATE_LOADED,
|
||||
@@ -15,46 +9,20 @@ from homeassistant.helpers.typing import HomeAssistantType
|
||||
|
||||
from tests.async_mock import patch
|
||||
from tests.components.roku import setup_integration
|
||||
from tests.test_util.aiohttp import AiohttpClientMocker
|
||||
|
||||
|
||||
async def test_config_entry_not_ready(
|
||||
hass: HomeAssistantType, requests_mock: Mocker
|
||||
hass: HomeAssistantType, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test the Roku configuration entry not ready."""
|
||||
with patch(
|
||||
"homeassistant.components.roku.Roku._call", side_effect=RokuException,
|
||||
):
|
||||
entry = await setup_integration(hass, requests_mock)
|
||||
|
||||
assert entry.state == ENTRY_STATE_SETUP_RETRY
|
||||
|
||||
|
||||
async def test_config_entry_not_ready_request(
|
||||
hass: HomeAssistantType, requests_mock: Mocker
|
||||
) -> None:
|
||||
"""Test the Roku configuration entry not ready."""
|
||||
with patch(
|
||||
"homeassistant.components.roku.Roku._call", side_effect=RequestException,
|
||||
):
|
||||
entry = await setup_integration(hass, requests_mock)
|
||||
|
||||
assert entry.state == ENTRY_STATE_SETUP_RETRY
|
||||
|
||||
|
||||
async def test_config_entry_not_ready_socket(
|
||||
hass: HomeAssistantType, requests_mock: Mocker
|
||||
) -> None:
|
||||
"""Test the Roku configuration entry not ready."""
|
||||
with patch(
|
||||
"homeassistant.components.roku.Roku._call", side_effect=SocketGIAError,
|
||||
):
|
||||
entry = await setup_integration(hass, requests_mock)
|
||||
entry = await setup_integration(hass, aioclient_mock, error=True)
|
||||
|
||||
assert entry.state == ENTRY_STATE_SETUP_RETRY
|
||||
|
||||
|
||||
async def test_unload_config_entry(
|
||||
hass: HomeAssistantType, requests_mock: Mocker
|
||||
hass: HomeAssistantType, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test the Roku configuration entry unloading."""
|
||||
with patch(
|
||||
@@ -63,7 +31,7 @@ async def test_unload_config_entry(
|
||||
), patch(
|
||||
"homeassistant.components.roku.remote.async_setup_entry", return_value=True,
|
||||
):
|
||||
entry = await setup_integration(hass, requests_mock)
|
||||
entry = await setup_integration(hass, aioclient_mock)
|
||||
|
||||
assert hass.data[DOMAIN][entry.entry_id]
|
||||
assert entry.state == ENTRY_STATE_LOADED
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
"""Tests for the Roku Media Player platform."""
|
||||
from datetime import timedelta
|
||||
|
||||
from requests.exceptions import (
|
||||
ConnectionError as RequestsConnectionError,
|
||||
ReadTimeout as RequestsReadTimeout,
|
||||
)
|
||||
from requests_mock import Mocker
|
||||
from roku import RokuException
|
||||
from rokuecp import RokuError
|
||||
|
||||
from homeassistant.components.media_player.const import (
|
||||
ATTR_APP_ID,
|
||||
ATTR_APP_NAME,
|
||||
ATTR_INPUT_SOURCE,
|
||||
ATTR_MEDIA_CHANNEL,
|
||||
ATTR_MEDIA_CONTENT_ID,
|
||||
ATTR_MEDIA_CONTENT_TYPE,
|
||||
ATTR_MEDIA_TITLE,
|
||||
ATTR_MEDIA_VOLUME_MUTED,
|
||||
DOMAIN as MP_DOMAIN,
|
||||
MEDIA_TYPE_APP,
|
||||
MEDIA_TYPE_CHANNEL,
|
||||
SERVICE_PLAY_MEDIA,
|
||||
SERVICE_SELECT_SOURCE,
|
||||
@@ -38,6 +38,7 @@ from homeassistant.const import (
|
||||
SERVICE_VOLUME_MUTE,
|
||||
SERVICE_VOLUME_UP,
|
||||
STATE_HOME,
|
||||
STATE_IDLE,
|
||||
STATE_PLAYING,
|
||||
STATE_STANDBY,
|
||||
STATE_UNAVAILABLE,
|
||||
@@ -45,9 +46,10 @@ from homeassistant.const import (
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from tests.async_mock import PropertyMock, patch
|
||||
from tests.async_mock import patch
|
||||
from tests.common import async_fire_time_changed
|
||||
from tests.components.roku import UPNP_SERIAL, setup_integration
|
||||
from tests.test_util.aiohttp import AiohttpClientMocker
|
||||
|
||||
MAIN_ENTITY_ID = f"{MP_DOMAIN}.my_roku_3"
|
||||
TV_ENTITY_ID = f"{MP_DOMAIN}.58_onn_roku_tv"
|
||||
@@ -56,34 +58,37 @@ TV_HOST = "192.168.1.161"
|
||||
TV_SERIAL = "YN00H5555555"
|
||||
|
||||
|
||||
async def test_setup(hass: HomeAssistantType, requests_mock: Mocker) -> None:
|
||||
async def test_setup(
|
||||
hass: HomeAssistantType, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test setup with basic config."""
|
||||
await setup_integration(hass, requests_mock)
|
||||
await setup_integration(hass, aioclient_mock)
|
||||
|
||||
entity_registry = await hass.helpers.entity_registry.async_get_registry()
|
||||
|
||||
main = entity_registry.async_get(MAIN_ENTITY_ID)
|
||||
|
||||
assert hass.states.get(MAIN_ENTITY_ID)
|
||||
assert main
|
||||
assert main.unique_id == UPNP_SERIAL
|
||||
|
||||
|
||||
async def test_idle_setup(hass: HomeAssistantType, requests_mock: Mocker) -> None:
|
||||
async def test_idle_setup(
|
||||
hass: HomeAssistantType, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test setup with idle device."""
|
||||
with patch(
|
||||
"homeassistant.components.roku.Roku.power_state",
|
||||
new_callable=PropertyMock(return_value="Off"),
|
||||
):
|
||||
await setup_integration(hass, requests_mock)
|
||||
await setup_integration(hass, aioclient_mock, power=False)
|
||||
|
||||
state = hass.states.get(MAIN_ENTITY_ID)
|
||||
assert state.state == STATE_STANDBY
|
||||
|
||||
|
||||
async def test_tv_setup(hass: HomeAssistantType, requests_mock: Mocker) -> None:
|
||||
async def test_tv_setup(
|
||||
hass: HomeAssistantType, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test Roku TV setup."""
|
||||
await setup_integration(
|
||||
hass,
|
||||
requests_mock,
|
||||
aioclient_mock,
|
||||
device="rokutv",
|
||||
app="tvinput-dtv",
|
||||
host=TV_HOST,
|
||||
@@ -91,41 +96,26 @@ async def test_tv_setup(hass: HomeAssistantType, requests_mock: Mocker) -> None:
|
||||
)
|
||||
|
||||
entity_registry = await hass.helpers.entity_registry.async_get_registry()
|
||||
|
||||
tv = entity_registry.async_get(TV_ENTITY_ID)
|
||||
|
||||
assert hass.states.get(TV_ENTITY_ID)
|
||||
assert tv
|
||||
assert tv.unique_id == TV_SERIAL
|
||||
|
||||
|
||||
async def test_availability(hass: HomeAssistantType, requests_mock: Mocker) -> None:
|
||||
async def test_availability(
|
||||
hass: HomeAssistantType, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test entity availability."""
|
||||
now = dt_util.utcnow()
|
||||
future = now + timedelta(minutes=1)
|
||||
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
await setup_integration(hass, requests_mock)
|
||||
await setup_integration(hass, aioclient_mock)
|
||||
|
||||
with patch("roku.Roku._get", side_effect=RokuException,), patch(
|
||||
"homeassistant.util.dt.utcnow", return_value=future
|
||||
):
|
||||
async_fire_time_changed(hass, future)
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get(MAIN_ENTITY_ID).state == STATE_UNAVAILABLE
|
||||
|
||||
future += timedelta(minutes=1)
|
||||
|
||||
with patch("roku.Roku._get", side_effect=RequestsConnectionError,), patch(
|
||||
"homeassistant.util.dt.utcnow", return_value=future
|
||||
):
|
||||
async_fire_time_changed(hass, future)
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get(MAIN_ENTITY_ID).state == STATE_UNAVAILABLE
|
||||
|
||||
future += timedelta(minutes=1)
|
||||
|
||||
with patch("roku.Roku._get", side_effect=RequestsReadTimeout,), patch(
|
||||
"homeassistant.util.dt.utcnow", return_value=future
|
||||
):
|
||||
with patch(
|
||||
"homeassistant.components.roku.Roku.update", side_effect=RokuError
|
||||
), patch("homeassistant.util.dt.utcnow", return_value=future):
|
||||
async_fire_time_changed(hass, future)
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get(MAIN_ENTITY_ID).state == STATE_UNAVAILABLE
|
||||
@@ -139,10 +129,10 @@ async def test_availability(hass: HomeAssistantType, requests_mock: Mocker) -> N
|
||||
|
||||
|
||||
async def test_supported_features(
|
||||
hass: HomeAssistantType, requests_mock: Mocker
|
||||
hass: HomeAssistantType, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test supported features."""
|
||||
await setup_integration(hass, requests_mock)
|
||||
await setup_integration(hass, aioclient_mock)
|
||||
|
||||
# Features supported for Rokus
|
||||
state = hass.states.get(MAIN_ENTITY_ID)
|
||||
@@ -161,12 +151,12 @@ async def test_supported_features(
|
||||
|
||||
|
||||
async def test_tv_supported_features(
|
||||
hass: HomeAssistantType, requests_mock: Mocker
|
||||
hass: HomeAssistantType, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test supported features for Roku TV."""
|
||||
await setup_integration(
|
||||
hass,
|
||||
requests_mock,
|
||||
aioclient_mock,
|
||||
device="rokutv",
|
||||
app="tvinput-dtv",
|
||||
host=TV_HOST,
|
||||
@@ -188,22 +178,58 @@ async def test_tv_supported_features(
|
||||
)
|
||||
|
||||
|
||||
async def test_attributes(hass: HomeAssistantType, requests_mock: Mocker) -> None:
|
||||
async def test_attributes(
|
||||
hass: HomeAssistantType, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test attributes."""
|
||||
await setup_integration(hass, requests_mock)
|
||||
await setup_integration(hass, aioclient_mock)
|
||||
|
||||
state = hass.states.get(MAIN_ENTITY_ID)
|
||||
assert state.state == STATE_HOME
|
||||
|
||||
assert state.attributes.get(ATTR_MEDIA_CONTENT_TYPE) is None
|
||||
assert state.attributes.get(ATTR_APP_ID) is None
|
||||
assert state.attributes.get(ATTR_APP_NAME) == "Roku"
|
||||
assert state.attributes.get(ATTR_INPUT_SOURCE) == "Roku"
|
||||
|
||||
|
||||
async def test_tv_attributes(hass: HomeAssistantType, requests_mock: Mocker) -> None:
|
||||
async def test_attributes_app(
|
||||
hass: HomeAssistantType, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test attributes for app."""
|
||||
await setup_integration(hass, aioclient_mock, app="netflix")
|
||||
|
||||
state = hass.states.get(MAIN_ENTITY_ID)
|
||||
assert state.state == STATE_PLAYING
|
||||
|
||||
assert state.attributes.get(ATTR_MEDIA_CONTENT_TYPE) == MEDIA_TYPE_APP
|
||||
assert state.attributes.get(ATTR_APP_ID) == "12"
|
||||
assert state.attributes.get(ATTR_APP_NAME) == "Netflix"
|
||||
assert state.attributes.get(ATTR_INPUT_SOURCE) == "Netflix"
|
||||
|
||||
|
||||
async def test_attributes_screensaver(
|
||||
hass: HomeAssistantType, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test attributes for app with screensaver."""
|
||||
await setup_integration(hass, aioclient_mock, app="screensaver")
|
||||
|
||||
state = hass.states.get(MAIN_ENTITY_ID)
|
||||
assert state.state == STATE_IDLE
|
||||
|
||||
assert state.attributes.get(ATTR_MEDIA_CONTENT_TYPE) is None
|
||||
assert state.attributes.get(ATTR_APP_ID) is None
|
||||
assert state.attributes.get(ATTR_APP_NAME) == "Roku"
|
||||
assert state.attributes.get(ATTR_INPUT_SOURCE) == "Roku"
|
||||
|
||||
|
||||
async def test_tv_attributes(
|
||||
hass: HomeAssistantType, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test attributes for Roku TV."""
|
||||
await setup_integration(
|
||||
hass,
|
||||
requests_mock,
|
||||
aioclient_mock,
|
||||
device="rokutv",
|
||||
app="tvinput-dtv",
|
||||
host=TV_HOST,
|
||||
@@ -213,29 +239,35 @@ async def test_tv_attributes(hass: HomeAssistantType, requests_mock: Mocker) ->
|
||||
state = hass.states.get(TV_ENTITY_ID)
|
||||
assert state.state == STATE_PLAYING
|
||||
|
||||
assert state.attributes.get(ATTR_MEDIA_CONTENT_TYPE) == MEDIA_TYPE_CHANNEL
|
||||
assert state.attributes.get(ATTR_APP_ID) == "tvinput.dtv"
|
||||
assert state.attributes.get(ATTR_APP_NAME) == "Antenna TV"
|
||||
assert state.attributes.get(ATTR_INPUT_SOURCE) == "Antenna TV"
|
||||
assert state.attributes.get(ATTR_MEDIA_CONTENT_TYPE) == MEDIA_TYPE_CHANNEL
|
||||
assert state.attributes.get(ATTR_MEDIA_CHANNEL) == "getTV (14.3)"
|
||||
assert state.attributes.get(ATTR_MEDIA_TITLE) == "Airwolf"
|
||||
|
||||
|
||||
async def test_services(hass: HomeAssistantType, requests_mock: Mocker) -> None:
|
||||
async def test_services(
|
||||
hass: HomeAssistantType, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test the different media player services."""
|
||||
await setup_integration(hass, requests_mock)
|
||||
await setup_integration(hass, aioclient_mock)
|
||||
|
||||
with patch("roku.Roku._post") as remote_mock:
|
||||
with patch("homeassistant.components.roku.Roku.remote") as remote_mock:
|
||||
await hass.services.async_call(
|
||||
MP_DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: MAIN_ENTITY_ID}, blocking=True
|
||||
)
|
||||
|
||||
remote_mock.assert_called_once_with("/keypress/PowerOff")
|
||||
remote_mock.assert_called_once_with("poweroff")
|
||||
|
||||
with patch("roku.Roku._post") as remote_mock:
|
||||
with patch("homeassistant.components.roku.Roku.remote") as remote_mock:
|
||||
await hass.services.async_call(
|
||||
MP_DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: MAIN_ENTITY_ID}, blocking=True
|
||||
)
|
||||
|
||||
remote_mock.assert_called_once_with("/keypress/PowerOn")
|
||||
remote_mock.assert_called_once_with("poweron")
|
||||
|
||||
with patch("roku.Roku._post") as remote_mock:
|
||||
with patch("homeassistant.components.roku.Roku.remote") as remote_mock:
|
||||
await hass.services.async_call(
|
||||
MP_DOMAIN,
|
||||
SERVICE_MEDIA_PLAY_PAUSE,
|
||||
@@ -243,9 +275,9 @@ async def test_services(hass: HomeAssistantType, requests_mock: Mocker) -> None:
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
remote_mock.assert_called_once_with("/keypress/Play")
|
||||
remote_mock.assert_called_once_with("play")
|
||||
|
||||
with patch("roku.Roku._post") as remote_mock:
|
||||
with patch("homeassistant.components.roku.Roku.remote") as remote_mock:
|
||||
await hass.services.async_call(
|
||||
MP_DOMAIN,
|
||||
SERVICE_MEDIA_NEXT_TRACK,
|
||||
@@ -253,9 +285,9 @@ async def test_services(hass: HomeAssistantType, requests_mock: Mocker) -> None:
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
remote_mock.assert_called_once_with("/keypress/Fwd")
|
||||
remote_mock.assert_called_once_with("forward")
|
||||
|
||||
with patch("roku.Roku._post") as remote_mock:
|
||||
with patch("homeassistant.components.roku.Roku.remote") as remote_mock:
|
||||
await hass.services.async_call(
|
||||
MP_DOMAIN,
|
||||
SERVICE_MEDIA_PREVIOUS_TRACK,
|
||||
@@ -263,9 +295,9 @@ async def test_services(hass: HomeAssistantType, requests_mock: Mocker) -> None:
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
remote_mock.assert_called_once_with("/keypress/Rev")
|
||||
remote_mock.assert_called_once_with("reverse")
|
||||
|
||||
with patch("roku.Roku._post") as remote_mock:
|
||||
with patch("homeassistant.components.roku.Roku.remote") as remote_mock:
|
||||
await hass.services.async_call(
|
||||
MP_DOMAIN,
|
||||
SERVICE_SELECT_SOURCE,
|
||||
@@ -273,9 +305,9 @@ async def test_services(hass: HomeAssistantType, requests_mock: Mocker) -> None:
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
remote_mock.assert_called_once_with("/keypress/Home")
|
||||
remote_mock.assert_called_once_with("home")
|
||||
|
||||
with patch("roku.Roku._post") as remote_mock:
|
||||
with patch("homeassistant.components.roku.Roku.launch") as remote_mock:
|
||||
await hass.services.async_call(
|
||||
MP_DOMAIN,
|
||||
SERVICE_SELECT_SOURCE,
|
||||
@@ -283,28 +315,30 @@ async def test_services(hass: HomeAssistantType, requests_mock: Mocker) -> None:
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
remote_mock.assert_called_once_with("/launch/12", params={"contentID": "12"})
|
||||
remote_mock.assert_called_once_with("12")
|
||||
|
||||
|
||||
async def test_tv_services(hass: HomeAssistantType, requests_mock: Mocker) -> None:
|
||||
async def test_tv_services(
|
||||
hass: HomeAssistantType, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test the media player services related to Roku TV."""
|
||||
await setup_integration(
|
||||
hass,
|
||||
requests_mock,
|
||||
aioclient_mock,
|
||||
device="rokutv",
|
||||
app="tvinput-dtv",
|
||||
host=TV_HOST,
|
||||
unique_id=TV_SERIAL,
|
||||
)
|
||||
|
||||
with patch("roku.Roku._post") as remote_mock:
|
||||
with patch("homeassistant.components.roku.Roku.remote") as remote_mock:
|
||||
await hass.services.async_call(
|
||||
MP_DOMAIN, SERVICE_VOLUME_UP, {ATTR_ENTITY_ID: TV_ENTITY_ID}, blocking=True
|
||||
)
|
||||
|
||||
remote_mock.assert_called_once_with("/keypress/VolumeUp")
|
||||
remote_mock.assert_called_once_with("volume_up")
|
||||
|
||||
with patch("roku.Roku._post") as remote_mock:
|
||||
with patch("homeassistant.components.roku.Roku.remote") as remote_mock:
|
||||
await hass.services.async_call(
|
||||
MP_DOMAIN,
|
||||
SERVICE_VOLUME_DOWN,
|
||||
@@ -312,9 +346,9 @@ async def test_tv_services(hass: HomeAssistantType, requests_mock: Mocker) -> No
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
remote_mock.assert_called_once_with("/keypress/VolumeDown")
|
||||
remote_mock.assert_called_once_with("volume_down")
|
||||
|
||||
with patch("roku.Roku._post") as remote_mock:
|
||||
with patch("homeassistant.components.roku.Roku.remote") as remote_mock:
|
||||
await hass.services.async_call(
|
||||
MP_DOMAIN,
|
||||
SERVICE_VOLUME_MUTE,
|
||||
@@ -322,9 +356,9 @@ async def test_tv_services(hass: HomeAssistantType, requests_mock: Mocker) -> No
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
remote_mock.assert_called_once_with("/keypress/VolumeMute")
|
||||
remote_mock.assert_called_once_with("volume_mute")
|
||||
|
||||
with patch("roku.Roku.launch") as tune_mock:
|
||||
with patch("homeassistant.components.roku.Roku.tune") as tune_mock:
|
||||
await hass.services.async_call(
|
||||
MP_DOMAIN,
|
||||
SERVICE_PLAY_MEDIA,
|
||||
@@ -336,4 +370,4 @@ async def test_tv_services(hass: HomeAssistantType, requests_mock: Mocker) -> No
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
tune_mock.assert_called_once()
|
||||
tune_mock.assert_called_once_with("55")
|
||||
Reference in new issue
Block a user