From bb7b66c37461ef58665d014ace4760e287f20be6 Mon Sep 17 00:00:00 2001 From: Abdellatif Anaflous <62770500+hktitof@users.noreply.github.com> Date: Sun, 20 Sep 2026 15:27:31 +0100 Subject: [PATCH] Raise HomeAssistantError when a LIFX Cloud scene fails to activate (#182756) --- homeassistant/components/lifx_cloud/const.py | 3 ++ homeassistant/components/lifx_cloud/scene.py | 12 +++-- .../components/lifx_cloud/strings.json | 7 +++ tests/components/lifx_cloud/__init__.py | 1 + tests/components/lifx_cloud/test_scene.py | 46 +++++++++++++++++++ 5 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 homeassistant/components/lifx_cloud/const.py create mode 100644 homeassistant/components/lifx_cloud/strings.json create mode 100644 tests/components/lifx_cloud/__init__.py create mode 100644 tests/components/lifx_cloud/test_scene.py diff --git a/homeassistant/components/lifx_cloud/const.py b/homeassistant/components/lifx_cloud/const.py new file mode 100644 index 000000000000..f81809dda8c4 --- /dev/null +++ b/homeassistant/components/lifx_cloud/const.py @@ -0,0 +1,3 @@ +"""Constants for the LIFX Cloud integration.""" + +DOMAIN = "lifx_cloud" diff --git a/homeassistant/components/lifx_cloud/scene.py b/homeassistant/components/lifx_cloud/scene.py index 968d51759c3d..6efb8a433fe2 100644 --- a/homeassistant/components/lifx_cloud/scene.py +++ b/homeassistant/components/lifx_cloud/scene.py @@ -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 diff --git a/homeassistant/components/lifx_cloud/strings.json b/homeassistant/components/lifx_cloud/strings.json new file mode 100644 index 000000000000..1f2de9d192f5 --- /dev/null +++ b/homeassistant/components/lifx_cloud/strings.json @@ -0,0 +1,7 @@ +{ + "exceptions": { + "activate_failed": { + "message": "Failed to activate the LIFX Cloud scene: {error}" + } + } +} diff --git a/tests/components/lifx_cloud/__init__.py b/tests/components/lifx_cloud/__init__.py new file mode 100644 index 000000000000..57f0b7a411fb --- /dev/null +++ b/tests/components/lifx_cloud/__init__.py @@ -0,0 +1 @@ +"""Tests for the LIFX Cloud integration.""" diff --git a/tests/components/lifx_cloud/test_scene.py b/tests/components/lifx_cloud/test_scene.py new file mode 100644 index 000000000000..3a94f22ce70a --- /dev/null +++ b/tests/components/lifx_cloud/test_scene.py @@ -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()