Add case insensitive testing to boolean string validation.

This commit is contained in:
Jan Harkes 2016-04-04 00:38:58 -04:00 committed by Paulus Schoutsen
parent eb415d7b96
commit cbe9a7d2a3
2 changed files with 16 additions and 0 deletions

View File

@ -23,6 +23,7 @@ longitude = vol.All(vol.Coerce(float), vol.Range(min=-180, max=180),
def boolean(value):
"""Validate and coerce a boolean value."""
if isinstance(value, str):
value = value.lower()
if value in ('1', 'true', 'yes', 'on', 'enable'):
return True
if value in ('0', 'false', 'no', 'off', 'disable'):

View File

@ -4,6 +4,21 @@ import voluptuous as vol
import homeassistant.helpers.config_validation as cv
def test_boolean():
"""Test boolean validation."""
schema = vol.Schema(cv.boolean)
for value in ('T', 'negative', 'lock'):
with pytest.raises(vol.MultipleInvalid):
schema(value)
for value in ('true', 'On', '1', 'YES', 'enable', 1, True):
assert schema(value)
for value in ('false', 'Off', '0', 'NO', 'disable', 0, False):
assert not schema(value)
def test_latitude():
"""Test latitude validation."""
schema = vol.Schema(cv.latitude)