Skip to main content

Asynchronous Python library to query battery management systems via Bluetooth Low Energy.

Project description

GitHub Release Python Version License

Aiobmsble

Requires Python 3 and uses asyncio and Bleak

Asynchronous Library to Query Battery Management Systems via Bluetooth LE

This library is intended to query data from battery management systems that use Bluetooth LE. This library can be used stand-alone in any Python environment (with necessary dependencies installed). It is developed to support BMS_BLE-HA integration that was written to make BMS data available to Home Assistant, but can be hopefully usefull for other use-cases as well.

Features

  • Support for autodetecting compatible BLE BMSs
  • Automatic detection of compatible BLE write mode
  • Asynchronous operation using asyncio
  • Any number of batteries in parallel
  • 100% test coverage plus fuzz tests for BLE data

[!CAUTION] This library shall not be used for safety relevant operations! The correctness or availability of data cannot be guaranteed (see warranty section of the license), since the implementation is mostly based on openly available information or non-validated vendor specifications. Further, issues with the Bluetooth connection, e.g. disturbances, can lead to unavailable or incorrect values.

Do not rely on the values to control actions that prevent battery damage, overheating (fire), or similar.

Supported Devices

The list of supported devices is maintained in the repository of the related Home Assistant integration.

Usage

In order to identify all devices that are reachable and supported by the library, simply run

aiobmsble

from the command line after installation.

From your Python code

In case you need a reference to include the code into your library, please see __main__.py.

From a Script

This example can also be found as an example in the respective folder.

"""Example of using the aiobmsble library to find a BLE device by name and print its sensor data.

Project: aiobmsble, https://pypi.org/p/aiobmsble/
License: Apache-2.0, http://www.apache.org/licenses/
"""

import asyncio
import logging
from typing import Final

from bleak import BleakScanner
from bleak.backends.device import BLEDevice
from bleak.exc import BleakError

from aiobmsble import BMSSample
from aiobmsble.bms.dummy_bms import BMS  # TODO: use the right BMS class for your device

NAME: Final[str] = "BT Device Name"  # TODO: replace with the name of your BLE device

# Configure logging
logging.basicConfig(level=logging.INFO)
logger: logging.Logger = logging.getLogger(__name__)


async def main(dev_name) -> None:
    """Find a BLE device by name and update its sensor data."""

    device: BLEDevice | None = await BleakScanner.find_device_by_name(dev_name)
    if device is None:
        logger.error("Device '%s' not found.", dev_name)
        return

    logger.info("Found device: %s (%s)", device.name, device.address)
    try:
        async with BMS(ble_device=device) as bms:
            logger.info("Updating BMS data...")
            data: BMSSample = await bms.async_update()
            logger.info("BMS data: %s", repr(data).replace(", ", ",\n\t"))
    except BleakError as ex:
        logger.error("Failed to update BMS: %s", type(ex).__name__)


if __name__ == "__main__":
    asyncio.run(main(NAME))  # pragma: no cover

Testing

For integration tests (using pytest) the library provides advertisement data that can be used to verify detection of BMSs. For your tests you can use

from aiobmsble.test_data import bms_advertisements

def test_advertisements() -> None:
    """Run some tests with the advertisements"""
    for advertisement, bms_type, _comments in bms_advertisements():
        ...

Installation

Install python and pip if you have not already, then run:

pip3 install pip --upgrade
pip3 install wheel

For Production:

pip3 install aiobmsble

This will install the latest library release and all of it's python dependencies.

For Development:

git clone https://github.com/patman15/aiobmsble.git
cd aiobmsble
pip3 install -e .[dev]

This gives you the latest library code from the main branch.

Troubleshooting

In case you have problems with the library, please enable debug logging. You can also run aiobmsble -v from the command line in order to query all known BMS that are reachable.

In case you have troubles you'd like to have help with

  • please record a debug log using aiobmsble -v -l debug.log,
  • open an issue with a good description of what your question/issue is and attach the log, or
  • open a bug if you think the behaviour you see is misbehaviour of the library, including a good description of what happened, your expectations,
  • and put the debug.log as attachment to the issue.

Thanks to

@gkathan, @downset, @gerritb, @Goaheadz, @alros100, @majonessyltetoy, @snipah, @Gruni22, @azisto, @BikeAtor, @Karatzie, @PG248, @SkeLLLa,@romanshypovskyi, @riogrande75, @ebagnoli, @andreas-bulling, @goblinmaks, @andreitoma-github, @hacsler, @ViPeR5000, @edelstahlratte, @nezra, @Fandu21, @rubenclark74, @geierwally1978, @Tulexcorp, @oliviercommelarbre, @shaf, @gavrilov, @SOLAR-RAIDER, @prodisz, @thecodingmax, @daubman, @krahabb, @ardeus-ua

for helping with making the library better.

References

Project details


Download files

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

Source Distribution

aiobmsble-0.10.0.tar.gz (73.4 kB view details)

Uploaded Source

Built Distribution

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

aiobmsble-0.10.0-py3-none-any.whl (107.6 kB view details)

Uploaded Python 3

File details

Details for the file aiobmsble-0.10.0.tar.gz.

File metadata

  • Download URL: aiobmsble-0.10.0.tar.gz
  • Upload date:
  • Size: 73.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for aiobmsble-0.10.0.tar.gz
Algorithm Hash digest
SHA256 ae468d19de75e5a33c4cce7bc54f90220fc4927da9d9b0f6aec396c4c7372722
MD5 827bd380a5a4ead83e106ec8dac8a1df
BLAKE2b-256 798bf128c9aca9cb4f1de9792712c870ddd51f0a0f116ef2908b8cfc15e4448f

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiobmsble-0.10.0.tar.gz:

Publisher: python-publish.yml on patman15/aiobmsble

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file aiobmsble-0.10.0-py3-none-any.whl.

File metadata

  • Download URL: aiobmsble-0.10.0-py3-none-any.whl
  • Upload date:
  • Size: 107.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for aiobmsble-0.10.0-py3-none-any.whl
Algorithm Hash digest
SHA256 043d0fac33a71a92cdbcce5837af4801011b4c7e03e999159ab97dc11e7f77ff
MD5 2b7ffbe20c3a849620ca38b948755fcd
BLAKE2b-256 03a4ba10b8554ad1e2b395f3f7ec5b3ce9b1ee1468c775ec9b331636a463c43b

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiobmsble-0.10.0-py3-none-any.whl:

Publisher: python-publish.yml on patman15/aiobmsble

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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