diff --git a/homeassistant/helpers/config_validation.py b/homeassistant/helpers/config_validation.py index 422f940e98e..b06e9974125 100644 --- a/homeassistant/helpers/config_validation.py +++ b/homeassistant/helpers/config_validation.py @@ -746,12 +746,21 @@ def deprecated( def validator(config: Dict) -> Dict: """Check if key is in config and log warning.""" if key in config: - KeywordStyleAdapter(logging.getLogger(module_name)).warning( - warning, - key=key, - replacement_key=replacement_key, - ) - + try: + KeywordStyleAdapter(logging.getLogger(module_name)).warning( + warning.replace( + "'{key}' option", + f"'{key}' option near {config.__config_file__}:{config.__line__}", # type: ignore + ), + key=key, + replacement_key=replacement_key, + ) + except AttributeError: + KeywordStyleAdapter(logging.getLogger(module_name)).warning( + warning, + key=key, + replacement_key=replacement_key, + ) value = config[key] if replacement_key: config.pop(key) diff --git a/tests/helpers/test_config_validation.py b/tests/helpers/test_config_validation.py index d0ae86f8f7e..f58fd1b22f9 100644 --- a/tests/helpers/test_config_validation.py +++ b/tests/helpers/test_config_validation.py @@ -1,4 +1,5 @@ """Test config validators.""" +from collections import OrderedDict from datetime import date, datetime, timedelta import enum import os @@ -799,6 +800,77 @@ def test_deprecated_cant_find_module(): ) +def test_deprecated_logger_with_config_attributes(caplog): + """Test if the logger outputs the correct message if the line and file attribute is available in config.""" + file: str = "configuration.yaml" + line: int = 54 + replacement = f"'mars' option near {file}:{line} is deprecated" + config = OrderedDict([("mars", "blah")]) + setattr(config, "__config_file__", file) + setattr(config, "__line__", line) + + cv.deprecated("mars", replacement_key="jupiter", default=False)(config) + + assert len(caplog.records) == 1 + assert replacement in caplog.text + + caplog.clear() + assert len(caplog.records) == 0 + + +def test_deprecated_logger_with_one_config_attribute(caplog): + """Test if the logger outputs the correct message if only one of line and file attribute is available in config.""" + file: str = "configuration.yaml" + line: int = 54 + replacement = f"'mars' option near {file}:{line} is deprecated" + config = OrderedDict([("mars", "blah")]) + setattr(config, "__config_file__", file) + + cv.deprecated("mars", replacement_key="jupiter", default=False)(config) + + assert len(caplog.records) == 1 + assert replacement not in caplog.text + assert ( + "The 'mars' option is deprecated, please replace it with 'jupiter'" + ) in caplog.text + + caplog.clear() + assert len(caplog.records) == 0 + + config = OrderedDict([("mars", "blah")]) + setattr(config, "__line__", line) + + cv.deprecated("mars", replacement_key="jupiter", default=False)(config) + + assert len(caplog.records) == 1 + assert replacement not in caplog.text + assert ( + "The 'mars' option is deprecated, please replace it with 'jupiter'" + ) in caplog.text + + caplog.clear() + assert len(caplog.records) == 0 + + +def test_deprecated_logger_without_config_attributes(caplog): + """Test if the logger outputs the correct message if the line and file attribute is not available in config.""" + file: str = "configuration.yaml" + line: int = 54 + replacement = f"'mars' option near {file}:{line} is deprecated" + config = OrderedDict([("mars", "blah")]) + + cv.deprecated("mars", replacement_key="jupiter", default=False)(config) + + assert len(caplog.records) == 1 + assert replacement not in caplog.text + assert ( + "The 'mars' option is deprecated, please replace it with 'jupiter'" + ) in caplog.text + + caplog.clear() + assert len(caplog.records) == 0 + + def test_key_dependency(): """Test key_dependency validator.""" schema = vol.Schema(cv.key_dependency("beer", "soda"))