diff --git a/homeassistant/util/yaml.py b/homeassistant/util/yaml.py index 4129a67bf577736b34b4cd27398a6c009b7eb82f..7d8789c507bd1c49302d4542df239e502e09429c 100644 --- a/homeassistant/util/yaml.py +++ b/homeassistant/util/yaml.py @@ -185,12 +185,9 @@ def _ordered_dict(loader: SafeLineLoader, if key in seen: fname = getattr(loader.stream, 'name', '') - first_mark = yaml.Mark(fname, 0, seen[key], -1, None, None) - second_mark = yaml.Mark(fname, 0, line, -1, None, None) - raise yaml.MarkedYAMLError( - context="duplicate key: \"{}\"".format(key), - context_mark=first_mark, problem_mark=second_mark, - ) + _LOGGER.error( + 'YAML file %s contains duplicate key "%s". ' + 'Check lines %d and %d.', fname, key, seen[key], line) seen[key] = line return _add_reference(OrderedDict(nodes), loader, node) diff --git a/tests/util/test_yaml.py b/tests/util/test_yaml.py index a15efb7a77eae63352a1572b074cf24472e3219d..1b0b808b9c4bbe0b94923f73ebabdb6a01012194 100644 --- a/tests/util/test_yaml.py +++ b/tests/util/test_yaml.py @@ -5,12 +5,22 @@ import unittest import logging from unittest.mock import patch +import pytest + from homeassistant.exceptions import HomeAssistantError from homeassistant.util import yaml from homeassistant.config import YAML_CONFIG_FILE, load_yaml_config_file from tests.common import get_test_config_dir, patch_yaml_files +@pytest.fixture(autouse=True) +def mock_credstash(): + """Mock credstash so it doesn't connect to the internet.""" + with patch.object(yaml, 'credstash') as mock_credstash: + mock_credstash.getSecret.return_value = None + yield mock_credstash + + class TestYaml(unittest.TestCase): """Test util.yaml loader.""" @@ -30,13 +40,6 @@ class TestYaml(unittest.TestCase): doc = yaml.yaml.safe_load(file) assert doc['key'] == 'value' - def test_duplicate_key(self): - """Test duplicate dict keys.""" - files = {YAML_CONFIG_FILE: 'key: thing1\nkey: thing2'} - with self.assertRaises(HomeAssistantError): - with patch_yaml_files(files): - load_yaml_config_file(YAML_CONFIG_FILE) - def test_unhashable_key(self): """Test an unhasable key.""" files = {YAML_CONFIG_FILE: 'message:\n {{ states.state }}'} @@ -411,3 +414,11 @@ def test_representing_yaml_loaded_data(): with patch_yaml_files(files): data = load_yaml_config_file(YAML_CONFIG_FILE) assert yaml.dump(data) == "key:\n- 1\n- '2'\n- 3\n" + + +def test_duplicate_key(caplog): + """Test duplicate dict keys.""" + files = {YAML_CONFIG_FILE: 'key: thing1\nkey: thing2'} + with patch_yaml_files(files): + load_yaml_config_file(YAML_CONFIG_FILE) + assert 'contains duplicate key' in caplog.text