mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Support shorthand templates in condition actions (#61177)
* Support shorthand templates in condition actions * Fix validation message * Fix tests
This commit is contained in:
@@ -1184,6 +1184,45 @@ def test_key_value_schemas():
|
||||
schema({"mode": mode, "data": data})
|
||||
|
||||
|
||||
def test_key_value_schemas_with_default():
|
||||
"""Test key value schemas."""
|
||||
schema = vol.Schema(
|
||||
cv.key_value_schemas(
|
||||
"mode",
|
||||
{
|
||||
"number": vol.Schema({"mode": "number", "data": int}),
|
||||
"string": vol.Schema({"mode": "string", "data": str}),
|
||||
},
|
||||
vol.Schema({"mode": cv.dynamic_template}),
|
||||
"a cool template",
|
||||
)
|
||||
)
|
||||
|
||||
with pytest.raises(vol.Invalid) as excinfo:
|
||||
schema(True)
|
||||
assert str(excinfo.value) == "Expected a dictionary"
|
||||
|
||||
for mode in None, {"a": "dict"}, "invalid":
|
||||
with pytest.raises(vol.Invalid) as excinfo:
|
||||
schema({"mode": mode})
|
||||
assert (
|
||||
str(excinfo.value)
|
||||
== f"Unexpected value for mode: '{mode}'. Expected number, string, a cool template"
|
||||
)
|
||||
|
||||
with pytest.raises(vol.Invalid) as excinfo:
|
||||
schema({"mode": "number", "data": "string-value"})
|
||||
assert str(excinfo.value) == "expected int for dictionary value @ data['data']"
|
||||
|
||||
with pytest.raises(vol.Invalid) as excinfo:
|
||||
schema({"mode": "string", "data": 1})
|
||||
assert str(excinfo.value) == "expected str for dictionary value @ data['data']"
|
||||
|
||||
for mode, data in (("number", 1), ("string", "hello")):
|
||||
schema({"mode": mode, "data": data})
|
||||
schema({"mode": "{{ 1 + 1}}"})
|
||||
|
||||
|
||||
def test_script(caplog):
|
||||
"""Test script validation is user friendly."""
|
||||
for data, msg in (
|
||||
|
||||
Reference in New Issue
Block a user