diff --git a/homeassistant/util/yaml.py b/homeassistant/util/yaml.py index cf773bb999f..42776241843 100644 --- a/homeassistant/util/yaml.py +++ b/homeassistant/util/yaml.py @@ -43,6 +43,9 @@ def load_yaml(fname: str) -> Union[List, Dict]: except yaml.YAMLError as exc: _LOGGER.error(exc) raise HomeAssistantError(exc) + except UnicodeDecodeError as exc: + _LOGGER.error('Unable to read file %s: %s', fname, exc) + raise HomeAssistantError(exc) def clear_secret_cache() -> None: diff --git a/tests/util/test_yaml.py b/tests/util/test_yaml.py index 7c7bb0b9255..79cae43b845 100644 --- a/tests/util/test_yaml.py +++ b/tests/util/test_yaml.py @@ -92,6 +92,7 @@ class TestYaml(unittest.TestCase): mock_walk.return_value = [ ['/tmp', ['tmp2'], ['zero.yaml']], ['/tmp/tmp2', [], ['one.yaml', 'two.yaml']], + ['/tmp/ignore', [], ['.ignore.yaml']] ] with patch_yaml_files({ @@ -123,6 +124,7 @@ class TestYaml(unittest.TestCase): mock_walk.return_value = [ ['/tmp', ['tmp2'], ['first.yaml']], ['/tmp/tmp2', [], ['second.yaml', 'third.yaml']], + ['/tmp/ignore', [], ['.ignore.yaml']] ] with patch_yaml_files({ @@ -155,6 +157,7 @@ class TestYaml(unittest.TestCase): mock_walk.return_value = [ ['/tmp', ['tmp2'], ['first.yaml']], ['/tmp/tmp2', [], ['second.yaml', 'third.yaml']], + ['/tmp/ignore', [], ['.ignore.yaml']] ] with patch_yaml_files({ @@ -191,6 +194,7 @@ class TestYaml(unittest.TestCase): mock_walk.return_value = [ ['/tmp', ['tmp2'], ['first.yaml']], ['/tmp/tmp2', [], ['second.yaml', 'third.yaml']], + ['/tmp/ignore', [], ['.ignore.yaml']] ] with patch_yaml_files({ @@ -208,6 +212,13 @@ class TestYaml(unittest.TestCase): "key4": "four" } + @patch('homeassistant.util.yaml.open', create=True) + def test_load_yaml_encoding_error(self, mock_open): + """Test raising a UnicodeDecodeError.""" + mock_open.side_effect = UnicodeDecodeError('', b'', 1, 0, '') + self.assertRaises(HomeAssistantError, yaml.load_yaml, 'test') + + FILES = {}