From 783e0f9a143181aae27c67e4c67cec14d2f35352 Mon Sep 17 00:00:00 2001 From: Raman Gupta <7243222+raman325@users.noreply.github.com> Date: Wed, 24 Feb 2021 14:15:47 -0500 Subject: [PATCH] Setup config entry even if vizio device is unreachable (#46864) --- .../components/vizio/media_player.py | 17 +++------ tests/components/vizio/conftest.py | 3 ++ tests/components/vizio/test_media_player.py | 37 +++++++++---------- 3 files changed, 27 insertions(+), 30 deletions(-) diff --git a/homeassistant/components/vizio/media_player.py b/homeassistant/components/vizio/media_player.py index 4c06c89692a..53c8a2bba88 100644 --- a/homeassistant/components/vizio/media_player.py +++ b/homeassistant/components/vizio/media_player.py @@ -25,7 +25,6 @@ from homeassistant.const import ( STATE_ON, ) from homeassistant.core import callback -from homeassistant.exceptions import PlatformNotReady from homeassistant.helpers import entity_platform from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.dispatcher import ( @@ -115,10 +114,6 @@ async def async_setup_entry( timeout=DEFAULT_TIMEOUT, ) - if not await device.can_connect_with_auth_check(): - _LOGGER.warning("Failed to connect to %s", host) - raise PlatformNotReady - apps_coordinator = hass.data[DOMAIN].get(CONF_APPS) entity = VizioDevice(config_entry, device, name, device_class, apps_coordinator) @@ -183,12 +178,6 @@ class VizioDevice(MediaPlayerEntity): async def async_update(self) -> None: """Retrieve latest state of the device.""" - if not self._model: - self._model = await self._device.get_model_name(log_api_exception=False) - - if not self._sw_version: - self._sw_version = await self._device.get_version(log_api_exception=False) - is_on = await self._device.get_power_state(log_api_exception=False) if is_on is None: @@ -205,6 +194,12 @@ class VizioDevice(MediaPlayerEntity): ) self._available = True + if not self._model: + self._model = await self._device.get_model_name(log_api_exception=False) + + if not self._sw_version: + self._sw_version = await self._device.get_version(log_api_exception=False) + if not is_on: self._state = STATE_OFF self._volume_level = None diff --git a/tests/components/vizio/conftest.py b/tests/components/vizio/conftest.py index 917e6f7f291..8124827dbf0 100644 --- a/tests/components/vizio/conftest.py +++ b/tests/components/vizio/conftest.py @@ -157,6 +157,9 @@ def vizio_cant_connect_fixture(): with patch( "homeassistant.components.vizio.config_flow.VizioAsync.validate_ha_config", AsyncMock(return_value=False), + ), patch( + "homeassistant.components.vizio.media_player.VizioAsync.get_power_state", + return_value=None, ): yield diff --git a/tests/components/vizio/test_media_player.py b/tests/components/vizio/test_media_player.py index 78976032b00..6d0ba2781e6 100644 --- a/tests/components/vizio/test_media_player.py +++ b/tests/components/vizio/test_media_player.py @@ -239,17 +239,6 @@ def _assert_source_list_with_apps( assert attr["source_list"] == list_to_test -async def _test_setup_failure(hass: HomeAssistantType, config: str) -> None: - """Test generic Vizio entity setup failure.""" - with patch( - "homeassistant.components.vizio.media_player.VizioAsync.can_connect_with_auth_check", - return_value=False, - ): - config_entry = MockConfigEntry(domain=DOMAIN, data=config, unique_id=UNIQUE_ID) - await _add_config_entry_to_hass(hass, config_entry) - assert len(hass.states.async_entity_ids(MP_DOMAIN)) == 0 - - async def _test_service( hass: HomeAssistantType, domain: str, @@ -334,18 +323,28 @@ async def test_init_tv_unavailable( await _test_setup_tv(hass, None) -async def test_setup_failure_speaker( - hass: HomeAssistantType, vizio_connect: pytest.fixture +async def test_setup_unavailable_speaker( + hass: HomeAssistantType, vizio_cant_connect: pytest.fixture ) -> None: - """Test speaker entity setup failure.""" - await _test_setup_failure(hass, MOCK_SPEAKER_CONFIG) + """Test speaker entity sets up as unavailable.""" + config_entry = MockConfigEntry( + domain=DOMAIN, data=MOCK_SPEAKER_CONFIG, unique_id=UNIQUE_ID + ) + await _add_config_entry_to_hass(hass, config_entry) + assert len(hass.states.async_entity_ids(MP_DOMAIN)) == 1 + assert hass.states.get("media_player.vizio").state == STATE_UNAVAILABLE -async def test_setup_failure_tv( - hass: HomeAssistantType, vizio_connect: pytest.fixture +async def test_setup_unavailable_tv( + hass: HomeAssistantType, vizio_cant_connect: pytest.fixture ) -> None: - """Test TV entity setup failure.""" - await _test_setup_failure(hass, MOCK_USER_VALID_TV_CONFIG) + """Test TV entity sets up as unavailable.""" + config_entry = MockConfigEntry( + domain=DOMAIN, data=MOCK_USER_VALID_TV_CONFIG, unique_id=UNIQUE_ID + ) + await _add_config_entry_to_hass(hass, config_entry) + assert len(hass.states.async_entity_ids(MP_DOMAIN)) == 1 + assert hass.states.get("media_player.vizio").state == STATE_UNAVAILABLE async def test_services(