From 05d7d31dfd4d4eb4a8b10a84319d7f2778e65298 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Wed, 15 Jun 2022 12:12:07 +0200 Subject: [PATCH] Improve Elgato error handling (#73444) --- homeassistant/components/elgato/button.py | 7 ++-- homeassistant/components/elgato/light.py | 28 ++++++++++------ tests/components/elgato/test_button.py | 21 +++++++----- tests/components/elgato/test_light.py | 41 +++++++++++++---------- 4 files changed, 58 insertions(+), 39 deletions(-) diff --git a/homeassistant/components/elgato/button.py b/homeassistant/components/elgato/button.py index f2cfc2b8673..c846c42c653 100644 --- a/homeassistant/components/elgato/button.py +++ b/homeassistant/components/elgato/button.py @@ -9,6 +9,7 @@ from homeassistant.components.button import ButtonEntity, ButtonEntityDescriptio from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_MAC from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity_platform import AddEntitiesCallback @@ -49,5 +50,7 @@ class ElgatoIdentifyButton(ElgatoEntity, ButtonEntity): """Identify the light, will make it blink.""" try: await self.client.identify() - except ElgatoError: - _LOGGER.exception("An error occurred while identifying the Elgato Light") + except ElgatoError as error: + raise HomeAssistantError( + "An error occurred while identifying the Elgato Light" + ) from error diff --git a/homeassistant/components/elgato/light.py b/homeassistant/components/elgato/light.py index 8b9a7bce7e1..e13119c2887 100644 --- a/homeassistant/components/elgato/light.py +++ b/homeassistant/components/elgato/light.py @@ -15,6 +15,7 @@ from homeassistant.components.light import ( from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_MAC from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.entity_platform import ( AddEntitiesCallback, async_get_current_platform, @@ -25,7 +26,7 @@ from homeassistant.helpers.update_coordinator import ( ) from . import HomeAssistantElgatoData -from .const import DOMAIN, LOGGER, SERVICE_IDENTIFY +from .const import DOMAIN, SERVICE_IDENTIFY from .entity import ElgatoEntity PARALLEL_UPDATES = 1 @@ -121,9 +122,12 @@ class ElgatoLight( """Turn off the light.""" try: await self.client.light(on=False) - except ElgatoError: - LOGGER.error("An error occurred while updating the Elgato Light") - await self.coordinator.async_refresh() + except ElgatoError as error: + raise HomeAssistantError( + "An error occurred while updating the Elgato Light" + ) from error + finally: + await self.coordinator.async_refresh() async def async_turn_on(self, **kwargs: Any) -> None: """Turn on the light.""" @@ -159,14 +163,18 @@ class ElgatoLight( saturation=saturation, temperature=temperature, ) - except ElgatoError: - LOGGER.error("An error occurred while updating the Elgato Light") - await self.coordinator.async_refresh() + except ElgatoError as error: + raise HomeAssistantError( + "An error occurred while updating the Elgato Light" + ) from error + finally: + await self.coordinator.async_refresh() async def async_identify(self) -> None: """Identify the light, will make it blink.""" try: await self.client.identify() - except ElgatoError: - LOGGER.exception("An error occurred while identifying the Elgato Light") - await self.coordinator.async_refresh() + except ElgatoError as error: + raise HomeAssistantError( + "An error occurred while identifying the Elgato Light" + ) from error diff --git a/tests/components/elgato/test_button.py b/tests/components/elgato/test_button.py index 6f182ee191c..2ab3d7ee7c4 100644 --- a/tests/components/elgato/test_button.py +++ b/tests/components/elgato/test_button.py @@ -8,6 +8,7 @@ from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRE from homeassistant.components.elgato.const import DOMAIN from homeassistant.const import ATTR_ENTITY_ID, ATTR_ICON, STATE_UNKNOWN from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers.entity import EntityCategory @@ -68,17 +69,19 @@ async def test_button_identify_error( hass: HomeAssistant, init_integration: MockConfigEntry, mock_elgato: MagicMock, - caplog: pytest.LogCaptureFixture, ) -> None: """Test an error occurs with the Elgato identify button.""" mock_elgato.identify.side_effect = ElgatoError - await hass.services.async_call( - BUTTON_DOMAIN, - SERVICE_PRESS, - {ATTR_ENTITY_ID: "button.identify"}, - blocking=True, - ) - await hass.async_block_till_done() + + with pytest.raises( + HomeAssistantError, match="An error occurred while identifying the Elgato Light" + ): + await hass.services.async_call( + BUTTON_DOMAIN, + SERVICE_PRESS, + {ATTR_ENTITY_ID: "button.identify"}, + blocking=True, + ) + await hass.async_block_till_done() assert len(mock_elgato.identify.mock_calls) == 1 - assert "An error occurred while identifying the Elgato Light" in caplog.text diff --git a/tests/components/elgato/test_light.py b/tests/components/elgato/test_light.py index 743abc1ad49..9cd4f9bd326 100644 --- a/tests/components/elgato/test_light.py +++ b/tests/components/elgato/test_light.py @@ -24,6 +24,7 @@ from homeassistant.const import ( STATE_UNAVAILABLE, ) from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import device_registry as dr, entity_registry as er from tests.common import MockConfigEntry @@ -183,13 +184,15 @@ async def test_light_unavailable( mock_elgato.state.side_effect = ElgatoError mock_elgato.light.side_effect = ElgatoError - await hass.services.async_call( - LIGHT_DOMAIN, - service, - {ATTR_ENTITY_ID: "light.frenck"}, - blocking=True, - ) - await hass.async_block_till_done() + with pytest.raises(HomeAssistantError): + await hass.services.async_call( + LIGHT_DOMAIN, + service, + {ATTR_ENTITY_ID: "light.frenck"}, + blocking=True, + ) + await hass.async_block_till_done() + state = hass.states.get("light.frenck") assert state assert state.state == STATE_UNAVAILABLE @@ -218,18 +221,20 @@ async def test_light_identify_error( hass: HomeAssistant, init_integration: MockConfigEntry, mock_elgato: MagicMock, - caplog: pytest.LogCaptureFixture, ) -> None: """Test error occurred during identifying an Elgato Light.""" mock_elgato.identify.side_effect = ElgatoError - await hass.services.async_call( - DOMAIN, - SERVICE_IDENTIFY, - { - ATTR_ENTITY_ID: "light.frenck", - }, - blocking=True, - ) - await hass.async_block_till_done() + with pytest.raises( + HomeAssistantError, match="An error occurred while identifying the Elgato Light" + ): + await hass.services.async_call( + DOMAIN, + SERVICE_IDENTIFY, + { + ATTR_ENTITY_ID: "light.frenck", + }, + blocking=True, + ) + await hass.async_block_till_done() + assert len(mock_elgato.identify.mock_calls) == 1 - assert "An error occurred while identifying the Elgato Light" in caplog.text