Skip to main content

bmx-ble

Python library for BMx battery monitor BLE advertisements and GATT notifications. The initial release supports BM2; the package name allows additional BM models to be supported later.

It decodes legacy percentage advertisements and encrypted enhanced voltage and percentage advertisements, validates the BM2 notification characteristic, and reads the BM2 over an active GATT connection. Nothing is cached: each advertisement returns its own reading, and each active read returns a fresh GATT reading or raises.

Installation

python -m pip install bmx-ble

Use

from bmx_ble import BM2Protocol

monitor = BM2Protocol()
advert = monitor.process_advertisement(
    service_info.manufacturer_data, raw=service_info.raw
)  # BM2Reading for this advertisement, or None.
reading = await monitor.async_poll(ble_device)
print(reading.voltage, reading.percentage, reading.status)

async_poll() needs a connectable bleak.backends.device.BLEDevice. Connection failures and notification timeouts propagate. While it runs, monitor.ignore_advertisement is true, so a scheduler can decline to queue another read for advertisements received during the connection.

Pass the current raw Bluetooth advertisement when available: scanner manufacturer data may contain records accumulated across many packets. Raw fields are parsed by their Bluetooth AD lengths, and only current manufacturer records are decoded. Without raw bytes, dictionaries with multiple records are ambiguous: Enhanced records are ignored, and only the Legacy record (whose manufacturer ID never changes, so its value is current) is decoded.

monitor.bm2_generation records the most capable advertisement format seen: it moves from UNKNOWN to LEGACY or ENHANCED and never downgrades, so a legacy packet from an Enhanced BM2 still returns only its own percentage.

Deciding when to connect

Legacy advertisements carry percentage only; Enhanced advertisements carry voltage and percentage. Neither carries status. With a chosen chemistry, percentage and status are derived from voltage. needs_active_read() returns whether a GATT connection is needed for data the advertisements cannot give:

from bmx_ble.battery import needs_active_read

if needs_active_read(monitor.bm2_generation, profile):
    reading = await monitor.async_poll(ble_device)
Generation Automatic chemistry Chosen chemistry
Unknown connect connect
Legacy connect (voltage, status) connect (voltage)
Enhanced connect (status) never connect

Development

python -m pip install -e '.[test]'
python -m pytest
python -m build

The Home Assistant integration is kept separately from this library.

Publishing

Create a public repository with this source and an enabled issue tracker. Set up PyPI Trusted Publishing for the repository's release workflow, tag a release matching the version in pyproject.toml, then run the publish workflow. Confirm the project name is available on PyPI before the first release.

License

MIT; see LICENSE.

Battery chemistry interpretation

Protocol readings remain available as BM2Reading. Battery calculations live in bmx_ble.battery and have no NumPy dependency:

from bmx_ble.battery import Battery, get_battery_profile, interpret_reading

profile = get_battery_profile(Battery.leadacid)
raw_reading = await monitor.async_poll(ble_device)
reading = interpret_reading(raw_reading, profile)
print(reading.voltage, reading.percentage, reading.status)

get_battery_profile() accepts Battery enum members and their stable string values, such as "automatic", "leadacid" and "lifepo4". Store these identifiers in configuration as display labels can change independently and labels such as "Lead-acid" are not accepted as identifiers. Custom batteries use custom_battery_profile() and explicit thresholds that you define.

Profiles and their curves are immutable. Automatic (via BM2) preserves the battery monitor's reported percentage and status instead of applying a voltage curve. Statuses are BatteryStatus members: critical, low, normal, charging, floating and unknown. BatteryStatus is a StrEnum, so each member also compares equal to its string value. unknown means the monitor reported a status code that isn't recognised; it does not imply charging. A status of None means the reading carried no status at all. Status codes 4 (charging) and 8 (floating) set the interpreted charging flag.

Use custom_battery_profile() for a custom curve:

from bmx_ble.battery import custom_battery_profile

profile = custom_battery_profile(
    battery_chemistry="My battery",
    critical_voltage=11.0,
    low_voltage=11.5,
    fifty_percent_voltage=12.3,
    hundred_percent_voltage=12.8,
    floating_voltage=13.5,
    charging_voltage=14.4,
)

All six custom thresholds must be finite and strictly increasing; invalid profiles raise BatteryConfigurationError. The four discharge points map to 0, 20, 50 and 100 percent. Linear interpolation clamps at the endpoints and truncates fractional percentages, matching the integration's previous behaviour.

interpret_reading() returns BatteryReading with battery_chemistry, voltage, percentage, status and charging. Missing protocol values remain missing so consumers can preserve their last readings. In automatic mode, advertisement readings without a status do not invent one. For a configured curve, a reading with both voltage and percentage gets a calculated percentage and status. The input BM2Reading is not modified.

Metadata

Release files for bmx-ble 0.4.1

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

Source distribution (sdist)

Source distribution for bmx-ble 0.4.1
File Size Uploaded Provenance Actions
bmx_ble-0.4.1.tar.gz 18.0 kB With provenance Details for bmx_ble-0.4.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for bmx-ble 0.4.1
File Interpreter ABI Platform Actions
bmx_ble-0.4.1-py3-none-any.whl Python 3 none any Details for bmx_ble-0.4.1-py3-none-any.whl

Total release size: 29.8 kB

Release files / bmx_ble-0.4.1.tar.gz

Download URL bmx_ble-0.4.1.tar.gz
Size 18.0 kB
Tags Source
SHA-256 checksum
How to use checksums
7546929c7c7442f5990b84e1aa1ce7e21d766668b0ba3267e399c354b998d820
BLAKE2b-256 checksum
How to use checksums
855493f67c500b8c49d6c9a831b606d3b7318bdff983d51844ccd715eba3cca8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 11, 2026.

Transparency log

Release files / bmx_ble-0.4.1-py3-none-any.whl

Download URL bmx_ble-0.4.1-py3-none-any.whl
Size 11.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1a4d9d43997d2ba5099446b766aa528b4e31b71c5bff7d11b216fd7db16dfb94
BLAKE2b-256 checksum
How to use checksums
64843bd2773e75f6e5f754ea74b5a56c2ddf78e0c926fe25ce80523bfd247ed8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 11, 2026.

Transparency log

Security for bmx-ble 0.4.1

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for bmx_ble-0.4.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 11, 2026.

Transparency log

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