automations: fix parse error on !lambda-tagged scalars#930
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #930 +/- ##
=======================================
Coverage 99.24% 99.24%
=======================================
Files 191 191
Lines 14197 14200 +3
=======================================
+ Hits 14090 14093 +3
Misses 107 107
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
Fixes automations/parse failing on YAML values tagged with !lambda by ensuring ruamel’s tagged scalar values are rendered into JSON-serializable shapes (either the existing {"_lambda": ...} sentinel for !lambda, or a plain string fallback for unknown tags). This aligns the automations editor backend response with orjson’s serialization requirements while preserving the existing lambda sentinel contract.
Changes:
- Update
_render_valueto detect ruamelTaggedScalarvalues and render!lambdaas the{"_lambda": ...}sentinel, falling back tostr(value)for other tags. - Add a regression fixture + parse-level test covering both
!lambdaplain scalars and!lambda |block scalars, including anorjson.dumpsround-trip assertion. - Replace the prior synthetic tagged-scalar branch test with one that uses a real ruamel-produced tagged scalar, plus an “unknown tag falls back to string” test.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
esphome_device_builder/controllers/automations/parsing.py |
Render ruamel TaggedScalar values into JSON-safe representations; preserve !lambda via the existing sentinel. |
tests/test_automations_parse.py |
Add end-to-end regression coverage for !lambda-tagged scalars and assert payload is orjson-serializable. |
tests/test_automations_branches.py |
Update branch tests to use real ruamel tagged scalars and validate unknown-tag fallback behavior. |
tests/fixtures/automation_yamls/lambda_tagged_scalars.yaml |
New fixture exercising !lambda plain scalar and `!lambda |
What does this implement/fix?
Opening an existing automation in the editor failed with
internal_error: Command failed: automations/parsefor any device YAML that uses!lambda-tagged scalars (e.g.delay: !lambda return 0;or alambda: !lambda |block). Theautomations/parsehandler ran fine, but the response carried a ruamelTaggedScalarobject straight intoorjson.dumps, which raisedTypeError: Type is not JSON serializable: TaggedScalar.Root cause:
_render_valueincontrollers/automations/parsing.pychecked for a.yaml_tagattribute, but ruamel 0.18+ surfaces tagged scalars asTaggedScalarinstances with the tag at.tag.value. The check never matched, so tagged scalars fell through unchanged.TaggedScalarin_render_valueand route!lambdathrough the existing{"_lambda": ...}sentinel!lambdaplain scalar and a!lambda |block, asserting the full payload round-trips throughorjson.dumpsTaggedScalar, plus a fallback-to-string case for an unknown tagRelated issue or feature (if applicable):
n/a
Types of changes
bugfixnew-featureenhancementbreaking-changerefactordocsmaintenancecidependenciesFrontend coordination
Checklist
ruff,codespell, yaml/json/python checks).tests/where applicable.components.jsonhas not been hand-edited (regenerate viascript/sync_components.pyif a sync is needed).docs/ARCHITECTURE.mdand/ordocs/API.md.