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)