Skip to content

Warn when a low battery keeps draining on external power - #16265

Closed
DanteCpp wants to merge 1 commit into
omacom:quattrofrom
DanteCpp:battery-discharging-on-power
Closed

DanteCpp wants to merge 1 commit into
omacom:quattrofrom
DanteCpp:battery-discharging-on-power

Conversation

@DanteCpp

@DanteCpp DanteCpp commented Oct 22, 2026 •

Copy link
Copy Markdown

A dock or weak charger can report external power while the battery is discharging. The battery service currently requires UPower.onBattery for low-battery warnings, so a battery can drain to empty without a warning in that state. This was observed with a DisplayLink dock on a 16-inch MacBook Pro (M1 Max): the USB-C supply stayed online while macsmc-ac was offline and the battery drained at 11–22 W.

Allow the low-battery warning after the battery has continuously reported Discharging on external power for 60 seconds. The delay avoids warning on the stale discharge state just after plugging into a working charger. A single one-shot timer tracks the hold; charging or leaving the external-power discharge state cancels it. The existing threshold and warning deduplication still apply.

This complements #13016 by addressing the missing low-battery warning noted in its review. The power panel and automatic power-profile selection retain their current behavior, including the shared UPower.onBattery source for profile selection. Rebased onto current quattro (d5b32d24d).

Validation:

  • battery-test.sh: all 19 assertions pass, covering the settling delay input, warning on sustained external-power discharge, deduplication, and charging recovery.
  • The warning regression fails against the quattro battery model: an 8% battery draining on external power still suppresses the warning after the hold.
  • power-test.sh: all 20 assertions pass. qmllint reports no diagnostics for the battery service; git diff --check passes.
  • An isolated Quickshell runtime loaded the actual service and model against simulated UPower D-Bus events. With the real 60-second timer, a short discharge cancelled the hold, sustained discharge invoked the warning after the deadline, charging reset the hold, and unplug/replug still selected the battery/AC profiles. Warning and profile commands were recorded by stubs, so the check did not change the desktop or actual power settings.

Remaining verification: the visible notification and behavior with the physical weak charger/dock have not been verified in the live desktop shell.

@DanteCpp
DanteCpp force-pushed the battery-discharging-on-power branch from b88afc8 to 7fd0d5a Compare November 6, 2026 11:29
@DanteCpp DanteCpp changed the title Treat a battery that keeps draining on external power as on battery Warn when a low battery keeps draining on external power Nov 6, 2026
@DanteCpp
DanteCpp marked this pull request as ready for review November 6, 2026 11:31
@greptile-apps

greptile-apps Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The change reaches the shell's low-battery warning workflow.

The PR appears safe to merge, with a non-blocking gap in tests for the discharge timer.

Findings

  1. P2 Discharge timer stays untested ▶

Summary

Allows low-battery warnings when external power stays online but the battery keeps discharging.

  • Low-battery warnings now cover batteries that keep draining on external power.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[External power and battery discharging] --> B[Start 60-second hold]
  B --> C{Discharge continues?}
  C -->|No| D[Cancel timer and clear hold]
  C -->|Yes, until deadline| E[Mark hold complete]
  E --> F{Battery at or below threshold?}
  F -->|Yes, not already warned| G[Send low-battery warning]
  F -->|No| H[Keep checking battery]
Loading

Reviews (1) · Last reviewed commit: "Warn when a low battery keeps draining o..." · Reviewed by Greptile

Comment on lines +35 to +43
// An online supply must not suppress the warning once discharge has settled.
const lowOnPower = { isPresent: true, percentage: 0.08, state: discharging }
assert(!battery.shouldWarnLowBattery(lowOnPower, false, discharging, 10, false, false).notify, 'battery waits out stale discharge after plugging in')
assertDeepEqual(
battery.shouldWarnLowBattery(lowOnPower, false, discharging, 10, false, true),
{ level: 8, notify: true, notifiedLowBattery: true },
'battery warns when sustained discharge on external power reaches the threshold'
)
assert(!battery.shouldWarnLowBattery(lowOnPower, false, discharging, 10, true, true).notify, 'battery does not repeat the warning on external power')

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.

P2 Discharge timer stays untested

The new tests pass drainingOnPowerHeld directly, so they never exercise the timer in Service.qml. They would still pass if the service warned immediately, failed to cancel a short discharge, or never restarted the hold.

Add a service-level regression test that ends discharge before the deadline, starts another discharge, and checks that the warning waits for a full new hold.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@omarchybot omarchybot added the bug Something isn't working label Nov 16, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

Reviewed at 7fd0d5a by Claude Fable 5.2, with Codex Medium as the second opinion. This was compared by reading the diffs against quattro; it was not run on a worker, for the reason below. Labelled bug: both models classified it that way separately.

Competing fixes. Three open pull requests fix the missing low-battery warning from #10939: #11161 (opened 2026-09-10), this one (2026-10-22) and #17881 (2026-11-06). #15009 made the same change as #11161 and was folded into it. Older fixes win by default, and #11161 fixes the cause: it drops the onBattery term so the warning follows the battery's own Discharging state. It has already been verified on a worker. Codex Medium, asked which should win without being given my answer, also picked #11161. So this one was not verified separately.

What this one adds over #11161 is the 60-second hold before trusting Discharging on external power. It changes one sequence: a battery already under the threshold that has not warned yet, plugged into a working charger whose state still reads Discharging when the next 30-second check runs. #11161 warns there and this does not. The cost is that a real drain on a dock warns up to a minute later, and a drain that is interrupted before 60 seconds restarts the hold. Whether that trade is wanted is the maintainer's call, so it has not been carried over to #11161.

One defect in the hold, found by Codex Medium and confirmed against the diff. shouldWarnLowBattery returns notifiedLowBattery: low, and at BatteryModel.js:21 low is false while the hold is still running. So:

  1. At 8% on battery the warning fires and the latch is saved.
  2. A weak charger is plugged in. onBattery goes false and the battery keeps reporting Discharging.
  3. The 30-second check runs before the hold expires and clears the latch.
  4. At 60 seconds drainingOnPowerHeld turns true, checkBattery() runs, and the warning fires a second time.

#11161 keeps the latch through that sequence and warns once. If the hold is kept, waiting to trust the report should not count as recovery.

The overlap with #16278, which merged, was also checked: it changes the text of omarchy-battery-status at 100% and touches none of this, so nothing here is covered by it. Codex Medium agreed; its independence is not guaranteed.

Next: this waits on the maintainer, who chooses between #11161, #17881 and this one. Nothing is needed from you unless they want the hold.

@omarchybot

Copy link
Copy Markdown
Collaborator

Closing as obsolete: #11161 merged into quattro and fixes the same bug — a low battery draining on a dock or under-powered charger now warns, because isDischarging reads the battery's own Discharging state instead of requiring UPower.onBattery. Thanks @DanteCpp for the diagnosis on the M1 Max with the DisplayLink dock; it described exactly the case that merge covers.

What this branch adds beyond it is the 60-second hold before believing Discharging on external power. With the merge in, that window does not produce a false warning: a battery already at or below the threshold has warned and latched notifiedLowBattery, and the stale Discharging right after plugging in keeps it latched, so nothing repeats; the latch clears once the state turns to Charging. The only case it would delay is a real drain crossing the threshold, which is the case the fix is for.

Checked by Claude Opus 5.5 comparing the two diffs, with Claude Opus 5.5 at high effort as the second opinion, which agreed the hold guards nothing the latch does not already cover (that agreement is not guaranteed independent). If you see a spurious warning on plug-in with #11161, reopen this with what the battery reported.

@omarchybot omarchybot closed this Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants