diff --git a/components/ups_hid/protocol_goldenmate.cpp b/components/ups_hid/protocol_goldenmate.cpp index 9b6ec74..a867cd8 100644 --- a/components/ups_hid/protocol_goldenmate.cpp +++ b/components/ups_hid/protocol_goldenmate.cpp @@ -96,7 +96,7 @@ bool GoldenMateProtocol::parse_megatec_string(const HidReport &report, UpsData & if (c >= '0' && c <= '9') { packed += c; } else if (c == ' ' || c == 0x00) { - // Normalize spaces/nulls to zeros + // Normalize spaces/nulls to zeros to keep the 32-character string perfectly aligned packed += '0'; } } @@ -111,13 +111,10 @@ bool GoldenMateProtocol::parse_megatec_string(const HidReport &report, UpsData & float battery_pct = std::atof(packed.substr(12, 3).c_str()); // --- SANITY CHECK FILTER --- - // If the hardware sends a transient zeroed-out buffer, drop the read. - // Returning false tells ESPHome to keep the previous valid sensor values. if (runtime_mins == 0.0f && battery_pct == 0.0f) { ESP_LOGW(GM_TAG, "Transient hardware read (all zeroes). Ignoring to prevent graph dips."); return false; } - // --------------------------- data.battery.runtime_minutes = runtime_mins; data.battery.level = battery_pct; @@ -126,11 +123,28 @@ bool GoldenMateProtocol::parse_megatec_string(const HidReport &report, UpsData & std::string status_str = packed.substr(24, 8); bool on_battery = (status_str.length() > 0 && status_str[0] == '1'); + // --- TIME-BASED DEBOUNCE LOGIC --- + static bool is_full = true; + static int below_100_count = 0; + + if (battery_pct >= 100.0f) { + is_full = true; + below_100_count = 0; // Reset counter immediately on a 100% read + } else { + below_100_count++; + // Require 2 consecutive reads below 100% to drop the 'Full' status + if (below_100_count >= 2) { + is_full = false; + } + } + if (on_battery) { data.power.status = "OB DISCHRG"; data.battery.status = "Discharging"; + is_full = false; // Force reset if we lose AC power + below_100_count = 2; // Keep counter aligned with state } else { - if (battery_pct >= 100.0f) { + if (is_full) { data.power.status = "OL"; data.battery.status = "Full"; } else {