Fix swallowed exceptions in action handlers for Yeelight (#177048)

This commit is contained in:
Martin
2026-07-27 15:07:19 +02:00
committed by GitHub
parent c46dc57a8c
commit 867c2deb79
3 changed files with 23 additions and 10 deletions
+9 -2
View File
@@ -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:
@@ -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": {
+9 -8
View File
@@ -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(