From 98bbb8869e654916d50ff0f95cf6f8ba9dde0235 Mon Sep 17 00:00:00 2001 From: sepahewe <38730064+sepahewe@users.noreply.github.com> Date: Mon, 30 Mar 2026 12:08:57 +0200 Subject: [PATCH] Add support for negative numbers in apps options (#6673) * Added support for negative numbers in options * Do not allow -. as float * Added tests for integers and floats in options. * Fixed ruff errors * Added tests for outside of int/float limits --- supervisor/addons/options.py | 4 +-- tests/addons/test_options.py | 59 ++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/supervisor/addons/options.py b/supervisor/addons/options.py index 1c7e4d737..46309c015 100644 --- a/supervisor/addons/options.py +++ b/supervisor/addons/options.py @@ -37,8 +37,8 @@ RE_SCHEMA_ELEMENT = re.compile( r"|device(?:\((?Psubsystem=[a-z]+)\))?" r"|str(?:\((?P\d+)?,(?P\d+)?\))?" r"|password(?:\((?P\d+)?,(?P\d+)?\))?" - r"|int(?:\((?P\d+)?,(?P\d+)?\))?" - r"|float(?:\((?P[\d\.]+)?,(?P[\d\.]+)?\))?" + r"|int(?:\((?P-?\d+)?,(?P-?\d+)?\))?" + r"|float(?:\((?P-?\d*\.?\d+)?,(?P-?\d*\.?\d+)?\))?" r"|match\((?P.*)\)" r"|list\((?P.+)\)" r")\??$" diff --git a/tests/addons/test_options.py b/tests/addons/test_options.py index 72e2d7a54..aa70a00b5 100644 --- a/tests/addons/test_options.py +++ b/tests/addons/test_options.py @@ -45,6 +45,65 @@ def test_simple_schema(coresys): )({"name": "Pascal", "fires": True}) +def test_simple_schema_integers(coresys): + """Test integer limits.""" + assert AddonOptions( + coresys, + {"name": "str", "password": "password", "pos": "int(0,10)", "neg": "int(-5,0)"}, + MOCK_ADDON_NAME, + MOCK_ADDON_SLUG, + )({"name": "Pascal", "password": "1234", "pos": 5, "neg": "-4"}) + + with pytest.raises(vol.error.Invalid): + assert AddonOptions( + coresys, + { + "name": "str", + "password": "password", + "pos": "int(0,10)", + "neg": "int(-5,0)", + }, + MOCK_ADDON_NAME, + MOCK_ADDON_SLUG, + )({"name": "Pascal", "password": "1234", "pos": 11, "neg": "-6"}) + + +def test_simple_schema_floats(coresys): + """Test float limits.""" + assert AddonOptions( + coresys, + { + "name": "str", + "password": "password", + "pos": "float(0.0,10.5)", + "neg": "float(-5.0,-.5)", + }, + MOCK_ADDON_NAME, + MOCK_ADDON_SLUG, + )({"name": "Pascal", "password": "1234", "pos": 5.0, "neg": "-4.0"}) + + with pytest.raises(vol.error.Invalid): + assert AddonOptions( + coresys, + { + "name": "str", + "password": "password", + "pos": "float(0.0,10.5)", + "neg": "float(-5.0,-.5)", + }, + MOCK_ADDON_NAME, + MOCK_ADDON_SLUG, + )({"name": "Pascal", "password": "1234", "pos": 11.0, "neg": "-6.0"}) + + with pytest.raises(vol.error.Invalid): + assert AddonOptions( + coresys, + {"name": "str", "password": "password", "float": "float(-1.0,-.)"}, + MOCK_ADDON_NAME, + MOCK_ADDON_SLUG, + )({"name": "Pascal", "password": "1234", "float": "0.0"}) + + def test_complex_schema_list(coresys): """Test with complex list schema.""" assert AddonOptions(