Skip to content

Warn on low battery even when line power is online - #11161

Merged
dhh merged 2 commits into
omacom:quattrofrom
sanjyay:fix/battery-warning-while-docked
Nov 21, 2026
Merged

dhh merged 2 commits into
omacom:quattrofrom
sanjyay:fix/battery-warning-while-docked

Conversation

@sanjyay

@sanjyay sanjyay commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

What

Detect battery discharge directly via device.state === dischargingState in BatteryModel.isDischarging, removing the requirement that UPower's global onBattery property be true.

Why

When a laptop is connected to a dock, hub, or under-wattage charger where system power consumption exceeds the power delivered by the power supply:

  • UPower.onBattery is false (because a line-power device is online).
  • UPower.displayDevice.state is Discharging (the battery percentage is actively dropping).

Because isDischarging previously required onBattery && device.state === dischargingState, it returned false while docked. As a result, shouldWarnLowBattery suppressed the low-battery notification across the entire discharge, leaving the user with zero warning before the system drained to 2% and was force-suspended by upowerd.

Checking device.state === dischargingState ensures that any time the battery is genuinely discharging past the low threshold, the warning is sent. The onBattery argument is preserved in the function signature for compatibility with existing callers.

Note that Service.qml's applyPowerProfile() legitimately continues to use UPower.onBattery for profile switching between AC and battery.

Testing

  • Updated test/shell.d/battery-test.sh:
    • Sabotage check: Replaced the outdated assertion expecting !isDischarging when onBattery is false with an assertion verifying isDischarging returns true even when line power is online (onBattery: false). Confirmed this fails on unmodified baseline and passes with the fix.
    • Added unit test asserting shouldWarnLowBattery triggers notification when onBattery: false and state: discharging under threshold.
    • Added assertion verifying non-discharging states (e.g. charging) return false.
    • All 9 unit test assertions pass.
  • Verified in the Omarchy disposable VM (omavm):
    • Booted clean fresh snapshot.
    • Deployed updated BatteryModel.js and verified unit assertions inside the guest session.
    • Restarted the Omarchy shell and confirmed clean desktop/bar loading with no QML or service warnings.

Fixes #10939

When a machine is connected to a dock or power supply that delivers less
power than the system draws, UPower.onBattery is false because a line-power
source is online, but device.state is Discharging.

Previously, isDischarging required onBattery to be true. As a result,
shouldWarnLowBattery returned false throughout the discharge and suppressed
the low-battery warning until the system was force-suspended at critical
charge.

Rely directly on device.state === dischargingState in isDischarging while
preserving the onBattery argument for API compatibility.

Fixes omacom#10939
@Oldrich333

Copy link
Copy Markdown

I saw a hard battery drain on Omarchy 4.0.4-1 with no low-battery toast. I cannot recover the previous UPower.onBattery value, so this is not a confirmed reproduction of the specific dock condition.

I applied this PR's state-only isDischarging change to a local clone of the Omarchy battery service. With a present device at 9%, state Discharging, and onBattery=false, shouldWarnLowBattery now returns notify=true; the stock model returns false. I also verified that the current notification service displays a critical test toast. This patch removes a real suppression path for the warning on this installation.

@omarchybot omarchybot added the verified Omarchy Triage has verified that this issue is ready for final review label Oct 2, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

Reviewed by Claude Opus 5.5 and verified on a disposable Omarchy worker. The fix is correct and proportionate: one condition in BatteryModel.isDischarging, which is exactly what suppressed the warning in #10939.

Reproduction. I wrote a separate check against the model, feeding it the UPower values from #10939: OnBattery false and the display device Discharging at 8%. On quattro it sends no warning. At 16bb60d it warns. With only BatteryModel.js reverted, the warning is gone again. In the same check, a Charging device at 8% still gets no warning, and an unplugged one at 8% still does. This tests the model with those inputs, not a laptop on an underpowered dock. The report and the comment above already show UPower producing that pair of values on real hardware.

Tests on the worker. ./test/cli passed. test/shell.d/battery-test.sh passed 9 of 9 and test/shell.d/power-test.sh passed 22 of 22, both against the running compositor. The shell was relaunched from this branch and loaded without errors. The PR's new docked assertion fails when the fix is reverted, so the test is not vacuous.

Second opinion. It did not run: the configured reviewer (Codex) had used up today's review budget. So this pull request has had one model's review, not two.

For the maintainer, both about other open pull requests on this model:

  • Delay the first low-battery check until UPower settles #8284 adds a test asserting that a discharging battery at 8% with line power online does not warn. That is the opposite of this change. Whichever lands second has to drop or invert that assertion.
  • The low-battery latch clears whenever the battery stops counting as low-and-discharging. Before this change that could not happen on line power. Now, a marginal charger whose state alternates between Charging and Discharging below 10% would re-send the critical warning each time the 30-second check catches it discharging again. I worked this out from the code and did not observe it. Keep low-battery latch across AC online flaps #9883 keeps the latch set until the level recovers above the threshold, which would cover this case.

Labelled bug and verified. It is not marked ready because the second opinion is still outstanding. Nothing is needed from the author.

@sanjyay

sanjyay commented Oct 2, 2026

Copy link
Copy Markdown
Contributor Author

@greptileai Please review the current head, including the docked discharge warning and its regression tests.

@greptile-apps

greptile-apps Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge.

Summary

Low-battery warnings now depend on the battery’s discharge state, even when line power is online.

  • Low-battery warnings follow the battery’s discharge state.

Reviews (2) · Last reviewed commit: "Drop the unused onBattery parameter from..." · Reviewed by Greptile

@sanjyay

sanjyay commented Oct 2, 2026

Copy link
Copy Markdown
Contributor Author

Revalidated 16bb60d in a disposable VM: all nine battery assertions, 22 power assertions, and the CLI suite pass. Reverting only BatteryModel.js makes the docked-discharge assertion fail. The desktop shell loads from this branch and reports the battery plugin enabled; the original shell and configuration were restored. These are model-input and VM integration checks, not physical underpowered-dock validation.

Greptile has now reviewed this head with no findings and a 5/5 score. No production change is needed for the current review.

I checked the related work noted above: #9883 owns the low-battery latch/flapping fix, so this PR does not duplicate it. #8284's startup settling change is separate, but its assertion suppressing a discharging device while line power is online conflicts with this PR's intended behavior; that assertion must be reconciled if both land.

@jandrusk

Copy link
Copy Markdown

This matches the docked-discharge case well, and the BatteryModel change is minimal.

#15009 is the same one-line intent from omarchybot. Happy either way — just please pick one so we don't double-merge. Dismiss-on-plug is separate (#14825 / #14739 / #14639).

isDischarging no longer reads onBattery, so taking it only suggests the warning still depends on it. Both callers are in Service.qml. The tests now also cover a missing battery and a charging battery staying silent, folded in from omacom#15009.

Co-Authored-By: Omarchybot <317366263+omarchybot@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@omarchybot omarchybot added the ready Good to merge label Oct 17, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

I pushed one commit to this branch, 62d4f52, carrying over what #15009 added on top of the same fix. #15009 is a newer pull request for #10939 that makes the same change. This one is older and handles every case it does, so this is the one to take.

What changed. isDischarging no longer reads onBattery, so the commit removes that parameter from isDischarging and shouldWarnLowBattery in BatteryModel.js and from both callers in Service.qml. It also adds a two-line comment saying why the warning reads the battery's own state. In battery-test.sh it adds assertions that a missing battery is not counted as discharging and that a charging battery at 8% does not warn. The docked assertions stay, written for the new signature. Behaviour is the same as at 16bb60d.

Tested on a disposable Omarchy worker, using this head merged with current quattro (33783b1), with the shell relaunched from that tree. I fed the model the UPower values from #10939: OnBattery false and the battery Discharging at 8%. On quattro it sends no warning, and here it does. On both, an unplugged battery at 8% warns, and a charging battery or a missing one does not. battery-test.sh fails when BatteryModel.js is swapped for quattro's. ./test/cli passed, battery-test.sh 18 of 18, power-test.sh 26 of 26 and video-background-test.sh 34 of 34. The shell's log shows no battery or QML errors.

Second opinion. Codex Medium reviewed the head and found nothing to change. It confirmed that no caller on this branch or on quattro still passes the old argument order. It also confirmed that a battery held at a charge limit reports PendingCharge or FullyCharged, so it still does not warn. Its independence is not guaranteed. It agreed that the one new exposure is hardware wrongly reporting Discharging below 10% while on AC, as noted in my earlier comment. #9883 keeps the latch set in that case.

For the maintainer, other open pull requests on this model:

Labelled verified and ready, at 62d4f52. Nothing is needed from the author. Waiting on the maintainer.

@dhh
dhh merged commit a91badb into omacom:quattro Nov 21, 2026
2 checks passed
dhh added a commit that referenced this pull request Nov 21, 2026
…tery (#19689)

#19293 added tests that still passed UPower's onBattery, which #11161 had
already removed from shouldWarnLowBattery, so every argument landed one slot
over and the AC-flap test failed. Plugging in and out now shows as the
battery's own state, so the flap test alternates it between discharging and
charging. The test still fails on the model from before #19293.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@emirb emirb added this to the 4.6.0 milestone Dec 1, 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 ready Good to merge verified Omarchy Triage has verified that this issue is ready for final review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Low-battery warning never fires while docked: onBattery guard suppresses it when a dock supplies power but the battery still drains

6 participants