Skip to main content

batteryinfo

This project provides cross-platform Python bindings specifically designed to access system battery information. Using the Rust battery crate as its foundation, it allows you to retrieve battery status, percent full, capacity, and more, regardless of the operating system.

Setup

Python

Install the latest version with

pip install batteryinfo

Usage

Here are some examples of how to use batteryinfo in Python:

Importing the module

import batteryinfo

Creating a Battery object

battery = batteryinfo.Battery()

Other options

# Create an instance of Battery with specific time format and temperature unit
battery = batteryinfo.Battery(index=0, time_format=batteryinfo.TimeFormat.Human, temp_unit=batteryinfo.TempUnit.DegC)

# Create an instance of Battery with specific time format, temperature unit, and refresh interval
battery = batteryinfo.Battery(time_format=batteryinfo.TimeFormat.Human, temp_unit=batteryinfo.TempUnit.DegF, refresh_interval=600)

Accessing Battery properties

print(f"Vendor: {battery.vendor}")
print(f"Model: {battery.model}")
print(f"Serial Number: {battery.serial_number}")
print(f"Technology: {battery.technology}")
print(f"Percent Full: {battery.percent}")
print(f"State: {battery.state}")
print(f"Capacity: {battery.capacity}")
print(f"Temperature: {battery.temperature if battery.temperature else 'N/A'}")
print(f"Cycle Count: {battery.cycle_count}")
print(f"Energy: {battery.energy}")
print(f"Energy Full: {battery.energy_full}")
print(f"Energy Full Design: {battery.energy_full_design}")
print(f"Energy Rate: {battery.energy_rate}")
print(f"Voltage: {battery.voltage}")
print(f"Time to Empty: {battery.time_to_empty}")
print(f"Time to Full: {battery.time_to_full}")

Available Properties

The following properties are available on the Battery object:

  • vendor: The vendor of the battery (optional).
  • model: The model of the battery (optional).
  • serial_number: The serial number of the battery (optional).
  • technology: The technology of the battery.
  • percent: The percentage of the battery that is full (as a Measurement object).
  • state: The state of the battery (Charging, Discharging, Full, Empty, Unknown).
  • capacity: The capacity of the battery (as a Measurement object).
  • temperature: The temperature of the battery (as a Measurement object).
  • cycle_count: The cycle count of the battery.
  • energy: The current energy of the battery (as a Measurement object).
  • energy_full: The full energy of the battery (as a Measurement object).
  • energy_full_design: The design energy of the battery (as a Measurement object).
  • energy_rate: The energy rate of the battery (as a Measurement object).
  • voltage: The voltage of the battery (as a Measurement object).
  • time_to_empty: The time to empty the battery.
  • time_to_full: The time to fully charge the battery.

Battery Constructor Parameters

The Battery constructor accepts the following parameters:

  • index (optional): The index of the battery to interact with. Default is 0. An index of 1 is used if you have a second battery in your system.
  • time_format (optional): The format to display time. Possible values are:
    • TimeFormat.Seconds: Display time in seconds.
    • TimeFormat.Minutes: Display time in minutes.
    • TimeFormat.Human: Display time in a human-readable format. For example, 1h,25m,52s. (Default)
  • temp_unit (optional): The unit to display temperature. Possible values are:
    • TempUnit.DegC: Display temperature in degrees Celsius.
    • TempUnit.DegF: Display temperature in degrees Fahrenheit. (Default)
  • refresh_interval (optional): The interval in milliseconds to refresh battery information. Default is 500 milliseconds.

Setting the Refresh Interval

The refresh_interval controls how frequently the battery data is updated, defaulting to 500 milliseconds. If needed, you can configure this parameter during object creation or later. It's only relevant for applications that repeatedly query battery information, as a single-use instance won't benefit from it.

Note: The 500ms refresh_interval (or value you have requested) acts as a cache timeout. When you request battery data like voltage, the system updates the values only if they're older than 500ms. If they're more recent, the cached values are returned.

Example usage:

# Passing refresh_interval in the constructor
battery = batteryinfo.Battery(refresh_interval=1000)

# Setting refresh_interval after creating the Battery object
battery.refresh_interval = 1000

There is also an option to manually refresh the battery information, but the refresh_interval (and likely the default value of 500 ms) will accomplish the goal well in most situations.

battery.refresh()

Measurement Object

The Measurement object has the following properties and methods:

  • value: The value of the measurement.
  • units: The units of the measurement.

Example usage:

# This first option provides the value and the units together.
print(f"Percent full: {battery.percent}")
# Provides the numeric value on its own so it can be used for comparisons and calculations.
print(f"Percent full raw value: {battery.percent.value}")
# Provides the units of measure such "%" or "V" for volts.
print(f"Percent full units: {battery.percent.units}")

Output

Percent full: 88.2%
Percent full raw value: 88.2
Percent full units: %

Enums

The following enums are available:

TimeFormat

  • Seconds: Display time in seconds.
  • Minutes: Display time in minutes.
  • Human: Display time in a human-readable format.

TempUnit

  • DegC: Display temperature in degrees Celsius.
  • DegF: Display temperature in degrees Fahrenheit.

Using the as_dict Method

The as_dict method returns all battery information as a Python dictionary. For fields represented by Measurement objects, the method returns a tuple (value, units).

# Get all battery information as a dictionary
battery_info = battery.as_dict()

# Print the dictionary
print(battery_info)

# Example output:
# {
#     "vendor": "BatteryVendor",
#     "model": "BatteryModel",
#     "serial_number": "123456789",
#     "technology": "Li-ion",
#     "percent": (71.1, "%"),
#     "state": "Charging",
#     "capacity": (95.0, "%"),
#     "temperature": (86.2, "°F"),
#     "cycle_count": 300,
#     "energy": (50.0, "Wh"),
#     "energy_full": (60.0, "Wh"),
#     "energy_full_design": (65.0, "Wh"),
#     "energy_rate": (10.0, "W"),
#     "voltage": (12.5, "V"),
#     "time_to_empty": None,
#     "time_to_full": "1h,5m,19s",
#     "battery_index": 0
# }

# Access specific fields
print("Vendor:", battery_info.get("vendor"))
print("Percent:", battery_info.get("percent"))  # Example: (71.1, "%")
print("Energy:", battery_info.get("energy"))    # Example: (50.0, "Wh")

Python Example - Displaying Battery Information Based on State

battery = batteryinfo.Battery()

state = battery.state
percent = f"{battery.percent.value}%"
if state == "Charging":
    time_to_full = battery.time_to_full
    print(f"Battery: {percent} (⇡ charging - full in {time_to_full})")
elif state == "Discharging":
    time_to_empty = battery.time_to_empty
    print(f"Battery: {percent} (⇣ discharging - empty in {time_to_empty})")
elif state == "Full":
    print(f"Battery: {percent} (✓ full)")
else:
    print(f"Battery: {percent} (state: {state})")

Example output:

Battery: 70.4% (⇣ discharging - empty in 2h,40m,38s)

License

This project is licensed under the MIT License.

Metadata

Release files for batteryinfo 0.1.5

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

Built distributions (wheels)

Table of built distributions (wheels) for batteryinfo 0.1.5
File Actions
batteryinfo-0.1.5-pp311-pypy311_pp73-win_amd64.whl PyPy 3.11 PyPy 3.11 7.3 Windows x86-64 Details for batteryinfo-0.1.5-pp311-pypy311_pp73-win_amd64.whl
batteryinfo-0.1.5-pp310-pypy310_pp73-win_amd64.whl PyPy 3.10 PyPy 3.10 7.3 Windows x86-64 Details for batteryinfo-0.1.5-pp310-pypy310_pp73-win_amd64.whl
batteryinfo-0.1.5-pp39-pypy39_pp73-win_amd64.whl PyPy 3.9 PyPy 3.9 7.3 Windows x86-64 Details for batteryinfo-0.1.5-pp39-pypy39_pp73-win_amd64.whl
batteryinfo-0.1.5-cp313-cp313t-win_amd64.whl CPython 3.13 CPython 3.13 free-threading Windows x86-64 Details for batteryinfo-0.1.5-cp313-cp313t-win_amd64.whl
batteryinfo-0.1.5-cp310-abi3-win_amd64.whl CPython 3.10 abi3 Windows x86-64 Details for batteryinfo-0.1.5-cp310-abi3-win_amd64.whl
batteryinfo-0.1.5-cp310-abi3-manylinux_2_34_x86_64.whl CPython 3.10 abi3 Linux glibc 2.34+ x86-64 Details for batteryinfo-0.1.5-cp310-abi3-manylinux_2_34_x86_64.whl

Total release size: 3.7 MB

Release files / batteryinfo-0.1.5-pp311-pypy311_pp73-win_amd64.whl

Download URL batteryinfo-0.1.5-pp311-pypy311_pp73-win_amd64.whl
Size 673.6 kB
Tags PyPy 3.11 PyPy 3.11 7.3 Windows x86-64
SHA-256 checksum
How to use checksums
4b695abf391524fd81bdc6b0ef477a791462c5227169d87aaf294f2b704bd274
BLAKE2b-256 checksum
How to use checksums
f7e52ab95c3b2c0399f525a3d757b2ac71bd8f8d5142b5079ddb13f00ea6ef4c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.3

Release files / batteryinfo-0.1.5-pp310-pypy310_pp73-win_amd64.whl

Download URL batteryinfo-0.1.5-pp310-pypy310_pp73-win_amd64.whl
Size 672.9 kB
Tags PyPy 3.10 PyPy 3.10 7.3 Windows x86-64
SHA-256 checksum
How to use checksums
a2f38aaad7108344bb1f805238f4bf80d8a72824823ced34ab7cbec371015a19
BLAKE2b-256 checksum
How to use checksums
7560149b6f0f80be5045596bfa5afa0334d279be092acfb3906a5dce063c3cdc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.3

Release files / batteryinfo-0.1.5-pp39-pypy39_pp73-win_amd64.whl

Download URL batteryinfo-0.1.5-pp39-pypy39_pp73-win_amd64.whl
Size 674.1 kB
Tags PyPy 3.9 PyPy 3.9 7.3 Windows x86-64
SHA-256 checksum
How to use checksums
ff99b3089e062869c18d1b1ef9b84eb8aca93d5d32ffdcd50405b203a64b4412
BLAKE2b-256 checksum
How to use checksums
dcaa34ab27e6ba0ed79e813fd6d11d78a84e94df268b75cc39372b20c9839073
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.3

Release files / batteryinfo-0.1.5-cp313-cp313t-win_amd64.whl

Download URL batteryinfo-0.1.5-cp313-cp313t-win_amd64.whl
Size 679.8 kB
Tags CPython 3.13 CPython 3.13 free-threading Windows x86-64
SHA-256 checksum
How to use checksums
2253c3e8df4ca55f47c6564c5282a0e95cd16b86561a7c101432c07b9b29f87b
BLAKE2b-256 checksum
How to use checksums
96375689a371bb9841bc0ed617c5bab4a8424b8d00b4386fc5b03bd2d64a30f2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.3

Release files / batteryinfo-0.1.5-cp310-abi3-win_amd64.whl

Download URL batteryinfo-0.1.5-cp310-abi3-win_amd64.whl
Size 674.4 kB
Tags CPython 3.10 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
7711376eeaa8b26d0daec16419f2c5780671e330c9561915e3397b6a3924006c
BLAKE2b-256 checksum
How to use checksums
c4c7283a15d4173fdd0dcf284c863dbb3e0244c0dc66162e072cd6837e7bc7f7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.3

Release files / batteryinfo-0.1.5-cp310-abi3-manylinux_2_34_x86_64.whl

Download URL batteryinfo-0.1.5-cp310-abi3-manylinux_2_34_x86_64.whl
Size 343.7 kB
Tags CPython 3.10 Linux glibc 2.34+ x86-64 abi3
SHA-256 checksum
How to use checksums
c05540677334c01de6c8aa4e8981410b02ed327630719c57bd9c3ed561b2f686
BLAKE2b-256 checksum
How to use checksums
99c9466782bc2a1d86dcd741098422b94892e2d43399c1dfec56b0c38fb04912
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.3

Security for batteryinfo 0.1.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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