Fix zwave_js config validation for values (#127972)

This commit is contained in:
Raman Gupta 2024-10-09 01:35:41 -04:00 committed by Franck Nijhof
parent 66c2fe091b
commit ed445d20b9
No known key found for this signature in database
GPG key ID: D62583BA8AB11CA3
2 changed files with 25 additions and 19 deletions

View file

@ -34,6 +34,8 @@ def boolean(value: Any) -> bool:
VALUE_SCHEMA = vol.Any( VALUE_SCHEMA = vol.Any(
boolean, boolean,
float,
int,
vol.Coerce(int), vol.Coerce(int),
vol.Coerce(float), vol.Coerce(float),
BITMASK_SCHEMA, BITMASK_SCHEMA,

View file

@ -1,27 +1,31 @@
"""Test the Z-Wave JS config validation helpers.""" """Test the Z-Wave JS config validation helpers."""
from typing import Any
import pytest import pytest
import voluptuous as vol import voluptuous as vol
from homeassistant.components.zwave_js.config_validation import boolean from homeassistant.components.zwave_js.config_validation import VALUE_SCHEMA, boolean
def test_boolean_validation() -> None: @pytest.mark.parametrize(
"""Test boolean config validator.""" ("test_cases", "expected_value"),
# test bool [
assert boolean(True) ([True, "true", "yes", "on", "ON", "enable"], True),
assert not boolean(False) ([False, "false", "no", "off", "NO", "disable"], False),
# test strings ([1.1, "1.1"], 1.1),
assert boolean("TRUE") ([1.0, "1.0"], 1.0),
assert not boolean("FALSE") ([1, "1"], 1),
assert boolean("ON") ],
assert not boolean("NO") )
# ensure 1's and 0's don't get converted to bool def test_validation(test_cases: list[Any], expected_value: Any) -> None:
"""Test config validation."""
for case in test_cases:
assert VALUE_SCHEMA(case) == expected_value
@pytest.mark.parametrize("value", ["invalid", "1", "0", 1, 0])
def test_invalid_boolean_validation(value: str | int) -> None:
"""Test invalid cases for boolean config validator."""
with pytest.raises(vol.Invalid): with pytest.raises(vol.Invalid):
boolean("1") boolean(value)
with pytest.raises(vol.Invalid):
boolean("0")
with pytest.raises(vol.Invalid):
boolean(1)
with pytest.raises(vol.Invalid):
boolean(0)