Add support for constant selector (#89573)

* Add support for constant selector

* Adapt to frontend PR changes
pull/89802/head
Erik Montnemery 2023-03-16 14:02:26 +01:00 committed by GitHub
parent c6568ffb62
commit 886c2635ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 86 additions and 0 deletions

View File

@ -358,6 +358,38 @@ class ConfigEntrySelector(Selector[ConfigEntrySelectorConfig]):
return config
class ConstantSelectorConfig(TypedDict, total=False):
"""Class to represent a constant selector config."""
label: str
translation_key: str
value: str | int | bool
@SELECTORS.register("constant")
class ConstantSelector(Selector[ConstantSelectorConfig]):
"""Constant selector."""
selector_type = "constant"
CONFIG_SCHEMA = vol.Schema(
{
vol.Optional("label"): str,
vol.Optional("translation_key"): cv.string,
vol.Required("value"): vol.Any(str, int, bool),
}
)
def __init__(self, config: ConstantSelectorConfig | None = None) -> None:
"""Instantiate a selector."""
super().__init__(config)
def __call__(self, data: Any) -> Any:
"""Validate the passed selection."""
vol.Schema(self.config["value"])(data)
return self.config["value"]
class DateSelectorConfig(TypedDict):
"""Class to represent a date selector config."""

View File

@ -836,3 +836,57 @@ def test_file_selector_schema(schema, valid_selections, invalid_selections) -> N
"""Test file selector."""
_test_selector("file", schema, valid_selections, invalid_selections)
@pytest.mark.parametrize(
("schema", "valid_selections", "invalid_selections"),
(
(
{"value": True, "label": "Blah"},
(True, 1),
(None, False, 0, "abc", "def"),
),
(
{"value": False},
(False, 0),
(None, True, 1, "abc", "def"),
),
(
{"value": 0},
(0, False),
(None, True, 1, "abc", "def"),
),
(
{"value": 1},
(1, True),
(None, False, 0, "abc", "def"),
),
(
{"value": 4},
(4,),
(None, False, True, 0, 1, "abc", "def"),
),
(
{"value": "dog"},
("dog",),
(None, False, True, 0, 1, "abc", "def"),
),
),
)
def test_constant_selector_schema(schema, valid_selections, invalid_selections) -> None:
"""Test constant selector."""
_test_selector("constant", schema, valid_selections, invalid_selections)
@pytest.mark.parametrize(
"schema",
(
{}, # Value is mandatory
{"value": []}, # Value must be str, int or bool
{"value": 123, "label": 123}, # Label must be str
),
)
def test_constant_selector_schema_error(schema) -> None:
"""Test constant selector."""
with pytest.raises(vol.Invalid):
selector.validate_selector({"constant": schema})