From 3c2df7f8f26d1baf6becce4fbe3f8c6a2326475b Mon Sep 17 00:00:00 2001 From: SukramJ Date: Sun, 29 Mar 2020 06:01:53 +0200 Subject: [PATCH] Fix homematicip_cloud tests that have uncaught exceptions (#33371) --- .../components/homematicip_cloud/device.py | 8 +++++-- .../components/homematicip_cloud/conftest.py | 14 ++++++++++++- .../homematicip_cloud/test_config_flow.py | 4 ++++ .../components/homematicip_cloud/test_init.py | 21 +++++++++++++------ tests/ignore_uncaught_exceptions.py | 15 ------------- 5 files changed, 38 insertions(+), 24 deletions(-) diff --git a/homeassistant/components/homematicip_cloud/device.py b/homeassistant/components/homematicip_cloud/device.py index f35b696767c..8e6ca1f75fe 100644 --- a/homeassistant/components/homematicip_cloud/device.py +++ b/homeassistant/components/homematicip_cloud/device.py @@ -121,9 +121,13 @@ class HomematicipGenericDevice(Entity): # Only go further if the device/entity should be removed from registries # due to a removal of the HmIP device. + if self.hmip_device_removed: - del self._hap.hmip_device_by_entity_id[self.entity_id] - await self.async_remove_from_registries() + try: + del self._hap.hmip_device_by_entity_id[self.entity_id] + await self.async_remove_from_registries() + except KeyError as err: + _LOGGER.debug("Error removing HMIP entity from registry: %s", err) async def async_remove_from_registries(self) -> None: """Remove entity/device from registry.""" diff --git a/tests/components/homematicip_cloud/conftest.py b/tests/components/homematicip_cloud/conftest.py index 927690d881f..b1933604fbe 100644 --- a/tests/components/homematicip_cloud/conftest.py +++ b/tests/components/homematicip_cloud/conftest.py @@ -3,6 +3,7 @@ from asynctest import CoroutineMock, MagicMock, Mock, patch from homematicip.aio.auth import AsyncAuth from homematicip.aio.connection import AsyncConnection from homematicip.aio.home import AsyncHome +from homematicip.base.enums import WeatherCondition, WeatherDayTime import pytest from homeassistant import config_entries @@ -115,10 +116,21 @@ def simple_mock_home_fixture(): devices=[], groups=[], location=Mock(), - weather=Mock(create=True), + weather=Mock( + temperature=0.0, + weatherCondition=WeatherCondition.UNKNOWN, + weatherDayTime=WeatherDayTime.DAY, + minTemperature=0.0, + maxTemperature=0.0, + humidity=0, + windSpeed=0.0, + windDirection=0, + vaporAmount=0.0, + ), id=42, dutyCycle=88, connected=True, + currentAPVersion="2.0.36", ) with patch( diff --git a/tests/components/homematicip_cloud/test_config_flow.py b/tests/components/homematicip_cloud/test_config_flow.py index 6436433a147..ec13fc79536 100644 --- a/tests/components/homematicip_cloud/test_config_flow.py +++ b/tests/components/homematicip_cloud/test_config_flow.py @@ -52,6 +52,8 @@ async def test_flow_works(hass, simple_mock_home): ), patch( "homeassistant.components.homematicip_cloud.hap.HomematicipAuth.async_register", return_value=True, + ), patch( + "homeassistant.components.homematicip_cloud.hap.HomematicipHAP.async_connect", ): result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={} @@ -151,6 +153,8 @@ async def test_import_config(hass, simple_mock_home): ), patch( "homeassistant.components.homematicip_cloud.hap.HomematicipAuth.async_register", return_value=True, + ), patch( + "homeassistant.components.homematicip_cloud.hap.HomematicipHAP.async_connect", ): result = await hass.config_entries.flow.async_init( HMIPC_DOMAIN, context={"source": "import"}, data=IMPORT_CONFIG diff --git a/tests/components/homematicip_cloud/test_init.py b/tests/components/homematicip_cloud/test_init.py index f97e7114b94..8f2753bc499 100644 --- a/tests/components/homematicip_cloud/test_init.py +++ b/tests/components/homematicip_cloud/test_init.py @@ -39,7 +39,12 @@ async def test_config_with_accesspoint_passed_to_config_entry( # no acccesspoint exists assert not hass.data.get(HMIPC_DOMAIN) - assert await async_setup_component(hass, HMIPC_DOMAIN, {HMIPC_DOMAIN: entry_config}) + with patch( + "homeassistant.components.homematicip_cloud.hap.HomematicipHAP.async_connect", + ): + assert await async_setup_component( + hass, HMIPC_DOMAIN, {HMIPC_DOMAIN: entry_config} + ) # config_entry created for access point config_entries = hass.config_entries.async_entries(HMIPC_DOMAIN) @@ -77,7 +82,13 @@ async def test_config_already_registered_not_passed_to_config_entry( CONF_AUTHTOKEN: "123", CONF_NAME: "name", } - assert await async_setup_component(hass, HMIPC_DOMAIN, {HMIPC_DOMAIN: entry_config}) + + with patch( + "homeassistant.components.homematicip_cloud.hap.HomematicipHAP.async_connect", + ): + assert await async_setup_component( + hass, HMIPC_DOMAIN, {HMIPC_DOMAIN: entry_config} + ) # no new config_entry created / still one config_entry config_entries = hass.config_entries.async_entries(HMIPC_DOMAIN) @@ -107,16 +118,14 @@ async def test_load_entry_fails_due_to_connection_error( assert hmip_config_entry.state == ENTRY_STATE_SETUP_RETRY -async def test_load_entry_fails_due_to_generic_exception( - hass, hmip_config_entry, simple_mock_home -): +async def test_load_entry_fails_due_to_generic_exception(hass, hmip_config_entry): """Test load entry fails due to generic exception.""" hmip_config_entry.add_to_hass(hass) with patch( "homeassistant.components.homematicip_cloud.hap.AsyncHome.get_current_state", side_effect=Exception, - ): + ), patch("homematicip.aio.connection.AsyncConnection.init",): assert await async_setup_component(hass, HMIPC_DOMAIN, {}) assert hass.data[HMIPC_DOMAIN][hmip_config_entry.unique_id] diff --git a/tests/ignore_uncaught_exceptions.py b/tests/ignore_uncaught_exceptions.py index 16bd736cef6..071504ad8b7 100644 --- a/tests/ignore_uncaught_exceptions.py +++ b/tests/ignore_uncaught_exceptions.py @@ -54,21 +54,6 @@ IGNORE_UNCAUGHT_EXCEPTIONS = [ ("tests.components.geonetnz_quakes.test_geo_location", "test_setup"), ("tests.components.geonetnz_quakes.test_sensor", "test_setup"), ("test_homeassistant_bridge", "test_homeassistant_bridge_fan_setup"), - ("tests.components.homematicip_cloud.test_config_flow", "test_flow_works"), - ("tests.components.homematicip_cloud.test_config_flow", "test_import_config"), - ("tests.components.homematicip_cloud.test_device", "test_hmip_remove_group"), - ( - "tests.components.homematicip_cloud.test_init", - "test_config_with_accesspoint_passed_to_config_entry", - ), - ( - "tests.components.homematicip_cloud.test_init", - "test_config_already_registered_not_passed_to_config_entry", - ), - ( - "tests.components.homematicip_cloud.test_init", - "test_load_entry_fails_due_to_generic_exception", - ), ("tests.components.hue.test_bridge", "test_handle_unauthorized"), ("tests.components.hue.test_init", "test_security_vuln_check"), ("tests.components.hue.test_light", "test_group_features"),