Skip to content

Drop !remove'd keys from the MQTT discovery block - #2982

Merged
J. Nick Koston (bdraco) merged 6 commits into
esphome:mainfrom
sebo-b:fix-mqtt-remove-markers
Nov 5, 2026
Merged

J. Nick Koston (bdraco) merged 6 commits into
esphome:mainfrom
sebo-b:fix-mqtt-remove-markers

Conversation

@sebo-b

@sebo-b Sebastian Baberowski (sebo-b) commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

What does this implement/fix?

Keys removed with !remove are dropped from the merged config, so MQTT discovery (and every other reader) no longer sees a removed package key as set.

load_device_yaml merges packages with esphome's resolve_packages, which leaves config_helpers.Remove markers in place; esphome strips them later in resolve_extend_remove. A device removing a package's TLS settings therefore got "broker could not be resolved" and no monitor. In the same way, a removed client_certificate still counted as client-certificate auth.

  • _loading: load_device_yaml now drops every key: !remove from the merged config at any depth (the dict-key half of resolve_extend_remove). id: !remove x list-item removals stay as markers for esphome config, and still count as deferred work in resolution_incomplete. This also fixes api: !remove / mqtt: !remove / deep_sleep: !remove over a package reading as present.
  • _tolerant_yaml: !remove loads as esphome's Remove(value) marker instead of None (its only other caller, yaml_declares_ota_old_key, checks for dicts only, so it is unaffected).
  • _mqtt_block: a device file whose own mqtt: block !removes a key is not resolved from that block alone. Such a block only overrides a package, so the broker comes from the merged block. Otherwise an inline broker: plus !remove would lose the package's port, TLS and login.

Tests: one parametrized coordinator test covers a package's port + certificate_authority removed, its client-certificate keys removed, and an inline broker: next to a !remove. Each case checks host, port, username and CA, and that no warning is logged. A loader test pins removed keys dropped at the top level and nested. test_resolution_incomplete pins a dict-key removal as resolved and an id: !remove list item as deferred.

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.

Copilot AI balanced review requested due to automatic review settings October 26, 2026 22:11
@github-actions github-actions Bot added the bugfix Bug fix label Oct 26, 2026
@codspeed

codspeed Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 28 untouched benchmarks
⏩ 1 skipped benchmark1


Comparing sebo-b:fix-mqtt-remove-markers (b888e8d) with main (5077a3d)

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. ↩

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.

🟡 Changes recommended

Raw-block filtering can bypass inherited TLS settings and potentially expose MQTT credentials over plaintext.

1 open issue
What changed in this PR

This PR fixes MQTT discovery for package-based device configurations that use !remove, filtering removed settings before broker resolution.

Changes:

  • Preserve !remove markers in tolerant YAML parsing.
  • Drop removed keys from raw and package-resolved MQTT blocks.
  • Add regression tests for removed TLS and client-certificate settings.
File Description
tests/​test_mqtt_coordinator_extract.py Tests monitor creation after package MQTT settings are removed.
esphome_device_builder/​helpers/​device_yaml/​_tolerant_yaml.py Preserves !remove as ESPHome markers.
esphome_device_builder/​helpers/​device_yaml/​_mqtt_block.py Filters removal markers from MQTT extracts.

Review effort: Balanced

@codecov

codecov Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.76%. Comparing base (5077a3d) to head (b888e8d).

Additional details and impacted files

Impacted file tree graph

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

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/_loading.py 100.00% <100.00%> (ø)
..._device_builder/helpers/device_yaml/_mqtt_block.py 100.00% <100.00%> (ø)
...vice_builder/helpers/device_yaml/_tolerant_yaml.py 100.00% <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.

@esphbot

ESPHome Bot (esphbot) commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Previous review — superseded by a newer review below.

@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

Copy link
Copy Markdown
Member

Simplify pass in 8e55418:

  • !remove is now dropped once, in the deep load: load_device_yaml strips every key: !remove from the merged config at any depth, the way esphome's resolve_extend_remove does for dict keys. That replaces the MQTT-only _drop_removed, and it also fixes api: !remove / mqtt: !remove / deep_sleep: !remove over a package being read as present.
    • id: !remove x (list-item removal) stays as a marker for esphome config. It still counts as deferred work, and a test pins that.
    • A plain dict-key removal no longer defers to the subprocess.
  • The main-block refusal in extract_mqtt_block stays in place. The tolerant loader has to keep the marker, so the strip runs on the deep config and not there.
  • The tolerant !remove constructor now builds Remove(str(node.value)), the same as esphome's loader.
  • The two new MQTT tests are folded into one parametrized test with a _load_package_device helper. Each case asserts host, port, username and CA, so the client-cert case is actually pinned.
  • Trimmed the rationale clause from the extract_mqtt_block docstring.

Skipped: reusing _holds_deferred_marker for the main-block check. Only top-level mqtt: keys shape the broker, so the shallow check is enough.

ESPHome Bot (@esphbot) review

# Conflicts:
#	esphome_device_builder/helpers/device_yaml/_mqtt_block.py
#	tests/test_mqtt_coordinator_extract.py
@bdraco

Copy link
Copy Markdown
Member

Merged main (post #2965) in 9484710. extract_mqtt_parts keeps its new has_packages return and still refuses a main block that !removes a key. Both sides' tests are kept.

ESPHome Bot (@esphbot) review

@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

ESPHome Bot (esphbot) commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Previous review — superseded by a newer review below.

@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.

@esphbot

ESPHome Bot (esphbot) commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

PR Review — Drop !remove'd keys from the MQTT discovery block

Merge-ready. The one open item from the last review, the out-of-date PR description, is now fixed. No new findings.

Strengths:

  • _drop_removed_keys runs once at the end of load_device_yaml (_loading.py:640). It does the dict-key half of esphome's resolve_extend_remove, so every consumer of the deep config (MQTT discovery, api, deep_sleep and the rest) treats a removed key as absent.
  • The strip still runs if resolve_packages fails and the config stays unmerged. That case is still flagged as deferred because _has_packages_block keeps resolution_incomplete true, so skipping the merge doesn't hide unfinished work.
  • id: !remove list items are left as markers for esphome config. The removed_list_item case checks that they still count as deferred.
  • extract_mqtt_parts refuses a device mqtt: block that contains a Remove marker. That closes the inline-broker/TLS-bypass path Copilot flagged: discovery falls back to the merged block, which keeps the package's port, TLS and login. The inline-broker test case covers this.
  • The parametrized coordinator test checks host, port, username and CA for each case, and also checks that no warning is logged.

Needs attention:

  • Nothing blocking. The PR description now matches the diff: the _loading, _tolerant_yaml and _mqtt_block split, list-item removals left for esphome config, and the tests as listed.


Checklist

  • Removed keys cannot leak into broker resolution (raw and merged paths)
  • Inline broker beside !remove still inherits package TLS/port/login
  • List-item id: !remove still defers to esphome config
  • Failed package merge still reported as incomplete after strip
  • PR description matches diff
  • Docstrings follow repo terse-contract convention

Automated review by Kōan (Claude) HEAD=8e2f0bc 16s

@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

Copy link
Copy Markdown
Member

Updated the PR description to match the deep-load strip (the _loading / _tolerant_yaml / _mqtt_block split, list-item id: !remove left for esphome config, and the current tests).

ESPHome Bot (@esphbot) review

@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.

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 were identified for the MQTT discovery configuration-removal changes.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@bdraco
J. Nick Koston (bdraco) merged commit 9217113 into esphome:main Nov 5, 2026
22 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] !remove under mqtt: breaks MQTT discovery for package-based devices

4 participants