[mqtt] Let Home Assistant discovery be switched at run time - #73
Merged
Conversation
MQTTDiscoveryInfo keeps no separate "enabled" flag — the prefix is the flag (`is_discovery_enabled()` is `!prefix.empty()`), so blanking it to disable discovery throws away the only copy of the configured prefix and nothing can ever turn discovery back on. set_discovery_enabled() closes that gap: set_discovery_info() now also records the prefix it was configured with, and the toggle swaps discovery_info_.prefix between that and "". Nothing else has to change for it to take effect — every publish path re-reads the flag through MQTTComponent::is_discovery_enabled(), called from call_setup() and call_loop(), so the switch lands on the next publish with no reconnect, and resend_discovery() re-announces everything at once when it goes back on. disable_discovery() clears the recorded prefix as well. That is the build-time `discovery: false, discover_ip: false` path, and a firmware that compiled discovery out should stay out — a run-time setting must not resurrect it. No YAML key, like set_enable_on_boot() before it: the only caller is the workspace's jxd_config, which applies the value the user stored in the web UI. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Adds runtime control for MQTT Home Assistant discovery while preserving its configured prefix for re-enabling.
Changes:
- Stores the configured discovery prefix.
- Adds runtime enable/disable toggling.
- Prevents build-time-disabled discovery from being re-enabled.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Summary |
|---|---|
esphome/components/mqtt/mqtt_client.h |
Declares the runtime toggle and configured-prefix storage. |
esphome/components/mqtt/mqtt_client.cpp |
Implements prefix preservation and runtime toggling. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
MQTTDiscoveryInfokeeps no separate "enabled" flag — the prefix is the flag(
is_discovery_enabled()is!prefix.empty(),mqtt_client.cpp:687). So blanking theprefix to switch discovery off throws away the only copy of the configured value, and
nothing can turn discovery back on afterwards.
set_discovery_enabled(bool)closes that gap:set_discovery_info()now also records the prefix it was configured with.discovery_info_.prefixbetween that recorded value and"".disable_discovery()clears the recorded prefix too — that is the build-timediscovery: false, discover_ip: falsepath, and a firmware that compiled discoveryout must stay out; a run-time setting has no business resurrecting it.
Nothing else has to change for the switch to take effect. Every publish path re-reads
the flag through
MQTTComponent::is_discovery_enabled()(mqtt_component.cpp:202-204),called from
call_setup()andcall_loop(), so it lands on the next publish with noreconnect — and
resend_discovery()re-announces everything at once when it goes back on.No YAML key, the same shape as
APIServer::set_enable_on_boot()in #70: the only calleris the workspace's
jxd_config, which applies the value the user stored in the device'sweb UI.
Behaviour for any config that does not call the new setter is unchanged — the
recorded prefix is written but never read.
Consumer
jethome-iot/esphome-workspace#TBD — the persisted
discoveryMQTT setting, its RESTendpoint and the dashboard checkbox. Verified there end to end on QEMU against
script/mqtt-sink.py: 21 discovery configs on a baseline boot, none after a reconnectwith the switch off, all 21 again within the same uptime when it is switched back on.
🤖 Generated with Claude Code