Hi! I’ve been using ESPHome bluetooth proxies with Home Assistant and have been having great success with some custom BTHome sensors.
However I’d love to be able to write data to devices via those bluetooth proxies, particularly to make an integration that detects devices advertising the “Nordic UART” service, connects to them and writes some data.
Is there an example/tutorial somewhere of how to connect to a device via Home Assistant’s BLE API (rather than just using the OS-provided bluetooth) or is that bluetooth_le_tracker implementation probably the best reference at the moment?
Looking at that, am I right in thinking that actually Home Assistant is using Bleak itself and forwarding the calls to ESPHome, so you can write code that uses the Bleak API, and as long as you use the BLE device provided by Home Assistant, you can just use normal Bleak API calls on it?
I’m interested in what you find. Creating some type of ‘generic’ system that will allow me to send and receive ‘larger’ quantities of data with a BLE device with GATT type conversation has been on my far too long TODO list for a while.
I briefly looked at @anon63427907 's new project, however have yet to experiment with it. Both to a couple of your Q’s and from what I think is how the referenced project works and my pretty neophyte skills in BLE area. From my experience with BLE on Linux, I walk very carefully and mostly avoid trying to do any BLE communications on Linux, my red flags are: BLE advertising reading going on on the host and 2nd multiple processes trying to access the Bluetooth stack on Linux. From my experience, I’ve found that if either or both are running on a Linux machine, I don’t try to do any BLE work there. Unless I can ‘own’ the BLE adapters 100% on the machine, I don’t go there. Just too fragile to share IMHO. And a dedicated Linux box with good BLE support, even a Raspberry Pi, is kind of heavy, expensive, and for coverage will most likely require multiples.
So to my yet to start direction, using a ESP or other dedicated MCU device with BLE support and Wfi support as a satellite to my Home Assistant or other ‘controller’ is where
I think the most stable, widest coverage and cost effective route is.
I think using MQTT as the path to move the control messages and chunks of data back and forth to HA might yield the most flexibility.
Both ESPHome and Tasmota (via their Berry subsystem) seem to have available components to build these intermediary devices.
Home assistant decoupled Bluetooth hardware and software layers and my integration is working without any Bluetooth chip or dongle, having a Bluetooth proxy over an Esp would do the same.
From software perspective, integration doesn’t care if it is physical chip or a remote Bluetooth proxy. Actually it is how i developed it in my development environment using with a remote proxy.
@anon63427907 that looks great - thank you! I’ll have a go with that! Looks like I asked the question at the right time as you only posted that integration up 10 days ago
So I’m right that you just use bleak API calls for bluetooth, but under the hood it’s piper through HomeAssistant and out to ESPHome/etc?
I actually didn’t check what home assistant is doing to distinguish between Bluetooth chip vs esphome, rather followed Bluetooth integration development guidelines from home assistant developer documentation.
I didn’t test myself Bluetooth chip myself as my pi doesn’t have good coverage but I am purely relying on esp Bluetooth proxies around the home
The documentation stinks for casual coders. Best to find an integration close to what you want, then edit from there. I wrote some stupid simple ones, but honestly there is some magic I do not understand on how they work. All of mine have the parser built in and do not rely on an external PyPi package. You only need the the Pypi if making an official integration.
As a note, I basically ported other people’s work (except for the renpho scale one), so I do not really know how to figure out the BT commands. I have tried using wireshark with a nordic sniffer and never could figure it out.
It does stink writing them. The super smart devs (bdraco) will answer questions on discord, but you really need to know how to ask.
For some advice: First, write a standard python code that uses the bleak library. Once you get that written and working, it is reasonably easy to turn it into an integration starting with an existing integration. This one responds to notifications, also has the standard bleak python code in the repo: https://github.com/jdeath/rd200v2 (write to char, listen for notification)
Others I have written: https://github.com/jdeath/Opal_NuggetIce_ble (directly reads char) https://github.com/jdeath/BLE-YC01 (directly reads char) https://github.com/jdeath/renpho (parses advertisements only)
That’s great, thanks @jaaem! Those look like a great place to get stuck in.
I’ve done a bunch with BLE, but I’ve never developed for HA before, or used the more advanced bits of Python like async, so this is quite a steep learning curve for me.
I have been struggling with a BLE temperature and humidity sensor - can you perchance advise? It seems as if some clever people have done most of the heavy lifting already, but clearly I am still too stupid. First prize would be to directly wrangle the device via Tasmota (or, failing that, ESPHome).