ronnnnnnnnnnnnn / etekcity_fitness_scale_ble

A Home Assistant custom integration for Etekcity Bluetooth Low Energy (BLE) fitness scales. Get real-time weight measurements and body metrics in your smart home setup.
MIT License
1 stars 1 forks source link

Help integrating - Etekcity EFS-A591S-KUS HR Smart Fitness Scale #3

Open jamesshannon opened 23 hours ago

jamesshannon commented 23 hours ago

Hi,

I have the EFS-A591S-KUS scale which I assumed would work with this integration. It is bluetooth + Wifi (ie, scale -> cloud -> cloud account - app) and works with ve sync app.

Your integration successfully created the device* but both sensors are permanently "Unknown".

I thought that this might just be a matter of sniffing the BLE traffic and figuring out in which format the data is being sent and doing a PR, but now that I'm looking at the logs, I see No Bluetooth adapters found, so maybe I'm actually doing something wrong with the bluetooth proxy setup?

2024-11-24 10:45:45.986 INFO (MainThread) [homeassistant.components.sensor] Setting up etekcity_fitness_scale_ble.sensor
2024-11-24 10:45:45.986 DEBUG (MainThread) [custom_components.etekcity_fitness_scale_ble.sensor] Setting up scale sensors for entry: 01JDE4XT2BGRAXQQNBETMZV95P
2024-11-24 10:45:45.986 DEBUG (MainThread) [custom_components.etekcity_fitness_scale_ble.coordinator] Setting display unit to: LB
2024-11-24 10:45:45.986 DEBUG (MainThread) [custom_components.etekcity_fitness_scale_ble.sensor] Adding sensor to Home Assistant: sensor.etekcity_apex_48_e7_29_33_21_be_weight
2024-11-24 10:45:45.987 DEBUG (MainThread) [custom_components.etekcity_fitness_scale_ble.sensor] Restoring previous state for sensor: sensor.etekcity_apex_48_e7_29_33_21_be_weight
2024-11-24 10:45:45.987 INFO (MainThread) [custom_components.etekcity_fitness_scale_ble.sensor] Sensor added to Home Assistant: sensor.etekcity_apex_48_e7_29_33_21_be_weight
2024-11-24 10:45:45.987 DEBUG (MainThread) [custom_components.etekcity_fitness_scale_ble.sensor] Adding sensor to Home Assistant: sensor.etekcity_apex_48_e7_29_33_21_be_impedance
2024-11-24 10:45:45.987 DEBUG (MainThread) [custom_components.etekcity_fitness_scale_ble.sensor] Restoring previous state for sensor: sensor.etekcity_apex_48_e7_29_33_21_be_impedance
2024-11-24 10:45:45.987 INFO (MainThread) [custom_components.etekcity_fitness_scale_ble.sensor] Sensor added to Home Assistant: sensor.etekcity_apex_48_e7_29_33_21_be_impedance
2024-11-24 10:45:46.083 DEBUG (MainThread) [custom_components.etekcity_fitness_scale_ble.coordinator] Starting ScaleDataUpdateCoordinator for address: 48:E7:29:33:21:BE
2024-11-24 10:45:46.083 DEBUG (MainThread) [custom_components.etekcity_fitness_scale_ble.coordinator] Initializing new EtekcitySmartFitnessScale client
2024-11-24 10:45:46.083 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up etekcity_fitness_scale_ble platform for sensor
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 365, in _async_setup_platform
    await asyncio.shield(awaitable)
  File "/config/custom_components/etekcity_fitness_scale_ble/sensor.py", line 168, in async_setup_entry
    await coordinator.async_start()
  File "/config/custom_components/etekcity_fitness_scale_ble/coordinator.py", line 89, in async_start
    await self._async_start()
  File "/config/custom_components/etekcity_fitness_scale_ble/coordinator.py", line 75, in _async_start
    await self._client.async_start()
  File "/usr/local/lib/python3.12/site-packages/etekcity_esf551_ble/parser.py", line 118, in async_start
    await self._scanner.start()
  File "/usr/local/lib/python3.12/site-packages/etekcity_esf551_ble/bluetooth.py", line 81, in start
    await self._backend.start()
  File "/usr/local/lib/python3.12/site-packages/bleak/backends/bluezdbus/scanner.py", line 173, in start
    adapter_path = manager.get_default_adapter()
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/bleak/backends/bluezdbus/manager.py", line 353, in get_default_adapter
    raise BleakError("No Bluetooth adapters found.")
bleak.exc.BleakError: No Bluetooth adapters found.

* I'm using an esp32 bluetooth proxy and the initial setup flow showed all possible BLE MAC addresses. When I initially set up with the incorrect device I got "Unknown error occurred", so I assumed that this was due to trying to read attributes from a device that didn't support them. Then "turned the scale on" and chose the correct MAC address and it worked. However, further testing shows that the "Unknown error occurred" was due to selecting "Calculate body composition metrics" and occurred even with the Etekcity scale, and that I'm able to set a scale device with any BLE device.

  1. In other words, the fact that this initialized correctly doesn't mean the scale is somehow supported.
  2. Though maybe "Calculate body composition metrics" helps to enforce that it's a supported scale, and the error here is more telling.
  3. FWIW, a filter on the MAC address would make things easier. My proxy was picking up a few dozen devices. Presumably all etekcity scales start with the same octets? FWIW, mine is 48:E7:29:33:21:BE.
jamesshannon commented 19 hours ago

This is my first attempt at using the esphome bluetooth proxy, but I believe I'm doing everything correct. Other integrations seem to be detecting bluetooth devices (for example, ibeacon popped up as an integration, and etekcity successfully sees the various MAC addresses).

However, I continue to get the "no adapters found" error from etekcity integration.

I note in the stack trace that bleak is called by this integration directly (as opposed to via a HA API). I suspect that the HA Bluetooth API, which also appears to use Bleak, maybe does some extra abstraction to support proxies?

Maybe the etekcity_esf551_ble package should be using the HA APIs instead? Which brings up an interesting point because, AFAIK, HA wants integrations to be a thin layer on top of a "generic" package (which is exactly what etekcity_fitness_scale_ble + etekcity_esf551_ble is). However, requiring the use of HA's implementation of Bleak would make the underlying package non-generic.

ronnnnnnnnnnnnn commented 18 hours ago

Hi @jamesshannon,

I'm not really familiar with the bluetooth proxy feature, so I don't know if it would work with my integration. Is it just straightforward plain proxying of BT traffic from devices to ESPHome to Home Assistant over BT as is? I mean, is HA just oblivious to it being a proxy, seeing it just as if it's the actual device? Otherwise I don't think it would work with the integration, as it wasn't designed for that but for direct BT connection to the scale.

Did you try it without the proxy?

If you sniff and capture the relevant BT traffic between the scale and the VeSync app on your phone, then I can give it a look and see if the protocol your model uses is similar to the one used by the ESF-551. If it is then we can rule that out and if not then I can try to see if and how I can maybe add support for it too.

ronnnnnnnnnnnnn commented 18 hours ago

Regarding the HA APIs - I don't really like that solution because just like you said it would make the underlying package non-generic and I don't want it to be tied to HA specifically. It's just a client for communicating and working with the scale. This integration uses it for HA, but the way I see it there's no reason for it to not be used for other platforms or projects or whatever anyone might want to use it for.

jamesshannon commented 17 hours ago

Hi.

the way I see it there's no reason for it to not be used for other platforms or projects or whatever anyone might want to use it for.

Yeah. It totally appreciate that perspective. That's the way I'd do it, and I believe it's a HA requirement for integrations (though wouldn't apply to yours as it's via HACS).

Which is why the need to use their bleak BT wrapper is so interesting. Because if you use it, then you're not making a generic package. (I say need because I assume at this point that it's required, and the source of the problem.)

Though I guess you could create etekcity_esf551_ble to do dependency injection? (Ie, when you instantiate the client you first get the HA BT wrapper and then pass that in instantiation; if it's not passed then the connection is established normally.

As for the other questions -- my HA instance is virtualized on a NAS, which doesn't have onboard BT. I could plug in a bluetooth dongle to the single USB port (after getting a hub), but the server is in the basement. The bluetooth proxy is just a proxy via ESPHome. I think that at most layers, HA is oblivious, though I'm sure there is some code at the lower levels that specifically interacts with ESPHome proxies -- proxy support is built-in to HA and I assume it presents most of the BT API stack with a combined view of BT traffic. THat's probably some of the reasoning for the wrapper.

Considering the error, I'm don't think decoding the scale traffic adds anything at this point. I need to solve the error first. I'll try tweaking with the code and see if it suddenly works... or at least gets beyond the error.