Add support for newer SamsungTV models (#31537)
* Added support for newer SamsungTV models * Fixed legacy port * store token in HA config directory * Change token name * rebasing and exception handling * implement update * fix error creating mediaplayer * Debug logging * Increase timeout * Restore update timeout * Store token_file path in config_entry * Introduction of samsung bridge class * Added bridge class functions * Code cleanup * more fixes * Begin testing * samsungtvws 1.2.0 * Config flow tests 0.1 * Fixed some mediaplayer tests * Fixed fixture in media player * use of constants and turn off * more media player tests * samsungtvws 1.3.1 and other fixes * WS tv update rewritten * more tests * test_init * fixed tests * removed reset mock * tests reset mock * close_remote and tests * deprecate port config * deprecate port config 2 * deprecate port config 3 * save token only if needed * cleanup * better websocket protocol detection * config removal
This commit is contained in:
1 parent
2e802c88f8
commit
a8758ed3a1
11 files changed
+646
-266
No files matched your search
@@ -4,6 +4,7 @@ from unittest.mock import call, patch
|
||||
from asynctest import mock
|
||||
import pytest
|
||||
from samsungctl.exceptions import AccessDenied, UnhandledResponse
|
||||
from samsungtvws.exceptions import ConnectionFailure
|
||||
from websocket import WebSocketProtocolException
|
||||
|
||||
from homeassistant.components.samsungtv.const import (
|
||||
@@ -36,15 +37,6 @@ MOCK_SSDP_DATA_NOPREFIX = {
|
||||
ATTR_UPNP_UDN: "fake2_uuid",
|
||||
}
|
||||
|
||||
AUTODETECT_WEBSOCKET = {
|
||||
"name": "HomeAssistant",
|
||||
"description": "HomeAssistant",
|
||||
"id": "ha.component.samsung",
|
||||
"method": "websocket",
|
||||
"port": None,
|
||||
"host": "fake_host",
|
||||
"timeout": 1,
|
||||
}
|
||||
AUTODETECT_LEGACY = {
|
||||
"name": "HomeAssistant",
|
||||
"description": "HomeAssistant",
|
||||
@@ -59,7 +51,9 @@ AUTODETECT_LEGACY = {
|
||||
@pytest.fixture(name="remote")
|
||||
def remote_fixture():
|
||||
"""Patch the samsungctl Remote."""
|
||||
with patch("samsungctl.Remote") as remote_class, patch(
|
||||
with patch(
|
||||
"homeassistant.components.samsungtv.bridge.Remote"
|
||||
) as remote_class, patch(
|
||||
"homeassistant.components.samsungtv.config_flow.socket"
|
||||
) as socket_class:
|
||||
remote = mock.Mock()
|
||||
@@ -71,9 +65,25 @@ def remote_fixture():
|
||||
yield remote
|
||||
|
||||
|
||||
async def test_user(hass, remote):
|
||||
"""Test starting a flow by user."""
|
||||
@pytest.fixture(name="remotews")
|
||||
def remotews_fixture():
|
||||
"""Patch the samsungtvws SamsungTVWS."""
|
||||
with patch(
|
||||
"homeassistant.components.samsungtv.bridge.SamsungTVWS"
|
||||
) as remotews_class, patch(
|
||||
"homeassistant.components.samsungtv.config_flow.socket"
|
||||
) as socket_class:
|
||||
remotews = mock.Mock()
|
||||
remotews.__enter__ = mock.Mock()
|
||||
remotews.__exit__ = mock.Mock()
|
||||
remotews_class.return_value = remotews
|
||||
socket = mock.Mock()
|
||||
socket_class.return_value = socket
|
||||
yield remotews
|
||||
|
||||
|
||||
async def test_user_legacy(hass, remote):
|
||||
"""Test starting a flow by user."""
|
||||
# show form
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": "user"}
|
||||
@@ -85,23 +95,51 @@ async def test_user(hass, remote):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=MOCK_USER_DATA
|
||||
)
|
||||
# legacy tv entry created
|
||||
assert result["type"] == "create_entry"
|
||||
assert result["title"] == "fake_name"
|
||||
assert result["data"][CONF_HOST] == "fake_host"
|
||||
assert result["data"][CONF_NAME] == "fake_name"
|
||||
assert result["data"][CONF_METHOD] == "legacy"
|
||||
assert result["data"][CONF_MANUFACTURER] is None
|
||||
assert result["data"][CONF_MODEL] is None
|
||||
assert result["data"][CONF_ID] is None
|
||||
|
||||
|
||||
async def test_user_missing_auth(hass):
|
||||
async def test_user_websocket(hass, remotews):
|
||||
"""Test starting a flow by user."""
|
||||
with patch(
|
||||
"homeassistant.components.samsungtv.bridge.Remote", side_effect=OSError("Boom")
|
||||
):
|
||||
# show form
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": "user"}
|
||||
)
|
||||
assert result["type"] == "form"
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
# entry was added
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=MOCK_USER_DATA
|
||||
)
|
||||
# legacy tv entry created
|
||||
assert result["type"] == "create_entry"
|
||||
assert result["title"] == "fake_name"
|
||||
assert result["data"][CONF_HOST] == "fake_host"
|
||||
assert result["data"][CONF_NAME] == "fake_name"
|
||||
assert result["data"][CONF_METHOD] == "websocket"
|
||||
assert result["data"][CONF_MANUFACTURER] is None
|
||||
assert result["data"][CONF_MODEL] is None
|
||||
assert result["data"][CONF_ID] is None
|
||||
|
||||
|
||||
async def test_user_legacy_missing_auth(hass):
|
||||
"""Test starting a flow by user with authentication."""
|
||||
with patch(
|
||||
"homeassistant.components.samsungtv.config_flow.Remote",
|
||||
"homeassistant.components.samsungtv.bridge.Remote",
|
||||
side_effect=AccessDenied("Boom"),
|
||||
), patch("homeassistant.components.samsungtv.config_flow.socket"):
|
||||
|
||||
# missing authentication
|
||||
# legacy device missing authentication
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": "user"}, data=MOCK_USER_DATA
|
||||
)
|
||||
@@ -109,14 +147,31 @@ async def test_user_missing_auth(hass):
|
||||
assert result["reason"] == "auth_missing"
|
||||
|
||||
|
||||
async def test_user_not_supported(hass):
|
||||
async def test_user_legacy_not_supported(hass):
|
||||
"""Test starting a flow by user for not supported device."""
|
||||
with patch(
|
||||
"homeassistant.components.samsungtv.config_flow.Remote",
|
||||
"homeassistant.components.samsungtv.bridge.Remote",
|
||||
side_effect=UnhandledResponse("Boom"),
|
||||
), patch("homeassistant.components.samsungtv.config_flow.socket"):
|
||||
# legacy device not supported
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": "user"}, data=MOCK_USER_DATA
|
||||
)
|
||||
assert result["type"] == "abort"
|
||||
assert result["reason"] == "not_supported"
|
||||
|
||||
# device not supported
|
||||
|
||||
async def test_user_websocket_not_supported(hass):
|
||||
"""Test starting a flow by user for not supported device."""
|
||||
with patch(
|
||||
"homeassistant.components.samsungtv.bridge.Remote", side_effect=OSError("Boom"),
|
||||
), patch(
|
||||
"homeassistant.components.samsungtv.bridge.SamsungTVWS",
|
||||
side_effect=WebSocketProtocolException("Boom"),
|
||||
), patch(
|
||||
"homeassistant.components.samsungtv.config_flow.socket"
|
||||
):
|
||||
# websocket device not supported
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": "user"}, data=MOCK_USER_DATA
|
||||
)
|
||||
@@ -127,11 +182,30 @@ async def test_user_not_supported(hass):
|
||||
async def test_user_not_successful(hass):
|
||||
"""Test starting a flow by user but no connection found."""
|
||||
with patch(
|
||||
"homeassistant.components.samsungtv.config_flow.Remote",
|
||||
"homeassistant.components.samsungtv.bridge.Remote", side_effect=OSError("Boom"),
|
||||
), patch(
|
||||
"homeassistant.components.samsungtv.bridge.SamsungTVWS",
|
||||
side_effect=OSError("Boom"),
|
||||
), patch("homeassistant.components.samsungtv.config_flow.socket"):
|
||||
), patch(
|
||||
"homeassistant.components.samsungtv.config_flow.socket"
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": "user"}, data=MOCK_USER_DATA
|
||||
)
|
||||
assert result["type"] == "abort"
|
||||
assert result["reason"] == "not_successful"
|
||||
|
||||
# device not connectable
|
||||
|
||||
async def test_user_not_successful_2(hass):
|
||||
"""Test starting a flow by user but no connection found."""
|
||||
with patch(
|
||||
"homeassistant.components.samsungtv.bridge.Remote", side_effect=OSError("Boom"),
|
||||
), patch(
|
||||
"homeassistant.components.samsungtv.bridge.SamsungTVWS",
|
||||
side_effect=ConnectionFailure("Boom"),
|
||||
), patch(
|
||||
"homeassistant.components.samsungtv.config_flow.socket"
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": "user"}, data=MOCK_USER_DATA
|
||||
)
|
||||
@@ -202,10 +276,10 @@ async def test_ssdp_noprefix(hass, remote):
|
||||
assert result["data"][CONF_ID] == "fake2_uuid"
|
||||
|
||||
|
||||
async def test_ssdp_missing_auth(hass):
|
||||
async def test_ssdp_legacy_missing_auth(hass):
|
||||
"""Test starting a flow from discovery with authentication."""
|
||||
with patch(
|
||||
"homeassistant.components.samsungtv.config_flow.Remote",
|
||||
"homeassistant.components.samsungtv.bridge.Remote",
|
||||
side_effect=AccessDenied("Boom"),
|
||||
), patch("homeassistant.components.samsungtv.config_flow.socket"):
|
||||
|
||||
@@ -224,10 +298,10 @@ async def test_ssdp_missing_auth(hass):
|
||||
assert result["reason"] == "auth_missing"
|
||||
|
||||
|
||||
async def test_ssdp_not_supported(hass):
|
||||
async def test_ssdp_legacy_not_supported(hass):
|
||||
"""Test starting a flow from discovery for not supported device."""
|
||||
with patch(
|
||||
"homeassistant.components.samsungtv.config_flow.Remote",
|
||||
"homeassistant.components.samsungtv.bridge.Remote",
|
||||
side_effect=UnhandledResponse("Boom"),
|
||||
), patch("homeassistant.components.samsungtv.config_flow.socket"):
|
||||
|
||||
@@ -246,13 +320,16 @@ async def test_ssdp_not_supported(hass):
|
||||
assert result["reason"] == "not_supported"
|
||||
|
||||
|
||||
async def test_ssdp_not_supported_2(hass):
|
||||
async def test_ssdp_websocket_not_supported(hass):
|
||||
"""Test starting a flow from discovery for not supported device."""
|
||||
with patch(
|
||||
"homeassistant.components.samsungtv.config_flow.Remote",
|
||||
"homeassistant.components.samsungtv.bridge.Remote", side_effect=OSError("Boom"),
|
||||
), patch(
|
||||
"homeassistant.components.samsungtv.bridge.SamsungTVWS",
|
||||
side_effect=WebSocketProtocolException("Boom"),
|
||||
), patch("homeassistant.components.samsungtv.config_flow.socket"):
|
||||
|
||||
), patch(
|
||||
"homeassistant.components.samsungtv.config_flow.socket"
|
||||
):
|
||||
# confirm to add the entry
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": "ssdp"}, data=MOCK_SSDP_DATA
|
||||
@@ -271,9 +348,39 @@ async def test_ssdp_not_supported_2(hass):
|
||||
async def test_ssdp_not_successful(hass):
|
||||
"""Test starting a flow from discovery but no device found."""
|
||||
with patch(
|
||||
"homeassistant.components.samsungtv.config_flow.Remote",
|
||||
"homeassistant.components.samsungtv.bridge.Remote", side_effect=OSError("Boom"),
|
||||
), patch(
|
||||
"homeassistant.components.samsungtv.bridge.SamsungTVWS",
|
||||
side_effect=OSError("Boom"),
|
||||
), patch("homeassistant.components.samsungtv.config_flow.socket"):
|
||||
), patch(
|
||||
"homeassistant.components.samsungtv.config_flow.socket"
|
||||
):
|
||||
|
||||
# confirm to add the entry
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": "ssdp"}, data=MOCK_SSDP_DATA
|
||||
)
|
||||
assert result["type"] == "form"
|
||||
assert result["step_id"] == "confirm"
|
||||
|
||||
# device not found
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input="whatever"
|
||||
)
|
||||
assert result["type"] == "abort"
|
||||
assert result["reason"] == "not_successful"
|
||||
|
||||
|
||||
async def test_ssdp_not_successful_2(hass):
|
||||
"""Test starting a flow from discovery but no device found."""
|
||||
with patch(
|
||||
"homeassistant.components.samsungtv.bridge.Remote", side_effect=OSError("Boom"),
|
||||
), patch(
|
||||
"homeassistant.components.samsungtv.bridge.SamsungTVWS",
|
||||
side_effect=ConnectionFailure("Boom"),
|
||||
), patch(
|
||||
"homeassistant.components.samsungtv.config_flow.socket"
|
||||
):
|
||||
|
||||
# confirm to add the entry
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
@@ -334,22 +441,32 @@ async def test_ssdp_already_configured(hass, remote):
|
||||
assert entry.data[CONF_ID] == "fake_uuid"
|
||||
|
||||
|
||||
async def test_autodetect_websocket(hass, remote):
|
||||
async def test_autodetect_websocket(hass, remote, remotews):
|
||||
"""Test for send key with autodetection of protocol."""
|
||||
with patch("homeassistant.components.samsungtv.config_flow.Remote") as remote:
|
||||
with patch(
|
||||
"homeassistant.components.samsungtv.bridge.Remote", side_effect=OSError("Boom"),
|
||||
), patch("homeassistant.components.samsungtv.bridge.SamsungTVWS") as remotews:
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": "user"}, data=MOCK_USER_DATA
|
||||
)
|
||||
assert result["type"] == "create_entry"
|
||||
assert result["data"][CONF_METHOD] == "websocket"
|
||||
assert remote.call_count == 1
|
||||
assert remote.call_args_list == [call(AUTODETECT_WEBSOCKET)]
|
||||
assert remotews.call_count == 1
|
||||
assert remotews.call_args_list == [
|
||||
call(
|
||||
host="fake_host",
|
||||
name="HomeAssistant",
|
||||
port=8001,
|
||||
timeout=31,
|
||||
token=None,
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
async def test_autodetect_auth_missing(hass, remote):
|
||||
"""Test for send key with autodetection of protocol."""
|
||||
with patch(
|
||||
"homeassistant.components.samsungtv.config_flow.Remote",
|
||||
"homeassistant.components.samsungtv.bridge.Remote",
|
||||
side_effect=[AccessDenied("Boom")],
|
||||
) as remote:
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
@@ -358,13 +475,13 @@ async def test_autodetect_auth_missing(hass, remote):
|
||||
assert result["type"] == "abort"
|
||||
assert result["reason"] == "auth_missing"
|
||||
assert remote.call_count == 1
|
||||
assert remote.call_args_list == [call(AUTODETECT_WEBSOCKET)]
|
||||
assert remote.call_args_list == [call(AUTODETECT_LEGACY)]
|
||||
|
||||
|
||||
async def test_autodetect_not_supported(hass, remote):
|
||||
"""Test for send key with autodetection of protocol."""
|
||||
with patch(
|
||||
"homeassistant.components.samsungtv.config_flow.Remote",
|
||||
"homeassistant.components.samsungtv.bridge.Remote",
|
||||
side_effect=[UnhandledResponse("Boom")],
|
||||
) as remote:
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
@@ -373,40 +490,52 @@ async def test_autodetect_not_supported(hass, remote):
|
||||
assert result["type"] == "abort"
|
||||
assert result["reason"] == "not_supported"
|
||||
assert remote.call_count == 1
|
||||
assert remote.call_args_list == [call(AUTODETECT_WEBSOCKET)]
|
||||
assert remote.call_args_list == [call(AUTODETECT_LEGACY)]
|
||||
|
||||
|
||||
async def test_autodetect_legacy(hass, remote):
|
||||
"""Test for send key with autodetection of protocol."""
|
||||
with patch(
|
||||
"homeassistant.components.samsungtv.config_flow.Remote",
|
||||
side_effect=[OSError("Boom"), mock.DEFAULT],
|
||||
) as remote:
|
||||
with patch("homeassistant.components.samsungtv.bridge.Remote") as remote:
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": "user"}, data=MOCK_USER_DATA
|
||||
)
|
||||
assert result["type"] == "create_entry"
|
||||
assert result["data"][CONF_METHOD] == "legacy"
|
||||
assert remote.call_count == 2
|
||||
assert remote.call_args_list == [
|
||||
call(AUTODETECT_WEBSOCKET),
|
||||
call(AUTODETECT_LEGACY),
|
||||
]
|
||||
assert remote.call_count == 1
|
||||
assert remote.call_args_list == [call(AUTODETECT_LEGACY)]
|
||||
|
||||
|
||||
async def test_autodetect_none(hass, remote):
|
||||
async def test_autodetect_none(hass, remote, remotews):
|
||||
"""Test for send key with autodetection of protocol."""
|
||||
with patch(
|
||||
"homeassistant.components.samsungtv.config_flow.Remote",
|
||||
"homeassistant.components.samsungtv.bridge.Remote", side_effect=OSError("Boom"),
|
||||
) as remote, patch(
|
||||
"homeassistant.components.samsungtv.bridge.SamsungTVWS",
|
||||
side_effect=OSError("Boom"),
|
||||
) as remote:
|
||||
) as remotews:
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": "user"}, data=MOCK_USER_DATA
|
||||
)
|
||||
assert result["type"] == "abort"
|
||||
assert result["reason"] == "not_successful"
|
||||
assert remote.call_count == 2
|
||||
assert remote.call_count == 1
|
||||
assert remote.call_args_list == [
|
||||
call(AUTODETECT_WEBSOCKET),
|
||||
call(AUTODETECT_LEGACY),
|
||||
]
|
||||
assert remotews.call_count == 2
|
||||
assert remotews.call_args_list == [
|
||||
call(
|
||||
host="fake_host",
|
||||
name="HomeAssistant",
|
||||
port=8001,
|
||||
timeout=31,
|
||||
token=None,
|
||||
),
|
||||
call(
|
||||
host="fake_host",
|
||||
name="HomeAssistant",
|
||||
port=8002,
|
||||
timeout=31,
|
||||
token=None,
|
||||
),
|
||||
]
|
||||
@@ -1,6 +1,6 @@
|
||||
"""Tests for the Samsung TV Integration."""
|
||||
from unittest.mock import call, patch
|
||||
|
||||
from asynctest import mock
|
||||
from asynctest.mock import call, patch
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.media_player.const import DOMAIN, SUPPORT_TURN_ON
|
||||
@@ -14,7 +14,6 @@ from homeassistant.const import (
|
||||
ATTR_SUPPORTED_FEATURES,
|
||||
CONF_HOST,
|
||||
CONF_NAME,
|
||||
CONF_PORT,
|
||||
SERVICE_VOLUME_UP,
|
||||
)
|
||||
from homeassistant.setup import async_setup_component
|
||||
@@ -25,7 +24,6 @@ MOCK_CONFIG = {
|
||||
{
|
||||
CONF_HOST: "fake_host",
|
||||
CONF_NAME: "fake_name",
|
||||
CONF_PORT: 1234,
|
||||
CONF_ON_ACTION: [{"delay": "00:00:01"}],
|
||||
}
|
||||
]
|
||||
@@ -34,8 +32,7 @@ REMOTE_CALL = {
|
||||
"name": "HomeAssistant",
|
||||
"description": "HomeAssistant",
|
||||
"id": "ha.component.samsung",
|
||||
"method": "websocket",
|
||||
"port": MOCK_CONFIG[SAMSUNGTV_DOMAIN][0][CONF_PORT],
|
||||
"method": "legacy",
|
||||
"host": MOCK_CONFIG[SAMSUNGTV_DOMAIN][0][CONF_HOST],
|
||||
"timeout": 1,
|
||||
}
|
||||
@@ -44,11 +41,17 @@ REMOTE_CALL = {
|
||||
@pytest.fixture(name="remote")
|
||||
def remote_fixture():
|
||||
"""Patch the samsungctl Remote."""
|
||||
with patch("homeassistant.components.samsungtv.socket") as socket1, patch(
|
||||
with patch(
|
||||
"homeassistant.components.samsungtv.bridge.Remote"
|
||||
) as remote_class, patch(
|
||||
"homeassistant.components.samsungtv.config_flow.socket"
|
||||
) as socket2, patch("homeassistant.components.samsungtv.config_flow.Remote"), patch(
|
||||
"homeassistant.components.samsungtv.media_player.SamsungRemote"
|
||||
) as remote:
|
||||
) as socket1, patch(
|
||||
"homeassistant.components.samsungtv.socket"
|
||||
) as socket2:
|
||||
remote = mock.Mock()
|
||||
remote.__enter__ = mock.Mock()
|
||||
remote.__exit__ = mock.Mock()
|
||||
remote_class.return_value = remote
|
||||
socket1.gethostbyname.return_value = "FAKE_IP_ADDRESS"
|
||||
socket2.gethostbyname.return_value = "FAKE_IP_ADDRESS"
|
||||
yield remote
|
||||
@@ -56,22 +59,24 @@ def remote_fixture():
|
||||
|
||||
async def test_setup(hass, remote):
|
||||
"""Test Samsung TV integration is setup."""
|
||||
await async_setup_component(hass, SAMSUNGTV_DOMAIN, MOCK_CONFIG)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
with patch("homeassistant.components.samsungtv.bridge.Remote") as remote:
|
||||
await async_setup_component(hass, SAMSUNGTV_DOMAIN, MOCK_CONFIG)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
|
||||
# test name and turn_on
|
||||
assert state
|
||||
assert state.name == "fake_name"
|
||||
assert (
|
||||
state.attributes[ATTR_SUPPORTED_FEATURES] == SUPPORT_SAMSUNGTV | SUPPORT_TURN_ON
|
||||
)
|
||||
# test name and turn_on
|
||||
assert state
|
||||
assert state.name == "fake_name"
|
||||
assert (
|
||||
state.attributes[ATTR_SUPPORTED_FEATURES]
|
||||
== SUPPORT_SAMSUNGTV | SUPPORT_TURN_ON
|
||||
)
|
||||
|
||||
# test host and port
|
||||
assert await hass.services.async_call(
|
||||
DOMAIN, SERVICE_VOLUME_UP, {ATTR_ENTITY_ID: ENTITY_ID}, True
|
||||
)
|
||||
assert remote.mock_calls[0] == call(REMOTE_CALL)
|
||||
# test host and port
|
||||
assert await hass.services.async_call(
|
||||
DOMAIN, SERVICE_VOLUME_UP, {ATTR_ENTITY_ID: ENTITY_ID}, True
|
||||
)
|
||||
assert remote.call_args == call(REMOTE_CALL)
|
||||
|
||||
|
||||
async def test_setup_duplicate_config(hass, remote, caplog):
|
||||
|
||||
@@ -7,6 +7,7 @@ from asynctest import mock
|
||||
from asynctest.mock import call, patch
|
||||
import pytest
|
||||
from samsungctl import exceptions
|
||||
from samsungtvws.exceptions import ConnectionFailure
|
||||
from websocket import WebSocketException
|
||||
|
||||
from homeassistant.components.media_player import DEVICE_CLASS_TV
|
||||
@@ -54,6 +55,17 @@ from tests.common import async_fire_time_changed
|
||||
|
||||
ENTITY_ID = f"{DOMAIN}.fake"
|
||||
MOCK_CONFIG = {
|
||||
SAMSUNGTV_DOMAIN: [
|
||||
{
|
||||
CONF_HOST: "fake",
|
||||
CONF_NAME: "fake",
|
||||
CONF_PORT: 55000,
|
||||
CONF_ON_ACTION: [{"delay": "00:00:01"}],
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
MOCK_CONFIGWS = {
|
||||
SAMSUNGTV_DOMAIN: [
|
||||
{
|
||||
CONF_HOST: "fake",
|
||||
@@ -75,14 +87,35 @@ MOCK_CONFIG_NOTURNON = {
|
||||
@pytest.fixture(name="remote")
|
||||
def remote_fixture():
|
||||
"""Patch the samsungctl Remote."""
|
||||
with patch("homeassistant.components.samsungtv.config_flow.Remote"), patch(
|
||||
with patch(
|
||||
"homeassistant.components.samsungtv.bridge.Remote"
|
||||
) as remote_class, patch(
|
||||
"homeassistant.components.samsungtv.config_flow.socket"
|
||||
) as socket1, patch(
|
||||
"homeassistant.components.samsungtv.media_player.SamsungRemote"
|
||||
) as remote_class, patch(
|
||||
"homeassistant.components.samsungtv.socket"
|
||||
) as socket2:
|
||||
remote = mock.Mock()
|
||||
remote.__enter__ = mock.Mock()
|
||||
remote.__exit__ = mock.Mock()
|
||||
remote_class.return_value = remote
|
||||
socket1.gethostbyname.return_value = "FAKE_IP_ADDRESS"
|
||||
socket2.gethostbyname.return_value = "FAKE_IP_ADDRESS"
|
||||
yield remote
|
||||
|
||||
|
||||
@pytest.fixture(name="remotews")
|
||||
def remotews_fixture():
|
||||
"""Patch the samsungtvws SamsungTVWS."""
|
||||
with patch(
|
||||
"homeassistant.components.samsungtv.bridge.SamsungTVWS"
|
||||
) as remote_class, patch(
|
||||
"homeassistant.components.samsungtv.config_flow.socket"
|
||||
) as socket1, patch(
|
||||
"homeassistant.components.samsungtv.socket"
|
||||
) as socket2:
|
||||
remote = mock.Mock()
|
||||
remote.__enter__ = mock.Mock()
|
||||
remote.__exit__ = mock.Mock()
|
||||
remote_class.return_value = remote
|
||||
socket1.gethostbyname.return_value = "FAKE_IP_ADDRESS"
|
||||
socket2.gethostbyname.return_value = "FAKE_IP_ADDRESS"
|
||||
@@ -140,7 +173,7 @@ async def test_update_off(hass, remote, mock_now):
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.samsungtv.media_player.SamsungRemote",
|
||||
"homeassistant.components.samsungtv.bridge.Remote",
|
||||
side_effect=[OSError("Boom"), mock.DEFAULT],
|
||||
):
|
||||
|
||||
@@ -154,14 +187,13 @@ async def test_update_off(hass, remote, mock_now):
|
||||
|
||||
|
||||
async def test_update_access_denied(hass, remote, mock_now):
|
||||
"""Testing update tv unhandled response exception."""
|
||||
"""Testing update tv access denied exception."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.samsungtv.media_player.SamsungRemote",
|
||||
"homeassistant.components.samsungtv.bridge.Remote",
|
||||
side_effect=exceptions.AccessDenied("Boom"),
|
||||
):
|
||||
|
||||
next_update = mock_now + timedelta(minutes=5)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=next_update):
|
||||
async_fire_time_changed(hass, next_update)
|
||||
@@ -174,12 +206,36 @@ async def test_update_access_denied(hass, remote, mock_now):
|
||||
]
|
||||
|
||||
|
||||
async def test_update_connection_failure(hass, remotews, mock_now):
|
||||
"""Testing update tv connection failure exception."""
|
||||
with patch(
|
||||
"homeassistant.components.samsungtv.bridge.Remote",
|
||||
side_effect=[OSError("Boom"), mock.DEFAULT],
|
||||
):
|
||||
await setup_samsungtv(hass, MOCK_CONFIGWS)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.samsungtv.bridge.SamsungTVWS",
|
||||
side_effect=ConnectionFailure("Boom"),
|
||||
):
|
||||
next_update = mock_now + timedelta(minutes=5)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=next_update):
|
||||
async_fire_time_changed(hass, next_update)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert [
|
||||
flow
|
||||
for flow in hass.config_entries.flow.async_progress()
|
||||
if flow["context"]["source"] == "reauth"
|
||||
]
|
||||
|
||||
|
||||
async def test_update_unhandled_response(hass, remote, mock_now):
|
||||
"""Testing update tv unhandled response exception."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.samsungtv.media_player.SamsungRemote",
|
||||
"homeassistant.components.samsungtv.bridge.Remote",
|
||||
side_effect=[exceptions.UnhandledResponse("Boom"), mock.DEFAULT],
|
||||
):
|
||||
|
||||
@@ -334,36 +390,30 @@ async def test_device_class(hass, remote):
|
||||
assert state.attributes[ATTR_DEVICE_CLASS] == DEVICE_CLASS_TV
|
||||
|
||||
|
||||
async def test_turn_off_websocket(hass, remote):
|
||||
async def test_turn_off_websocket(hass, remotews):
|
||||
"""Test for turn_off."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
with patch(
|
||||
"homeassistant.components.samsungtv.bridge.Remote",
|
||||
side_effect=[OSError("Boom"), mock.DEFAULT],
|
||||
):
|
||||
await setup_samsungtv(hass, MOCK_CONFIGWS)
|
||||
assert await hass.services.async_call(
|
||||
DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: ENTITY_ID}, True
|
||||
)
|
||||
# key called
|
||||
assert remotews.send_key.call_count == 1
|
||||
assert remotews.send_key.call_args_list == [call("KEY_POWER")]
|
||||
|
||||
|
||||
async def test_turn_off_legacy(hass, remote):
|
||||
"""Test for turn_off."""
|
||||
await setup_samsungtv(hass, MOCK_CONFIG_NOTURNON)
|
||||
assert await hass.services.async_call(
|
||||
DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: ENTITY_ID}, True
|
||||
DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: ENTITY_ID_NOTURNON}, True
|
||||
)
|
||||
# key called
|
||||
assert remote.control.call_count == 1
|
||||
assert remote.control.call_args_list == [call("KEY_POWER")]
|
||||
|
||||
|
||||
async def test_turn_off_legacy(hass):
|
||||
"""Test for turn_off."""
|
||||
with patch("homeassistant.components.samsungtv.config_flow.socket"), patch(
|
||||
"homeassistant.components.samsungtv.config_flow.Remote",
|
||||
side_effect=[OSError("Boom"), mock.DEFAULT],
|
||||
), patch(
|
||||
"homeassistant.components.samsungtv.media_player.SamsungRemote"
|
||||
) as remote_class, patch(
|
||||
"homeassistant.components.samsungtv.socket"
|
||||
):
|
||||
remote = mock.Mock()
|
||||
remote_class.return_value = remote
|
||||
await setup_samsungtv(hass, MOCK_CONFIG_NOTURNON)
|
||||
assert await hass.services.async_call(
|
||||
DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: ENTITY_ID_NOTURNON}, True
|
||||
)
|
||||
# key called
|
||||
assert remote.control.call_count == 1
|
||||
assert remote.control.call_args_list == [call("KEY_POWEROFF")]
|
||||
assert remote.control.call_args_list == [call("KEY_POWEROFF")]
|
||||
|
||||
|
||||
async def test_turn_off_os_error(hass, remote, caplog):
|
||||
@@ -374,7 +424,7 @@ async def test_turn_off_os_error(hass, remote, caplog):
|
||||
assert await hass.services.async_call(
|
||||
DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: ENTITY_ID}, True
|
||||
)
|
||||
assert "Could not establish connection." in caplog.text
|
||||
assert "Could not establish connection" in caplog.text
|
||||
|
||||
|
||||
async def test_volume_up(hass, remote):
|
||||
@@ -526,11 +576,12 @@ async def test_play_media(hass, remote):
|
||||
|
||||
async def test_play_media_invalid_type(hass, remote):
|
||||
"""Test for play_media with invalid media type."""
|
||||
with patch(
|
||||
"homeassistant.components.samsungtv.media_player.SamsungRemote"
|
||||
) as remote, patch("homeassistant.components.samsungtv.config_flow.socket"):
|
||||
with patch("homeassistant.components.samsungtv.bridge.Remote") as remote, patch(
|
||||
"homeassistant.components.samsungtv.config_flow.socket"
|
||||
):
|
||||
url = "https://example.com"
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
remote.reset_mock()
|
||||
assert await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_PLAY_MEDIA,
|
||||
@@ -549,11 +600,12 @@ async def test_play_media_invalid_type(hass, remote):
|
||||
|
||||
async def test_play_media_channel_as_string(hass, remote):
|
||||
"""Test for play_media with invalid channel as string."""
|
||||
with patch(
|
||||
"homeassistant.components.samsungtv.media_player.SamsungRemote"
|
||||
) as remote, patch("homeassistant.components.samsungtv.config_flow.socket"):
|
||||
with patch("homeassistant.components.samsungtv.bridge.Remote") as remote, patch(
|
||||
"homeassistant.components.samsungtv.config_flow.socket"
|
||||
):
|
||||
url = "https://example.com"
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
remote.reset_mock()
|
||||
assert await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_PLAY_MEDIA,
|
||||
@@ -572,10 +624,11 @@ async def test_play_media_channel_as_string(hass, remote):
|
||||
|
||||
async def test_play_media_channel_as_non_positive(hass, remote):
|
||||
"""Test for play_media with invalid channel as non positive integer."""
|
||||
with patch(
|
||||
"homeassistant.components.samsungtv.media_player.SamsungRemote"
|
||||
) as remote, patch("homeassistant.components.samsungtv.config_flow.socket"):
|
||||
with patch("homeassistant.components.samsungtv.bridge.Remote") as remote, patch(
|
||||
"homeassistant.components.samsungtv.config_flow.socket"
|
||||
):
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
remote.reset_mock()
|
||||
assert await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_PLAY_MEDIA,
|
||||
@@ -610,10 +663,11 @@ async def test_select_source(hass, remote):
|
||||
|
||||
async def test_select_source_invalid_source(hass, remote):
|
||||
"""Test for select_source with invalid source."""
|
||||
with patch(
|
||||
"homeassistant.components.samsungtv.media_player.SamsungRemote"
|
||||
) as remote, patch("homeassistant.components.samsungtv.config_flow.socket"):
|
||||
with patch("homeassistant.components.samsungtv.bridge.Remote") as remote, patch(
|
||||
"homeassistant.components.samsungtv.config_flow.socket"
|
||||
):
|
||||
await setup_samsungtv(hass, MOCK_CONFIG)
|
||||
remote.reset_mock()
|
||||
assert await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_SELECT_SOURCE,
|
||||
|
||||
Reference in new issue
Block a user