init home-assistant
This commit is contained in:
1
home-assistant/tests/components/androidtv/__init__.py
Normal file
1
home-assistant/tests/components/androidtv/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Tests for the androidtv component."""
|
||||
122
home-assistant/tests/components/androidtv/common.py
Normal file
122
home-assistant/tests/components/androidtv/common.py
Normal file
@@ -0,0 +1,122 @@
|
||||
"""Test code shared between test files."""
|
||||
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.components.androidtv.const import (
|
||||
CONF_ADB_SERVER_IP,
|
||||
CONF_ADB_SERVER_PORT,
|
||||
CONF_ADBKEY,
|
||||
DEFAULT_ADB_SERVER_PORT,
|
||||
DEFAULT_PORT,
|
||||
DEVICE_ANDROIDTV,
|
||||
DEVICE_FIRETV,
|
||||
DOMAIN,
|
||||
)
|
||||
from homeassistant.components.androidtv.entity import PREFIX_ANDROIDTV, PREFIX_FIRETV
|
||||
from homeassistant.const import CONF_DEVICE_CLASS, CONF_HOST, CONF_NAME, CONF_PORT
|
||||
from homeassistant.util import slugify
|
||||
|
||||
from . import patchers
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
ADB_PATCH_KEY = "patch_key"
|
||||
TEST_ENTITY_NAME = "entity_name"
|
||||
TEST_HOST_NAME = "127.0.0.1"
|
||||
|
||||
SHELL_RESPONSE_OFF = ""
|
||||
SHELL_RESPONSE_STANDBY = "1"
|
||||
|
||||
# Android device with Python ADB implementation
|
||||
CONFIG_ANDROID_PYTHON_ADB = {
|
||||
ADB_PATCH_KEY: patchers.KEY_PYTHON,
|
||||
TEST_ENTITY_NAME: f"{PREFIX_ANDROIDTV} {TEST_HOST_NAME}",
|
||||
DOMAIN: {
|
||||
CONF_HOST: TEST_HOST_NAME,
|
||||
CONF_PORT: DEFAULT_PORT,
|
||||
CONF_DEVICE_CLASS: DEVICE_ANDROIDTV,
|
||||
},
|
||||
}
|
||||
|
||||
# Android device with Python ADB implementation imported from YAML
|
||||
CONFIG_ANDROID_PYTHON_ADB_YAML = {
|
||||
ADB_PATCH_KEY: patchers.KEY_PYTHON,
|
||||
TEST_ENTITY_NAME: "ADB yaml import",
|
||||
DOMAIN: {
|
||||
CONF_NAME: "ADB yaml import",
|
||||
**CONFIG_ANDROID_PYTHON_ADB[DOMAIN],
|
||||
},
|
||||
}
|
||||
|
||||
# Android device with Python ADB implementation with custom adbkey
|
||||
CONFIG_ANDROID_PYTHON_ADB_KEY = {
|
||||
ADB_PATCH_KEY: patchers.KEY_PYTHON,
|
||||
TEST_ENTITY_NAME: CONFIG_ANDROID_PYTHON_ADB[TEST_ENTITY_NAME],
|
||||
DOMAIN: {
|
||||
**CONFIG_ANDROID_PYTHON_ADB[DOMAIN],
|
||||
CONF_ADBKEY: "user_provided_adbkey",
|
||||
},
|
||||
}
|
||||
|
||||
# Android device with ADB server
|
||||
CONFIG_ANDROID_ADB_SERVER = {
|
||||
ADB_PATCH_KEY: patchers.KEY_SERVER,
|
||||
TEST_ENTITY_NAME: f"{PREFIX_ANDROIDTV} {TEST_HOST_NAME}",
|
||||
DOMAIN: {
|
||||
CONF_HOST: TEST_HOST_NAME,
|
||||
CONF_PORT: DEFAULT_PORT,
|
||||
CONF_DEVICE_CLASS: DEVICE_ANDROIDTV,
|
||||
CONF_ADB_SERVER_IP: patchers.ADB_SERVER_HOST,
|
||||
CONF_ADB_SERVER_PORT: DEFAULT_ADB_SERVER_PORT,
|
||||
},
|
||||
}
|
||||
|
||||
# Fire TV device with Python ADB implementation
|
||||
CONFIG_FIRETV_PYTHON_ADB = {
|
||||
ADB_PATCH_KEY: patchers.KEY_PYTHON,
|
||||
TEST_ENTITY_NAME: f"{PREFIX_FIRETV} {TEST_HOST_NAME}",
|
||||
DOMAIN: {
|
||||
CONF_HOST: TEST_HOST_NAME,
|
||||
CONF_PORT: DEFAULT_PORT,
|
||||
CONF_DEVICE_CLASS: DEVICE_FIRETV,
|
||||
},
|
||||
}
|
||||
|
||||
# Fire TV device with ADB server
|
||||
CONFIG_FIRETV_ADB_SERVER = {
|
||||
ADB_PATCH_KEY: patchers.KEY_SERVER,
|
||||
TEST_ENTITY_NAME: f"{PREFIX_FIRETV} {TEST_HOST_NAME}",
|
||||
DOMAIN: {
|
||||
CONF_HOST: TEST_HOST_NAME,
|
||||
CONF_PORT: DEFAULT_PORT,
|
||||
CONF_DEVICE_CLASS: DEVICE_FIRETV,
|
||||
CONF_ADB_SERVER_IP: patchers.ADB_SERVER_HOST,
|
||||
CONF_ADB_SERVER_PORT: DEFAULT_ADB_SERVER_PORT,
|
||||
},
|
||||
}
|
||||
|
||||
CONFIG_ANDROID_DEFAULT = CONFIG_ANDROID_PYTHON_ADB
|
||||
CONFIG_FIRETV_DEFAULT = CONFIG_FIRETV_PYTHON_ADB
|
||||
|
||||
|
||||
def setup_mock_entry(
|
||||
config: dict[str, Any],
|
||||
entity_domain: str,
|
||||
*,
|
||||
options=None,
|
||||
version=1,
|
||||
minor_version=2,
|
||||
) -> tuple[str, str, MockConfigEntry]:
|
||||
"""Prepare mock entry for entities tests."""
|
||||
patch_key = config[ADB_PATCH_KEY]
|
||||
entity_id = f"{entity_domain}.{slugify(config[TEST_ENTITY_NAME])}"
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data=config[DOMAIN],
|
||||
unique_id="a1:b1:c1:d1:e1:f1",
|
||||
options=options,
|
||||
version=version,
|
||||
minor_version=minor_version,
|
||||
)
|
||||
|
||||
return patch_key, entity_id, config_entry
|
||||
38
home-assistant/tests/components/androidtv/conftest.py
Normal file
38
home-assistant/tests/components/androidtv/conftest.py
Normal file
@@ -0,0 +1,38 @@
|
||||
"""Fixtures for the Android TV integration tests."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from . import patchers
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def adb_device_tcp_fixture() -> Generator[None]:
|
||||
"""Patch ADB Device TCP."""
|
||||
with patch(
|
||||
"androidtv.adb_manager.adb_manager_async.AdbDeviceTcpAsync",
|
||||
patchers.AdbDeviceTcpAsyncFake,
|
||||
):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def load_adbkey_fixture() -> Generator[None]:
|
||||
"""Patch load_adbkey."""
|
||||
with patch(
|
||||
"homeassistant.components.androidtv.ADBPythonSync.load_adbkey",
|
||||
return_value="signer for testing",
|
||||
):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def keygen_fixture() -> Generator[None]:
|
||||
"""Patch keygen."""
|
||||
with patch(
|
||||
"homeassistant.components.androidtv.keygen",
|
||||
return_value=Mock(),
|
||||
):
|
||||
yield
|
||||
208
home-assistant/tests/components/androidtv/patchers.py
Normal file
208
home-assistant/tests/components/androidtv/patchers.py
Normal file
@@ -0,0 +1,208 @@
|
||||
"""Define patches used for androidtv tests."""
|
||||
|
||||
from typing import Any
|
||||
from unittest.mock import patch
|
||||
|
||||
from androidtv.adb_manager.adb_manager_async import DeviceAsync
|
||||
from androidtv.constants import CMD_DEVICE_PROPERTIES, CMD_MAC_ETH0, CMD_MAC_WLAN0
|
||||
|
||||
from homeassistant.components.androidtv.const import (
|
||||
DEFAULT_ADB_SERVER_PORT,
|
||||
DEVICE_ANDROIDTV,
|
||||
DEVICE_FIRETV,
|
||||
)
|
||||
|
||||
ADB_SERVER_HOST = "127.0.0.1"
|
||||
KEY_PYTHON = "python"
|
||||
KEY_SERVER = "server"
|
||||
|
||||
ADB_DEVICE_TCP_ASYNC_FAKE = "AdbDeviceTcpAsyncFake"
|
||||
DEVICE_ASYNC_FAKE = "DeviceAsyncFake"
|
||||
|
||||
PROPS_DEV_INFO = "fake\nfake\n0123456\nfake"
|
||||
PROPS_DEV_MAC = "ether ab:cd:ef:gh:ij:kl brd"
|
||||
|
||||
|
||||
class AdbDeviceTcpAsyncFake:
|
||||
"""A fake of the `adb_shell.adb_device_async.AdbDeviceTcpAsync` class."""
|
||||
|
||||
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
||||
"""Initialize a fake `adb_shell.adb_device_async.AdbDeviceTcpAsync` instance."""
|
||||
self.available = False
|
||||
|
||||
async def close(self):
|
||||
"""Close the socket connection."""
|
||||
self.available = False
|
||||
|
||||
async def connect(self, *args, **kwargs):
|
||||
"""Try to connect to a device."""
|
||||
raise NotImplementedError
|
||||
|
||||
async def shell(self, cmd, *args, **kwargs) -> bytes | str | None:
|
||||
"""Send an ADB shell command."""
|
||||
return None
|
||||
|
||||
|
||||
class ClientAsyncFakeSuccess:
|
||||
"""A fake of the `ClientAsync` class when the connection and shell commands succeed."""
|
||||
|
||||
def __init__(self, host=ADB_SERVER_HOST, port=DEFAULT_ADB_SERVER_PORT) -> None:
|
||||
"""Initialize a `ClientAsyncFakeSuccess` instance."""
|
||||
self._devices = []
|
||||
|
||||
async def device(self, serial):
|
||||
"""Mock the `ClientAsync.device` method when the device is connected via ADB."""
|
||||
device = DeviceAsyncFake(serial)
|
||||
self._devices.append(device)
|
||||
return device
|
||||
|
||||
|
||||
class ClientAsyncFakeFail:
|
||||
"""A fake of the `ClientAsync` class when the connection and shell commands fail."""
|
||||
|
||||
def __init__(self, host=ADB_SERVER_HOST, port=DEFAULT_ADB_SERVER_PORT) -> None:
|
||||
"""Initialize a `ClientAsyncFakeFail` instance."""
|
||||
self._devices = []
|
||||
|
||||
async def device(self, serial) -> DeviceAsync | None:
|
||||
"""Mock the `ClientAsync.device` method when the device is not connected via ADB."""
|
||||
self._devices = []
|
||||
return None
|
||||
|
||||
|
||||
class DeviceAsyncFake:
|
||||
"""A fake of the `DeviceAsync` class."""
|
||||
|
||||
def __init__(self, host) -> None:
|
||||
"""Initialize a `DeviceAsyncFake` instance."""
|
||||
self.host = host
|
||||
|
||||
async def shell(self, cmd):
|
||||
"""Send an ADB shell command."""
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
def patch_connect(success):
|
||||
"""Mock the `adb_shell.adb_device_async.AdbDeviceTcpAsync` and `ClientAsync` classes."""
|
||||
|
||||
async def connect_success_python(self, *args, **kwargs):
|
||||
"""Mock the `AdbDeviceTcpAsyncFake.connect` method when it succeeds."""
|
||||
self.available = True
|
||||
|
||||
async def connect_fail_python(self, *args, **kwargs):
|
||||
"""Mock the `AdbDeviceTcpAsyncFake.connect` method when it fails."""
|
||||
raise OSError
|
||||
|
||||
if success:
|
||||
return {
|
||||
KEY_PYTHON: patch(
|
||||
f"{__name__}.{ADB_DEVICE_TCP_ASYNC_FAKE}.connect",
|
||||
connect_success_python,
|
||||
),
|
||||
KEY_SERVER: patch(
|
||||
"androidtv.adb_manager.adb_manager_async.ClientAsync",
|
||||
ClientAsyncFakeSuccess,
|
||||
),
|
||||
}
|
||||
return {
|
||||
KEY_PYTHON: patch(
|
||||
f"{__name__}.{ADB_DEVICE_TCP_ASYNC_FAKE}.connect", connect_fail_python
|
||||
),
|
||||
KEY_SERVER: patch(
|
||||
"androidtv.adb_manager.adb_manager_async.ClientAsync", ClientAsyncFakeFail
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
def patch_shell(response=None, error=False, mac_eth=False, exc=None):
|
||||
"""Mock the `AdbDeviceTcpAsyncFake.shell` and `DeviceAsyncFake.shell` methods."""
|
||||
|
||||
async def shell_success(self, cmd, *args, **kwargs):
|
||||
"""Mock the `AdbDeviceTcpAsyncFake.shell` and `DeviceAsyncFake.shell` methods when they are successful."""
|
||||
self.shell_cmd = cmd
|
||||
if cmd == CMD_DEVICE_PROPERTIES:
|
||||
return PROPS_DEV_INFO
|
||||
if cmd == CMD_MAC_WLAN0:
|
||||
return PROPS_DEV_MAC
|
||||
if cmd == CMD_MAC_ETH0:
|
||||
return PROPS_DEV_MAC if mac_eth else None
|
||||
return response
|
||||
|
||||
async def shell_fail_python(self, cmd, *args, **kwargs):
|
||||
"""Mock the `AdbDeviceTcpAsyncFake.shell` method when it fails."""
|
||||
self.shell_cmd = cmd
|
||||
raise exc or ValueError
|
||||
|
||||
async def shell_fail_server(self, cmd):
|
||||
"""Mock the `DeviceAsyncFake.shell` method when it fails."""
|
||||
self.shell_cmd = cmd
|
||||
raise ConnectionResetError
|
||||
|
||||
if not error:
|
||||
return {
|
||||
KEY_PYTHON: patch(
|
||||
f"{__name__}.{ADB_DEVICE_TCP_ASYNC_FAKE}.shell", shell_success
|
||||
),
|
||||
KEY_SERVER: patch(f"{__name__}.{DEVICE_ASYNC_FAKE}.shell", shell_success),
|
||||
}
|
||||
return {
|
||||
KEY_PYTHON: patch(
|
||||
f"{__name__}.{ADB_DEVICE_TCP_ASYNC_FAKE}.shell", shell_fail_python
|
||||
),
|
||||
KEY_SERVER: patch(f"{__name__}.{DEVICE_ASYNC_FAKE}.shell", shell_fail_server),
|
||||
}
|
||||
|
||||
|
||||
def patch_androidtv_update(
|
||||
state,
|
||||
current_app,
|
||||
running_apps,
|
||||
device,
|
||||
is_volume_muted,
|
||||
volume_level,
|
||||
hdmi_input,
|
||||
):
|
||||
"""Patch the `AndroidTV.update()` method."""
|
||||
return {
|
||||
DEVICE_ANDROIDTV: patch(
|
||||
"androidtv.androidtv.androidtv_async.AndroidTVAsync.update",
|
||||
return_value=(
|
||||
state,
|
||||
current_app,
|
||||
running_apps,
|
||||
device,
|
||||
is_volume_muted,
|
||||
volume_level,
|
||||
hdmi_input,
|
||||
),
|
||||
),
|
||||
DEVICE_FIRETV: patch(
|
||||
"androidtv.firetv.firetv_async.FireTVAsync.update",
|
||||
return_value=(state, current_app, running_apps, hdmi_input),
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
def isfile(filepath):
|
||||
"""Mock `os.path.isfile`."""
|
||||
return filepath.endswith("adbkey")
|
||||
|
||||
|
||||
PATCH_SCREENCAP = patch(
|
||||
"androidtv.basetv.basetv_async.BaseTVAsync.adb_screencap",
|
||||
return_value=b"image",
|
||||
)
|
||||
PATCH_SETUP_ENTRY = patch(
|
||||
"homeassistant.components.androidtv.async_setup_entry",
|
||||
return_value=True,
|
||||
)
|
||||
PATCH_ACCESS = patch("homeassistant.components.androidtv.os.access", return_value=True)
|
||||
PATCH_ISFILE = patch("homeassistant.components.androidtv.os.path.isfile", isfile)
|
||||
PATCH_LAUNCH_APP = patch("androidtv.basetv.basetv_async.BaseTVAsync.launch_app")
|
||||
PATCH_STOP_APP = patch("androidtv.basetv.basetv_async.BaseTVAsync.stop_app")
|
||||
|
||||
# Cause the update to raise an unexpected type of exception
|
||||
PATCH_ANDROIDTV_UPDATE_EXCEPTION = patch(
|
||||
"androidtv.androidtv.androidtv_async.AndroidTVAsync.update",
|
||||
side_effect=ZeroDivisionError,
|
||||
)
|
||||
520
home-assistant/tests/components/androidtv/test_config_flow.py
Normal file
520
home-assistant/tests/components/androidtv/test_config_flow.py
Normal file
@@ -0,0 +1,520 @@
|
||||
"""Tests for the AndroidTV config flow."""
|
||||
|
||||
from typing import Any
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.androidtv.config_flow import (
|
||||
APPS_NEW_ID,
|
||||
CONF_APP_DELETE,
|
||||
CONF_APP_ID,
|
||||
CONF_APP_NAME,
|
||||
CONF_RULE_DELETE,
|
||||
CONF_RULE_ID,
|
||||
CONF_RULE_VALUES,
|
||||
RULES_NEW_ID,
|
||||
)
|
||||
from homeassistant.components.androidtv.const import (
|
||||
CONF_ADB_SERVER_IP,
|
||||
CONF_ADB_SERVER_PORT,
|
||||
CONF_ADBKEY,
|
||||
CONF_APPS,
|
||||
CONF_EXCLUDE_UNNAMED_APPS,
|
||||
CONF_GET_SOURCES,
|
||||
CONF_SCREENCAP_INTERVAL,
|
||||
CONF_STATE_DETECTION_RULES,
|
||||
CONF_TURN_OFF_COMMAND,
|
||||
CONF_TURN_ON_COMMAND,
|
||||
DEFAULT_ADB_SERVER_PORT,
|
||||
DEFAULT_PORT,
|
||||
DEVICE_ANDROIDTV,
|
||||
DOMAIN,
|
||||
PROP_ETHMAC,
|
||||
PROP_WIFIMAC,
|
||||
)
|
||||
from homeassistant.config_entries import SOURCE_USER
|
||||
from homeassistant.const import CONF_DEVICE_CLASS, CONF_HOST, CONF_PORT
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
from .patchers import PATCH_ACCESS, PATCH_ISFILE, PATCH_SETUP_ENTRY
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
ADBKEY = "adbkey"
|
||||
ETH_MAC = "a1:b1:c1:d1:e1:f1"
|
||||
WIFI_MAC = "a2:b2:c2:d2:e2:f2"
|
||||
INVALID_MAC = "ff:ff:ff:ff:ff:ff"
|
||||
HOST = "127.0.0.1"
|
||||
VALID_DETECT_RULE = [{"paused": {"media_session_state": 3}}]
|
||||
|
||||
# Android device with Python ADB implementation
|
||||
CONFIG_PYTHON_ADB = {
|
||||
CONF_HOST: HOST,
|
||||
CONF_PORT: DEFAULT_PORT,
|
||||
CONF_DEVICE_CLASS: DEVICE_ANDROIDTV,
|
||||
}
|
||||
|
||||
# Android device with ADB server
|
||||
CONFIG_ADB_SERVER = {
|
||||
CONF_HOST: HOST,
|
||||
CONF_PORT: DEFAULT_PORT,
|
||||
CONF_DEVICE_CLASS: DEVICE_ANDROIDTV,
|
||||
CONF_ADB_SERVER_IP: "127.0.0.1",
|
||||
CONF_ADB_SERVER_PORT: DEFAULT_ADB_SERVER_PORT,
|
||||
}
|
||||
|
||||
CONNECT_METHOD = (
|
||||
"homeassistant.components.androidtv.config_flow.async_connect_androidtv"
|
||||
)
|
||||
|
||||
|
||||
class MockConfigDevice:
|
||||
"""Mock class to emulate Android device."""
|
||||
|
||||
def __init__(self, eth_mac=ETH_MAC, wifi_mac=None) -> None:
|
||||
"""Initialize a fake device to test config flow."""
|
||||
self.available = True
|
||||
self.device_properties = {PROP_ETHMAC: eth_mac, PROP_WIFIMAC: wifi_mac}
|
||||
|
||||
async def adb_close(self):
|
||||
"""Fake method to close connection."""
|
||||
self.available = False
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("config", "eth_mac", "wifi_mac"),
|
||||
[
|
||||
(CONFIG_PYTHON_ADB, ETH_MAC, None),
|
||||
(CONFIG_ADB_SERVER, ETH_MAC, None),
|
||||
(CONFIG_PYTHON_ADB, None, WIFI_MAC),
|
||||
(CONFIG_ADB_SERVER, None, WIFI_MAC),
|
||||
(CONFIG_PYTHON_ADB, ETH_MAC, WIFI_MAC),
|
||||
(CONFIG_ADB_SERVER, ETH_MAC, WIFI_MAC),
|
||||
],
|
||||
)
|
||||
async def test_user(
|
||||
hass: HomeAssistant,
|
||||
config: dict[str, Any],
|
||||
eth_mac: str | None,
|
||||
wifi_mac: str | None,
|
||||
) -> None:
|
||||
"""Test user config."""
|
||||
flow_result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER, "show_advanced_options": True}
|
||||
)
|
||||
assert flow_result["type"] is FlowResultType.FORM
|
||||
assert flow_result["step_id"] == "user"
|
||||
|
||||
# test with all provided
|
||||
with (
|
||||
patch(
|
||||
CONNECT_METHOD,
|
||||
return_value=(MockConfigDevice(eth_mac, wifi_mac), None),
|
||||
),
|
||||
PATCH_SETUP_ENTRY as mock_setup_entry,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
flow_result["flow_id"], user_input=config
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == HOST
|
||||
assert result["data"] == config
|
||||
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_user_adbkey(hass: HomeAssistant) -> None:
|
||||
"""Test user step with adbkey file."""
|
||||
config_data = CONFIG_PYTHON_ADB.copy()
|
||||
config_data[CONF_ADBKEY] = ADBKEY
|
||||
|
||||
with (
|
||||
patch(
|
||||
CONNECT_METHOD,
|
||||
return_value=(MockConfigDevice(), None),
|
||||
),
|
||||
PATCH_ISFILE,
|
||||
PATCH_ACCESS,
|
||||
PATCH_SETUP_ENTRY as mock_setup_entry,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER, "show_advanced_options": True},
|
||||
data=config_data,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == HOST
|
||||
assert result["data"] == config_data
|
||||
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_error_both_key_server(hass: HomeAssistant) -> None:
|
||||
"""Test we abort if both adb key and server are provided."""
|
||||
config_data = CONFIG_ADB_SERVER.copy()
|
||||
|
||||
config_data[CONF_ADBKEY] = ADBKEY
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER, "show_advanced_options": True},
|
||||
data=config_data,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"base": "key_and_server"}
|
||||
|
||||
with (
|
||||
patch(
|
||||
CONNECT_METHOD,
|
||||
return_value=(MockConfigDevice(), None),
|
||||
),
|
||||
PATCH_SETUP_ENTRY,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=CONFIG_ADB_SERVER
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result2["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result2["title"] == HOST
|
||||
assert result2["data"] == CONFIG_ADB_SERVER
|
||||
|
||||
|
||||
async def test_error_invalid_key(hass: HomeAssistant) -> None:
|
||||
"""Test we abort if component is already setup."""
|
||||
config_data = CONFIG_PYTHON_ADB.copy()
|
||||
config_data[CONF_ADBKEY] = ADBKEY
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER, "show_advanced_options": True},
|
||||
data=config_data,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"base": "adbkey_not_file"}
|
||||
|
||||
with (
|
||||
patch(
|
||||
CONNECT_METHOD,
|
||||
return_value=(MockConfigDevice(), None),
|
||||
),
|
||||
PATCH_SETUP_ENTRY,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=CONFIG_ADB_SERVER
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result2["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result2["title"] == HOST
|
||||
assert result2["data"] == CONFIG_ADB_SERVER
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("config", "eth_mac", "wifi_mac"),
|
||||
[
|
||||
(CONFIG_ADB_SERVER, None, None),
|
||||
(CONFIG_PYTHON_ADB, None, None),
|
||||
(CONFIG_ADB_SERVER, INVALID_MAC, None),
|
||||
(CONFIG_PYTHON_ADB, INVALID_MAC, None),
|
||||
(CONFIG_ADB_SERVER, None, INVALID_MAC),
|
||||
(CONFIG_PYTHON_ADB, None, INVALID_MAC),
|
||||
],
|
||||
)
|
||||
async def test_invalid_mac(
|
||||
hass: HomeAssistant,
|
||||
config: dict[str, Any],
|
||||
eth_mac: str | None,
|
||||
wifi_mac: str | None,
|
||||
) -> None:
|
||||
"""Test for invalid mac address."""
|
||||
with patch(
|
||||
CONNECT_METHOD,
|
||||
return_value=(MockConfigDevice(eth_mac, wifi_mac), None),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
data=config,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "invalid_unique_id"
|
||||
|
||||
|
||||
async def test_abort_if_host_exist(hass: HomeAssistant) -> None:
|
||||
"""Test we abort if component is already setup."""
|
||||
MockConfigEntry(
|
||||
domain=DOMAIN, data=CONFIG_ADB_SERVER, unique_id=ETH_MAC
|
||||
).add_to_hass(hass)
|
||||
|
||||
config_data = CONFIG_PYTHON_ADB
|
||||
# Should fail, same HOST
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
data=config_data,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_abort_if_unique_exist(hass: HomeAssistant) -> None:
|
||||
"""Test we abort if component is already setup."""
|
||||
config_data = CONFIG_ADB_SERVER.copy()
|
||||
config_data[CONF_HOST] = "127.0.0.2"
|
||||
MockConfigEntry(domain=DOMAIN, data=config_data, unique_id=ETH_MAC).add_to_hass(
|
||||
hass
|
||||
)
|
||||
|
||||
# Should fail, same SerialNo
|
||||
with patch(
|
||||
CONNECT_METHOD,
|
||||
return_value=(MockConfigDevice(), None),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
data=CONFIG_ADB_SERVER,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_on_connect_failed(hass: HomeAssistant) -> None:
|
||||
"""Test when we have errors connecting the router."""
|
||||
flow_result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER, "show_advanced_options": True},
|
||||
)
|
||||
|
||||
with patch(CONNECT_METHOD, return_value=(None, "Error")):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
flow_result["flow_id"], user_input=CONFIG_ADB_SERVER
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
with patch(
|
||||
CONNECT_METHOD,
|
||||
side_effect=TypeError,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=CONFIG_ADB_SERVER
|
||||
)
|
||||
assert result2["type"] is FlowResultType.FORM
|
||||
assert result2["errors"] == {"base": "unknown"}
|
||||
|
||||
with (
|
||||
patch(
|
||||
CONNECT_METHOD,
|
||||
return_value=(MockConfigDevice(), None),
|
||||
),
|
||||
PATCH_SETUP_ENTRY,
|
||||
):
|
||||
result3 = await hass.config_entries.flow.async_configure(
|
||||
result2["flow_id"], user_input=CONFIG_ADB_SERVER
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result3["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result3["title"] == HOST
|
||||
assert result3["data"] == CONFIG_ADB_SERVER
|
||||
|
||||
|
||||
async def test_options_flow(hass: HomeAssistant) -> None:
|
||||
"""Test config flow options."""
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data=CONFIG_ADB_SERVER,
|
||||
unique_id=ETH_MAC,
|
||||
options={
|
||||
CONF_APPS: {"app1": "App1"},
|
||||
CONF_STATE_DETECTION_RULES: {"com.plexapp.android": VALID_DETECT_RULE},
|
||||
},
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
with PATCH_SETUP_ENTRY:
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "init"
|
||||
|
||||
# test app form with existing app
|
||||
result = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
CONF_APPS: "app1",
|
||||
},
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "apps"
|
||||
|
||||
# test change value in apps form
|
||||
result = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
CONF_APP_NAME: "Appl1",
|
||||
},
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "init"
|
||||
|
||||
# test app form with new app
|
||||
result = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
CONF_APPS: APPS_NEW_ID,
|
||||
},
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "apps"
|
||||
|
||||
# test save value for new app
|
||||
result = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
CONF_APP_ID: "app2",
|
||||
CONF_APP_NAME: "Appl2",
|
||||
},
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "init"
|
||||
|
||||
# test app form for delete
|
||||
result = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
CONF_APPS: "app1",
|
||||
},
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "apps"
|
||||
|
||||
# test delete app1
|
||||
result = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
CONF_APP_NAME: "Appl1",
|
||||
CONF_APP_DELETE: True,
|
||||
},
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "init"
|
||||
|
||||
# test rules form with existing rule
|
||||
result = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
CONF_STATE_DETECTION_RULES: "com.plexapp.android",
|
||||
},
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "rules"
|
||||
|
||||
# test change value in rule form with invalid json rule
|
||||
result = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
CONF_RULE_VALUES: "a",
|
||||
},
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "rules"
|
||||
assert result["errors"] == {"base": "invalid_det_rules"}
|
||||
|
||||
# test change value in rule form with invalid rule
|
||||
result = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
CONF_RULE_VALUES: {"a": "b"},
|
||||
},
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "rules"
|
||||
assert result["errors"] == {"base": "invalid_det_rules"}
|
||||
|
||||
# test change value in rule form with valid rule
|
||||
result = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
CONF_RULE_VALUES: ["standby"],
|
||||
},
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "init"
|
||||
|
||||
# test rule form with new rule
|
||||
result = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
CONF_STATE_DETECTION_RULES: RULES_NEW_ID,
|
||||
},
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "rules"
|
||||
|
||||
# test save value for new rule
|
||||
result = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
CONF_RULE_ID: "rule2",
|
||||
CONF_RULE_VALUES: VALID_DETECT_RULE,
|
||||
},
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "init"
|
||||
|
||||
# test rules form with delete existing rule
|
||||
result = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
CONF_STATE_DETECTION_RULES: "com.plexapp.android",
|
||||
},
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "rules"
|
||||
|
||||
# test delete rule
|
||||
result = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
CONF_RULE_DELETE: True,
|
||||
},
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "init"
|
||||
|
||||
result = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
CONF_GET_SOURCES: True,
|
||||
CONF_EXCLUDE_UNNAMED_APPS: True,
|
||||
CONF_SCREENCAP_INTERVAL: 1,
|
||||
CONF_TURN_OFF_COMMAND: "off",
|
||||
CONF_TURN_ON_COMMAND: "on",
|
||||
},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
|
||||
apps_options = config_entry.options[CONF_APPS]
|
||||
assert apps_options.get("app1") is None
|
||||
assert apps_options["app2"] == "Appl2"
|
||||
|
||||
assert config_entry.options[CONF_GET_SOURCES] is True
|
||||
assert config_entry.options[CONF_EXCLUDE_UNNAMED_APPS] is True
|
||||
assert config_entry.options[CONF_SCREENCAP_INTERVAL] == 1
|
||||
assert config_entry.options[CONF_TURN_OFF_COMMAND] == "off"
|
||||
assert config_entry.options[CONF_TURN_ON_COMMAND] == "on"
|
||||
@@ -0,0 +1,39 @@
|
||||
"""Tests for the diagnostics data provided by the AndroidTV integration."""
|
||||
|
||||
from homeassistant.components.androidtv.diagnostics import TO_REDACT
|
||||
from homeassistant.components.diagnostics import async_redact_data
|
||||
from homeassistant.components.media_player import DOMAIN as MP_DOMAIN
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from . import patchers
|
||||
from .common import CONFIG_ANDROID_DEFAULT, SHELL_RESPONSE_OFF, setup_mock_entry
|
||||
|
||||
from tests.components.diagnostics import get_diagnostics_for_config_entry
|
||||
from tests.typing import ClientSessionGenerator
|
||||
|
||||
|
||||
async def test_diagnostics(
|
||||
hass: HomeAssistant,
|
||||
hass_client: ClientSessionGenerator,
|
||||
) -> None:
|
||||
"""Test diagnostics."""
|
||||
patch_key, _, mock_config_entry = setup_mock_entry(
|
||||
CONFIG_ANDROID_DEFAULT, MP_DOMAIN
|
||||
)
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
with (
|
||||
patchers.patch_connect(True)[patch_key],
|
||||
patchers.patch_shell(SHELL_RESPONSE_OFF)[patch_key],
|
||||
):
|
||||
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
assert mock_config_entry.state is ConfigEntryState.LOADED
|
||||
|
||||
entry_dict = async_redact_data(mock_config_entry.as_dict(), TO_REDACT)
|
||||
result = await get_diagnostics_for_config_entry(
|
||||
hass, hass_client, mock_config_entry
|
||||
)
|
||||
|
||||
assert result["entry"] == entry_dict | {"discovery_keys": {}}
|
||||
34
home-assistant/tests/components/androidtv/test_init.py
Normal file
34
home-assistant/tests/components/androidtv/test_init.py
Normal file
@@ -0,0 +1,34 @@
|
||||
"""Tests for AndroidTV integration initialization."""
|
||||
|
||||
from homeassistant.components.androidtv.const import (
|
||||
CONF_SCREENCAP,
|
||||
CONF_SCREENCAP_INTERVAL,
|
||||
)
|
||||
from homeassistant.components.media_player import DOMAIN as MP_DOMAIN
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from . import patchers
|
||||
from .common import CONFIG_ANDROID_DEFAULT, SHELL_RESPONSE_OFF, setup_mock_entry
|
||||
|
||||
|
||||
async def test_migrate_version(
|
||||
hass: HomeAssistant,
|
||||
) -> None:
|
||||
"""Test migration to new version."""
|
||||
patch_key, _, mock_config_entry = setup_mock_entry(
|
||||
CONFIG_ANDROID_DEFAULT,
|
||||
MP_DOMAIN,
|
||||
options={CONF_SCREENCAP: False},
|
||||
minor_version=1,
|
||||
)
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
with (
|
||||
patchers.patch_connect(True)[patch_key],
|
||||
patchers.patch_shell(SHELL_RESPONSE_OFF)[patch_key],
|
||||
):
|
||||
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_config_entry.options[CONF_SCREENCAP_INTERVAL] == 0
|
||||
assert mock_config_entry.minor_version == 2
|
||||
1161
home-assistant/tests/components/androidtv/test_media_player.py
Normal file
1161
home-assistant/tests/components/androidtv/test_media_player.py
Normal file
File diff suppressed because it is too large
Load Diff
164
home-assistant/tests/components/androidtv/test_remote.py
Normal file
164
home-assistant/tests/components/androidtv/test_remote.py
Normal file
@@ -0,0 +1,164 @@
|
||||
"""The tests for the androidtv remote platform."""
|
||||
|
||||
from typing import Any
|
||||
from unittest.mock import call, patch
|
||||
|
||||
from androidtv.constants import KEYS
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.androidtv.const import (
|
||||
CONF_TURN_OFF_COMMAND,
|
||||
CONF_TURN_ON_COMMAND,
|
||||
)
|
||||
from homeassistant.components.remote import (
|
||||
ATTR_NUM_REPEATS,
|
||||
DOMAIN as REMOTE_DOMAIN,
|
||||
SERVICE_SEND_COMMAND,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
ATTR_COMMAND,
|
||||
ATTR_ENTITY_ID,
|
||||
SERVICE_TURN_OFF,
|
||||
SERVICE_TURN_ON,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ServiceValidationError
|
||||
|
||||
from . import patchers
|
||||
from .common import (
|
||||
CONFIG_ANDROID_DEFAULT,
|
||||
CONFIG_FIRETV_DEFAULT,
|
||||
SHELL_RESPONSE_OFF,
|
||||
SHELL_RESPONSE_STANDBY,
|
||||
setup_mock_entry,
|
||||
)
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
def _setup(config: dict[str, Any]) -> tuple[str, str, MockConfigEntry]:
|
||||
"""Prepare mock entry for the media player tests."""
|
||||
return setup_mock_entry(config, REMOTE_DOMAIN)
|
||||
|
||||
|
||||
async def _test_service(
|
||||
hass: HomeAssistant,
|
||||
entity_id,
|
||||
ha_service_name,
|
||||
androidtv_method,
|
||||
additional_service_data=None,
|
||||
expected_call_args=None,
|
||||
) -> None:
|
||||
"""Test generic Android media player entity service."""
|
||||
if expected_call_args is None:
|
||||
expected_call_args = [None]
|
||||
|
||||
service_data = {ATTR_ENTITY_ID: entity_id}
|
||||
if additional_service_data:
|
||||
service_data.update(additional_service_data)
|
||||
|
||||
androidtv_patch = (
|
||||
"androidtv.androidtv_async.AndroidTVAsync"
|
||||
if "android" in entity_id
|
||||
else "firetv.firetv_async.FireTVAsync"
|
||||
)
|
||||
with patch(f"androidtv.{androidtv_patch}.{androidtv_method}") as api_call:
|
||||
await hass.services.async_call(
|
||||
REMOTE_DOMAIN,
|
||||
ha_service_name,
|
||||
service_data=service_data,
|
||||
blocking=True,
|
||||
)
|
||||
assert api_call.called
|
||||
assert api_call.call_count == len(expected_call_args)
|
||||
expected_calls = [call(s) if s else call() for s in expected_call_args]
|
||||
assert api_call.call_args_list == expected_calls
|
||||
|
||||
|
||||
@pytest.mark.parametrize("config", [CONFIG_ANDROID_DEFAULT, CONFIG_FIRETV_DEFAULT])
|
||||
async def test_services_remote(hass: HomeAssistant, config) -> None:
|
||||
"""Test services for remote entity."""
|
||||
patch_key, entity_id, config_entry = _setup(config)
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
with patchers.patch_connect(True)[patch_key]:
|
||||
with patchers.patch_shell(SHELL_RESPONSE_OFF)[patch_key]:
|
||||
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
with (
|
||||
patchers.patch_shell(SHELL_RESPONSE_STANDBY)[patch_key],
|
||||
patchers.PATCH_SCREENCAP,
|
||||
):
|
||||
await _test_service(hass, entity_id, SERVICE_TURN_OFF, "turn_off")
|
||||
await _test_service(hass, entity_id, SERVICE_TURN_ON, "turn_on")
|
||||
await _test_service(
|
||||
hass,
|
||||
entity_id,
|
||||
SERVICE_SEND_COMMAND,
|
||||
"adb_shell",
|
||||
{ATTR_COMMAND: ["BACK", "test"], ATTR_NUM_REPEATS: 2},
|
||||
[
|
||||
f"input keyevent {KEYS["BACK"]}",
|
||||
"test",
|
||||
f"input keyevent {KEYS["BACK"]}",
|
||||
"test",
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("config", [CONFIG_ANDROID_DEFAULT, CONFIG_FIRETV_DEFAULT])
|
||||
async def test_services_remote_custom(hass: HomeAssistant, config) -> None:
|
||||
"""Test services with custom options for remote entity."""
|
||||
patch_key, entity_id, config_entry = _setup(config)
|
||||
config_entry.add_to_hass(hass)
|
||||
hass.config_entries.async_update_entry(
|
||||
config_entry,
|
||||
options={
|
||||
CONF_TURN_OFF_COMMAND: "test off",
|
||||
CONF_TURN_ON_COMMAND: "test on",
|
||||
},
|
||||
)
|
||||
|
||||
with patchers.patch_connect(True)[patch_key]:
|
||||
with patchers.patch_shell(SHELL_RESPONSE_OFF)[patch_key]:
|
||||
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
with (
|
||||
patchers.patch_shell(SHELL_RESPONSE_STANDBY)[patch_key],
|
||||
patchers.PATCH_SCREENCAP,
|
||||
):
|
||||
await _test_service(
|
||||
hass, entity_id, SERVICE_TURN_OFF, "adb_shell", None, ["test off"]
|
||||
)
|
||||
await _test_service(
|
||||
hass, entity_id, SERVICE_TURN_ON, "adb_shell", None, ["test on"]
|
||||
)
|
||||
|
||||
|
||||
async def test_remote_unicode_decode_error(hass: HomeAssistant) -> None:
|
||||
"""Test sending a command via the send_command remote service that raises a UnicodeDecodeError exception."""
|
||||
patch_key, entity_id, config_entry = _setup(CONFIG_ANDROID_DEFAULT)
|
||||
config_entry.add_to_hass(hass)
|
||||
response = b"test response"
|
||||
|
||||
with patchers.patch_connect(True)[patch_key]:
|
||||
with patchers.patch_shell(SHELL_RESPONSE_OFF)[patch_key]:
|
||||
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
with patch(
|
||||
"androidtv.basetv.basetv_async.BaseTVAsync.adb_shell",
|
||||
side_effect=UnicodeDecodeError("utf-8", response, 0, len(response), "TEST"),
|
||||
) as api_call:
|
||||
try:
|
||||
await hass.services.async_call(
|
||||
REMOTE_DOMAIN,
|
||||
SERVICE_SEND_COMMAND,
|
||||
service_data={ATTR_ENTITY_ID: entity_id, ATTR_COMMAND: "BACK"},
|
||||
blocking=True,
|
||||
)
|
||||
pytest.fail("Exception not raised")
|
||||
except ServiceValidationError:
|
||||
assert api_call.call_count == 1
|
||||
Reference in New Issue
Block a user