Skip to content

automations: fix parse error on !lambda-tagged scalars#930

Merged
marcelveldt merged 1 commit into
mainfrom
fix-automations-parse-tagged-lambda-scalar
May 21, 2026
Merged

automations: fix parse error on !lambda-tagged scalars#930
marcelveldt merged 1 commit into
mainfrom
fix-automations-parse-tagged-lambda-scalar

Conversation

@marcelveldt
Copy link
Copy Markdown
Contributor

What does this implement/fix?

Opening an existing automation in the editor failed with internal_error: Command failed: automations/parse for any device YAML that uses !lambda-tagged scalars (e.g. delay: !lambda return 0; or a lambda: !lambda | block). The automations/parse handler ran fine, but the response carried a ruamel TaggedScalar object straight into orjson.dumps, which raised TypeError: Type is not JSON serializable: TaggedScalar.

Root cause: _render_value in controllers/automations/parsing.py checked for a .yaml_tag attribute, but ruamel 0.18+ surfaces tagged scalars as TaggedScalar instances with the tag at .tag.value. The check never matched, so tagged scalars fell through unchanged.

  • detect TaggedScalar in _render_value and route !lambda through the existing {"_lambda": ...} sentinel
  • fall back to the plain string value for any other tag so the parse response stays JSON-serialisable
  • add a fixture + regression test covering a !lambda plain scalar and a !lambda | block, asserting the full payload round-trips through orjson.dumps
  • replace the synthetic-stand-in branch test with one driven by a real ruamel-produced TaggedScalar, plus a fallback-to-string case for an unknown tag

Related issue or feature (if applicable):

n/a

Types of changes

  • Bugfix (non-breaking change which fixes an issue) — bugfix
  • New feature (non-breaking change which adds functionality) — new-feature
  • Enhancement to an existing feature — enhancement
  • Breaking change (fix or feature that would cause existing functionality to not work as expected) — breaking-change
  • Refactor (no behaviour change) — refactor
  • Documentation only — docs
  • Maintenance / chore — maintenance
  • CI / workflow change — ci
  • Dependencies bump — dependencies

Frontend coordination

  • No frontend change needed
  • Companion frontend PR: esphome/device-builder-dashboard-frontend#

Checklist

  • The code change is tested and works locally.
  • Pre-commit hooks pass (ruff, codespell, yaml/json/python checks).
  • Tests have been added or updated under tests/ where applicable.
  • components.json has not been hand-edited (regenerate via script/sync_components.py if a sync is needed).
  • Architecture-level changes are reflected in docs/ARCHITECTURE.md and/or docs/API.md.

Copilot AI review requested due to automatic review settings May 21, 2026 20:22
@github-actions github-actions Bot added the bugfix Bug fix label May 21, 2026
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 21, 2026

Merging this PR will not alter performance

✅ 25 untouched benchmarks


Comparing fix-automations-parse-tagged-lambda-scalar (21b3946) with main (8a340ad)

Open in CodSpeed

@codecov-commenter
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.24%. Comparing base (8a340ad) to head (21b3946).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #930   +/-   ##
=======================================
  Coverage   99.24%   99.24%           
=======================================
  Files         191      191           
  Lines       14197    14200    +3     
=======================================
+ Hits        14090    14093    +3     
  Misses        107      107           
Flag Coverage Δ
py3.12 99.19% <100.00%> (+<0.01%) ⬆️
py3.14 99.24% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
..._device_builder/controllers/automations/parsing.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_value to detect ruamel TaggedScalar values and render !lambda as the {"_lambda": ...} sentinel, falling back to str(value) for other tags.
  • Add a regression fixture + parse-level test covering both !lambda plain scalars and !lambda | block scalars, including an orjson.dumps round-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

@marcelveldt marcelveldt merged commit 332c3f8 into main May 21, 2026
17 checks passed
@marcelveldt marcelveldt deleted the fix-automations-parse-tagged-lambda-scalar branch May 21, 2026 20:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants