mirror of
https://github.com/home-assistant/core.git
synced 2025-11-13 13:00:11 +00:00
Raise and suppress stack trace when reloading yaml fails (#102410)
* Allow async_integration_yaml_config to raise * Docstr - split check * Implement as wrapper, return dataclass * Fix setup error handling * Fix reload test mock * Move log_messages to error handler * Remove unreachable code * Remove config test helper * Refactor and ensure notifications during setup * Remove redundat error, adjust tests notifications * Fix patch * Apply suggestions from code review Co-authored-by: Erik Montnemery <erik@montnemery.com> * Follow up comments * Add call_back decorator * Split long lines * Update exception abbreviations --------- Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
@@ -3,10 +3,12 @@ import logging
|
||||
from unittest.mock import AsyncMock, Mock, patch
|
||||
|
||||
import pytest
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import config
|
||||
from homeassistant.const import SERVICE_RELOAD
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigValidationError, HomeAssistantError
|
||||
from homeassistant.helpers.entity_component import EntityComponent
|
||||
from homeassistant.helpers.entity_platform import async_get_platforms
|
||||
from homeassistant.helpers.reload import (
|
||||
@@ -139,7 +141,9 @@ async def test_setup_reload_service_when_async_process_component_config_fails(
|
||||
|
||||
yaml_path = get_fixture_path("helpers/reload_configuration.yaml")
|
||||
with patch.object(config, "YAML_CONFIG_FILE", yaml_path), patch.object(
|
||||
config, "async_process_component_config", return_value=None
|
||||
config,
|
||||
"async_process_component_config",
|
||||
return_value=config.IntegrationConfigInfo(None, []),
|
||||
):
|
||||
await hass.services.async_call(
|
||||
PLATFORM,
|
||||
@@ -208,8 +212,49 @@ async def test_async_integration_yaml_config(hass: HomeAssistant) -> None:
|
||||
yaml_path = get_fixture_path(f"helpers/{DOMAIN}_configuration.yaml")
|
||||
with patch.object(config, "YAML_CONFIG_FILE", yaml_path):
|
||||
processed_config = await async_integration_yaml_config(hass, DOMAIN)
|
||||
assert processed_config == {DOMAIN: [{"name": "one"}, {"name": "two"}]}
|
||||
# Test fetching yaml config does not raise when the raise_on_failure option is set
|
||||
processed_config = await async_integration_yaml_config(
|
||||
hass, DOMAIN, raise_on_failure=True
|
||||
)
|
||||
assert processed_config == {DOMAIN: [{"name": "one"}, {"name": "two"}]}
|
||||
|
||||
assert processed_config == {DOMAIN: [{"name": "one"}, {"name": "two"}]}
|
||||
|
||||
async def test_async_integration_failing_yaml_config(hass: HomeAssistant) -> None:
|
||||
"""Test reloading yaml config for an integration fails.
|
||||
|
||||
In case an integration reloads its yaml configuration it should throw when
|
||||
the new config failed to load and raise_on_failure is set to True.
|
||||
"""
|
||||
schema_without_name_attr = vol.Schema({vol.Required("some_option"): str})
|
||||
|
||||
mock_integration(hass, MockModule(DOMAIN, config_schema=schema_without_name_attr))
|
||||
|
||||
yaml_path = get_fixture_path(f"helpers/{DOMAIN}_configuration.yaml")
|
||||
with patch.object(config, "YAML_CONFIG_FILE", yaml_path):
|
||||
# Test fetching yaml config does not raise without raise_on_failure option
|
||||
processed_config = await async_integration_yaml_config(hass, DOMAIN)
|
||||
assert processed_config is None
|
||||
# Test fetching yaml config does not raise when the raise_on_failure option is set
|
||||
with pytest.raises(ConfigValidationError):
|
||||
await async_integration_yaml_config(hass, DOMAIN, raise_on_failure=True)
|
||||
|
||||
|
||||
async def test_async_integration_failing_on_reload(hass: HomeAssistant) -> None:
|
||||
"""Test reloading yaml config for an integration fails with an other exception.
|
||||
|
||||
In case an integration reloads its yaml configuration it should throw when
|
||||
the new config failed to load and raise_on_failure is set to True.
|
||||
"""
|
||||
mock_integration(hass, MockModule(DOMAIN))
|
||||
|
||||
yaml_path = get_fixture_path(f"helpers/{DOMAIN}_configuration.yaml")
|
||||
with patch.object(config, "YAML_CONFIG_FILE", yaml_path), patch(
|
||||
"homeassistant.config.async_process_component_config",
|
||||
side_effect=HomeAssistantError(),
|
||||
), pytest.raises(HomeAssistantError):
|
||||
# Test fetching yaml config does raise when the raise_on_failure option is set
|
||||
await async_integration_yaml_config(hass, DOMAIN, raise_on_failure=True)
|
||||
|
||||
|
||||
async def test_async_integration_missing_yaml_config(hass: HomeAssistant) -> None:
|
||||
|
||||
Reference in New Issue
Block a user