MQTT: honor mqttRetainState for button press events - #114
Conversation
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Disabling retention must also clear previously retained button payloads.
Review effort: Lite
Findings: 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()); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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>

Summary
Button press MQTT publishes (
publishButton) were always sent withretained=false, so the "Last Press" timestamp sensor in Home Assistant went back tounknownafter 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"
mqttRetainState(SP_MQTT_RETAIN_STATE), default on.shouldRetainState()) for sensors/relays/dimmer, but had no UI toggle — it was only settable via the HTTP/settingsAPI. It's now visible and editable from the on-device Settings screen too.Code change
MQTTServer.publishButtonnow respectsshouldRetainState()instead of hardcodingretained=false. When enabled, the broker replays the last button payload on resubscribe, restoring the correct last-press timestamp. HA's MQTTevententity 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.javaapp/src/main/java/me/rapierxbox/shellyelevatev2/SettingsFragment.ktapp/src/main/res/layout/settings_fragment.xmlapp/src/main/res/values/strings.xml