Skip to main content

A Python module written in rust for battery information

Project description

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.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

batteryinfo-0.1.5-pp311-pypy311_pp73-win_amd64.whl (673.6 kB view details)

Uploaded PyPyWindows x86-64

batteryinfo-0.1.5-pp310-pypy310_pp73-win_amd64.whl (672.9 kB view details)

Uploaded PyPyWindows x86-64

batteryinfo-0.1.5-pp39-pypy39_pp73-win_amd64.whl (674.1 kB view details)

Uploaded PyPyWindows x86-64

batteryinfo-0.1.5-cp313-cp313t-win_amd64.whl (679.8 kB view details)

Uploaded CPython 3.13tWindows x86-64

batteryinfo-0.1.5-cp310-abi3-win_amd64.whl (674.4 kB view details)

Uploaded CPython 3.10+Windows x86-64

batteryinfo-0.1.5-cp310-abi3-manylinux_2_34_x86_64.whl (343.7 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.34+ x86-64

File details

Details for the file batteryinfo-0.1.5-pp311-pypy311_pp73-win_amd64.whl.

File metadata

File hashes

Hashes for batteryinfo-0.1.5-pp311-pypy311_pp73-win_amd64.whl
Algorithm Hash digest
SHA256 4b695abf391524fd81bdc6b0ef477a791462c5227169d87aaf294f2b704bd274
MD5 71df142e0fae614f3b65b9be3d2b194e
BLAKE2b-256 f7e52ab95c3b2c0399f525a3d757b2ac71bd8f8d5142b5079ddb13f00ea6ef4c

See more details on using hashes here.

File details

Details for the file batteryinfo-0.1.5-pp310-pypy310_pp73-win_amd64.whl.

File metadata

File hashes

Hashes for batteryinfo-0.1.5-pp310-pypy310_pp73-win_amd64.whl
Algorithm Hash digest
SHA256 a2f38aaad7108344bb1f805238f4bf80d8a72824823ced34ab7cbec371015a19
MD5 41f5c7914698ddd388ab7840464a5374
BLAKE2b-256 7560149b6f0f80be5045596bfa5afa0334d279be092acfb3906a5dce063c3cdc

See more details on using hashes here.

File details

Details for the file batteryinfo-0.1.5-pp39-pypy39_pp73-win_amd64.whl.

File metadata

File hashes

Hashes for batteryinfo-0.1.5-pp39-pypy39_pp73-win_amd64.whl
Algorithm Hash digest
SHA256 ff99b3089e062869c18d1b1ef9b84eb8aca93d5d32ffdcd50405b203a64b4412
MD5 1fef66943fe6244fc48657a87f1d4b99
BLAKE2b-256 dcaa34ab27e6ba0ed79e813fd6d11d78a84e94df268b75cc39372b20c9839073

See more details on using hashes here.

File details

Details for the file batteryinfo-0.1.5-cp313-cp313t-win_amd64.whl.

File metadata

File hashes

Hashes for batteryinfo-0.1.5-cp313-cp313t-win_amd64.whl
Algorithm Hash digest
SHA256 2253c3e8df4ca55f47c6564c5282a0e95cd16b86561a7c101432c07b9b29f87b
MD5 0310108d32d9058f6541dd3aa6b29f5f
BLAKE2b-256 96375689a371bb9841bc0ed617c5bab4a8424b8d00b4386fc5b03bd2d64a30f2

See more details on using hashes here.

File details

Details for the file batteryinfo-0.1.5-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: batteryinfo-0.1.5-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 674.4 kB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for batteryinfo-0.1.5-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 7711376eeaa8b26d0daec16419f2c5780671e330c9561915e3397b6a3924006c
MD5 3a82f93114de3dd7854eb7473c2be3d3
BLAKE2b-256 c4c7283a15d4173fdd0dcf284c863dbb3e0244c0dc66162e072cd6837e7bc7f7

See more details on using hashes here.

File details

Details for the file batteryinfo-0.1.5-cp310-abi3-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for batteryinfo-0.1.5-cp310-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 c05540677334c01de6c8aa4e8981410b02ed327630719c57bd9c3ed561b2f686
MD5 a556b771a1220c2271cdb4572615713a
BLAKE2b-256 99c9466782bc2a1d86dcd741098422b94892e2d43399c1dfec56b0c38fb04912

See more details on using hashes here.

Supported by

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