Bluetooth-Devices / bthome-ble

Parser for BTHome BLE devices
https://bthome.io/
MIT License
69 stars 14 forks source link

Can bthome-ble be used as a standalone library? #133

Closed conorpwr closed 2 months ago

conorpwr commented 2 months ago

Is your feature request related to a problem? Please describe. I have a few ATC thermometers I'd like to start graphing/recording the data but without going to the effort of setting up a full blown HomeAssistant set up. I've tried figure out this library on how to parse data but I appear to be missing something obvious about how to retrieve the data after it's parsed (or if its possible as is).

Describe the solution you'd like Some straight forward sample code on how to take a BLE advertisement from bleak and get the parsed output back assuming this is possible using this library

Additional context I've tried mock up my own attempt based on some of the tests and with liberal print() debugging, it appears to be parsing things but on the flip side, I don't appear to be able to get parsed/clean results back. I can see the raw bytes in various places.

Sample code attached:

import asyncio
import time
from bleak import BleakScanner
from bthome_ble import BTHomeBluetoothDeviceData
from home_assistant_bluetooth import BluetoothServiceInfoBleak

known_device = {'my:mac:add:ress': 'location1'}

testdev = BTHomeBluetoothDeviceData()

async def get_adv():
        result = await BleakScanner.discover(timeout=10, return_adv=True)
        for dev, adv in result.values():
                if dev.address in known_device:
                        logging.warn('here is an match')
                        print(adv)
                        advertisement = BluetoothServiceInfoBleak (
                                name=dev.name,
                                address=dev.address,
                                rssi=adv.rssi,
                                manufacturer_data={},
                                service_data=adv.service_data,
                                service_uuids=adv.service_uuids,
                                source=None,
                                device=None,
                                advertisement=None,
                                connectable=False,
                                time=time.time(),
                        )

                        testdev.update(advertisement)

        print(testdev)

asyncio.run(get_adv())

Some sample data from me trying to play with this library in interactive mode

>>> print(testdev.last_service_info.as_dict())
{'name': 'device-name', 'address': 'mac:addr', 'rssi': -75, 'manufacturer_data': {}, 'service_data': {'0000fcd2-0000-1000-8000-00805f9b34fb': b'@\x00\xbd\x01d\x02w\x0b\x03\xd6\r'}, 'service_uuids': [], 'source': None, 'advertisement': None, 'device': None, 'connectable': False, 'time': 1720397501.9690812}
>>> print(testdev.last_service_info.time)
1720397501.9690812
>>> print(testdev.last_service_info.rssi)
-75
>>> print(testdev.last_service_info.service_data)
{'0000fcd2-0000-1000-8000-00805f9b34fb': b'@\x00\xbd\x01d\x02w\x0b\x03\xd6\r'}
>>> print(testdev.last_service_info.service_uuids)
[]
Ernst79 commented 2 months ago

It might not be easily possible with this library, as it is written to update HA sensors. So you will need to rewrite the part that updates the HA sensors, to something else. For BLE monitor, which is a custom component for HA, I have also rewritten this library a little bit. Perhaps that parser can help you out.

https://github.com/custom-components/ble_monitor/blob/master/custom_components/ble_monitor/ble_parser/bthome.py

conorpwr commented 2 months ago

Well it's somewhat of a relief to know I didn't miss something completely obvious then!

Thanks for the link to the other parser, I'll try experiment with that code for a bit and see what I can come up with. If I make something suitably generic (unlikely fwiw) I'll send a pull request.

Appreciate the quick response!

conorpwr commented 2 months ago

If anyone is digging through issues like me, here is some sample code (https://github.com/conorpwr/homebtble-to-prometheus) to collect and parse BTHomeV2 announcements only based on Ernst79's https://github.com/custom-components/ble_monitor/