mirror of
https://github.com/home-assistant/core.git
synced 2026-09-26 09:23:17 -04:00
Raise HomeAssistantError when a LIFX Cloud scene fails to activate (#182756)
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
"""Constants for the LIFX Cloud integration."""
|
||||
|
||||
DOMAIN = "lifx_cloud"
|
||||
@@ -12,11 +12,14 @@ import probatio
|
||||
from homeassistant.components.scene import Scene
|
||||
from homeassistant.const import CONF_PLATFORM, CONF_TIMEOUT, CONF_TOKEN
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
|
||||
from .const import DOMAIN
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DEFAULT_TIMEOUT = 10
|
||||
@@ -93,6 +96,9 @@ class LifxCloudScene(Scene):
|
||||
async with asyncio.timeout(self._timeout):
|
||||
await httpsession.put(url, headers=self._headers)
|
||||
|
||||
# pylint: disable-next=home-assistant-action-swallowed-exception
|
||||
except TimeoutError, aiohttp.ClientError:
|
||||
_LOGGER.exception("Error on %s", url)
|
||||
except (TimeoutError, aiohttp.ClientError) as err:
|
||||
raise HomeAssistantError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="activate_failed",
|
||||
translation_placeholders={"error": str(err)},
|
||||
) from err
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"exceptions": {
|
||||
"activate_failed": {
|
||||
"message": "Failed to activate the LIFX Cloud scene: {error}"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
"""Tests for the LIFX Cloud integration."""
|
||||
@@ -0,0 +1,46 @@
|
||||
"""Test the LIFX Cloud scene."""
|
||||
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import aiohttp
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.lifx_cloud.scene import LifxCloudScene
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
|
||||
SCENE_DATA = {"name": "Living room", "uuid": "abc-123"}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def scene(hass: HomeAssistant) -> LifxCloudScene:
|
||||
"""Return a LIFX Cloud scene entity."""
|
||||
return LifxCloudScene(hass, {"Authorization": "Bearer token"}, 10, SCENE_DATA)
|
||||
|
||||
|
||||
async def test_activate(scene: LifxCloudScene) -> None:
|
||||
"""The activate request goes out to the cloud."""
|
||||
with patch(
|
||||
"homeassistant.components.lifx_cloud.scene.async_get_clientsession"
|
||||
) as session:
|
||||
session.return_value.put = AsyncMock()
|
||||
await scene.async_activate()
|
||||
|
||||
assert session.return_value.put.call_count == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"error",
|
||||
[
|
||||
pytest.param(TimeoutError(), id="timeout"),
|
||||
pytest.param(aiohttp.ClientError("boom"), id="client_error"),
|
||||
],
|
||||
)
|
||||
async def test_activate_failure_raises(scene: LifxCloudScene, error: Exception) -> None:
|
||||
"""A failed activate raises HomeAssistantError."""
|
||||
with patch(
|
||||
"homeassistant.components.lifx_cloud.scene.async_get_clientsession"
|
||||
) as session:
|
||||
session.return_value.put = AsyncMock(side_effect=error)
|
||||
with pytest.raises(HomeAssistantError):
|
||||
await scene.async_activate()
|
||||
Reference in New Issue
Block a user