Improve Elgato error handling (#73444)

This commit is contained in:
Franck Nijhof 2022-06-15 12:12:07 +02:00 committed by GitHub
parent e05e79e53d
commit 05d7d31dfd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 39 deletions

View File

@ -9,6 +9,7 @@ from homeassistant.components.button import ButtonEntity, ButtonEntityDescriptio
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_MAC from homeassistant.const import CONF_MAC
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -49,5 +50,7 @@ class ElgatoIdentifyButton(ElgatoEntity, ButtonEntity):
"""Identify the light, will make it blink.""" """Identify the light, will make it blink."""
try: try:
await self.client.identify() await self.client.identify()
except ElgatoError: except ElgatoError as error:
_LOGGER.exception("An error occurred while identifying the Elgato Light") raise HomeAssistantError(
"An error occurred while identifying the Elgato Light"
) from error

View File

@ -15,6 +15,7 @@ from homeassistant.components.light import (
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_MAC from homeassistant.const import CONF_MAC
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import ( from homeassistant.helpers.entity_platform import (
AddEntitiesCallback, AddEntitiesCallback,
async_get_current_platform, async_get_current_platform,
@ -25,7 +26,7 @@ from homeassistant.helpers.update_coordinator import (
) )
from . import HomeAssistantElgatoData from . import HomeAssistantElgatoData
from .const import DOMAIN, LOGGER, SERVICE_IDENTIFY from .const import DOMAIN, SERVICE_IDENTIFY
from .entity import ElgatoEntity from .entity import ElgatoEntity
PARALLEL_UPDATES = 1 PARALLEL_UPDATES = 1
@ -121,9 +122,12 @@ class ElgatoLight(
"""Turn off the light.""" """Turn off the light."""
try: try:
await self.client.light(on=False) await self.client.light(on=False)
except ElgatoError: except ElgatoError as error:
LOGGER.error("An error occurred while updating the Elgato Light") raise HomeAssistantError(
await self.coordinator.async_refresh() "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: async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn on the light.""" """Turn on the light."""
@ -159,14 +163,18 @@ class ElgatoLight(
saturation=saturation, saturation=saturation,
temperature=temperature, temperature=temperature,
) )
except ElgatoError: except ElgatoError as error:
LOGGER.error("An error occurred while updating the Elgato Light") raise HomeAssistantError(
await self.coordinator.async_refresh() "An error occurred while updating the Elgato Light"
) from error
finally:
await self.coordinator.async_refresh()
async def async_identify(self) -> None: async def async_identify(self) -> None:
"""Identify the light, will make it blink.""" """Identify the light, will make it blink."""
try: try:
await self.client.identify() await self.client.identify()
except ElgatoError: except ElgatoError as error:
LOGGER.exception("An error occurred while identifying the Elgato Light") raise HomeAssistantError(
await self.coordinator.async_refresh() "An error occurred while identifying the Elgato Light"
) from error

View File

@ -8,6 +8,7 @@ from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRE
from homeassistant.components.elgato.const import DOMAIN from homeassistant.components.elgato.const import DOMAIN
from homeassistant.const import ATTR_ENTITY_ID, ATTR_ICON, STATE_UNKNOWN from homeassistant.const import ATTR_ENTITY_ID, ATTR_ICON, STATE_UNKNOWN
from homeassistant.core import HomeAssistant 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 import device_registry as dr, entity_registry as er
from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity import EntityCategory
@ -68,17 +69,19 @@ async def test_button_identify_error(
hass: HomeAssistant, hass: HomeAssistant,
init_integration: MockConfigEntry, init_integration: MockConfigEntry,
mock_elgato: MagicMock, mock_elgato: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test an error occurs with the Elgato identify button.""" """Test an error occurs with the Elgato identify button."""
mock_elgato.identify.side_effect = ElgatoError mock_elgato.identify.side_effect = ElgatoError
await hass.services.async_call(
BUTTON_DOMAIN, with pytest.raises(
SERVICE_PRESS, HomeAssistantError, match="An error occurred while identifying the Elgato Light"
{ATTR_ENTITY_ID: "button.identify"}, ):
blocking=True, await hass.services.async_call(
) BUTTON_DOMAIN,
await hass.async_block_till_done() SERVICE_PRESS,
{ATTR_ENTITY_ID: "button.identify"},
blocking=True,
)
await hass.async_block_till_done()
assert len(mock_elgato.identify.mock_calls) == 1 assert len(mock_elgato.identify.mock_calls) == 1
assert "An error occurred while identifying the Elgato Light" in caplog.text

View File

@ -24,6 +24,7 @@ from homeassistant.const import (
STATE_UNAVAILABLE, STATE_UNAVAILABLE,
) )
from homeassistant.core import HomeAssistant 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 import device_registry as dr, entity_registry as er
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
@ -183,13 +184,15 @@ async def test_light_unavailable(
mock_elgato.state.side_effect = ElgatoError mock_elgato.state.side_effect = ElgatoError
mock_elgato.light.side_effect = ElgatoError mock_elgato.light.side_effect = ElgatoError
await hass.services.async_call( with pytest.raises(HomeAssistantError):
LIGHT_DOMAIN, await hass.services.async_call(
service, LIGHT_DOMAIN,
{ATTR_ENTITY_ID: "light.frenck"}, service,
blocking=True, {ATTR_ENTITY_ID: "light.frenck"},
) blocking=True,
await hass.async_block_till_done() )
await hass.async_block_till_done()
state = hass.states.get("light.frenck") state = hass.states.get("light.frenck")
assert state assert state
assert state.state == STATE_UNAVAILABLE assert state.state == STATE_UNAVAILABLE
@ -218,18 +221,20 @@ async def test_light_identify_error(
hass: HomeAssistant, hass: HomeAssistant,
init_integration: MockConfigEntry, init_integration: MockConfigEntry,
mock_elgato: MagicMock, mock_elgato: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test error occurred during identifying an Elgato Light.""" """Test error occurred during identifying an Elgato Light."""
mock_elgato.identify.side_effect = ElgatoError mock_elgato.identify.side_effect = ElgatoError
await hass.services.async_call( with pytest.raises(
DOMAIN, HomeAssistantError, match="An error occurred while identifying the Elgato Light"
SERVICE_IDENTIFY, ):
{ await hass.services.async_call(
ATTR_ENTITY_ID: "light.frenck", DOMAIN,
}, SERVICE_IDENTIFY,
blocking=True, {
) ATTR_ENTITY_ID: "light.frenck",
await hass.async_block_till_done() },
blocking=True,
)
await hass.async_block_till_done()
assert len(mock_elgato.identify.mock_calls) == 1 assert len(mock_elgato.identify.mock_calls) == 1
assert "An error occurred while identifying the Elgato Light" in caplog.text