Skip to main content

laptop_battery_status

Reports the battery charge, the time remaining, and whether the computer is plugged in. Works on Windows, macOS, and Linux, and uses nothing but the Python standard library. Runs on Python 3.5 and later.

Installation

pip install laptop_battery_status

Quickstart

>>> import laptop_battery_status as lbs
>>> lbs.is_plugged_in()
True
>>> lbs.is_charging()
True
>>> lbs.battery_level()
87.0
>>> lbs.battery_minutes_remaining()
inf

Unplug the laptop and the same calls report the runtime left:

>>> lbs.is_plugged_in()
False
>>> lbs.battery_minutes_remaining()
272.0

Functions

Function Returns
is_plugged_in() True if running on AC power, otherwise False.
battery_level() A float from 0.0 to 100.0 for the percent of charge left.
battery_minutes_remaining() A float of the minutes of runtime left, or float('inf') while plugged in or while the estimate is unknown.
is_charging() True if the battery is actively charging, otherwise False.
battery_present() True if a battery is installed, otherwise False.

battery_minutes_remaining() returns float('inf') whenever the computer is plugged in, because the runtime isn't limited by the battery then, and also when the operating system hasn't worked out an estimate yet. Note that a plugged-in computer whose battery is already full is not charging, so is_plugged_in() can return True while is_charging() returns False.

Machines with no battery

The battery functions raise NoBatteryError on a desktop computer or in a virtual machine. Call battery_present() first if that's a case your program needs to handle:

import laptop_battery_status

if laptop_battery_status.battery_present():
    print('Battery at %s%%' % (laptop_battery_status.battery_level(),))
else:
    print('This computer runs on wall power.')

is_plugged_in() is the exception: it works fine on a desktop, where it always returns True.

Unknown values

battery_level() returns None when a battery is installed but the operating system reports the charge as unknown, so check it for None before doing arithmetic on it.

battery_minutes_remaining() returns float('inf') when the operating system hasn't worked out an estimate yet, which is common for the first minute or two after unplugging. That means float('inf') covers both "plugged in" and "not known yet"; call is_plugged_in() if your program needs to tell those two apart.

Exceptions

All exceptions subclass BatteryStatusError, which subclasses RuntimeError.

  • NoBatteryError - no battery is installed.
  • UnsupportedPlatformError - the operating system isn't Windows, macOS, or Linux.
  • BatteryStatusError - the power information couldn't be read, or the OS reported the AC power or charging state as unknown.

How it works

No third party packages are used.

  • Windows calls the Win32 GetSystemPowerStatus() function through ctypes.
  • macOS calls the IOKit IOPowerSources functions through ctypes, and falls back to parsing the output of pmset -g batt.
  • Linux reads the files under /sys/class/power_supply, and falls back to parsing the output of upower and then acpi. The kernel doesn't provide a time estimate, so the remaining minutes are calculated from the current charge and the rate it's being drawn at, which makes the number jumpier than the one in a desktop environment's battery menu.

Machines with more than one battery are reported as a single combined battery.

License

MIT

Metadata

Release files for laptop-battery-status 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for laptop-battery-status 0.2.0
File Size Uploaded
laptop_battery_status-0.2.0.tar.gz 14.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for laptop-battery-status 0.2.0
File Interpreter ABI Platform
laptop_battery_status-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 29.9 kB

Release files / laptop_battery_status-0.2.0.tar.gz

Download URL laptop_battery_status-0.2.0.tar.gz
Size 14.5 kB
Tags Source
SHA-256 checksum
How to use checksums
40ebe1425245c3a6b8108f3be2656548b221f8dbc984963b1f60941c31049974
BLAKE2b-256 checksum
How to use checksums
107273c9e2d64c606d08d8c6239e8eba7220fbd1bd7783acc8d59dfcf26025cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.8

Release files / laptop_battery_status-0.2.0-py3-none-any.whl

Download URL laptop_battery_status-0.2.0-py3-none-any.whl
Size 15.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2987cb5e1cb0a2f60f7c02537d2917932884bb1123b4062a31ace0bb7752459f
BLAKE2b-256 checksum
How to use checksums
2197cbd6b99aa7a612d31e7ba0af4cd0a40dc4cea9e40620d4e1ca7867f2040f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.8

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page