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
Introduces delayed BLE actions #3434
Conversation
@ppanagiotis, I think I managed to fix the Let me know if you manage to take this for a spin with your switchbot. I'm curious to see if we get it to work well this time. |
I ran some more tests and there is still a bug somewhere:
If I run with the |
I believe the last commit fixes the bug I mentioned in the previous comment. Right now the following multi-ble-client config seems to be working as expected for me: esphome:
name: esphome_ble_led_client
board: lolin32
platform: ESP32
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
api:
password: !secret home_assistant_api
esp32_ble_tracker:
ble_client:
- id: esphome_ble_led_ble_client
mac_address: D0:90:AB:CD:EF:E2
maintain_connection: false
- id: esphome_ble_led_ble_client2
mac_address: D0:90:AB:CD:EF:A2
switch:
- platform: template
name: "ESP32 LED Switch"
optimistic: true
turn_on_action:
- ble_client.ble_write:
id: esphome_ble_led_ble_client
service_uuid: d7204ebc-4bc5-4a6a-a7e5-7a86f998b7a9
characteristic_uuid: 9be42785-94ae-4c1b-aa02-cd87c1c647c8
value: [0x7f, 0xab]
turn_off_action:
- ble_client.ble_write:
id: esphome_ble_led_ble_client
service_uuid: d7204ebc-4bc5-4a6a-a7e5-7a86f998b7a9
characteristic_uuid: 9be42785-94ae-4c1b-aa02-cd87c1c647c8
value: [0x4b, 0x9d]
- platform: template
name: "ESP32 LED Switch 2"
optimistic: true
turn_on_action:
- ble_client.ble_write:
id: esphome_ble_led_ble_client2
service_uuid: d7204ebc-4bc5-4a6a-a7e5-7a86f998b7a9
characteristic_uuid: 9be42785-94ae-4c1b-aa02-cd87c1c647c8
value: [0x7f, 0xab]
turn_off_action:
- ble_client.ble_write:
id: esphome_ble_led_ble_client2
service_uuid: d7204ebc-4bc5-4a6a-a7e5-7a86f998b7a9
characteristic_uuid: 9be42785-94ae-4c1b-aa02-cd87c1c647c8
value: [0x4b, 0x9d]
logger:
level: DEBUG |
@rbaron It works like a charm with both button and curtains!!! Thank you very much!! |
1906cb8
to
38f2309
Compare
Hi, This works really well, thanks for the two PRs. I've been using it for a while to manage a bunch of switchbot curtains, got everything working and rendering correctly in HA, including the position slider. The only thing missing is being able to set the position using that slider. I'm not sure what that would involve but any chance we could make the Thanks |
Hi @Ulrar, That's the direction I have in mind too, but I wanted to start simple and get the basics in, which turned out to be already relatively involved. I am happy to work on it once/if we get these two PRs merged in. |
38f2309
to
e855e5a
Compare
Rebased against |
e855e5a
to
644f8bd
Compare
I've been using this branch for a while, with both delayed and regular @Ulrar has been kindly maintaining a fork of this PR that's usable as an external_components:
- source: github://pr#3434
components: [ ble_client ] There are a few things I want to clean up in the code before requesting review, but I would say this is already usable as an external_component. |
@rbaron Glad to see these going in ! I've been using it since myself too and it's working pretty great. I expect we'll need to wait for the next release of ESPHome before we can point at this PR directly unless I missed something it's been merged but hasn't been released yet |
You're right @Ulrar. Users either need to:
|
With this commit, the BLE connection to the peripheral will only be established once the actions are triggered.
Oddly, with two BLEClients, the ESP_GATTC_CONNECT_EVT was called for each one, which caused their conn_id data member to be overridden. Following the example from ble_multiple_connect_main.c and pulling the conn_id from the ESP_GATTC_OPEN_EVT instead (which fires only once for the correct gattc_if) seems to fix the issue.
The bug was fixed by the parent commit.
2d2a6cd
to
8c2b4c2
Compare
@Ulrar this PR should now work with templatable values, which lets us set the SwitchBot position, in addition to fully open/close 😃. I've been using it like this: esp32_ble_tracker:
ble_client:
- id: switchbot_ble_client
mac_address: F1:E0:37:0D:EE:B5
maintain_connection: false
cover:
- platform: template
name: "Office Curtain Test"
device_class: curtain
optimistic: true
has_position: true
open_action:
- ble_client.ble_write:
id: switchbot_ble_client
service_uuid: cba20d00-224d-11e6-9fb8-0002a5d5c51b
characteristic_uuid: CBA20002-224D-11E6-9FB8-0002A5D5C51B
value: [0x57, 0x0F, 0x45, 0x01, 0x05, 0xFF, 0x00]
close_action:
- ble_client.ble_write:
id: switchbot_ble_client
service_uuid: cba20d00-224d-11e6-9fb8-0002a5d5c51b
characteristic_uuid: cba20002-224d-11e6-9fb8-0002a5d5c51b
value: [0x57, 0x0F, 0x45, 0x01, 0x05, 0xFF, 0x64]
position_action:
- ble_client.ble_write:
id: switchbot_ble_client
service_uuid: cba20d00-224d-11e6-9fb8-0002a5d5c51b
characteristic_uuid: cba20002-224d-11e6-9fb8-0002a5d5c51b
value: !lambda |-
uint8_t byte_position = 100 * (1.0f - pos);
ESP_LOGW("POS: ", "pos: %f, byte_position: %d", pos, byte_position);
return {0x57, 0x0F, 0x45, 0x01, 0x05, 0xFF, byte_position}; It's a lot of YAML for such a simple functionality, but it's generic enough to cater for generic "BLE writes". Hopefully we can wrap some of this into a Two things preventing me from turning this draft into a PR:
|
That's great news ! I've just updated my branch with your changes now and tested it, works like a charm. I get the same delay on the first action after a reboot on my doit devkits v1, I've seen somewhere that if you use some arduino code to have the board initiate a pairing request to the switchbot then re-flash ESPHome the delay goes away, but I haven't gotten around to try it myself yet. A SwitchBot component would be great, took a quick look a while back and I'm not that clear on how to go about creating one, but I haven't taken the time to look into it properly yet. |
Just to confirm, my repo isn't needed anymore with 2022.8.0, this is enough now : external_components:
- source: github://pr#3434
components: [ ble_client ] I'll delete my repo in a while, I'll give time to anyone who might have been using it to point to his PR instead |
There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days. Thank you for your contributions. |
With the new bluetooth_proxy this may not be as needed anymore, but it's probably still a worthwhile addition |
Thanks @Ulrar, I agree with you. I will close this for now. |
What does this implement/fix?
This PR introduces delayed BLE actions. This may be useful for triggering BLE writes on non-persistent connections. Using a delayed BLE action will cause a BLE connection to be established, a write to be sent to the specified characteristic and a subsequent disconnection.
This PR also makes BLE action more gracefully handle errors in both delayed (this PR) and eager (#3398) cases. Previously, a write would fail if the connection had not been established at the time of the action execution. Now, the write will be pending until the connection is established, at which point the write will be triggered.
Types of changes
Pull request in esphome-docs with documentation (if applicable): esphome/esphome-docs#
Test Environment
Example entry for
config.yaml
:I've set up a minimal ESP32 Arduino-based BLE service to test this PR with in rbaron/ble-led.
Checklist:
tests/
folder).If user exposed functionality or configuration variables are added/changed: