Skip to content

Commit 49c3a88

Browse files
Make cv.empty_config_schema log an error instead of raise (home-assistant#93646)
* Make cv.empty_config_schema log an error instead of raise * Add test * Update homeassistant/helpers/config_validation.py Co-authored-by: c0ffeeca7 <38767475+c0ffeeca7@users.noreply.github.com> --------- Co-authored-by: c0ffeeca7 <38767475+c0ffeeca7@users.noreply.github.com>
1 parent 7ff1c79 commit 49c3a88

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

homeassistant/helpers/config_validation.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,9 +1045,33 @@ def expand_condition_shorthand(value: Any | None) -> Any:
10451045

10461046

10471047
# Schemas
1048-
def empty_config_schema(domain: str) -> vol.Schema:
1049-
"""Return a config schema which accepts no configuration parameters."""
1050-
return vol.Schema({vol.Optional(domain): vol.Schema({})}, extra=vol.ALLOW_EXTRA)
1048+
def empty_config_schema(domain: str) -> Callable[[dict], dict]:
1049+
"""Return a config schema which logs if there are configuration parameters."""
1050+
1051+
module = inspect.getmodule(inspect.stack(context=0)[2].frame)
1052+
if module is not None:
1053+
module_name = module.__name__
1054+
else:
1055+
# If Python is unable to access the sources files, the call stack frame
1056+
# will be missing information, so let's guard.
1057+
# https://github.com/home-assistant/core/issues/24982
1058+
module_name = __name__
1059+
logger_func = logging.getLogger(module_name).error
1060+
1061+
def validator(config: dict) -> dict:
1062+
if domain in config and config[domain]:
1063+
logger_func(
1064+
(
1065+
"The %s integration does not support any configuration parameters, "
1066+
"got %s. Please remove the configuration parameters from your "
1067+
"configuration."
1068+
),
1069+
domain,
1070+
config[domain],
1071+
)
1072+
return config
1073+
1074+
return validator
10511075

10521076

10531077
PLATFORM_SCHEMA = vol.Schema(

tests/helpers/test_config_validation.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,3 +1468,25 @@ def test_positive_time_period_template() -> None:
14681468
schema("{{ 'invalid' }}")
14691469
schema({"{{ 'invalid' }}": 5})
14701470
schema({"minutes": "{{ 'invalid' }}"})
1471+
1472+
1473+
def test_empty_schema(caplog: pytest.LogCaptureFixture) -> None:
1474+
"""Test if the current module cannot be inspected."""
1475+
expected_message = (
1476+
"The test_domain integration does not support any configuration parameters"
1477+
)
1478+
1479+
cv.empty_config_schema("test_domain")({})
1480+
assert expected_message not in caplog.text
1481+
1482+
cv.empty_config_schema("test_domain")({"test_domain": {}})
1483+
assert expected_message not in caplog.text
1484+
1485+
cv.empty_config_schema("test_domain")({"test_domain": {"foo": "bar"}})
1486+
assert expected_message in caplog.text
1487+
1488+
1489+
def test_empty_schema_cant_find_module() -> None:
1490+
"""Test if the current module cannot be inspected."""
1491+
with patch("inspect.getmodule", return_value=None):
1492+
cv.empty_config_schema("test_domain")({"test_domain": {"foo": "bar"}})

0 commit comments

Comments
 (0)