button_relay_toggle_v2: bound retained-command guard to a time window, fix active_low re-drive - #28
Conversation
…ndow, fix active_low re-drive - _bootOverridePending was armed unconditionally on every settings save and MQTT reconnect (for every configured relay, not just ones with a non-Default boot state) and stayed armed indefinitely until the *next* /set message, however much later that arrived. A genuine, live command from Home Assistant landing in that window was silently reinterpreted as a stale retained redelivery and overridden back to the relay's current state. Replaced the boolean with a short (3s) time-bounded deadline: retained messages are redelivered by the broker essentially immediately on subscribe, so this still catches that case without swallowing a live command that just happens to be first after a reconnect minutes earlier. - When relay_active_low was toggled via settings save while the physical pin was unchanged, the code only called pinMode(OUTPUT) and never re-drove the pin, so the reported logical on/off state (used for MQTT/HA and the button toggle) silently flipped even though the relay's real electrical state hadn't changed. Now re-drives the pin to preserve the previous logical state when only active_low changes. - Fixed a corrupted arrow character in the readme's wiring diagram. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01J6bJtispewBKR2dXBcNWpw
|
The Generated by Claude Code |
|
Re-run passed — confirms the earlier Generated by Claude Code |
Summary
Follow-up review of
button_relay_toggle_v2(after #7/#25), looking for anything left. Found and fixed two real bugs, plus a doc typo:_bootOverridePending[i]gets armed on every settings save and MQTT reconnect viasetupRelaySubscriptions()(for every relay with a valid pin, not just non-Default boot-state ones), and stays armed until the next/setmessage arrives — however much later that is. If a real, live Home Assistant command happened to be the first one received after a reconnect that occurred minutes earlier, it was misclassified as "the stale retained redelivery" and silently overridden back to the relay's current state, dropping the user's command. Replaced the boolean with a short (3s) time-bounded deadline (_overrideDeadline[i]) — retained messages are redelivered by the broker essentially immediately on subscribe, so this window still catches that case without swallowing an unrelated later command.relay_active_lowalone silently flipped the reported state. InreadFromConfig()'s "same pin" branch, when onlyrelay_active_lowchanged (pin/wiring untouched), the code calledpinMode(OUTPUT)but never re-drove the pin. Since the reported logical on/off state is derived asactiveLow ? !digitalRead(pin) : digitalRead(pin), flipping the setting flipped what HA/MQTT reported even though the relay's actual electrical state never changed. Now captures the oldactive_lowvalue before the config read and, if it changed, re-drives the pin so the logical state stays the same as before the save.?instead of->) in the readme's wiring diagram.I looked at a third finding from the review — plumbing the MQTT
retainflag through toonMqttMessage()instead of approximating it by timing — but that requires changing theUsermod::onMqttMessage()interface used by every usermod (wled00/mqtt.cpp,wled00/fcn_declare.h), which is out of scope for a usermod-only fix. Noting it here as a possible follow-up for core.Test plan
relay_active_lowalone via settings and confirm HA doesn't see a spurious state flip; and confirm a live HA command right after a settings save/reconnect is no longer overridden.🤖 Generated with Claude Code
https://claude.ai/code/session_01J6bJtispewBKR2dXBcNWpw
Generated by Claude Code