From 867c2deb794f2fd347e8efb4ac3d77e99282d57d Mon Sep 17 00:00:00 2001 From: Martin <32802427+mstu01@users.noreply.github.com> Date: Mon, 27 Jul 2026 15:07:19 +0200 Subject: [PATCH] Fix swallowed exceptions in action handlers for Yeelight (#177048) --- homeassistant/components/yeelight/light.py | 11 +++++++++-- homeassistant/components/yeelight/strings.json | 5 +++++ tests/components/yeelight/test_light.py | 17 +++++++++-------- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/yeelight/light.py b/homeassistant/components/yeelight/light.py index 93c1e1c33ccb..8ddc715f907b 100644 --- a/homeassistant/components/yeelight/light.py +++ b/homeassistant/components/yeelight/light.py @@ -51,6 +51,7 @@ from .const import ( CONF_TRANSITION, DATA_CUSTOM_EFFECTS_KEY, DATA_UPDATED, + DOMAIN, MODELS_WITH_DELAYED_ON_TRANSITION, POWER_STATE_CHANGE_TIME, ) @@ -605,9 +606,15 @@ class YeelightBaseLight(YeelightEntity, LightEntity): """Set the music mode on or off.""" try: await self._async_set_music_mode(music_mode) - # pylint: disable-next=home-assistant-action-swallowed-exception except AssertionError as ex: - _LOGGER.error("Unable to turn on music mode, consider disabling it: %s", ex) + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="set_music_mode_failed", + translation_placeholders={ + "name": self.device.name, + "error": str(ex) or type(ex).__name__, + }, + ) from ex @_async_cmd async def _async_set_music_mode(self, music_mode) -> None: diff --git a/homeassistant/components/yeelight/strings.json b/homeassistant/components/yeelight/strings.json index c863b7c4fa04..df92dcfc90ad 100644 --- a/homeassistant/components/yeelight/strings.json +++ b/homeassistant/components/yeelight/strings.json @@ -43,6 +43,11 @@ } } }, + "exceptions": { + "set_music_mode_failed": { + "message": "Unable to set music mode for {name}: {error}. Consider disabling the music mode option." + } + }, "options": { "step": { "init": { diff --git a/tests/components/yeelight/test_light.py b/tests/components/yeelight/test_light.py index a2366be1b969..4b1b336fa7a0 100644 --- a/tests/components/yeelight/test_light.py +++ b/tests/components/yeelight/test_light.py @@ -454,15 +454,16 @@ async def test_services(hass: HomeAssistant, caplog: pytest.LogCaptureFixture) - # set_music_mode failure enable mocked_bulb.async_start_music = MagicMock(side_effect=AssertionError) - assert "Unable to turn on music mode, consider disabling it" not in caplog.text - await hass.services.async_call( - DOMAIN, - SERVICE_SET_MUSIC_MODE, - {ATTR_ENTITY_ID: ENTITY_LIGHT, ATTR_MODE_MUSIC: "true"}, - blocking=True, - ) + with pytest.raises(HomeAssistantError) as err: + await hass.services.async_call( + DOMAIN, + SERVICE_SET_MUSIC_MODE, + {ATTR_ENTITY_ID: ENTITY_LIGHT, ATTR_MODE_MUSIC: "true"}, + blocking=True, + ) assert mocked_bulb.async_start_music.mock_calls == [call()] - assert "Unable to turn on music mode, consider disabling it" in caplog.text + assert err.value.translation_domain == DOMAIN + assert err.value.translation_key == "set_music_mode_failed" # set_music_mode disable await _async_test_service(