File tree Expand file tree Collapse file tree 2 files changed +49
-3
lines changed Expand file tree Collapse file tree 2 files changed +49
-3
lines changed Original file line number Diff line number Diff line change @@ -1045,9 +1045,33 @@ def expand_condition_shorthand(value: Any | None) -> Any:
1045
1045
1046
1046
1047
1047
# 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
1051
1075
1052
1076
1053
1077
PLATFORM_SCHEMA = vol .Schema (
Original file line number Diff line number Diff line change @@ -1468,3 +1468,25 @@ def test_positive_time_period_template() -> None:
1468
1468
schema ("{{ 'invalid' }}" )
1469
1469
schema ({"{{ 'invalid' }}" : 5 })
1470
1470
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" }})
You can’t perform that action at this time.
0 commit comments