From 0383030266340013d34c8717f452e95151962cd3 Mon Sep 17 00:00:00 2001 From: Raman Gupta <7243222+raman325@users.noreply.github.com> Date: Fri, 18 Sep 2020 07:18:56 -0400 Subject: [PATCH] Fix Vizio async_unload_entry bug (#40210) --- homeassistant/components/vizio/__init__.py | 2 +- tests/components/vizio/test_init.py | 27 +++++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/vizio/__init__.py b/homeassistant/components/vizio/__init__.py index 25960da72cf..a7a9c404f74 100644 --- a/homeassistant/components/vizio/__init__.py +++ b/homeassistant/components/vizio/__init__.py @@ -94,7 +94,7 @@ async def async_unload_entry( and entry.data[CONF_DEVICE_CLASS] == DEVICE_CLASS_TV for entry in hass.config_entries.async_entries(DOMAIN) ): - hass.data[DOMAIN].pop(CONF_APPS) + hass.data[DOMAIN].pop(CONF_APPS, None) if not hass.data[DOMAIN]: hass.data.pop(DOMAIN) diff --git a/tests/components/vizio/test_init.py b/tests/components/vizio/test_init.py index 33764de8696..cd611662597 100644 --- a/tests/components/vizio/test_init.py +++ b/tests/components/vizio/test_init.py @@ -6,7 +6,7 @@ from homeassistant.components.vizio.const import DOMAIN from homeassistant.helpers.typing import HomeAssistantType from homeassistant.setup import async_setup_component -from .const import MOCK_USER_VALID_TV_CONFIG, UNIQUE_ID +from .const import MOCK_SPEAKER_CONFIG, MOCK_USER_VALID_TV_CONFIG, UNIQUE_ID from tests.common import MockConfigEntry @@ -24,12 +24,12 @@ async def test_setup_component( assert len(hass.states.async_entity_ids(MP_DOMAIN)) == 1 -async def test_load_and_unload( +async def test_tv_load_and_unload( hass: HomeAssistantType, vizio_connect: pytest.fixture, vizio_update: pytest.fixture, ) -> None: - """Test loading and unloading entry.""" + """Test loading and unloading TV entry.""" config_entry = MockConfigEntry( domain=DOMAIN, data=MOCK_USER_VALID_TV_CONFIG, unique_id=UNIQUE_ID ) @@ -43,3 +43,24 @@ async def test_load_and_unload( await hass.async_block_till_done() assert len(hass.states.async_entity_ids(MP_DOMAIN)) == 0 assert DOMAIN not in hass.data + + +async def test_speaker_load_and_unload( + hass: HomeAssistantType, + vizio_connect: pytest.fixture, + vizio_update: pytest.fixture, +) -> None: + """Test loading and unloading speaker entry.""" + config_entry = MockConfigEntry( + domain=DOMAIN, data=MOCK_SPEAKER_CONFIG, unique_id=UNIQUE_ID + ) + config_entry.add_to_hass(hass) + 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 await config_entry.async_unload(hass) + await hass.async_block_till_done() + assert len(hass.states.async_entity_ids(MP_DOMAIN)) == 0 + assert DOMAIN not in hass.data