Skip to content

[mqtt] Let Home Assistant discovery be switched at run time - #73

Merged
zkoalexey merged 1 commit into
dev-jethubfrom
feature/mqtt-discovery-toggle
Aug 25, 2026
Merged

[mqtt] Let Home Assistant discovery be switched at run time#73
zkoalexey merged 1 commit into
dev-jethubfrom
feature/mqtt-discovery-toggle

Conversation

@zkoalexey

Copy link
Copy Markdown

MQTTDiscoveryInfo keeps no separate "enabled" flag — the prefix is the flag
(is_discovery_enabled() is !prefix.empty(), mqtt_client.cpp:687). So blanking the
prefix 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.
  • The toggle swaps discovery_info_.prefix between that recorded value and "".
  • disable_discovery() clears the recorded prefix too — that is the build-time
    discovery: false, discover_ip: false path, and a firmware that compiled discovery
    out 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() and call_loop(), so it lands on the next publish with no
reconnect — 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 caller
is the workspace's jxd_config, which applies the value the user stored in the device's
web 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 discovery MQTT setting, its REST
endpoint 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 reconnect
with the switch off, all 21 again within the same uptime when it is switched back on.

🤖 Generated with Claude Code

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>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

@zkoalexey
zkoalexey merged commit 0902dec into dev-jethub Aug 25, 2026
24 checks passed
@zkoalexey
zkoalexey deleted the feature/mqtt-discovery-toggle branch August 25, 2026 08:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants