Add qr code selector (#109214)

This commit is contained in:
Bram Kragten 2024-01-31 19:27:03 +01:00 committed by GitHub
parent bbdb9b61c4
commit d361d47516
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 72 additions and 0 deletions

View File

@ -565,6 +565,49 @@ class ConstantSelector(Selector[ConstantSelectorConfig]):
return self.config["value"]
class QrErrorCorrectionLevel(StrEnum):
"""Possible error correction levels for QR code selector."""
LOW = "low"
MEDIUM = "medium"
QUARTILE = "quartile"
HIGH = "high"
class QrCodeSelectorConfig(TypedDict, total=False):
"""Class to represent a QR code selector config."""
data: str
scale: int
error_correction_level: QrErrorCorrectionLevel
@SELECTORS.register("qr_code")
class QrCodeSelector(Selector[QrCodeSelectorConfig]):
"""QR code selector."""
selector_type = "qr_code"
CONFIG_SCHEMA = vol.Schema(
{
vol.Required("data"): str,
vol.Optional("scale"): int,
vol.Optional("error_correction_level"): vol.All(
vol.Coerce(QrErrorCorrectionLevel), lambda val: val.value
),
}
)
def __init__(self, config: QrCodeSelectorConfig | None = None) -> None:
"""Instantiate a selector."""
super().__init__(config)
def __call__(self, data: Any) -> Any:
"""Validate the passed selection."""
vol.Schema(vol.Any(str, None))(data)
return self.config["data"]
class ConversationAgentSelectorConfig(TypedDict, total=False):
"""Class to represent a conversation agent selector config."""

View File

@ -1112,3 +1112,32 @@ def test_condition_selector_schema(
def test_trigger_selector_schema(schema, valid_selections, invalid_selections) -> None:
"""Test trigger sequence selector."""
_test_selector("trigger", schema, valid_selections, invalid_selections)
@pytest.mark.parametrize(
("schema", "valid_selections", "invalid_selections"),
(
(
{"data": "test", "scale": 5},
("test",),
(False, 0, []),
),
(
{"data": "test"},
("test",),
(True, 1, []),
),
(
{
"data": "test",
"scale": 5,
"error_correction_level": selector.QrErrorCorrectionLevel.HIGH,
},
("test",),
(True, 1, []),
),
),
)
def test_qr_code_selector_schema(schema, valid_selections, invalid_selections) -> None:
"""Test QR code selector."""
_test_selector("qr_code", schema, valid_selections, invalid_selections)