patman15 / BMS_BLE-HA

This integration allows to monitor Bluetooth Low Energy (BLE) battery management systems (BMS) from within Home Assistant.
GNU Lesser General Public License v2.1
12 stars 2 forks source link

BT Pin code supported? #41

Open HansWegman opened 2 weeks ago

HansWegman commented 2 weeks ago

Checklist

Describe the issue

I just tried to install the integration to connect by XiaoXiangElectric Battery (JBD) which went like a charm, unfortunately although my battery was detected nicely through the ESPHome proxy and 11 entities were added, none have any data. I suspect this is because I have added a bluetooth pin to the battery which unfortunately cannot be removed. I have set it to "000000" now but still no data and the integration reports: "Failed to initialise BMS Hans Battery, continuing". Any idea if it is possible to enter the pin somewhere?

log

2024-09-01 17:21:35.661 WARNING (SyncWorker_0) [homeassistant.loader] We found a custom integration bms_ble which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant 2024-09-01 17:21:41.997 INFO (MainThread) [homeassistant.setup] Setting up bms_ble 2024-09-01 17:21:41.997 INFO (MainThread) [homeassistant.setup] Setup of domain bms_ble took 0.00 seconds 2024-09-01 17:21:41.997 DEBUG (MainThread) [custom_components.bms_ble] Setup of 2024-09-01 17:22:23.632 DEBUG (MainThread) [custom_components.bms_ble] Bluetooth device detected: <BluetoothServiceInfoBleak name=Hans Battery address=A4:C1:37:04:2D:BE rssi=-65 manufacturer_data={11710: b'\x047\xc1\xa4'} service_data={} service_uuids=['0000ff00-0000-1000-8000-00805f9b34fb'] source=10:06:1C:16:7C:28 connectable=True time=240246.576593881 tx_power=None> 2024-09-01 17:22:23.634 DEBUG (MainThread) [custom_components.bms_ble] Setup of 2024-09-01 17:22:23.679 DEBUG (MainThread) [custom_components.bms_ble] Initializing coordinator for Hans Battery (A4:C1:37:04:2D:BE) as Jiabaida Smart BMS 2024-09-01 17:22:23.679 DEBUG (MainThread) [custom_components.bms_ble] device data: {'name': 'Hans Battery', 'address': 'A4:C1:37:04:2D:BE', 'rssi': -63, 'manufacturer_data': {11710: b'\x047\xc1\xa4'}, 'service_data': {}, 'service_uuids': ['0000ff00-0000-1000-8000-00805f9b34fb'], 'source': '10:06:1C:16:7C:28', 'advertisement': AdvertisementData(local_name='Hans Battery', manufacturer_data={11710: b'\x047\xc1\xa4'}, service_uuids=['0000ff00-0000-1000-8000-00805f9b34fb'], tx_power=-127, rssi=-63), 'device': BLEDevice(A4:C1:37:04:2D:BE, Hans Battery), 'connectable': True, 'time': 240285.365139811, 'tx_power': None} 2024-09-01 17:22:23.679 DEBUG (MainThread) [custom_components.bms_ble] BMS Hans Battery data update 2024-09-01 17:22:23.680 DEBUG (MainThread) [custom_components.bms_ble.plugins.jbd_bms] Connecting BMS (Hans Battery) 2024-09-01 17:22:35.168 DEBUG (MainThread) [custom_components.bms_ble] Device communication timeout 2024-09-01 17:22:35.169 DEBUG (MainThread) [custom_components.bms_ble] Finished fetching Hans Battery data in 11.489 seconds (success: False) 2024-09-01 17:22:35.169 WARNING (MainThread) [custom_components.bms_ble] Failed to initialize BMS Hans Battery, continuing 2024-09-01 17:22:35.176 INFO (MainThread) [homeassistant.components.binary_sensor] Setting up bms_ble.binary_sensor 2024-09-01 17:22:35.178 INFO (MainThread) [homeassistant.components.sensor] Setting up bms_ble.sensor 2024-09-01 17:23:05.091 DEBUG (MainThread) [custom_components.bms_ble] BMS Hans Battery data update 2024-09-01 17:23:05.092 DEBUG (MainThread) [custom_components.bms_ble.plugins.jbd_bms] BMS Hans Battery already connected 2024-09-01 17:23:15.095 DEBUG (MainThread) [custom_components.bms_ble] Device communication timeout 2024-09-01 17:23:15.095 DEBUG (MainThread) [custom_components.bms_ble] Finished fetching Hans Battery data in 10.004 seconds (success: False) 2024-09-01 17:23:45.091 DEBUG (MainThread) [custom_components.bms_ble] BMS Hans Battery data update 2024-09-01 17:23:45.091 DEBUG (MainThread) [custom_components.bms_ble.plugins.jbd_bms] BMS Hans Battery already connected 2024-09-01 17:23:55.094 DEBUG (MainThread) [custom_components.bms_ble] Device communication timeout 2024-09-01 17:23:55.095 DEBUG (MainThread) [custom_components.bms_ble] Finished fetching Hans Battery data in 10.003 seconds (success: False)

patman15 commented 1 week ago

Looks like the device does not respond, which is highly likely due to the set pin. Unfortunately, there is currently no function to set a pin for a battery in the integration. Would need to take a look on how to implement that. Would you volunteer as beta-tester (since I have no battery with pin available)? Thanks for the log, simplified the response a lot!

patman15 commented 1 week ago

You could also try to pair the battery from the terminal using bluetoothctl see https://commandmasters.com/commands/bluetoothctl-linux/ chapter 4.

HansWegman commented 1 week ago

Thanks for your quick reply! I will be very happy to do the beta test for sure! And I will try to pair from the terminal right away, will let you know asap.

HansWegman commented 1 week ago

Hmm, tried to pair but maybe from the wrong terminal? I have a ssh and terminal add-on in HA which reports: unable to open mgmt_socket, device A4:C1:37:04:2D:BE not available.

HansWegman commented 1 week ago

And maybe a silly suggestion since my programming skills are very low but maybe this could be solved (by someone with te right skills) with a small setting added to the ESPHome proxy based upon this:

on_passkey_request This automation is triggered when the BLE device requests a passkey for authentication.

ble_client:

patman15 commented 1 week ago

unable to open mgmt_socket, device A4:C1:37:04:2D:BE not available.

You see that as reply to which command?

When you enter the terminal you should see:

       ▄██▄           _   _                                    
     ▄██████▄        | | | | ___  _ __ ___   ___               
   ▄████▀▀████▄      | |_| |/ _ \| '_ ` _ \ / _ \              
 ▄█████    █████▄    |  _  | (_) | | | | | |  __/              
▄██████▄  ▄██████▄   |_| |_|\___/|_| |_| |_|\___|          _   
████████  ██▀  ▀██      / \   ___ ___(_)___| |_ __ _ _ __ | |_ 
███▀▀███  ██   ▄██     / _ \ / __/ __| / __| __/ _` | '_ \| __|
██    ██  ▀ ▄█████    / ___ \\__ \__ \ \__ \ || (_| | | | | |_ 
███▄▄ ▀█  ▄███████   /_/   \_\___/___/_|___/\__\__,_|_| |_|\__|
▀█████▄   ███████▀

Welcome to the Home Assistant command line.

System information
  IPv4 addresses for enp0s18: 192.---------
  IPv6 addresses for enp0s18: ---------------------------

  OS Version:               Home Assistant OS 13.1
  Home Assistant Core:      2024.8.3

  Home Assistant URL:       http://home.local:8123
  Observer URL:             http://home.local:4357
➜  ~ 
HansWegman commented 1 week ago

Yes, I tried it there indeed but my BMS device is not listed there... It is in the log of the proxy though, see the attached logfile. logs_m5stack-atom-lite-167c28_run.txt

HansWegman commented 1 week ago

I changed the log settings in the proxy to VERY_VERBOSE and copied the parts with the Battery which seem to indicate it gets connected if I understand it well. Still do not understand why it does not appear in HA though. I attached a screenshot of the bluetoothctl devices output as well. Also strange, I reloaded your integration without the proxy and it failed on the entities. When I connected the proxy again it reloaded well.

devices

Proxy_very_verbose Log battery part.txt

patman15 commented 1 week ago

Which install type did you choose for Homeassistant? Are you using HASS or other?

Touched, not typed.

HansWegman commented 1 week ago

I am running HA core 2024.8.3 with supervisor 2024.08.0 on OS 13.1 on generic-x86-64

patman15 commented 1 week ago

Can you please check the output of bluetoothctl list to show the adapters? I think it is an issue with the proxy. Pairing needs to be between the adapter and the device, IMHO. That means that since you are using a proxy the BMS needs to be paired with the proxy (which you can't do unless it shows up in bluetoothctl). Probably you can use the automation you have used so far? Sorry, just guessing a bit, but maybe it helps to move forward. What is a bit irritating is the message unable to open mgmt_socket that should not happen. Can you bring the battery closer to the actual host so that we can check the pairing works there?

HansWegman commented 1 week ago

Attached is the result of the list command. I guess however looking at the log that the MBS is paired with the proxy. I also sniffed the communication between the original app and the device and it looks like there is some special communications. I will include the log as well.

What I did, I removed and reinstalled the app so it would ask for the password again. I started the log I opened the app and entered the password 123123 Than the readings came in, battery 99% charged I switched to some other screens in the app I closed the app I reopened the app and this time I did not need to enter the password, I guess it saves it and uses the same or another way to connect The data reappeared

Im am looking at it and trying to make some sense out of it: XiaoXiang log 2.log

Screenshot of list command
patman15 commented 1 week ago

Which app did you reinstall that works and asks for the pin again? Just for reference so I can have a look.

Touched, not typed.

HansWegman commented 1 week ago

The XiaoXiangElectric app on my iPhone, there is also an Android version, see https://xiaoxiangbms.com/download/.

patman15 commented 1 week ago

Hi! I digged a bit through the documentation on the Internet regarding the proxy and pairing, which is very weak regarding hints. Can you please try to bring the BMS into the range of your controller directly attached to homeassistant (and probably also disable the BT proxy) and then try to pair the device once see https://github.com/patman15/BMS_BLE-HA/issues/41#issuecomment-2323453783. If I interpret the documentation correct, it should afterwards also work with the BT proxy, but the pairing needs to happen directly without the proxy.

HansWegman commented 1 week ago

Hi Patrick,

that is going to be quite a challenge but I will see whether I can figure something out to make that possible. Will get back to you hopefully soon.

patman15 commented 1 week ago

Hi Hans,

I have not too much hope, but you could try this code: https://github.com/patman15/BMS_BLE-HA/tree/feature/pairing See https://github.com/patman15/BMS_BLE-HA/tree/feature/pairing?tab=readme-ov-file#manual on how to get the code manually to your system. Please send me the debug log to see what happens.

I found some hints that pairing is not supported by the bleak library which is used by Home Assistant.