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 = 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.4-pp311-pypy311_pp73-win_amd64.whl (673.7 kB view details)

Uploaded PyPyWindows x86-64

batteryinfo-0.1.4-pp310-pypy310_pp73-win_amd64.whl (673.5 kB view details)

Uploaded PyPyWindows x86-64

batteryinfo-0.1.4-pp39-pypy39_pp73-win_amd64.whl (673.7 kB view details)

Uploaded PyPyWindows x86-64

batteryinfo-0.1.4-cp313-cp313t-win_amd64.whl (680.9 kB view details)

Uploaded CPython 3.13tWindows x86-64

batteryinfo-0.1.4-cp310-abi3-win_amd64.whl (674.6 kB view details)

Uploaded CPython 3.10+Windows x86-64

batteryinfo-0.1.4-cp310-abi3-manylinux_2_34_x86_64.whl (342.8 kB view details)

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

File details

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

File metadata

File hashes

Hashes for batteryinfo-0.1.4-pp311-pypy311_pp73-win_amd64.whl
Algorithm Hash digest
SHA256 d073db1cf0ce7f4542d9244841eebe5aef1bbc104388be8656887e4000fa0647
MD5 b4e780126dff890fd423e5db8c2283ee
BLAKE2b-256 e8f6d30524d445007705c935b9acbb9378d4666e761f605e3e51207024b94704

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for batteryinfo-0.1.4-pp310-pypy310_pp73-win_amd64.whl
Algorithm Hash digest
SHA256 63827d6949812b1b27fd9d6e9defc650777ae227b6041b895ee22f40580d7b9e
MD5 f1a0142784da4bda89cad75071081392
BLAKE2b-256 2267b8ffb643356a3c972ce5269d5fc7fe42ed024a43266968f349a21c9458f5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for batteryinfo-0.1.4-pp39-pypy39_pp73-win_amd64.whl
Algorithm Hash digest
SHA256 82c7774d68959fc9a634afc6f56dafd9c272a14c7c4712f62011162a1d372c95
MD5 d8dbb5b7bed29b9209ae869ec0e6dc3d
BLAKE2b-256 5f47a0f957b705c35c3978721bccc6e2394d048de7791e594d13a9dbd031a959

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for batteryinfo-0.1.4-cp313-cp313t-win_amd64.whl
Algorithm Hash digest
SHA256 73374c0931973c9db501572eb3396b900d73557fe6f0cee71b7f30f16fac941d
MD5 7cd0a015fad1344b6827bba4bf21d991
BLAKE2b-256 e326f5d721228f8e9e6a66b0755aa81c429ce9a57a0488b0ad8c226ce5ac3da3

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for batteryinfo-0.1.4-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 02f9dc8bf980c81ad15a09b9f3ee7a1d7381207b049f5cc857ac3322a001accb
MD5 a27e4d7e37d247730d7f63336e8c3c6a
BLAKE2b-256 25b8bf8664be6b074f921ad206a63c013401af6ef1244f6bbafb13b0a326baf4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for batteryinfo-0.1.4-cp310-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 875d0d7e9bdc3a408030d48a493ecab6489c1f59cdd5d25bdc134b476cfeb0a0
MD5 4c0cadef604ab2b0faa8937608ba4a19
BLAKE2b-256 eaf02421b5dad0b50f427428bc578be4d9b79ad52123752a3a0868a6cece35ed

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