diff --git a/homeassistant/components/vizio/media_player.py b/homeassistant/components/vizio/media_player.py index db0282b1d1d3..d9b1f635061b 100644 --- a/homeassistant/components/vizio/media_player.py +++ b/homeassistant/components/vizio/media_player.py @@ -208,12 +208,17 @@ class VizioDevice(VizioEntity, MediaPlayerEntity): else () ), ) - # find_app_name returns None on a catalog miss; the app_name state - # attribute contract expects the UNKNOWN_APP sentinel instead + # A catalog miss only warrants the UNKNOWN_APP sentinel on the app + # input; HDMI and the like report a config that is never an app. if app_name == NO_APP_RUNNING: self._attr_app_name = None elif app_name is None: - self._attr_app_name = UNKNOWN_APP + self._attr_app_name = ( + UNKNOWN_APP + if self._current_input is not None + and is_app_input(self._current_input) + else None + ) else: self._attr_app_name = app_name diff --git a/tests/components/vizio/test_media_player.py b/tests/components/vizio/test_media_player.py index 0385be4c6348..4c510c433cc6 100644 --- a/tests/components/vizio/test_media_player.py +++ b/tests/components/vizio/test_media_player.py @@ -855,14 +855,45 @@ async def test_apps_update( assert len(apps) == len(APP_RECORDS) +@pytest.mark.parametrize( + ("entry_data", "app_config", "expected_app_name"), + [ + pytest.param( + MOCK_USER_VALID_TV_CONFIG, + UNKNOWN_APP_CONFIG_OBJ, + None, + id="unknown_config_hides_app_name", + ), + pytest.param( + MOCK_TV_WITH_ADDITIONAL_APPS_CONFIG, + CUSTOM_CONFIG_OBJ, + ADDITIONAL_APP_CONFIG["name"], + id="additional_config_keeps_mapped_name", + ), + ], +) @pytest.mark.usefixtures("vizio_connect", "vizio_update_with_apps_on_input") async def test_vizio_update_with_apps_on_input( - hass: HomeAssistant, mock_tv_config_entry: MockConfigEntry + hass: HomeAssistant, + entry_data: dict[str, Any], + app_config: AppConfig, + expected_app_name: str | None, ) -> None: - """Test a vizio TV with apps that is on a TV input.""" - await setup_integration(hass, mock_tv_config_entry) + """Test a vizio TV with apps that is on a TV input. + + The device reports an app config even on HDMI. An unrecognized one must + not surface as the unknown-app sentinel, while a name mapped through + additional_configs is deliberate user configuration and is kept. + """ + config_entry = MockConfigEntry(domain=DOMAIN, data=entry_data, unique_id=UNIQUE_ID) + with patch( + "homeassistant.components.vizio.Vizio.get_current_app_config", + return_value=app_config, + ): + await setup_integration(hass, config_entry) attr = _get_attr_and_assert_base_attr(hass, MediaPlayerDeviceClass.TV, STATE_ON) - # app ID should not be in the attributes + assert attr[ATTR_INPUT_SOURCE] == CURRENT_INPUT + assert attr.get("app_name") == expected_app_name assert "app_id" not in attr