Improve Elgato error handling (#73444)

pull/73536/head
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.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

View File

@ -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

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.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

View File

@ -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