Compare commits

...
2 changed files with 46 additions and 4 deletions

No files matched your search

+1 -3
View File
@@ -1013,9 +1013,7 @@ def convert(schema, config_var, path):
config_var[S_TYPE] = "trigger"
if ejs.extended_schemas[repr(data)][0] == automation.AUTOMATION_SCHEMA:
extra_schema = ejs.extended_schemas[repr(data)][1]
if (
extra_schema is not None and len(extra_schema) > 1
): # usually only trigger_id here
if extra_schema:
config = convert_config(extra_schema, path + "/extra")
if "schema" in config:
automation_schema = config["schema"]
+45 -1
View File
@@ -13,7 +13,7 @@ from typing import Any
import pytest
from esphome import config_validation as cv
from esphome import automation, config_validation as cv
SCRIPT_PATH = (
Path(__file__).resolve().parent.parent.parent
@@ -141,6 +141,41 @@ def test_convert_walks_callable_schema_extractor() -> None:
assert "foo" in config_var["schema"]["config_vars"]
@pytest.mark.parametrize(
("extra", "expected_vars"),
[
({cv.Required("foo"): cv.string}, {"foo", "then"}),
(
{cv.Optional("foo"): cv.string, cv.Optional("bar"): cv.int_},
{"foo", "bar", "then"},
),
({cv.GenerateID("trigger_id"): cv.declare_id(automation.Trigger)}, None),
],
)
def test_convert_automation_extra_schema(
extra: dict, expected_vars: set[str] | None, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A trigger's own keys are dumped even when there is only one; trigger_id alone is not."""
from esphome.schema_extractors import SCHEMA_EXTRACT
validator = automation.validate_automation(extra)
# Schema.extend registers itself only when extraction is on at import, so register it here
data = validator(SCHEMA_EXTRACT)
monkeypatch.setitem(
_bls.ejs.extended_schemas,
repr(data),
[automation.AUTOMATION_SCHEMA, extra],
)
config_var: dict = {}
_bls.convert(validator, config_var, "/test")
assert config_var["type"] == "trigger"
if expected_vars is None:
assert "schema" not in config_var
else:
assert set(config_var["schema"]["config_vars"]) == expected_vars
def test_convert_sees_pin_through_optional_pin_wrapper() -> None:
"""A pin schema wrapped by PIN_SCHEMA_REGISTRY.optional() is still dumped as a pin."""
from esphome import pins
@@ -366,6 +401,15 @@ def test_uart_clock_source_preserves_variant_metadata(
# ---------------------------------------------------------------------------
def test_single_key_trigger_schema_is_dumped(language_schema_dir: Path) -> None:
"""modbus_client's on_no_response has only `retry` besides trigger_id, and it is still dumped."""
modbus = json.loads((language_schema_dir / "modbus_client.json").read_text())
action_base = modbus["modbus_client"]["schemas"]["_ACTION_BASE_SCHEMA"]
trigger = action_base["schema"]["config_vars"]["on_no_response"]
assert set(trigger["schema"]["config_vars"]) == {"retry", "then"}
@pytest.fixture(scope="module")
def lvgl_schema(language_schema_dir: Path) -> dict:
return json.loads((language_schema_dir / "lvgl.json").read_text())