Automatically update app list for Vizio SmartTV's (#38641)

This commit is contained in:
Raman Gupta authored and GitHub committed 2020-09-02 11:55:10 +02:00
1 parent 9f5baa0bf7
commit 7ff633f531
13 files changed
+225 -47

No files matched your search

+11 -4
View File
@@ -1,5 +1,6 @@
"""Configure py.test."""
import pytest
from pyvizio.api.apps import AppConfig
from pyvizio.const import DEVICE_CLASS_SPEAKER, MAX_VOLUME
from .const import (
@@ -57,6 +58,15 @@ def vizio_get_unique_id_fixture():
yield
@pytest.fixture(name="vizio_data_coordinator_update", autouse=True)
def vizio_data_coordinator_update_fixture():
"""Mock get data coordinator update."""
with patch(
"homeassistant.components.vizio.gen_apps_list_from_url", return_value=APP_LIST,
):
yield
@pytest.fixture(name="vizio_no_unique_id")
def vizio_no_unique_id_fixture():
"""Mock no vizio unique ID returrned."""
@@ -191,15 +201,12 @@ def vizio_update_with_apps_fixture(vizio_update: pytest.fixture):
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_config",
return_value=CURRENT_APP_CONFIG,
return_value=AppConfig(**CURRENT_APP_CONFIG),
):
yield
+15 -1
View File
@@ -72,7 +72,21 @@ INPUT_LIST = ["HDMI", "USB", "Bluetooth", "AUX"]
CURRENT_APP = "Hulu"
CURRENT_APP_CONFIG = {CONF_APP_ID: "3", CONF_NAME_SPACE: 4, CONF_MESSAGE: None}
APP_LIST = ["Hulu", "Netflix"]
APP_LIST = [
{
"name": "Hulu",
"country": ["*"],
"id": ["1"],
"config": [{"NAME_SPACE": 4, "APP_ID": "3", "MESSAGE": None}],
},
{
"name": "Netflix",
"country": ["*"],
"id": ["2"],
"config": [{"NAME_SPACE": 1, "APP_ID": "2", "MESSAGE": None}],
},
]
APP_NAME_LIST = [app["name"] for app in APP_LIST]
INPUT_LIST_WITH_APPS = INPUT_LIST + ["CAST"]
CUSTOM_CONFIG = {CONF_APP_ID: "test", CONF_MESSAGE: None, CONF_NAME_SPACE: 10}
ADDITIONAL_APP_CONFIG = {
+48 -21
View File
@@ -109,12 +109,18 @@ async def test_user_flow_all_fields(
assert CONF_APPS not in result["data"]
async def test_speaker_options_flow(hass: HomeAssistantType) -> None:
async def test_speaker_options_flow(
hass: HomeAssistantType,
vizio_connect: pytest.fixture,
vizio_bypass_update: pytest.fixture,
) -> None:
"""Test options config flow for speaker."""
entry = MockConfigEntry(domain=DOMAIN, data=MOCK_SPEAKER_CONFIG)
entry.add_to_hass(hass)
assert not entry.options
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=MOCK_SPEAKER_CONFIG
)
await hass.async_block_till_done()
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
entry = result["result"]
result = await hass.config_entries.options.async_init(entry.entry_id, data=None)
@@ -131,12 +137,18 @@ async def test_speaker_options_flow(hass: HomeAssistantType) -> None:
assert CONF_APPS not in result["data"]
async def test_tv_options_flow_no_apps(hass: HomeAssistantType) -> None:
async def test_tv_options_flow_no_apps(
hass: HomeAssistantType,
vizio_connect: pytest.fixture,
vizio_bypass_update: pytest.fixture,
) -> None:
"""Test options config flow for TV without providing apps option."""
entry = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_VALID_TV_CONFIG)
entry.add_to_hass(hass)
assert not entry.options
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=MOCK_USER_VALID_TV_CONFIG
)
await hass.async_block_till_done()
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
entry = result["result"]
result = await hass.config_entries.options.async_init(entry.entry_id, data=None)
@@ -156,12 +168,18 @@ async def test_tv_options_flow_no_apps(hass: HomeAssistantType) -> None:
assert CONF_APPS not in result["data"]
async def test_tv_options_flow_with_apps(hass: HomeAssistantType) -> None:
async def test_tv_options_flow_with_apps(
hass: HomeAssistantType,
vizio_connect: pytest.fixture,
vizio_bypass_update: pytest.fixture,
) -> None:
"""Test options config flow for TV with providing apps option."""
entry = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_VALID_TV_CONFIG)
entry.add_to_hass(hass)
assert not entry.options
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=MOCK_USER_VALID_TV_CONFIG
)
await hass.async_block_till_done()
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
entry = result["result"]
result = await hass.config_entries.options.async_init(entry.entry_id, data=None)
@@ -182,14 +200,23 @@ async def test_tv_options_flow_with_apps(hass: HomeAssistantType) -> None:
assert result["data"][CONF_APPS] == {CONF_INCLUDE: [CURRENT_APP]}
async def test_tv_options_flow_start_with_volume(hass: HomeAssistantType) -> None:
async def test_tv_options_flow_start_with_volume(
hass: HomeAssistantType,
vizio_connect: pytest.fixture,
vizio_bypass_update: pytest.fixture,
) -> None:
"""Test options config flow for TV with providing apps option after providing volume step in initial config."""
entry = MockConfigEntry(
domain=DOMAIN,
data=MOCK_USER_VALID_TV_CONFIG,
options={CONF_VOLUME_STEP: VOLUME_STEP},
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=MOCK_USER_VALID_TV_CONFIG
)
entry.add_to_hass(hass)
await hass.async_block_till_done()
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
entry = result["result"]
result = await hass.config_entries.options.async_init(
entry.entry_id, data={CONF_VOLUME_STEP: VOLUME_STEP}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert entry.options
assert entry.options == {CONF_VOLUME_STEP: VOLUME_STEP}
+7 -1
View File
@@ -4,6 +4,7 @@ import pytest
from homeassistant.components.media_player.const import DOMAIN as MP_DOMAIN
from homeassistant.components.vizio.const import DOMAIN
from homeassistant.helpers.typing import HomeAssistantType
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from homeassistant.setup import async_setup_component
from .const import MOCK_USER_VALID_TV_CONFIG, UNIQUE_ID
@@ -37,7 +38,12 @@ async def test_load_and_unload(
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert len(hass.states.async_entity_ids(MP_DOMAIN)) == 1
assert DOMAIN in hass.data
assert "apps" in hass.data[DOMAIN]
assert isinstance(hass.data[DOMAIN]["apps"], DataUpdateCoordinator)
assert await hass.config_entries.async_unload(config_entry.entry_id)
assert await config_entry.async_unload(hass)
await hass.async_block_till_done()
assert len(hass.states.async_entity_ids(MP_DOMAIN)) == 0
assert "apps" not in hass.data.get(DOMAIN, {})
assert DOMAIN not in hass.data
+46 -6
View File
@@ -8,6 +8,7 @@ import pytest
from pytest import raises
from pyvizio.api.apps import AppConfig
from pyvizio.const import (
APPS,
DEVICE_CLASS_SPEAKER as VIZIO_DEVICE_CLASS_SPEAKER,
DEVICE_CLASS_TV as VIZIO_DEVICE_CLASS_TV,
INPUT_APPS,
@@ -51,6 +52,7 @@ from homeassistant.util import dt as dt_util
from .const import (
ADDITIONAL_APP_CONFIG,
APP_LIST,
APP_NAME_LIST,
CURRENT_APP,
CURRENT_APP_CONFIG,
CURRENT_EQ,
@@ -358,7 +360,11 @@ async def test_services(
await _test_service(hass, MP_DOMAIN, "pow_on", SERVICE_TURN_ON, None)
await _test_service(hass, MP_DOMAIN, "pow_off", SERVICE_TURN_OFF, None)
await _test_service(
hass, MP_DOMAIN, "mute_on", SERVICE_VOLUME_MUTE, {ATTR_MEDIA_VOLUME_MUTED: True}
hass,
MP_DOMAIN,
"mute_on",
SERVICE_VOLUME_MUTE,
{ATTR_MEDIA_VOLUME_MUTED: True},
)
await _test_service(
hass,
@@ -511,7 +517,7 @@ async def test_setup_with_apps(
hass, MOCK_USER_VALID_TV_CONFIG, CURRENT_APP_CONFIG
):
attr = hass.states.get(ENTITY_ID).attributes
_assert_source_list_with_apps(list(INPUT_LIST_WITH_APPS + APP_LIST), attr)
_assert_source_list_with_apps(list(INPUT_LIST_WITH_APPS + APP_NAME_LIST), attr)
assert CURRENT_APP in attr["source_list"]
assert attr["source"] == CURRENT_APP
assert attr["app_name"] == CURRENT_APP
@@ -524,6 +530,7 @@ async def test_setup_with_apps(
SERVICE_SELECT_SOURCE,
{ATTR_INPUT_SOURCE: CURRENT_APP},
CURRENT_APP,
APP_LIST,
)
@@ -580,13 +587,13 @@ async def test_setup_with_apps_additional_apps_config(
_assert_source_list_with_apps(
list(
INPUT_LIST_WITH_APPS
+ APP_LIST
+ APP_NAME_LIST
+ [
app["name"]
for app in MOCK_TV_WITH_ADDITIONAL_APPS_CONFIG[CONF_APPS][
CONF_ADDITIONAL_CONFIGS
]
if app["name"] not in APP_LIST
if app["name"] not in APP_NAME_LIST
]
),
attr,
@@ -603,6 +610,7 @@ async def test_setup_with_apps_additional_apps_config(
SERVICE_SELECT_SOURCE,
{ATTR_INPUT_SOURCE: "Netflix"},
"Netflix",
APP_LIST,
)
await _test_service(
hass,
@@ -649,7 +657,7 @@ async def test_setup_with_unknown_app_config(
hass, MOCK_USER_VALID_TV_CONFIG, UNKNOWN_APP_CONFIG
):
attr = hass.states.get(ENTITY_ID).attributes
_assert_source_list_with_apps(list(INPUT_LIST_WITH_APPS + APP_LIST), attr)
_assert_source_list_with_apps(list(INPUT_LIST_WITH_APPS + APP_NAME_LIST), attr)
assert attr["source"] == UNKNOWN_APP
assert attr["app_name"] == UNKNOWN_APP
assert attr["app_id"] == UNKNOWN_APP_CONFIG
@@ -666,7 +674,7 @@ async def test_setup_with_no_running_app(
hass, MOCK_USER_VALID_TV_CONFIG, vars(AppConfig())
):
attr = hass.states.get(ENTITY_ID).attributes
_assert_source_list_with_apps(list(INPUT_LIST_WITH_APPS + APP_LIST), attr)
_assert_source_list_with_apps(list(INPUT_LIST_WITH_APPS + APP_NAME_LIST), attr)
assert attr["source"] == "CAST"
assert "app_id" not in attr
assert "app_name" not in attr
@@ -694,3 +702,35 @@ async def test_setup_tv_without_mute(
_assert_sources_and_volume(attr, VIZIO_DEVICE_CLASS_TV)
assert "sound_mode" not in attr
assert "is_volume_muted" not in attr
async def test_apps_update(
hass: HomeAssistantType,
vizio_connect: pytest.fixture,
vizio_update_with_apps: pytest.fixture,
caplog: pytest.fixture,
) -> None:
"""Test device setup with apps where no app is running."""
with patch(
"homeassistant.components.vizio.gen_apps_list_from_url", return_value=None,
):
async with _cm_for_test_setup_tv_with_apps(
hass, MOCK_USER_VALID_TV_CONFIG, vars(AppConfig())
):
# Check source list, remove TV inputs, and verify that the integration is
# using the default APPS list
sources = hass.states.get(ENTITY_ID).attributes["source_list"]
apps = list(set(sources) - set(INPUT_LIST))
assert len(apps) == len(APPS)
with patch(
"homeassistant.components.vizio.gen_apps_list_from_url",
return_value=APP_LIST,
):
async_fire_time_changed(hass, dt_util.now() + timedelta(days=2))
await hass.async_block_till_done()
# Check source list, remove TV inputs, and verify that the integration is
# now using the APP_LIST list
sources = hass.states.get(ENTITY_ID).attributes["source_list"]
apps = list(set(sources) - set(INPUT_LIST))
assert len(apps) == len(APP_LIST)