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_life()
inf

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

>>> lbs.is_plugged_in()
False
>>> lbs.battery_life()
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_life() A float of the minutes of runtime left, or float('inf') while plugged in.
is_charging() True if the battery is actively charging, otherwise False.
battery_present() True if a battery is installed, otherwise False.

battery_life() returns float('inf') whenever the computer is plugged in, because the runtime isn't limited by the battery then. 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() and battery_life() return None when a battery is installed but the operating system reports the value as unknown. This is common for the first minute or two after unplugging, while the OS is still working out a time estimate, so check battery_life() for None before doing arithmetic on it.

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.1.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.1.0
File Size Uploaded
laptop_battery_status-0.1.0.tar.gz 13.0 kB Details

Built distribution (wheel)

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

Total release size: 26.9 kB

Release files / laptop_battery_status-0.1.0.tar.gz

Download URL laptop_battery_status-0.1.0.tar.gz
Size 13.0 kB
Tags Source
SHA-256 checksum
How to use checksums
504f126be293258d8d9f22047470082b6439a755b6ab9d56df9c64c481058417
BLAKE2b-256 checksum
How to use checksums
f090d4e48936c8312aa18efde87ef38b7489846868dfd2f9bf3b2a3cb275840d
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.1.0-py3-none-any.whl

Download URL laptop_battery_status-0.1.0-py3-none-any.whl
Size 13.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ceab66538c437d6fd1e9ba62e46bd11f0662278b2192407ffe768b114c3c3b9a
BLAKE2b-256 checksum
How to use checksums
aa8adb2ed8b12b486830a7a36b8e897ea4b6b8eac389665dd92e763269fa18db
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

0.2.0

2 release files

This release

0.1.0 This release

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