tesla-local-control / tesla-local-control-addon

Control your Tesla locally from HomeAssistant
Apache License 2.0
10 stars 2 forks source link

Bluetooth adapter issues #27

Open raphmur opened 1 week ago

raphmur commented 1 week ago

I am facing this when running the presence detection...:

2024-06-19 15:41:37.387 ERROR (MainThread) [habluetooth.scanner] hci0 (E4:5F:01:D9:EC:B5): Error stopping scanner: [org.freedesktop.DBus.Error.UnknownObject] Method "StopDiscovery" with signature "" on interface "org.bluez.Adapter1" doesn't exist
2024-06-19 15:41:42.393 WARNING (MainThread) [bluetooth_auto_recovery.recover] Could not determine the power state of the Bluetooth adapter hci0 [E4:5F:01:D9:EC:B5] due to timeout after 5 seconds
2024-06-19 15:41:42.399 WARNING (MainThread) [bluetooth_auto_recovery.recover] Could not cycle the Bluetooth adapter hci0 [E4:5F:01:D9:EC:B5]: [Errno 16] Resource busy
2024-06-19 15:41:42.400 WARNING (MainThread) [bluetooth_auto_recovery.recover] Bluetooth management socket connection lost: [Errno 22] Invalid argument
2024-06-19 15:41:47.401 WARNING (MainThread) [bluetooth_auto_recovery.recover] Could not reset the power state of the Bluetooth adapter hci0 [E4:5F:01:D9:EC:B5] due to timeout after 5 seconds
2024-06-19 15:41:47.404 WARNING (MainThread) [bluetooth_auto_recovery.recover] Closing Bluetooth adapter hci0 [E4:5F:01:D9:EC:B5] failed: [Errno 9] Bad file descriptor
2024-06-19 15:41:47.406 ERROR (MainThread) [habluetooth.scanner] hci0 (E4:5F:01:D9:EC:B5): Failed to restart Bluetooth scanner: hci0 (E4:5F:01:D9:EC:B5): Failed to start Bluetooth: adapter 'hci0' not found; Try power cycling the Bluetooth hardware.

I think I had two processes using the bluetooth device at the same time. Which failed and caused the bluetooth adapter to crash. Then the main loop hanged waiting for bluetooth feedback.

Any idea? I would suggest to set a timeout in the call to bluetoothctt and if this fails more than 3 times: restart the adapter (if possible).

@baylanger ?

baylanger commented 1 week ago

I think hcitool is required to reset the adapter but I'm not positive. [edit] I tried to install it but the container image can't find bluez-tools package.

Also... do you have HAOS 12.4 ? Someone reported bluez is stable like it hasn't been in a long time. I wonder if the failure is due to a bad driver.

baylanger commented 1 week ago

Could not reset the power state of the Bluetooth adapter hci0 [E4:5F:01:D9:EC:B5] due to timeout after 5 seconds

Can you run the command w/ a 60 seconds timeout and see if it succeed to "reset" the adapter? Unclear what "reset power state" means, perhaps it's a power off / on ?

baylanger commented 1 week ago

I wasn't able earlier to find the source for 12.4 that might fix some Bluetooth issue, here it is:

Bluetooth adapter bug

raphmur commented 1 week ago

Ok thanks, I'll try

baylanger commented 1 week ago

Found an app already in the container to reset the controller, it fails and my guess is that HAOS doesn't give access to the device .. in write mode?! I'm unclear on this, see below the output.

# bluemoon -h
bluemoon - Bluemoon configuration utility
Usage:
    bluemoon [options]
Options:
    -A, --bdaddr [addr]    Set Bluetooth address
    -F, --firmware [file]  Load firmware
    -C, --check <file>     Check firmware image
    -R, --reset            Reset controller
    -B, --coldboot         Cold boot controller
    -E, --exception        Trigger exception
    -i, --index <num>      Use specified controller
    -h, --help             Show help options
c81b838b-tesla-local-commands:/data# bluemoon -R
Bluemoon configuration utility ver 5.76
Failed to open HCI user channel
c81b838b-tesla-local-commands:/data# bluemoon -B
Bluemoon configuration utility ver 5.76
Failed to open HCI user channel