-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ble presence] Run if enable, BLE_MAC calculate & rm config. opt [0.0.8a][ready] #25
base: main
Are you sure you want to change the base?
Conversation
102ac22
to
c94f2b0
Compare
Just one minor note, the S...C string is not a BLE MAC. In BLE it is named BLE Local Name. Hence I suggest changing it to BLE_LNAME to not be misleading. |
I didn't have the time to read the multiple Bluetooth & BLE bibles. In the current PRs the name of the var was renamed to BLE_SC_NAME. For Bluetooth newbies to know the proper name, I propose BLE_LOCAL_NAME. I struggle last night with my Search engine to figure it out... Thanx for pointing us to the right name 👍 We haven't figure out yet how to scan for LE devices yet but below some good info. BLE Advertisement Interval
BLE Advertiser Address
Ref: |
And there's this known Bluetooth adapter bug ... perhaps fixed in latest HAOS (12.4). Not sure if this is why the "send key" wasn't working and perhaps this is why bluetoothctl wasn't able to do any discovery... I thought it was because "bluetoothclt" and "tesla-control" ran and caused a racing condition 🤔 I did update, I'll definitely run those 2 apps and try to cause the driver to fail... hopefully it is now stable. |
There are two advertised values of concern: The BLE advertiser address is the MAC address of the BLE. It must be there after scan all the time. Tesla car sends also iBeacons that contain more info (UUID, Minor no, Major no, etc) but it is not necessary to be scanned for as we just need to find only if the car is close or not. As I said the scanning should be passive: bluetoothctl --timeout 60 scan on > scan-results.log Normally each scan results line should contain MAC and BLE_LOCAL_NAME but depending on the version of the bluetoothctl/OS type, etc, sometimes the BLE_LOCAL_NAME is not obtained even though it is advertised. In this case only the MAC is shown, maybe together with RSSI. This makes things a bit more complex. So for each of the MACs in scan-results.log do: bluetoothctl info $MAC This will give you among other things the BLE_LOCAL_NAME on the first line and you can use it to check against the car's one: Device 11:22:33:44:55:66 (public) The info command above should be executed while scanning otherwise it will give you unknown device (on rpi ). On some systems like Ubuntu, BlueZ will cache the MAC and BLE_LOCAL_NAME and info command will succeed all the time even when scan is off. But we need to make sure it works on all the systems so info command needs to be executed while scanning. And for that, scanning and info command should have each a separate thread/process. If you want to go and get car's dash name, connect to the car and in this case the scan is active by interrogating the device and resolving BLE services. bluetoothctl --timeout 5 connect $MAC This will give to you something like this: Device 11:22:33:44:55:66 Name: 🔑 TeslaDashName 1st note: it will be nice to show the user the RSSI obtained form info command as it will give the user the idea of how far the car is from the BLE device and help on troubleshooting the connection. 2nd note: the bluetoothctl was not meant to be used for what we try to do here hence the inconsistencies of the output. The proper way to implement is directly with BlueZ API (thre is a python lib). But yeah it works. |
@BogdanDIA thanx for all the valuable information. Just pushed a commit to rename the var to BLE_LOCAL_NAME - but this PR generates that value automatically. That said, until we figure out a way to scan for the "local name" OR be able to calculate the advertised addr (MAC) using the "local name" (?) , this PR shouldn't be merged yet. If the MAC is available using bluetoothctl , then we should use that. @raphmur I guess we should put this PR on hold. I'm going to create another PR to allow the end user to set a MAC. We can probably keep what's in place in |
Probably I was not clear but is should be very simple. The idea is to find MAC<->CAR_BLE_NAME correspondence (the procedure is above). And this should be done only once at the initial stage when MAC is not known. bluetoothctl --timeout 10 scan on | grep $MAC Can add a threshold for number of finds to get a solid result. |
@baylanger please advise where you are on it. I suggest to pack all this clever MAC calculation in one dedicated sh file that will do all the setup work:
... and to be honest, this addon is already for fairly "geeky" users. Remember what is needed to setup FleetAPI proxy? :) What do you think? As you suggest @baylanger PR on hold |
👍 but. ... but but ... read below 🥇
My BLE dongle was too far from my car.... until ~1hr ago finally it showed up in bluetoothctl. Then I started to code ... and once I had code to match and extract the MAC address, I realized that if "scan on" previously was enable, bluetoothctl doesn't always display the car's "MAC + LNAME". Like everyone usually do, then I decided to read the manual .... to realize the MAC address is just... useless! With the "scan on" command:
But the "devices" command lists all the previously detected ones. The output shows up both the MAC + LNAME:
In the end, if we run the "scan on" following by the "devices" command; if the car isn't too far from the dongle, either it will show up immediately in the "devices" list otherwise soon or later, "scan on" will pick it up. We can do a direct match using the LNAME 🎉🎉🎉 unless there's a case I'm not aware, I see no reason for us to extract the MAC.
I think we now have a way to make this presence a lot more easier for the end user ;) We can have a toggle switch to enable the detection or not.
lol
I should finish the new PR today ... but it's late here :D |
@baylanger using 'devices command' is fine but most of the data in bluetoothctl is cached in bluez. Rpi is not really affected but big OSs like Ubuntu are. Internet is full of issues coming from caching in bluez. To solve the problem just remove the car's entry from devices (you will need the MAC): |
Per the tests I did, is removing the entry from the cache still needed today with the latest bluez? Here what I found out after writing my last comment:
Note: 6 seconds seem quite short if a vehicle signals is a weak. We should probably loop & retry a few times within HA rules (10 times within 30 minutes). @raphmur was it in HA's system logs that you saw this warning.. was it a warning or scanning wasn't possible anymore? 🤔
Each time I tried to I'm going to make the PR on the assumption that caching works fine today because that seems to be the case. Let me know if you have concerns. [edit 1] Once the scan is over, I do notice the entries in the caching list gets removed exactly 30 seconds later. |
@baylanger I think it was in the Supervisor logs. I did not capture it when I saw it unfortunately, but the use of bluetoothctl more than 10 times in 30 mins caused a sort of ban. You can maybe test it at same periodicity of MQTT loop and see if it still happens? |
I have run ~200 scans since yesterday. What's great, not once the driver failed. During those scans, the container was running as well a scan every ~180 seconds. What didn't run in parallel is tesla-control and bluetoothctl , coud that cause issues? It sure is easy to launch them in parallel for a while and see if anything fails. While doing all those scans, if HA complained the container was doing too many scans, well ... it might complain but it didn't prevent them to complete. Perhaps HAOS 12.4 did it? |
I'm not sure how to start this comment :D The comment I wrote above is True ONLY IF the bluetooth_le_tracker integration isn't configured. If that integration is configured my vehicle is found 100% of the time in the cache using the Do we need to be aware if bluetooth_le_tracker is configured? Would the interaction with bluetoothctl differ?
While I have this one in mind... What if someone has 2 Tesla?
I'm far to be done w/ the PR - I had too many questions on my mind today 🤔 , will continue tomorrow. |
I will catch up with this conversation when I get home from holidays, so I have the means of testing. Note I am working on a multiple car solution in my repo. It should be ready for testing in a few days |
As I said on rpi caching is minimum and it will be fine but on an Ubuntu caching will stay there for long time. On my Ubuntu reports a device that I don't have at home for 3 week now. Each system has a different caching configuration and bluetoothctl configuration so it is better to make sure the code works in any case.
You may want to play with the timeouts and number of loops. |
I'm puzzle to understand why the RPi vs Ubuntu differ so differently in terms of caching. What OS release and what runs on each of those boxes? Do they run have the same bluez release - and if so, what differs in bluez config? |
rpi5 has bluez 5.66 and Ubuntu 20.04 has 5.33 but various params can be configured at build time. I guess it is the distribution builders decision. BTW, the caching is in /var/lib/bluetooth/ dir and can be deleted manually but I do not recommend :) |
From Home Assistant Bluetooth:
From Bluez github Issue 191 ; a few PR were merged in relation to the issue. I would strongly suggest you update bluez on your Ubuntu box and see if it behaves better. It's hard to imagine the hardware would make a difference on the caching, unless the config on the RPi is good and bad on Ubuntu - but if HA tells us to use 5.63 and later, I would definitely follow that guideline. |
BTW 5.33 was released 9 years ago. |
Agree, it is an old system. However, in a standalone docker implementation (not related to HA) we'll find any kind of systems, including very old ones. |
@iainbullock couldn't tag you in #32 - see whats in there and the comment on what I was on in past days. Hopefully we can all gather tomorrow. I'm on East coast Canada and tomorrow (Monday) not working. Raohael and I connected via WhatsApp if you have that, try to connect with him. |
I'm back from my holidays and trying to catch up with this (and various other things), before going back to work tomorrow (Tuesday). It would be good to have a call to see where we are with the projects. I'm in the UK, time zone is currently UTC+1 I could do a call this afternoon UK time (though have to take my motorbike for service, so it depends when). Shall we use the Discord private channel to make the arrangements? |
I'm in France. |
. Remove BLE_MAC option from configuration
. Added function to calculate BLE_MAC
. Added option to enable or not BLE presence detection
. Added DEBUG and BLE_PRESENCE_ENABLE to standalone
Fixed #26
This is again part of splitting the big PR #16 into multiple smaller PRs.