Skip to main content

A Python module written in rust for battery information

Project description

batteryinfo

This project provides Python bindings for the Rust battery crate, allowing interaction with system batteries, including retrieving information about battery status, capacity, temperature, and more.

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.

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.3-pp311-pypy311_pp73-win_amd64.whl (668.9 kB view details)

Uploaded PyPyWindows x86-64

batteryinfo-0.1.3-pp310-pypy310_pp73-win_amd64.whl (668.8 kB view details)

Uploaded PyPyWindows x86-64

batteryinfo-0.1.3-pp39-pypy39_pp73-win_amd64.whl (669.8 kB view details)

Uploaded PyPyWindows x86-64

batteryinfo-0.1.3-cp313-cp313t-win_amd64.whl (676.0 kB view details)

Uploaded CPython 3.13tWindows x86-64

batteryinfo-0.1.3-cp310-abi3-win_amd64.whl (670.6 kB view details)

Uploaded CPython 3.10+Windows x86-64

batteryinfo-0.1.3-cp310-abi3-manylinux_2_34_x86_64.whl (338.4 kB view details)

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

File details

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

File metadata

File hashes

Hashes for batteryinfo-0.1.3-pp311-pypy311_pp73-win_amd64.whl
Algorithm Hash digest
SHA256 7f32a34e164fca32b80ad94d17dfbefba9173a9d687b12da6dc0aac6fa54e5e1
MD5 35108dddd11c03c07e25c32e007b2be8
BLAKE2b-256 7fa0d53439d3bb1361207a3b96847f89dc0efde889c7e94ce090acdc84334ba7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for batteryinfo-0.1.3-pp310-pypy310_pp73-win_amd64.whl
Algorithm Hash digest
SHA256 b593f5a365a9ffcf34bd33b58a9b087c5d1f5ddf0c0999e8b74978d9701e1e71
MD5 7aa495c7aa9c476da5c901e7cb6b3a52
BLAKE2b-256 439ad5a516cbee5a6f0290bf75a4cf829b9d63a94f50639374b357871739dda2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for batteryinfo-0.1.3-pp39-pypy39_pp73-win_amd64.whl
Algorithm Hash digest
SHA256 4a72d78bff0144caa5c10ab09690842417b043bd142e76e69a731e42301377c1
MD5 7e5e95807ff15729a4d1422b8f6548b2
BLAKE2b-256 5a253bd2acef66c572ebfe96d2fdbae9cff94ae2df19100da71f81d60eaa376c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for batteryinfo-0.1.3-cp313-cp313t-win_amd64.whl
Algorithm Hash digest
SHA256 a670c9195c997df05c2b69f2b967f8466019134a4aa8e87de073520007f4dd56
MD5 39e3a9893f4d67efae9219d7ef412237
BLAKE2b-256 c2f31fe69214300dc4f3f1a7c247e9471b2e07597812f5d70e2e326937228b6f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: batteryinfo-0.1.3-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 670.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.3-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 de707e19e9c439ef7f8d4196680424bc0b1f6c9cb7b5552af556c999d69e0068
MD5 df74acf90fb7a1b611ee7633825b1e06
BLAKE2b-256 30f2b71a6e2ad9bb542e518a9128e31cd7e92b35e6bf8f5519e7555e42fed52a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for batteryinfo-0.1.3-cp310-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 2ff561d8d8d9162aba7c42c4aa1517986d010cd50806bcb83d910d464ec48210
MD5 8191b2d04df79286ccf485f32de41f47
BLAKE2b-256 0163414832ab47844fd6c203b03db1e75b73cb360a729cfc59888654294a1863

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