11"""Tests for Vizio config flow."""
22from datetime import timedelta
33import logging
4- from typing import Any , Dict
4+ from typing import Any , Dict , Optional
55from unittest .mock import call
66
77from asynctest import patch
88import pytest
99from pytest import raises
10- from pyvizio ._api .apps import AppConfig
10+ from pyvizio .api .apps import AppConfig
1111from pyvizio .const import (
1212 DEVICE_CLASS_SPEAKER as VIZIO_DEVICE_CLASS_SPEAKER ,
1313 DEVICE_CLASS_TV as VIZIO_DEVICE_CLASS_TV ,
5757 ADDITIONAL_APP_CONFIG ,
5858 APP_LIST ,
5959 CURRENT_APP ,
60+ CURRENT_APP_CONFIG ,
6061 CURRENT_INPUT ,
6162 CUSTOM_CONFIG ,
6263 ENTITY_ID ,
7172 MOCK_USER_VALID_TV_CONFIG ,
7273 NAME ,
7374 UNIQUE_ID ,
75+ UNKNOWN_APP_CONFIG ,
7476 VOLUME_STEP ,
7577)
7678
8082
8183
8284async def _test_setup (
83- hass : HomeAssistantType , ha_device_class : str , vizio_power_state : bool
85+ hass : HomeAssistantType , ha_device_class : str , vizio_power_state : Optional [ bool ]
8486) -> None :
8587 """Test Vizio Device entity setup."""
8688 if vizio_power_state :
@@ -112,7 +114,7 @@ async def _test_setup(
112114 "homeassistant.components.vizio.media_player.VizioAsync.get_power_state" ,
113115 return_value = vizio_power_state ,
114116 ), patch (
115- "homeassistant.components.vizio.media_player.VizioAsync.get_current_app " ,
117+ "homeassistant.components.vizio.media_player.VizioAsync.get_current_app_config " ,
116118 ) as service_call :
117119 config_entry .add_to_hass (hass )
118120 assert await hass .config_entries .async_setup (config_entry .entry_id )
@@ -136,7 +138,10 @@ async def _test_setup(
136138
137139
138140async def _test_setup_with_apps (
139- hass : HomeAssistantType , device_config : Dict [str , Any ], app : str
141+ hass : HomeAssistantType ,
142+ device_config : Dict [str , Any ],
143+ app : Optional [str ],
144+ app_config : Dict [str , Any ],
140145) -> None :
141146 """Test Vizio Device with apps entity setup."""
142147 config_entry = MockConfigEntry (
@@ -152,12 +157,9 @@ async def _test_setup_with_apps(
152157 ), patch (
153158 "homeassistant.components.vizio.media_player.VizioAsync.get_power_state" ,
154159 return_value = True ,
155- ), patch (
156- "homeassistant.components.vizio.media_player.VizioAsync.get_current_app" ,
157- return_value = app ,
158160 ), patch (
159161 "homeassistant.components.vizio.media_player.VizioAsync.get_current_app_config" ,
160- return_value = AppConfig (** ADDITIONAL_APP_CONFIG [ "config" ] ),
162+ return_value = AppConfig (** app_config ),
161163 ):
162164 config_entry .add_to_hass (hass )
163165 assert await hass .config_entries .async_setup (config_entry .entry_id )
@@ -193,11 +195,20 @@ async def _test_setup_with_apps(
193195 list_to_test .remove (app_to_remove )
194196
195197 assert attr ["source_list" ] == list_to_test
196- assert app in attr ["source_list" ] or app == UNKNOWN_APP
197- if app == UNKNOWN_APP :
198- assert attr ["source" ] == ADDITIONAL_APP_CONFIG ["name" ]
199- else :
198+
199+ if app :
200+ assert app in attr ["source_list" ] or app == UNKNOWN_APP
200201 assert attr ["source" ] == app
202+ assert attr ["app_name" ] == app
203+ if app == UNKNOWN_APP :
204+ assert attr ["app_id" ] == app_config
205+ else :
206+ assert "app_id" not in attr
207+ else :
208+ assert attr ["source" ] == "CAST"
209+ assert "app_id" not in attr
210+ assert "app_name" not in attr
211+
201212 assert (
202213 attr ["volume_level" ]
203214 == float (int (MAX_VOLUME [VIZIO_DEVICE_CLASS_TV ] / 2 ))
@@ -222,7 +233,7 @@ async def _test_service(
222233 hass : HomeAssistantType ,
223234 vizio_func_name : str ,
224235 ha_service_name : str ,
225- additional_service_data : dict ,
236+ additional_service_data : Optional [ Dict [ str , Any ]] ,
226237 * args ,
227238 ** kwargs ,
228239) -> None :
@@ -363,8 +374,8 @@ async def test_options_update(
363374
364375async def _test_update_availability_switch (
365376 hass : HomeAssistantType ,
366- initial_power_state : bool ,
367- final_power_state : bool ,
377+ initial_power_state : Optional [ bool ] ,
378+ final_power_state : Optional [ bool ] ,
368379 caplog : pytest .fixture ,
369380) -> None :
370381 now = dt_util .utcnow ()
@@ -431,7 +442,9 @@ async def test_setup_with_apps(
431442 caplog : pytest .fixture ,
432443) -> None :
433444 """Test device setup with apps."""
434- await _test_setup_with_apps (hass , MOCK_USER_VALID_TV_CONFIG , CURRENT_APP )
445+ await _test_setup_with_apps (
446+ hass , MOCK_USER_VALID_TV_CONFIG , CURRENT_APP , CURRENT_APP_CONFIG
447+ )
435448 await _test_service (
436449 hass ,
437450 "launch_app" ,
@@ -448,7 +461,9 @@ async def test_setup_with_apps_include(
448461 caplog : pytest .fixture ,
449462) -> None :
450463 """Test device setup with apps and apps["include"] in config."""
451- await _test_setup_with_apps (hass , MOCK_TV_WITH_INCLUDE_CONFIG , CURRENT_APP )
464+ await _test_setup_with_apps (
465+ hass , MOCK_TV_WITH_INCLUDE_CONFIG , CURRENT_APP , CURRENT_APP_CONFIG
466+ )
452467
453468
454469async def test_setup_with_apps_exclude (
@@ -458,7 +473,9 @@ async def test_setup_with_apps_exclude(
458473 caplog : pytest .fixture ,
459474) -> None :
460475 """Test device setup with apps and apps["exclude"] in config."""
461- await _test_setup_with_apps (hass , MOCK_TV_WITH_EXCLUDE_CONFIG , CURRENT_APP )
476+ await _test_setup_with_apps (
477+ hass , MOCK_TV_WITH_EXCLUDE_CONFIG , CURRENT_APP , CURRENT_APP_CONFIG
478+ )
462479
463480
464481async def test_setup_with_apps_additional_apps_config (
@@ -468,7 +485,12 @@ async def test_setup_with_apps_additional_apps_config(
468485 caplog : pytest .fixture ,
469486) -> None :
470487 """Test device setup with apps and apps["additional_configs"] in config."""
471- await _test_setup_with_apps (hass , MOCK_TV_WITH_ADDITIONAL_APPS_CONFIG , UNKNOWN_APP )
488+ await _test_setup_with_apps (
489+ hass ,
490+ MOCK_TV_WITH_ADDITIONAL_APPS_CONFIG ,
491+ ADDITIONAL_APP_CONFIG ["name" ],
492+ ADDITIONAL_APP_CONFIG ["config" ],
493+ )
472494
473495 await _test_service (
474496 hass ,
@@ -508,3 +530,27 @@ def test_invalid_apps_config(hass: HomeAssistantType):
508530
509531 with raises (vol .Invalid ):
510532 vol .Schema (vol .All (VIZIO_SCHEMA , validate_apps ))(MOCK_SPEAKER_APPS_FAILURE )
533+
534+
535+ async def test_setup_with_unknown_app_config (
536+ hass : HomeAssistantType ,
537+ vizio_connect : pytest .fixture ,
538+ vizio_update_with_apps : pytest .fixture ,
539+ caplog : pytest .fixture ,
540+ ) -> None :
541+ """Test device setup with apps where app config returned is unknown."""
542+ await _test_setup_with_apps (
543+ hass , MOCK_USER_VALID_TV_CONFIG , UNKNOWN_APP , UNKNOWN_APP_CONFIG
544+ )
545+
546+
547+ async def test_setup_with_no_running_app (
548+ hass : HomeAssistantType ,
549+ vizio_connect : pytest .fixture ,
550+ vizio_update_with_apps : pytest .fixture ,
551+ caplog : pytest .fixture ,
552+ ) -> None :
553+ """Test device setup with apps where no app is running."""
554+ await _test_setup_with_apps (
555+ hass , MOCK_USER_VALID_TV_CONFIG , None , vars (AppConfig ())
556+ )
0 commit comments