Fix swallowed exceptions in homeassistant action handlers (#170922)

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Michael
2026-05-25 21:02:49 +02:00
committed by GitHub
parent d5bae0a2cf
commit 060f447e4a
5 changed files with 78 additions and 14 deletions
@@ -289,10 +289,12 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: # noqa:
"""Service handler for reloading core config."""
try:
conf = await conf_util.async_hass_config_yaml(hass)
# pylint: disable-next=home-assistant-action-swallowed-exception
except HomeAssistantError as err:
_LOGGER.error(err)
return
except (HomeAssistantError, FileNotFoundError) as err:
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="core_config_reload_failed",
translation_placeholders={"error": str(err)},
) from err
# auth only processed during startup
await core_config.async_process_ha_core_config(hass, conf.get(DOMAIN) or {})
@@ -183,10 +183,12 @@ async def async_setup_platform(
"""Reload the scene config."""
try:
config = await conf_util.async_hass_config_yaml(hass)
# pylint: disable-next=home-assistant-action-swallowed-exception
except HomeAssistantError as err:
_LOGGER.error(err)
return
except (HomeAssistantError, FileNotFoundError) as err:
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="scene_config_reload_failed",
translation_placeholders={"error": str(err)},
) from err
integration = await async_get_integration(hass, SCENE_DOMAIN)
@@ -21,6 +21,9 @@
"config_validator_unknown_err": {
"message": "Unknown error calling {domain} config validator - {error}."
},
"core_config_reload_failed": {
"message": "Failed to reload the Home Assistant Core configuration - {error}"
},
"max_length_exceeded": {
"message": "Value {value} for property {property_name} has a maximum length of {max_length} characters."
},
@@ -48,6 +51,9 @@
"platform_schema_validator_err": {
"message": "Unknown error when validating config for {domain} from integration {p_name} - {error}."
},
"scene_config_reload_failed": {
"message": "Failed to reload the Home Assistant scene platform configuration - {error}"
},
"service_config_entry_not_found": {
"message": "Integration {domain} config entry with ID {entry_id} was not found."
},
+26 -4
View File
@@ -132,18 +132,40 @@ async def test_reload_core_conf(hass: HomeAssistant) -> None:
@patch("homeassistant.config.os.path.isfile", Mock(return_value=True))
@patch("homeassistant.components.homeassistant._LOGGER.error")
@patch("homeassistant.core_config.async_process_ha_core_config")
@pytest.mark.parametrize(
("files_patch", "expected_error"),
[
(
{config.YAML_CONFIG_FILE: yaml.dump(["invalid", "config"])},
"YAML file .*configuration.yaml does not contain a dict",
),
({"not_existing": "blabla"}, "File not found: .*configuration.yaml"),
],
)
async def test_reload_core_with_wrong_conf(
mock_process, mock_error, hass: HomeAssistant
mock_process,
mock_error,
hass: HomeAssistant,
files_patch: dict[str, str],
expected_error: str,
) -> None:
"""Test reload core conf service."""
files = {config.YAML_CONFIG_FILE: yaml.dump(["invalid", "config"])}
await async_setup_component(hass, ha.DOMAIN, {})
with patch_yaml_files(files, True):
with (
patch_yaml_files(files_patch, True),
pytest.raises(
HomeAssistantError,
match=(
"Failed to reload the Home Assistant Core configuration - "
f"{expected_error}"
),
),
):
await hass.services.async_call(
ha.DOMAIN, SERVICE_RELOAD_CORE_CONFIG, blocking=True
)
assert mock_error.called
assert mock_error.called is False
assert mock_process.called is False
+34 -2
View File
@@ -4,15 +4,17 @@ from unittest.mock import patch
import pytest
import voluptuous as vol
import yaml
from homeassistant import config
from homeassistant.components.homeassistant import scene as ha_scene
from homeassistant.components.homeassistant.scene import EVENT_SCENE_RELOADED
from homeassistant.const import STATE_UNKNOWN
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceValidationError
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
from homeassistant.setup import async_setup_component
from tests.common import async_capture_events, async_mock_service
from tests.common import async_capture_events, async_mock_service, patch_yaml_files
async def test_reload_config_service(hass: HomeAssistant) -> None:
@@ -46,6 +48,36 @@ async def test_reload_config_service(hass: HomeAssistant) -> None:
assert hass.states.get("scene.bye") is not None
@pytest.mark.parametrize(
("files_patch", "expected_error"),
[
(
{config.YAML_CONFIG_FILE: yaml.dump(["invalid", "config"])},
"YAML file .*configuration.yaml does not contain a dict",
),
({"not_existing": "blabla"}, "File not found: .*configuration.yaml"),
],
)
async def test_reload_config_service_failed(
hass: HomeAssistant, files_patch: dict[str, str], expected_error: str
) -> None:
"""Test error handling when the reload config service fails."""
assert await async_setup_component(hass, "scene", {})
await hass.async_block_till_done()
with (
patch_yaml_files(files_patch, True),
pytest.raises(
HomeAssistantError,
match=(
"Failed to reload the Home Assistant scene platform configuration - "
f"{expected_error}"
),
),
):
await hass.services.async_call("scene", "reload", blocking=True)
async def test_apply_service(hass: HomeAssistant) -> None:
"""Test the apply service."""
assert await async_setup_component(hass, "scene", {})