2020-01-10 02:19:10 +00:00
|
|
|
"""Tests for the Samsung TV Integration."""
|
2022-03-04 22:06:15 +00:00
|
|
|
from copy import deepcopy
|
|
|
|
from unittest.mock import Mock, patch
|
2022-02-17 20:47:58 +00:00
|
|
|
|
|
|
|
import pytest
|
2021-01-01 21:31:56 +00:00
|
|
|
|
2020-01-10 02:19:10 +00:00
|
|
|
from homeassistant.components.media_player.const import DOMAIN, SUPPORT_TURN_ON
|
|
|
|
from homeassistant.components.samsungtv.const import (
|
|
|
|
CONF_ON_ACTION,
|
|
|
|
DOMAIN as SAMSUNGTV_DOMAIN,
|
2021-05-22 15:41:18 +00:00
|
|
|
METHOD_WEBSOCKET,
|
2020-01-10 02:19:10 +00:00
|
|
|
)
|
|
|
|
from homeassistant.components.samsungtv.media_player import SUPPORT_SAMSUNGTV
|
2021-06-24 18:15:16 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntryState
|
2020-01-10 02:19:10 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ENTITY_ID,
|
|
|
|
ATTR_SUPPORTED_FEATURES,
|
|
|
|
CONF_HOST,
|
2021-06-24 18:15:16 +00:00
|
|
|
CONF_MAC,
|
2021-05-22 15:41:18 +00:00
|
|
|
CONF_METHOD,
|
2020-01-10 02:19:10 +00:00
|
|
|
CONF_NAME,
|
|
|
|
SERVICE_VOLUME_UP,
|
|
|
|
)
|
2021-05-22 15:41:18 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2020-01-10 02:19:10 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
|
|
|
|
|
|
|
ENTITY_ID = f"{DOMAIN}.fake_name"
|
|
|
|
MOCK_CONFIG = {
|
|
|
|
SAMSUNGTV_DOMAIN: [
|
|
|
|
{
|
|
|
|
CONF_HOST: "fake_host",
|
|
|
|
CONF_NAME: "fake_name",
|
|
|
|
CONF_ON_ACTION: [{"delay": "00:00:01"}],
|
2021-05-22 15:41:18 +00:00
|
|
|
CONF_METHOD: METHOD_WEBSOCKET,
|
2020-01-10 02:19:10 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2021-06-24 18:15:16 +00:00
|
|
|
MOCK_CONFIG_WITHOUT_PORT = {
|
|
|
|
SAMSUNGTV_DOMAIN: [
|
|
|
|
{
|
|
|
|
CONF_HOST: "fake_host",
|
|
|
|
CONF_NAME: "fake",
|
|
|
|
CONF_ON_ACTION: [{"delay": "00:00:01"}],
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2020-01-10 02:19:10 +00:00
|
|
|
REMOTE_CALL = {
|
|
|
|
"name": "HomeAssistant",
|
2020-02-03 19:34:02 +00:00
|
|
|
"description": "HomeAssistant",
|
2020-01-10 02:19:10 +00:00
|
|
|
"id": "ha.component.samsung",
|
|
|
|
"host": MOCK_CONFIG[SAMSUNGTV_DOMAIN][0][CONF_HOST],
|
2021-05-22 15:41:18 +00:00
|
|
|
"method": "legacy",
|
2020-03-12 09:29:11 +00:00
|
|
|
"port": None,
|
2020-01-10 02:19:10 +00:00
|
|
|
"timeout": 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-27 20:30:45 +00:00
|
|
|
@pytest.fixture(name="autouse_rest_api", autouse=True)
|
|
|
|
def autouse_rest_api(rest_api) -> Mock:
|
|
|
|
"""Enable auto use of the rest api fixture for these tests."""
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("remotews", "remoteencws_failing")
|
2022-02-17 20:47:58 +00:00
|
|
|
async def test_setup(hass: HomeAssistant) -> None:
|
2020-01-10 02:19:10 +00:00
|
|
|
"""Test Samsung TV integration is setup."""
|
2022-02-16 08:29:52 +00:00
|
|
|
await async_setup_component(hass, SAMSUNGTV_DOMAIN, MOCK_CONFIG)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
state = hass.states.get(ENTITY_ID)
|
2020-01-10 02:19:10 +00:00
|
|
|
|
2022-02-16 08:29:52 +00:00
|
|
|
# test name and turn_on
|
|
|
|
assert state
|
|
|
|
assert state.name == "fake_name"
|
|
|
|
assert (
|
|
|
|
state.attributes[ATTR_SUPPORTED_FEATURES] == SUPPORT_SAMSUNGTV | SUPPORT_TURN_ON
|
|
|
|
)
|
2020-01-10 02:19:10 +00:00
|
|
|
|
2022-02-16 08:29:52 +00:00
|
|
|
# test host and port
|
|
|
|
assert await hass.services.async_call(
|
|
|
|
DOMAIN, SERVICE_VOLUME_UP, {ATTR_ENTITY_ID: ENTITY_ID}, True
|
|
|
|
)
|
2020-01-10 02:19:10 +00:00
|
|
|
|
|
|
|
|
2022-02-17 20:47:58 +00:00
|
|
|
async def test_setup_from_yaml_without_port_device_offline(hass: HomeAssistant) -> None:
|
2021-06-24 18:15:16 +00:00
|
|
|
"""Test import from yaml when the device is offline."""
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.samsungtv.bridge.Remote", side_effect=OSError
|
2022-03-22 10:11:41 +00:00
|
|
|
), patch(
|
|
|
|
"homeassistant.components.samsungtv.bridge.SamsungTVEncryptedWSAsyncRemote.start_listening",
|
|
|
|
side_effect=OSError,
|
2021-06-24 18:15:16 +00:00
|
|
|
), patch(
|
2022-03-02 19:30:33 +00:00
|
|
|
"homeassistant.components.samsungtv.bridge.SamsungTVWSAsyncRemote.open",
|
2021-06-24 18:15:16 +00:00
|
|
|
side_effect=OSError,
|
2021-11-24 10:25:25 +00:00
|
|
|
), patch(
|
2022-02-22 18:31:16 +00:00
|
|
|
"homeassistant.components.samsungtv.bridge.SamsungTVWSBridge.async_device_info",
|
2021-11-24 10:25:25 +00:00
|
|
|
return_value=None,
|
2021-06-24 18:15:16 +00:00
|
|
|
):
|
|
|
|
await async_setup_component(hass, SAMSUNGTV_DOMAIN, MOCK_CONFIG)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
config_entries_domain = hass.config_entries.async_entries(SAMSUNGTV_DOMAIN)
|
|
|
|
assert len(config_entries_domain) == 1
|
|
|
|
assert config_entries_domain[0].state == ConfigEntryState.SETUP_RETRY
|
|
|
|
|
|
|
|
|
2022-03-27 20:30:45 +00:00
|
|
|
@pytest.mark.usefixtures("remotews", "remoteencws_failing")
|
2022-02-17 20:47:58 +00:00
|
|
|
async def test_setup_from_yaml_without_port_device_online(hass: HomeAssistant) -> None:
|
2021-06-24 18:15:16 +00:00
|
|
|
"""Test import from yaml when the device is online."""
|
2022-02-16 08:29:52 +00:00
|
|
|
await async_setup_component(hass, SAMSUNGTV_DOMAIN, MOCK_CONFIG)
|
|
|
|
await hass.async_block_till_done()
|
2021-06-24 18:15:16 +00:00
|
|
|
|
|
|
|
config_entries_domain = hass.config_entries.async_entries(SAMSUNGTV_DOMAIN)
|
|
|
|
assert len(config_entries_domain) == 1
|
2022-03-03 18:07:08 +00:00
|
|
|
assert config_entries_domain[0].data[CONF_MAC] == "aa:bb:ww:ii:ff:ii"
|
2021-06-24 18:15:16 +00:00
|
|
|
|
|
|
|
|
2022-02-17 20:47:58 +00:00
|
|
|
@pytest.mark.usefixtures("remote")
|
|
|
|
async def test_setup_duplicate_config(
|
|
|
|
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
|
|
|
) -> None:
|
2020-01-10 02:19:10 +00:00
|
|
|
"""Test duplicate setup of platform."""
|
2022-02-16 08:29:52 +00:00
|
|
|
duplicate = {
|
2020-01-10 02:19:10 +00:00
|
|
|
SAMSUNGTV_DOMAIN: [
|
|
|
|
MOCK_CONFIG[SAMSUNGTV_DOMAIN][0],
|
|
|
|
MOCK_CONFIG[SAMSUNGTV_DOMAIN][0],
|
|
|
|
]
|
|
|
|
}
|
2022-02-16 08:29:52 +00:00
|
|
|
await async_setup_component(hass, SAMSUNGTV_DOMAIN, duplicate)
|
2020-01-10 02:19:10 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert hass.states.get(ENTITY_ID) is None
|
2021-10-07 10:58:00 +00:00
|
|
|
assert len(hass.states.async_all("media_player")) == 0
|
2020-01-10 02:19:10 +00:00
|
|
|
assert "duplicate host entries found" in caplog.text
|
|
|
|
|
|
|
|
|
2022-03-27 20:30:45 +00:00
|
|
|
@pytest.mark.usefixtures("remote", "remotews", "remoteencws_failing")
|
2022-02-17 20:47:58 +00:00
|
|
|
async def test_setup_duplicate_entries(hass: HomeAssistant) -> None:
|
2020-01-10 02:19:10 +00:00
|
|
|
"""Test duplicate setup of platform."""
|
|
|
|
await async_setup_component(hass, SAMSUNGTV_DOMAIN, MOCK_CONFIG)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert hass.states.get(ENTITY_ID)
|
2021-10-07 10:58:00 +00:00
|
|
|
assert len(hass.states.async_all("media_player")) == 1
|
2020-01-10 02:19:10 +00:00
|
|
|
await async_setup_component(hass, SAMSUNGTV_DOMAIN, MOCK_CONFIG)
|
2021-10-07 10:58:00 +00:00
|
|
|
assert len(hass.states.async_all("media_player")) == 1
|
2022-03-04 22:06:15 +00:00
|
|
|
|
|
|
|
|
2022-03-27 20:30:45 +00:00
|
|
|
@pytest.mark.usefixtures("remotews", "remoteencws_failing")
|
2022-03-04 22:06:15 +00:00
|
|
|
async def test_setup_h_j_model(
|
|
|
|
hass: HomeAssistant, rest_api: Mock, caplog: pytest.LogCaptureFixture
|
|
|
|
) -> None:
|
|
|
|
"""Test Samsung TV integration is setup."""
|
|
|
|
device_info = deepcopy(rest_api.rest_device_info.return_value)
|
|
|
|
device_info["device"]["modelName"] = "UE48JU6400"
|
|
|
|
rest_api.rest_device_info.return_value = device_info
|
|
|
|
await async_setup_component(hass, SAMSUNGTV_DOMAIN, MOCK_CONFIG)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
state = hass.states.get(ENTITY_ID)
|
|
|
|
assert state
|
|
|
|
assert "H and J series use an encrypted protocol" in caplog.text
|