Raise HomeAssistantError when a LIFX Cloud scene fails to activate (#182756)

This commit is contained in:
Abdellatif Anaflous
2026-09-20 16:27:31 +02:00
committed by GitHub
parent 701da374cf
commit bb7b66c374
5 changed files with 66 additions and 3 deletions
@@ -0,0 +1,3 @@
"""Constants for the LIFX Cloud integration."""
DOMAIN = "lifx_cloud"
+9 -3
View File
@@ -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}"
}
}
}
+1
View File
@@ -0,0 +1 @@
"""Tests for the LIFX Cloud integration."""
+46
View File
@@ -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()