Skip to main content

Cross-platform battery status

Project description

battery v1.0: Cross-platform battery status

The following functions are available:

  • is_discharging: Check if battery is discharging (i.e. in use).
  • is_charging: Check if battery is charging.
  • percent: Return percentage charge of battery in [0, 100].
  • minutes_to_empty: Minutes of battery life left (may be empty if not known yet).
  • minutes_to_full: Minutes until the battery is charged (may be 0 if full, or empty if not on charge or not known).
  • capacity: Battery's current capacity in mWh (mAh on macOS).
  • design_capacity: Battery's original capacity in mWh (mAh on macOS).

The following OS versions are supported:

  • macOS 10.2 and later
  • Linux 2.6.24 and later
  • Windows Vista and later

A NotImplementedError is raised for unsupported OSes.

Pull requests and issues are always welcome for better compatibility!

Usage example

Obtain with pip install battery and use like:

import battery
source = 'battery' if battery.is_discharging() else 'AC power'
print(f'On {source} at {battery.percent()}%')
print(f'Battery is at {battery.capacity() / battery.design_capacity()*100:4.1f}% health.')

The CLI always returns a JSON value:

$ python -m battery
{"is_discharging": true, "percent": 92, "minutes_to_empty": 215, "minutes_to_full": null, "capacity": 2388, "design_capacity": 4381}
$ python -m battery percent
92

Other notes

This package is public domain! Feel free to hack with it as needed.

Also check out psutil!

Note that the Python is just a wrapper around the annotated .toml of commands that give standardised output. (For maximum compatibility this is converted via make to a .json for use.)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

battery-1.0.3.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

battery-1.0.3-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file battery-1.0.3.tar.gz.

File metadata

  • Download URL: battery-1.0.3.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.1

File hashes

Hashes for battery-1.0.3.tar.gz
Algorithm Hash digest
SHA256 5eecda8c0ebf5c3402f18eb4105fb46c2f6f5bd9e0e93490a34bed5e5411f163
MD5 1d51f329edd905603f5f26f79d8805d7
BLAKE2b-256 51475c542df57bf7c8079c3e9408c05541b9c1845385ebf41e3d236c61627728

See more details on using hashes here.

File details

Details for the file battery-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: battery-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.1

File hashes

Hashes for battery-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 4cbf39ee7a85dc149aa6546f04b06457f81c481c3398b8bade2ff242d8b08d67
MD5 c073567b3bcf52a818bb33c063b11931
BLAKE2b-256 af98cc50d69b84f92a4087fca77387b8ad4d36954f1d13e043e67dfd10a54046

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page