Skip to content

Ignore encryption: !remove in the raw encryption heuristic - #3019

Merged
J. Nick Koston (bdraco) merged 2 commits into
esphome:mainfrom
kris-anderson:fix-remove-encryption-heuristic
Nov 5, 2026
Merged

J. Nick Koston (bdraco) merged 2 commits into
esphome:mainfrom
kris-anderson:fix-remove-encryption-heuristic

Conversation

@kris-anderson

Copy link
Copy Markdown
Contributor

What does this implement/fix?

The raw-text encryption heuristic (_nested_key_re) no longer counts encryption: !remove under api: or ota: as declared encryption.

api_encrypted is the OR of the resolved config, this heuristic and the mDNS signal. The resolved config already ignores a removed encryption: (the Remove marker isn't a mapping), but the regex matched the line, so the device read as encrypted. The API-info probe then refused it with "no Noise key resolved" after every cooldown. On a dashboard that can't hear the device's mDNS, deployed_version stayed empty: no update badge, and Update All skipped it. Apollo's AIR-1 package declares api: encryption:, so !remove is how users keep it on the plaintext API.

The change is a negative lookahead after {key}:. It adds no alternation, so the backtracking note on the pattern still holds.

Tests: api_encrypted stays False at scan time for api: encryption: !remove, and yaml_has_ota_encryption gets a removed case. Both fail without the fix.

Related: #2982 handles !remove markers in the resolved mqtt: block. It touches different files.

Related issue or feature (if applicable):

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-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.index.json / definitions/components/*.json have 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.

🤖 Generated with Claude Code

A package's api or ota encryption removed with `!remove` was read as declared, so the API-info probe refused the device for lack of a Noise key and its deployed version stayed empty.
Copilot AI balanced review requested due to automatic review settings November 5, 2026 07:08
@github-actions github-actions Bot added the bugfix Bug fix label Nov 5, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are recorded for the change to ignore !remove encryption values.

No open issues 🎉

What changed in this PR

Fixes #3018 by preventing the raw YAML encryption heuristic from treating encryption: !remove as declared encryption. This lets affected plaintext API devices reach version probing instead of being rejected for a missing Noise key.

Changes:

  • Exclude !remove values from the shared API/OTA encryption regex.
  • Add regression coverage for scan-time API encryption state and OTA encryption detection.
File Description
tests/​test_encryption_key_extraction.py Adds API and OTA regression cases for removed encryption.
esphome_device_builder/​helpers/​device_yaml/​_parsing.py Updates the nested-key regex to ignore !remove values.

Review effort: Balanced

@codspeed

codspeed Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 1 benchmark is dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 28 untouched benchmarks
⏩ 1 skipped benchmark1


Comparing kris-anderson:fix-remove-encryption-heuristic (8df7369) with main (bf2b8cf)

Open in CodSpeed

Footnotes

  1. 1 benchmark was skipped, so the baseline result was used instead. If it was deleted from the codebase, click here and archive it to remove it from the performance reports. ↩

@codecov

codecov Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.76%. Comparing base (bf2b8cf) to head (8df7369).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #3019   +/-   ##
=======================================
  Coverage   99.76%   99.76%           
=======================================
  Files         300      300           
  Lines       26484    26484           
=======================================
  Hits        26423    26423           
  Misses         61       61           
Flag Coverage Δ
py3.12 99.73% <ø> (+<0.01%) ⬆️
py3.14 99.62% <ø> (ø)

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

Files with missing lines Coverage Δ
...ome_device_builder/helpers/device_yaml/_parsing.py 100.00% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@bdraco

Copy link
Copy Markdown
Member

Merged main to bring the branch up to date; no conflicts, and the PR's tests pass locally.

ESPHome Bot (@esphbot) review

@esphbot

Copy link
Copy Markdown
Contributor

PR Review — Ignore encryption: !remove in the raw encryption heuristic

Merge-ready. The fix is small, targeted and correct. I found nothing to change.

What the PR does well:

  • Fix at the source. Both _RAW_API_ENCRYPTION_RE and _RAW_OTA_ENCRYPTION_RE come from _nested_key_re (_parsing.py:253-254). One negative lookahead there fixes the API path and the OTA draft fallback together, with no new branches in the code that calls them.
  • Safe regex change. (?![ \t]*!remove\b) is a fixed-width check with no alternation, so the anti-backtracking note above the pattern still holds.
  • Edge cases behave correctly. \b rejects lookalikes such as !removed. Extra spaces and a trailing # comment after !remove are still treated as a removal. If the !remove line is skipped, the regex can still match a later real encryption: line in the same block.
  • Same rule as the resolved config. The resolved-config path already ignores a Remove marker because it isn't a mapping, so the raw-text heuristic and the resolved check now agree.
  • Tests hit the real bug path. _loading.py:366 combines yaml_has_api_encryption with the resolved and mDNS signals. The new scan-level test (test_load_device_from_storage_api_encryption_removed) goes through that combination rather than only calling the regex. The OTA removed case covers the other caller.
  • Follows repo conventions. The comment is a short note on why the rule exists, and the test docstrings are one line each.

No issues to report.



Checklist

  • Correctness of regex change (lookahead, word boundary, backtracking)
  • Both callers (API and OTA heuristics) covered by tests
  • Consistent with resolved-config semantics
  • Docstring/comment conventions
  • Diff matches PR description

Automated review by Kōan (Claude) HEAD=8df7369 23s

@esphbot ESPHome Bot (esphbot) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Tip

No blocking issues found — ready to merge.

@bdraco
J. Nick Koston (bdraco) merged commit 100d1f6 into esphome:main Nov 5, 2026
23 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 7, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

bugfix Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] api: encryption: !remove marks a device as encrypted, so its version probe never runs

4 participants