From 6e81ae096ed60dcbb1345339623313c323147576 Mon Sep 17 00:00:00 2001 From: Anders Melchiorsen Date: Sun, 7 Oct 2018 12:35:44 +0200 Subject: [PATCH] Disallow list/dict for string configuration (#17202) --- homeassistant/helpers/config_validation.py | 9 ++++++--- tests/helpers/test_config_validation.py | 8 +++++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/homeassistant/helpers/config_validation.py b/homeassistant/helpers/config_validation.py index bb4dcf6a55f..9ce4b6b166d 100644 --- a/homeassistant/helpers/config_validation.py +++ b/homeassistant/helpers/config_validation.py @@ -335,9 +335,12 @@ def slugify(value): def string(value: Any) -> str: """Coerce value to string, except for None.""" - if value is not None: - return str(value) - raise vol.Invalid('string value is None') + if value is None: + raise vol.Invalid('string value is None') + if isinstance(value, (list, dict)): + raise vol.Invalid('value should be a string') + + return str(value) def temperature_unit(value) -> str: diff --git a/tests/helpers/test_config_validation.py b/tests/helpers/test_config_validation.py index ab575c61789..cfd84dbc3b3 100644 --- a/tests/helpers/test_config_validation.py +++ b/tests/helpers/test_config_validation.py @@ -356,9 +356,15 @@ def test_string(): """Test string validation.""" schema = vol.Schema(cv.string) - with pytest.raises(vol.MultipleInvalid): + with pytest.raises(vol.Invalid): schema(None) + with pytest.raises(vol.Invalid): + schema([]) + + with pytest.raises(vol.Invalid): + schema({}) + for value in (True, 1, 'hello'): schema(value)