Add app support for TVs to Vizio integration (#32432)
* add app support * code cleanup, add additional test, add CONF_APPS storage logic for import * simplify schema defaults logic * remove unnecessary lower() and fix docstring * remove default return for popping CONF_APPS during import update because we know entry data has CONF_APPS due to if statement * further simplification * even more simplification * fix type hints * move app configuration to separate step, fix tests, and only make app updates if device_type == tv * remove errors variable from tv_apps and move tv_apps schema out of ConfigFlow for consistency * slight refactor * remove unused error from strings.json * set unique id as early as possible * correct which dictionary to use to set unique id in pair_tv step
This commit is contained in:
1 parent
873bf887a5
commit
a579fcf248
12 files changed
+729
-73
No files matched your search
@@ -5,9 +5,12 @@ from pyvizio.const import DEVICE_CLASS_SPEAKER, MAX_VOLUME
|
||||
|
||||
from .const import (
|
||||
ACCESS_TOKEN,
|
||||
APP_LIST,
|
||||
CH_TYPE,
|
||||
CURRENT_APP,
|
||||
CURRENT_INPUT,
|
||||
INPUT_LIST,
|
||||
INPUT_LIST_WITH_APPS,
|
||||
MODEL,
|
||||
RESPONSE_TOKEN,
|
||||
UNIQUE_ID,
|
||||
@@ -154,3 +157,22 @@ def vizio_update_fixture():
|
||||
return_value=VERSION,
|
||||
):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture(name="vizio_update_with_apps")
|
||||
def vizio_update_with_apps_fixture(vizio_update: pytest.fixture):
|
||||
"""Mock valid updates to vizio device that supports apps."""
|
||||
with patch(
|
||||
"homeassistant.components.vizio.media_player.VizioAsync.get_inputs_list",
|
||||
return_value=get_mock_inputs(INPUT_LIST_WITH_APPS),
|
||||
), patch(
|
||||
"homeassistant.components.vizio.media_player.VizioAsync.get_apps_list",
|
||||
return_value=APP_LIST,
|
||||
), patch(
|
||||
"homeassistant.components.vizio.media_player.VizioAsync.get_current_input",
|
||||
return_value="CAST",
|
||||
), patch(
|
||||
"homeassistant.components.vizio.media_player.VizioAsync.get_current_app",
|
||||
return_value=CURRENT_APP,
|
||||
):
|
||||
yield
|
||||
@@ -6,11 +6,23 @@ from homeassistant.components.media_player import (
|
||||
DEVICE_CLASS_TV,
|
||||
DOMAIN as MP_DOMAIN,
|
||||
)
|
||||
from homeassistant.components.vizio.const import CONF_VOLUME_STEP
|
||||
from homeassistant.components.vizio.const import (
|
||||
CONF_ADDITIONAL_CONFIGS,
|
||||
CONF_APP_ID,
|
||||
CONF_APPS,
|
||||
CONF_APPS_TO_INCLUDE_OR_EXCLUDE,
|
||||
CONF_CONFIG,
|
||||
CONF_INCLUDE_OR_EXCLUDE,
|
||||
CONF_MESSAGE,
|
||||
CONF_NAME_SPACE,
|
||||
CONF_VOLUME_STEP,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
CONF_ACCESS_TOKEN,
|
||||
CONF_DEVICE_CLASS,
|
||||
CONF_EXCLUDE,
|
||||
CONF_HOST,
|
||||
CONF_INCLUDE,
|
||||
CONF_NAME,
|
||||
CONF_PIN,
|
||||
CONF_PORT,
|
||||
@@ -52,6 +64,22 @@ class MockCompletePairingResponse(object):
|
||||
self.auth_token = auth_token
|
||||
|
||||
|
||||
CURRENT_INPUT = "HDMI"
|
||||
INPUT_LIST = ["HDMI", "USB", "Bluetooth", "AUX"]
|
||||
|
||||
CURRENT_APP = "Hulu"
|
||||
APP_LIST = ["Hulu", "Netflix"]
|
||||
INPUT_LIST_WITH_APPS = INPUT_LIST + ["CAST"]
|
||||
CUSTOM_APP_NAME = "APP3"
|
||||
CUSTOM_CONFIG = {CONF_APP_ID: "test", CONF_MESSAGE: None, CONF_NAME_SPACE: 10}
|
||||
ADDITIONAL_APP_CONFIG = {
|
||||
"name": CUSTOM_APP_NAME,
|
||||
CONF_CONFIG: CUSTOM_CONFIG,
|
||||
}
|
||||
|
||||
ENTITY_ID = f"{MP_DOMAIN}.{slugify(NAME)}"
|
||||
|
||||
|
||||
MOCK_PIN_CONFIG = {CONF_PIN: PIN}
|
||||
|
||||
MOCK_USER_VALID_TV_CONFIG = {
|
||||
@@ -73,6 +101,58 @@ MOCK_IMPORT_VALID_TV_CONFIG = {
|
||||
CONF_VOLUME_STEP: VOLUME_STEP,
|
||||
}
|
||||
|
||||
MOCK_TV_WITH_INCLUDE_CONFIG = {
|
||||
CONF_NAME: NAME,
|
||||
CONF_HOST: HOST,
|
||||
CONF_DEVICE_CLASS: DEVICE_CLASS_TV,
|
||||
CONF_ACCESS_TOKEN: ACCESS_TOKEN,
|
||||
CONF_VOLUME_STEP: VOLUME_STEP,
|
||||
CONF_APPS: {CONF_INCLUDE: [CURRENT_APP]},
|
||||
}
|
||||
|
||||
MOCK_TV_WITH_EXCLUDE_CONFIG = {
|
||||
CONF_NAME: NAME,
|
||||
CONF_HOST: HOST,
|
||||
CONF_DEVICE_CLASS: DEVICE_CLASS_TV,
|
||||
CONF_ACCESS_TOKEN: ACCESS_TOKEN,
|
||||
CONF_VOLUME_STEP: VOLUME_STEP,
|
||||
CONF_APPS: {CONF_EXCLUDE: ["Netflix"]},
|
||||
}
|
||||
|
||||
MOCK_TV_WITH_ADDITIONAL_APPS_CONFIG = {
|
||||
CONF_NAME: NAME,
|
||||
CONF_HOST: HOST,
|
||||
CONF_DEVICE_CLASS: DEVICE_CLASS_TV,
|
||||
CONF_ACCESS_TOKEN: ACCESS_TOKEN,
|
||||
CONF_VOLUME_STEP: VOLUME_STEP,
|
||||
CONF_APPS: {CONF_ADDITIONAL_CONFIGS: [ADDITIONAL_APP_CONFIG]},
|
||||
}
|
||||
|
||||
MOCK_SPEAKER_APPS_FAILURE = {
|
||||
CONF_NAME: NAME,
|
||||
CONF_HOST: HOST,
|
||||
CONF_DEVICE_CLASS: DEVICE_CLASS_SPEAKER,
|
||||
CONF_ACCESS_TOKEN: ACCESS_TOKEN,
|
||||
CONF_VOLUME_STEP: VOLUME_STEP,
|
||||
CONF_APPS: {CONF_ADDITIONAL_CONFIGS: [ADDITIONAL_APP_CONFIG]},
|
||||
}
|
||||
|
||||
MOCK_TV_APPS_FAILURE = {
|
||||
CONF_NAME: NAME,
|
||||
CONF_HOST: HOST,
|
||||
CONF_DEVICE_CLASS: DEVICE_CLASS_TV,
|
||||
CONF_ACCESS_TOKEN: ACCESS_TOKEN,
|
||||
CONF_VOLUME_STEP: VOLUME_STEP,
|
||||
CONF_APPS: None,
|
||||
}
|
||||
|
||||
MOCK_TV_APPS_WITH_VALID_APPS_CONFIG = {
|
||||
CONF_HOST: HOST,
|
||||
CONF_DEVICE_CLASS: DEVICE_CLASS_TV,
|
||||
CONF_ACCESS_TOKEN: ACCESS_TOKEN,
|
||||
CONF_APPS: {CONF_INCLUDE: [CURRENT_APP]},
|
||||
}
|
||||
|
||||
MOCK_TV_CONFIG_NO_TOKEN = {
|
||||
CONF_NAME: NAME,
|
||||
CONF_HOST: HOST,
|
||||
@@ -85,6 +165,15 @@ MOCK_SPEAKER_CONFIG = {
|
||||
CONF_DEVICE_CLASS: DEVICE_CLASS_SPEAKER,
|
||||
}
|
||||
|
||||
MOCK_INCLUDE_APPS = {
|
||||
CONF_INCLUDE_OR_EXCLUDE: CONF_INCLUDE.title(),
|
||||
CONF_APPS_TO_INCLUDE_OR_EXCLUDE: [CURRENT_APP],
|
||||
}
|
||||
MOCK_INCLUDE_NO_APPS = {
|
||||
CONF_INCLUDE_OR_EXCLUDE: CONF_INCLUDE.title(),
|
||||
CONF_APPS_TO_INCLUDE_OR_EXCLUDE: [],
|
||||
}
|
||||
|
||||
VIZIO_ZEROCONF_SERVICE_TYPE = "_viziocast._tcp.local."
|
||||
ZEROCONF_NAME = f"{NAME}.{VIZIO_ZEROCONF_SERVICE_TYPE}"
|
||||
ZEROCONF_HOST = HOST.split(":")[0]
|
||||
@@ -97,8 +186,3 @@ MOCK_ZEROCONF_SERVICE_INFO = {
|
||||
CONF_PORT: ZEROCONF_PORT,
|
||||
"properties": {"name": "SB4031-D5"},
|
||||
}
|
||||
|
||||
CURRENT_INPUT = "HDMI"
|
||||
INPUT_LIST = ["HDMI", "USB", "Bluetooth", "AUX"]
|
||||
|
||||
ENTITY_ID = f"{MP_DOMAIN}.{slugify(NAME)}"
|
||||
@@ -6,7 +6,12 @@ import voluptuous as vol
|
||||
|
||||
from homeassistant import data_entry_flow
|
||||
from homeassistant.components.media_player import DEVICE_CLASS_SPEAKER, DEVICE_CLASS_TV
|
||||
from homeassistant.components.vizio.config_flow import _get_config_schema
|
||||
from homeassistant.components.vizio.const import (
|
||||
CONF_APPS,
|
||||
CONF_APPS_TO_INCLUDE_OR_EXCLUDE,
|
||||
CONF_INCLUDE,
|
||||
CONF_INCLUDE_OR_EXCLUDE,
|
||||
CONF_VOLUME_STEP,
|
||||
DEFAULT_NAME,
|
||||
DEFAULT_VOLUME_STEP,
|
||||
@@ -25,12 +30,16 @@ from homeassistant.helpers.typing import HomeAssistantType
|
||||
|
||||
from .const import (
|
||||
ACCESS_TOKEN,
|
||||
CURRENT_APP,
|
||||
HOST,
|
||||
HOST2,
|
||||
MOCK_IMPORT_VALID_TV_CONFIG,
|
||||
MOCK_INCLUDE_APPS,
|
||||
MOCK_INCLUDE_NO_APPS,
|
||||
MOCK_PIN_CONFIG,
|
||||
MOCK_SPEAKER_CONFIG,
|
||||
MOCK_TV_CONFIG_NO_TOKEN,
|
||||
MOCK_TV_WITH_EXCLUDE_CONFIG,
|
||||
MOCK_USER_VALID_TV_CONFIG,
|
||||
MOCK_ZEROCONF_SERVICE_INFO,
|
||||
NAME,
|
||||
@@ -86,12 +95,48 @@ async def test_user_flow_all_fields(
|
||||
result["flow_id"], user_input=MOCK_USER_VALID_TV_CONFIG
|
||||
)
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result["step_id"] == "tv_apps"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=MOCK_INCLUDE_APPS
|
||||
)
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["title"] == NAME
|
||||
assert result["data"][CONF_NAME] == NAME
|
||||
assert result["data"][CONF_HOST] == HOST
|
||||
assert result["data"][CONF_DEVICE_CLASS] == DEVICE_CLASS_TV
|
||||
assert result["data"][CONF_ACCESS_TOKEN] == ACCESS_TOKEN
|
||||
assert result["data"][CONF_APPS][CONF_INCLUDE] == [CURRENT_APP]
|
||||
|
||||
|
||||
async def test_user_apps_with_tv(
|
||||
hass: HomeAssistantType,
|
||||
vizio_connect: pytest.fixture,
|
||||
vizio_bypass_setup: pytest.fixture,
|
||||
) -> None:
|
||||
"""Test TV can have selected apps during user setup."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=MOCK_IMPORT_VALID_TV_CONFIG
|
||||
)
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result["step_id"] == "tv_apps"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=MOCK_INCLUDE_APPS
|
||||
)
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["title"] == NAME
|
||||
assert result["data"][CONF_NAME] == NAME
|
||||
assert result["data"][CONF_HOST] == HOST
|
||||
assert result["data"][CONF_DEVICE_CLASS] == DEVICE_CLASS_TV
|
||||
assert result["data"][CONF_ACCESS_TOKEN] == ACCESS_TOKEN
|
||||
assert result["data"][CONF_APPS][CONF_INCLUDE] == [CURRENT_APP]
|
||||
assert CONF_APPS_TO_INCLUDE_OR_EXCLUDE not in result["data"]
|
||||
assert CONF_INCLUDE_OR_EXCLUDE not in result["data"]
|
||||
|
||||
|
||||
async def test_options_flow(hass: HomeAssistantType) -> None:
|
||||
@@ -218,13 +263,13 @@ async def test_user_error_on_could_not_connect(
|
||||
assert result["errors"] == {"base": "cant_connect"}
|
||||
|
||||
|
||||
async def test_user_tv_pairing(
|
||||
async def test_user_tv_pairing_no_apps(
|
||||
hass: HomeAssistantType,
|
||||
vizio_connect: pytest.fixture,
|
||||
vizio_bypass_setup: pytest.fixture,
|
||||
vizio_complete_pairing: pytest.fixture,
|
||||
) -> None:
|
||||
"""Test pairing config flow when access token not provided for tv during user entry."""
|
||||
"""Test pairing config flow when access token not provided for tv during user entry and no apps configured."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=MOCK_TV_CONFIG_NO_TOKEN
|
||||
)
|
||||
@@ -237,15 +282,18 @@ async def test_user_tv_pairing(
|
||||
)
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result["step_id"] == "pairing_complete"
|
||||
assert result["step_id"] == "tv_apps"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=MOCK_INCLUDE_NO_APPS
|
||||
)
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["title"] == NAME
|
||||
assert result["data"][CONF_NAME] == NAME
|
||||
assert result["data"][CONF_HOST] == HOST
|
||||
assert result["data"][CONF_DEVICE_CLASS] == DEVICE_CLASS_TV
|
||||
assert CONF_APPS not in result["data"]
|
||||
|
||||
|
||||
async def test_user_start_pairing_failure(
|
||||
@@ -385,12 +433,12 @@ async def test_import_flow_update_options(
|
||||
)
|
||||
|
||||
|
||||
async def test_import_flow_update_name(
|
||||
async def test_import_flow_update_name_and_apps(
|
||||
hass: HomeAssistantType,
|
||||
vizio_connect: pytest.fixture,
|
||||
vizio_bypass_update: pytest.fixture,
|
||||
) -> None:
|
||||
"""Test import config flow with updated name."""
|
||||
"""Test import config flow with updated name and apps."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_IMPORT},
|
||||
@@ -404,6 +452,7 @@ async def test_import_flow_update_name(
|
||||
|
||||
updated_config = MOCK_IMPORT_VALID_TV_CONFIG.copy()
|
||||
updated_config[CONF_NAME] = NAME2
|
||||
updated_config[CONF_APPS] = {CONF_INCLUDE: [CURRENT_APP]}
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_IMPORT},
|
||||
@@ -413,6 +462,39 @@ async def test_import_flow_update_name(
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "updated_entry"
|
||||
assert hass.config_entries.async_get_entry(entry_id).data[CONF_NAME] == NAME2
|
||||
assert hass.config_entries.async_get_entry(entry_id).data[CONF_APPS] == {
|
||||
CONF_INCLUDE: [CURRENT_APP]
|
||||
}
|
||||
|
||||
|
||||
async def test_import_flow_update_remove_apps(
|
||||
hass: HomeAssistantType,
|
||||
vizio_connect: pytest.fixture,
|
||||
vizio_bypass_update: pytest.fixture,
|
||||
) -> None:
|
||||
"""Test import config flow with removed apps."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_IMPORT},
|
||||
data=vol.Schema(VIZIO_SCHEMA)(MOCK_TV_WITH_EXCLUDE_CONFIG),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["result"].data[CONF_NAME] == NAME
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
entry_id = result["result"].entry_id
|
||||
|
||||
updated_config = MOCK_TV_WITH_EXCLUDE_CONFIG.copy()
|
||||
updated_config.pop(CONF_APPS)
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_IMPORT},
|
||||
data=vol.Schema(VIZIO_SCHEMA)(updated_config),
|
||||
)
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "updated_entry"
|
||||
assert hass.config_entries.async_get_entry(entry_id).data.get(CONF_APPS) is None
|
||||
|
||||
|
||||
async def test_import_needs_pairing(
|
||||
@@ -452,6 +534,49 @@ async def test_import_needs_pairing(
|
||||
assert result["data"][CONF_DEVICE_CLASS] == DEVICE_CLASS_TV
|
||||
|
||||
|
||||
async def test_import_with_apps_needs_pairing(
|
||||
hass: HomeAssistantType,
|
||||
vizio_connect: pytest.fixture,
|
||||
vizio_bypass_setup: pytest.fixture,
|
||||
vizio_complete_pairing: pytest.fixture,
|
||||
) -> None:
|
||||
"""Test pairing config flow when access token not provided for tv but apps are included during import."""
|
||||
import_config = MOCK_TV_CONFIG_NO_TOKEN.copy()
|
||||
import_config[CONF_APPS] = {CONF_INCLUDE: [CURRENT_APP]}
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_IMPORT}, data=import_config
|
||||
)
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
# Mock inputting info without apps to make sure apps get stored
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input=_get_config_schema(MOCK_TV_CONFIG_NO_TOKEN)(MOCK_TV_CONFIG_NO_TOKEN),
|
||||
)
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result["step_id"] == "pair_tv"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=MOCK_PIN_CONFIG
|
||||
)
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result["step_id"] == "pairing_complete_import"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["title"] == NAME
|
||||
assert result["data"][CONF_NAME] == NAME
|
||||
assert result["data"][CONF_HOST] == HOST
|
||||
assert result["data"][CONF_DEVICE_CLASS] == DEVICE_CLASS_TV
|
||||
assert result["data"][CONF_APPS][CONF_INCLUDE] == [CURRENT_APP]
|
||||
|
||||
|
||||
async def test_import_error(
|
||||
hass: HomeAssistantType,
|
||||
vizio_connect: pytest.fixture,
|
||||
|
||||
@@ -1,14 +1,21 @@
|
||||
"""Tests for Vizio config flow."""
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
from typing import Any, Dict
|
||||
from unittest.mock import call
|
||||
|
||||
from asynctest import patch
|
||||
import pytest
|
||||
from pytest import raises
|
||||
from pyvizio._api.apps import AppConfig
|
||||
from pyvizio.const import (
|
||||
DEVICE_CLASS_SPEAKER as VIZIO_DEVICE_CLASS_SPEAKER,
|
||||
DEVICE_CLASS_TV as VIZIO_DEVICE_CLASS_TV,
|
||||
INPUT_APPS,
|
||||
MAX_VOLUME,
|
||||
UNKNOWN_APP,
|
||||
)
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.media_player import (
|
||||
ATTR_INPUT_SOURCE,
|
||||
@@ -27,16 +34,41 @@ from homeassistant.components.media_player import (
|
||||
SERVICE_VOLUME_SET,
|
||||
SERVICE_VOLUME_UP,
|
||||
)
|
||||
from homeassistant.components.vizio.const import CONF_VOLUME_STEP, DOMAIN
|
||||
from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON, STATE_UNAVAILABLE
|
||||
from homeassistant.components.vizio import validate_apps
|
||||
from homeassistant.components.vizio.const import (
|
||||
CONF_ADDITIONAL_CONFIGS,
|
||||
CONF_APPS,
|
||||
CONF_VOLUME_STEP,
|
||||
DOMAIN,
|
||||
VIZIO_SCHEMA,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
CONF_EXCLUDE,
|
||||
CONF_INCLUDE,
|
||||
STATE_OFF,
|
||||
STATE_ON,
|
||||
STATE_UNAVAILABLE,
|
||||
)
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from .const import (
|
||||
ADDITIONAL_APP_CONFIG,
|
||||
APP_LIST,
|
||||
CURRENT_APP,
|
||||
CURRENT_INPUT,
|
||||
CUSTOM_APP_NAME,
|
||||
CUSTOM_CONFIG,
|
||||
ENTITY_ID,
|
||||
INPUT_LIST,
|
||||
INPUT_LIST_WITH_APPS,
|
||||
MOCK_SPEAKER_APPS_FAILURE,
|
||||
MOCK_SPEAKER_CONFIG,
|
||||
MOCK_TV_APPS_FAILURE,
|
||||
MOCK_TV_WITH_ADDITIONAL_APPS_CONFIG,
|
||||
MOCK_TV_WITH_EXCLUDE_CONFIG,
|
||||
MOCK_TV_WITH_INCLUDE_CONFIG,
|
||||
MOCK_USER_VALID_TV_CONFIG,
|
||||
NAME,
|
||||
UNIQUE_ID,
|
||||
@@ -45,6 +77,8 @@ from .const import (
|
||||
|
||||
from tests.common import MockConfigEntry, async_fire_time_changed
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def _test_setup(
|
||||
hass: HomeAssistantType, ha_device_class: str, vizio_power_state: bool
|
||||
@@ -60,12 +94,16 @@ async def _test_setup(
|
||||
if ha_device_class == DEVICE_CLASS_SPEAKER:
|
||||
vizio_device_class = VIZIO_DEVICE_CLASS_SPEAKER
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN, data=MOCK_SPEAKER_CONFIG, unique_id=UNIQUE_ID
|
||||
domain=DOMAIN,
|
||||
data=vol.Schema(VIZIO_SCHEMA)(MOCK_SPEAKER_CONFIG),
|
||||
unique_id=UNIQUE_ID,
|
||||
)
|
||||
else:
|
||||
vizio_device_class = VIZIO_DEVICE_CLASS_TV
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN, data=MOCK_USER_VALID_TV_CONFIG, unique_id=UNIQUE_ID
|
||||
domain=DOMAIN,
|
||||
data=vol.Schema(VIZIO_SCHEMA)(MOCK_USER_VALID_TV_CONFIG),
|
||||
unique_id=UNIQUE_ID,
|
||||
)
|
||||
|
||||
with patch(
|
||||
@@ -94,6 +132,72 @@ async def _test_setup(
|
||||
)
|
||||
|
||||
|
||||
async def _test_setup_with_apps(
|
||||
hass: HomeAssistantType, device_config: Dict[str, Any], app: str
|
||||
) -> None:
|
||||
"""Test Vizio Device with apps entity setup."""
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN, data=vol.Schema(VIZIO_SCHEMA)(device_config), unique_id=UNIQUE_ID
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.vizio.media_player.VizioAsync.get_all_audio_settings",
|
||||
return_value={
|
||||
"volume": int(MAX_VOLUME[VIZIO_DEVICE_CLASS_TV] / 2),
|
||||
"mute": "Off",
|
||||
},
|
||||
), patch(
|
||||
"homeassistant.components.vizio.media_player.VizioAsync.get_power_state",
|
||||
return_value=True,
|
||||
), patch(
|
||||
"homeassistant.components.vizio.media_player.VizioAsync.get_current_app",
|
||||
return_value=app,
|
||||
), patch(
|
||||
"homeassistant.components.vizio.media_player.VizioAsync.get_current_app_config",
|
||||
return_value=AppConfig(**ADDITIONAL_APP_CONFIG["config"]),
|
||||
):
|
||||
config_entry.add_to_hass(hass)
|
||||
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
attr = hass.states.get(ENTITY_ID).attributes
|
||||
assert attr["friendly_name"] == NAME
|
||||
assert attr["device_class"] == DEVICE_CLASS_TV
|
||||
assert hass.states.get(ENTITY_ID).state == STATE_ON
|
||||
|
||||
if device_config.get(CONF_APPS, {}).get(CONF_INCLUDE) or device_config.get(
|
||||
CONF_APPS, {}
|
||||
).get(CONF_EXCLUDE):
|
||||
list_to_test = list(INPUT_LIST_WITH_APPS + [CURRENT_APP])
|
||||
elif device_config.get(CONF_APPS, {}).get(CONF_ADDITIONAL_CONFIGS):
|
||||
list_to_test = list(
|
||||
INPUT_LIST_WITH_APPS
|
||||
+ APP_LIST
|
||||
+ [
|
||||
app["name"]
|
||||
for app in device_config[CONF_APPS][CONF_ADDITIONAL_CONFIGS]
|
||||
]
|
||||
)
|
||||
else:
|
||||
list_to_test = list(INPUT_LIST_WITH_APPS + APP_LIST)
|
||||
|
||||
for app_to_remove in INPUT_APPS:
|
||||
if app_to_remove in list_to_test:
|
||||
list_to_test.remove(app_to_remove)
|
||||
|
||||
assert attr["source_list"] == list_to_test
|
||||
assert app in attr["source_list"] or app == UNKNOWN_APP
|
||||
if app == UNKNOWN_APP:
|
||||
assert attr["source"] == ADDITIONAL_APP_CONFIG["name"]
|
||||
else:
|
||||
assert attr["source"] == app
|
||||
assert (
|
||||
attr["volume_level"]
|
||||
== float(int(MAX_VOLUME[VIZIO_DEVICE_CLASS_TV] / 2))
|
||||
/ MAX_VOLUME[VIZIO_DEVICE_CLASS_TV]
|
||||
)
|
||||
|
||||
|
||||
async def _test_setup_failure(hass: HomeAssistantType, config: str) -> None:
|
||||
"""Test generic Vizio entity setup failure."""
|
||||
with patch(
|
||||
@@ -311,3 +415,89 @@ async def test_update_available_to_unavailable(
|
||||
) -> None:
|
||||
"""Test device becomes unavailable after being available."""
|
||||
await _test_update_availability_switch(hass, True, None, caplog)
|
||||
|
||||
|
||||
async def test_setup_with_apps(
|
||||
hass: HomeAssistantType,
|
||||
vizio_connect: pytest.fixture,
|
||||
vizio_update_with_apps: pytest.fixture,
|
||||
caplog: pytest.fixture,
|
||||
) -> None:
|
||||
"""Test device setup with apps."""
|
||||
await _test_setup_with_apps(hass, MOCK_USER_VALID_TV_CONFIG, CURRENT_APP)
|
||||
await _test_service(
|
||||
hass,
|
||||
"launch_app",
|
||||
SERVICE_SELECT_SOURCE,
|
||||
{ATTR_INPUT_SOURCE: CURRENT_APP},
|
||||
CURRENT_APP,
|
||||
)
|
||||
|
||||
|
||||
async def test_setup_with_apps_include(
|
||||
hass: HomeAssistantType,
|
||||
vizio_connect: pytest.fixture,
|
||||
vizio_update_with_apps: pytest.fixture,
|
||||
caplog: pytest.fixture,
|
||||
) -> None:
|
||||
"""Test device setup with apps and apps["include"] in config."""
|
||||
await _test_setup_with_apps(hass, MOCK_TV_WITH_INCLUDE_CONFIG, CURRENT_APP)
|
||||
|
||||
|
||||
async def test_setup_with_apps_exclude(
|
||||
hass: HomeAssistantType,
|
||||
vizio_connect: pytest.fixture,
|
||||
vizio_update_with_apps: pytest.fixture,
|
||||
caplog: pytest.fixture,
|
||||
) -> None:
|
||||
"""Test device setup with apps and apps["exclude"] in config."""
|
||||
await _test_setup_with_apps(hass, MOCK_TV_WITH_EXCLUDE_CONFIG, CURRENT_APP)
|
||||
|
||||
|
||||
async def test_setup_with_apps_additional_apps_config(
|
||||
hass: HomeAssistantType,
|
||||
vizio_connect: pytest.fixture,
|
||||
vizio_update_with_apps: pytest.fixture,
|
||||
caplog: pytest.fixture,
|
||||
) -> None:
|
||||
"""Test device setup with apps and apps["additional_configs"] in config."""
|
||||
await _test_setup_with_apps(hass, MOCK_TV_WITH_ADDITIONAL_APPS_CONFIG, UNKNOWN_APP)
|
||||
|
||||
await _test_service(
|
||||
hass,
|
||||
"launch_app",
|
||||
SERVICE_SELECT_SOURCE,
|
||||
{ATTR_INPUT_SOURCE: CURRENT_APP},
|
||||
CURRENT_APP,
|
||||
)
|
||||
await _test_service(
|
||||
hass,
|
||||
"launch_app_config",
|
||||
SERVICE_SELECT_SOURCE,
|
||||
{ATTR_INPUT_SOURCE: CUSTOM_APP_NAME},
|
||||
**CUSTOM_CONFIG,
|
||||
)
|
||||
|
||||
# Test that invalid app does nothing
|
||||
with patch(
|
||||
"homeassistant.components.vizio.media_player.VizioAsync.launch_app"
|
||||
) as service_call1, patch(
|
||||
"homeassistant.components.vizio.media_player.VizioAsync.launch_app_config"
|
||||
) as service_call2:
|
||||
await hass.services.async_call(
|
||||
MP_DOMAIN,
|
||||
SERVICE_SELECT_SOURCE,
|
||||
service_data={ATTR_ENTITY_ID: ENTITY_ID, ATTR_INPUT_SOURCE: "_"},
|
||||
blocking=True,
|
||||
)
|
||||
assert not service_call1.called
|
||||
assert not service_call2.called
|
||||
|
||||
|
||||
def test_invalid_apps_config(hass: HomeAssistantType):
|
||||
"""Test that schema validation fails on certain conditions."""
|
||||
with raises(vol.Invalid):
|
||||
vol.Schema(vol.All(VIZIO_SCHEMA, validate_apps))(MOCK_TV_APPS_FAILURE)
|
||||
|
||||
with raises(vol.Invalid):
|
||||
vol.Schema(vol.All(VIZIO_SCHEMA, validate_apps))(MOCK_SPEAKER_APPS_FAILURE)
|
||||
Reference in new issue
Block a user