Skip to content

MQTT: honor mqttRetainState for button press events - #114

Merged
dbochicchio merged 6 commits into
mainfrom
feature/mqtt-button-retain
Sep 27, 2026
Merged

dbochicchio merged 6 commits into
mainfrom
feature/mqtt-button-retain

Conversation

@dbochicchio

Copy link
Copy Markdown
Collaborator

Summary

Button press MQTT publishes (publishButton) were always sent with retained=false, so the "Last Press" timestamp sensor in Home Assistant went back to unknown after any MQTT reconnect (WiFi blip, app/HA restart), even though no button was actually pressed. This made it hard to detect real presses via state-change automations.

New setting: "Retain published state"

  • Location: Settings → MQTT section, right below "Home Assistant auto-discovery".
  • Preference key: mqttRetainState (SP_MQTT_RETAIN_STATE), default on.
  • This preference already existed and was read internally (shouldRetainState()) for sensors/relays/dimmer, but had no UI toggle — it was only settable via the HTTP /settings API. It's now visible and editable from the on-device Settings screen too.
  • Hint text: "Keep sensor, relay and button state on the broker so it survives a reconnect. Momentary events (switch press/release) are never retained regardless of this setting."

Code change

  • MQTTServer.publishButton now respects shouldRetainState() instead of hardcoding retained=false. When enabled, the broker replays the last button payload on resubscribe, restoring the correct last-press timestamp. HA's MQTT event entity already discards replayed retained messages on its own, so this doesn't cause a duplicate/false event to fire.

Files changed

  • app/src/main/java/me/rapierxbox/shellyelevatev2/mqtt/MQTTServer.java
  • app/src/main/java/me/rapierxbox/shellyelevatev2/SettingsFragment.kt
  • app/src/main/res/layout/settings_fragment.xml
  • app/src/main/res/values/strings.xml

Copilot AI lite review requested due to automatic review settings September 27, 2026 17:10
@dbochicchio dbochicchio added enhancement New feature or request done / tested labels Sep 27, 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.

Copilot review overview

🟡 Changes recommended

Disabling retention must also clear previously retained button payloads.

Review effort: Lite
Findings: 1 Medium severity

Open (1)
What changed in this PR

Adds a visible MQTT retention setting and applies it to button publications so Home Assistant can restore last-press timestamps after reconnects.

Changes:

  • Adds and binds the “Retain published state” setting.
  • Adds labels and explanatory hint text.
  • Applies the retention preference to button MQTT messages.
  • Disabling retention does not currently clear previously retained button payloads.
File Summary
app/​src/​main/​res/​values/​strings.xml Adds setting labels and hint text.
app/​src/​main/​res/​layout/​settings_fragment.xml Adds the MQTT retention switch and hint.
app/​src/​main/​java/​me/​rapierxbox/​shellyelevatev2/​SettingsFragment.kt Registers and binds the retention setting.
app/​src/​main/​java/​me/​rapierxbox/​shellyelevatev2/​mqtt/​MQTTServer.java Applies retention to button publications.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

publishInternal(topic, json.toString(), 1, false);
// retained (when enabled) so the last-press timestamp sensor survives a reconnect instead of going unknown;
// the ha mqtt event entity already discards replayed retained messages on its own, so it won't refire
publishInternal(topic, json.toString(), 1, shouldRetainState());

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.

Implemented in eaaec23: when retain is toggled from true to false, MQTT now clears retained button payloads (including power and tracked button-topic variants) before the next state sync so stale retained button events are removed.

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.

Expanded the retained-payload cleanup to cover every retained state topic published under this device’s MQTT namespace, while preserving availability and discovery topics. The change is in commit eaaec23.

Copilot AI and others added 3 commits September 27, 2026 17:17
Co-authored-by: dbochicchio <31511185+dbochicchio@users.noreply.github.com>
Co-authored-by: dbochicchio <31511185+dbochicchio@users.noreply.github.com>
Co-authored-by: dbochicchio <31511185+dbochicchio@users.noreply.github.com>
Co-authored-by: dbochicchio <31511185+dbochicchio@users.noreply.github.com>
Co-authored-by: dbochicchio <31511185+dbochicchio@users.noreply.github.com>
@dbochicchio
dbochicchio merged commit 2997481 into main Sep 27, 2026
1 check passed
@dbochicchio
dbochicchio deleted the feature/mqtt-button-retain branch September 27, 2026 18:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

done / tested enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants