digaus / esphome-components-eqiva

Other
18 stars 9 forks source link

No connection with ESPHome 2024.2.0 #11

Closed RoP09 closed 2 months ago

RoP09 commented 4 months ago

Hello,

I have update to ESPHome 2024.2.0 today, and after this the ESP32 is no longer connecting to the lock.

Also a new pairing is not successful (nothing happens).

Any ideas? Maybe due to this change? https://github.com/esphome/esphome/pull/5277

This is all one can see basically: Screenshot 2024-02-21 15 44 57

RoP09 commented 4 months ago

Fyi, going back to legacy 2023.12.9 and it immediately connects again to the lock.

digaus commented 4 months ago

Yup looks like it. Will have to modify my code. Will update this issue once I found the time.

user45876 commented 4 months ago

Unfortunately, this is still the case with ESPHome 2024.2.1. I think, if, a fix in the underlying keyble code is the only thing that could help?

user45876 commented 4 months ago

Well, it was closed - unsure, why, as esphome seems to he breaking it. Kindly asked for reconsidering.

digaus commented 4 months ago

As the esphome maintainer mentioned it has to be resolved on my side. It is unfortunate that they introduced a breaking change but this can happen and I will update my code when I find the time.

Until then you could still specify a different esphome version for the ble client component. See here for more details:

https://esphome.io/components/external_components.html

user45876 commented 4 months ago

Thank you for the hint. Like this?

external_components:
  # use ble_client from ESPHome 2023.12.9
  - source: 
      type: git 
      url: https://github.com/esphome/esphome 
      ref: 2023.12.9
    components: [ ble_client ]
digaus commented 4 months ago

Thank you for the hint. Like this?

external_components:
  # use ble_client from ESPHome 2023.12.9
  - source: 
      type: git 
      url: https://github.com/esphome/esphome 
      ref: 2023.12.9
    components: [ ble_client ]

Have not tried it but looks good

user45876 commented 4 months ago

Workaround :) I checked the compiler run for the used dependencies and also realized that we are using the esp32_ble components. This is a workaround, again thanks to digaus for making all of this possible :)

external_components:
  - source: github://digaus/esphome-components-eqiva

  # use esp32_ble components from ESPHome 2023.12.9
  - source: github://esphome/esphome@2023.12.9
    components: [ esp32_ble, esp32_ble_client, esp32_ble_tracker ]
paul-leitner commented 3 months ago

I just stumbled upon this issue as well after updating my ESP devices to the latest esphome version. As far as I could tell, the major change leading to the component being stuck in BLE state "IDLE" is the newly added flag for "autoconnect". As an easy fix, I just added `this->parent->set_auto_connect(true);` after the code setting up the user data and device mac address in. This seems to do the trick for me and the lock is connected and working again. Maybe the auto_connect flag should be set to false upon a controlled disconnect to prevent further connection retries.

@digaus can you check whether this has some unwanted side-effects or is needed in other parts of the code as well? I made the change in my fork and tested it as source for the external component like this:

external_components:
  - source: github://paul-leitner/esphome-components-eqiva
digaus commented 3 months ago

I just stumbled upon this issue as well after updating my ESP devices to the latest esphome version. As far as I could tell, the major change leading to the component being stuck in BLE state "IDLE" is the newly added flag for "autoconnect". As an easy fix, I just added `this->parent->set_auto_connect(true);` after the code setting up the user data and device mac address in. This seems to do the trick for me and the lock is connected and working again. Maybe the auto_connect flag should be set to false upon a controlled disconnect to prevent further connection retries.

@digaus can you check whether this has some unwanted side-effects or is needed in other parts of the code as well? I made the change in my fork and tested it as source for the external component like this:

external_components:
  - source: github://paul-leitner/esphome-components-eqiva

While this might work I think it is better to change my plugin to use the new connect / disconnect / write functions directly from the ble_client implementation. This should reduce my code and hopefully also work better.

I will see if I can do it this week. Maybe I will modify it to be able to connect to two devices like in the ble_client example.

Strakomania commented 3 months ago

I just stumbled upon this issue as well after updating my ESP devices to the latest esphome version. As far as I could tell, the major change leading to the component being stuck in BLE state "IDLE" is the newly added flag for "autoconnect". As an easy fix, I just added `this->parent->set_auto_connect(true);` after the code setting up the user data and device mac address in. This seems to do the trick for me and the lock is connected and working again. Maybe the auto_connect flag should be set to false upon a controlled disconnect to prevent further connection retries.

@digaus can you check whether this has some unwanted side-effects or is needed in other parts of the code as well? I made the change in my fork and tested it as source for the external component like this:

external_components:
  - source: github://paul-leitner/esphome-components-eqiva

Thanks @paul-leitner this solution works for me aswell!

hmmmonteiro commented 2 months ago

Hi everyone.

First of all, big thanks to @digaus for the hard work, and taking time to share it. I've been wanting to take on previous work from Marius, tc-maxx and lumokitho but never found the time.

I started testing on a NodeMCU and since it all went well i went on and purchased a Sonoff NSPanel, which i'm planing on deploying the solution. Things went great on the NodeMCU (previous version of esphome) and when i got the NSPanel i got pretty frustrated because it wasn't working. My first thought was it had something to do with the new hardware and didn't cross my mind it could be a breaking change from esphome.

Fast forward, i was pleased to find this issue and had some success with @paul-leitner solution. It wasn't always fluid and connection got dropped once in a while. Also, the proposed change is a breaking change for earlier esphome versions.

A workaround which has been working pretty nice to me, keeping original repo, i suggest

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  on_connect:
    - esp32_ble_tracker.start_scan:
        continuous: true
    - if:
        condition:
          lambda: 'return (strcmp(ESPHOME_VERSION, "2023.12.9") > 0);'
        then:
          lambda: 'id(key_ble)->set_auto_connect(true);'
    - eqiva_key_ble.connect:
       mac_address: !lambda 'return id(mac_address).state;' 
       user_id: !lambda 'return id(user_id).state;'
       user_key: !lambda 'return id(user_key).state;'
  on_disconnect:
    - esp32_ble_tracker.stop_scan:
RoP09 commented 2 months ago

Hi everyone.

First of all, big thanks to @digaus for the hard work, and taking time to share it. I've been wanting to take on previous work from Marius, tc-maxx and lumokitho but never found the time.

I started testing on a NodeMCU and since it all went well i went on and purchased a Sonoff NSPanel, which i'm planing on deploying the solution. Things went great on the NodeMCU (previous version of esphome) and when i got the NSPanel i got pretty frustrated because it wasn't working. My first thought was it had something to do with the new hardware and didn't cross my mind it could be a breaking change from esphome.

Fast forward, i was pleased to find this issue and had some success with @paul-leitner solution. It wasn't always fluid and connection got dropped once in a while. Also, the proposed change is a breaking change for earlier esphome versions.

A workaround which has been working pretty nice to me, keeping original repo, i suggest

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  on_connect:
    - esp32_ble_tracker.start_scan:
        continuous: true
    - if:
        condition:
          lambda: 'return (strcmp(ESPHOME_VERSION, "2023.12.9") > 0);'
        then:
          lambda: 'id(key_ble)->set_auto_connect(true);'
    - eqiva_key_ble.connect:
       mac_address: !lambda 'return id(mac_address).state;' 
       user_id: !lambda 'return id(user_id).state;'
       user_key: !lambda 'return id(user_key).state;'
  on_disconnect:
    - esp32_ble_tracker.stop_scan:

Does not work for me, the only way it works is reverting back to the 2023 revision using the addon. If I do like you propsed the lock is not fully connecting:

13:04:51 [D] [text_sensor:064] 'Lock BLE State': Sending state 'IDLE' 13:04:51 [D] [text_sensor:064] 'Lock BLE State': Sending state 'IDLE' 13:04:51 [D] [eqiva_key_ble:058] ESP_GATTC_DISCONNECT_EVT 13:04:51 [D] [text_sensor:064] 'Mac Address': Sending state '00:1A:22:13:6F:77' 13:04:51 [D] [esp32_ble_client:110] [0] [00:1A:22:13:6F:77] ESP_GATTC_OPEN_EVT 13:04:51 [W] [esp32_ble_client:143] [0] [00:1A:22:13:6F:77] Connection failed, status=133 13:04:51 [D] [text_sensor:064] 'Lock BLE State': Sending state 'IDLE' 13:04:51 [D] [esp32_ble_tracker:266] Starting scan... 13:04:55 [D] [esp32_ble_client:110] [0] [00:1A:22:13:6F:77] Found device 13:04:55 [D] [esp32_ble_tracker:661] Found device 00:1A:22:13:6F:77 RSSI=-93 13:04:55 [D] [esp32_ble_tracker:682] Address Type: PUBLIC 13:04:55 [D] [esp32_ble_tracker:684] Name: 'KEY-BLE' 13:04:55 [D] [text_sensor:064] 'Lock BLE State': Sending state 'DISCOVERED' 13:04:55 [D] [esp32_ble_tracker:215] Pausing scan to make connection... 13:04:55 [D] [text_sensor:064] 'Lock BLE State': Sending state 'READY_TO_CONNECT' 13:04:55 [I] [esp32_ble_client:067] [0] [00:1A:22:13:6F:77] 0x00 Attempting BLE connection 13:04:55 [D] [text_sensor:064] 'Lock BLE State': Sending state 'CONNECTING' 13:04:58 [D] [esp-idf:000] W (409853) BT_HCI: hcif disc complete: hdl 0x0, rsn 0x3e 13:05:02 [D] [esp-idf:000] W (413870) BT_HCI: hcif disc complete: hdl 0x0, rsn 0x3e 13:05:13 [D] [esp-idf:000] W (424899) BT_HCI: hcif disc complete: hdl 0x0, rsn 0x3e

hmmmonteiro commented 2 months ago

Weird ...

[12:21:14][I][eqiva_ble:015]: Found Eqiva device (MAC: 00:1A:22:18:A6:5C) (UUID): 0x1A00  (Name): KEY-BLE
[12:21:14][D][esp32_ble_client:110]: [0] [00:1A:22:18:A6:5C] Found device
[12:21:14][D][esp32_ble_tracker:661]: Found device 00:1A:22:18:A6:5C RSSI=-59
[12:21:14][D][esp32_ble_tracker:682]:   Address Type: PUBLIC
[12:21:14][D][esp32_ble_tracker:684]:   Name: 'KEY-BLE'
[12:21:14][D][text_sensor:064]: 'Lock BLE State': Sending state 'DISCOVERED'
[12:21:14][D][esp32_ble_tracker:215]: Pausing scan to make connection...
[12:21:14][D][text_sensor:064]: 'Lock BLE State': Sending state 'READY_TO_CONNECT'
[12:21:14][I][esp32_ble_client:067]: [0] [00:1A:22:18:A6:5C] 0x00 Attempting BLE connection
[12:21:14][D][text_sensor:064]: 'Lock BLE State': Sending state 'CONNECTING'
[12:21:14][D][nextion:276]: Manually set nextion report ready
[12:21:14][D][nextion:354]: Nextion is setup
[12:21:14][E][nextion:285]: Nextion queue is empty!
[12:21:15][D][text_sensor:064]: 'Mac Address': Sending state '00:1A:22:18:A6:5C'
[12:21:15][D][esp32_ble_client:110]: [0] [00:1A:22:18:A6:5C] ESP_GATTC_CONNECT_EVT
[12:21:15][D][text_sensor:064]: 'Lock BLE State': Sending state 'CONNECTING'
[12:21:15][D][eqiva_key_ble:231]: OTHER EVENT 40
[12:21:15][D][text_sensor:064]: 'Mac Address': Sending state '00:1A:22:18:A6:5C'
[12:21:15][D][esp32_ble_client:110]: [0] [00:1A:22:18:A6:5C] ESP_GATTC_OPEN_EVT
[12:21:15][D][text_sensor:064]: 'Lock BLE State': Sending state 'CONNECTED'
[12:21:15][D][text_sensor:064]: 'Lock BLE State': Sending state 'CONNECTED'
[12:21:15][D][eqiva_key_ble:231]: OTHER EVENT 2
[12:21:15][D][esp32_ble_tracker:266]: Starting scan...
[12:21:16][D][text_sensor:064]: 'Mac Address': Sending state '00:1A:22:18:A6:5C'
[12:21:16][D][esp32_ble_client:306]: [0] [00:1A:22:18:A6:5C] Event 46
[12:21:16][D][text_sensor:064]: 'Lock BLE State': Sending state 'CONNECTED'
[12:21:16][D][eqiva_key_ble:231]: OTHER EVENT 46
[12:21:16][D][text_sensor:064]: 'Mac Address': Sending state '00:1A:22:18:A6:5C'
[12:21:16][D][text_sensor:064]: 'Lock BLE State': Sending state 'CONNECTED'
[12:21:16][D][eqiva_key_ble:063]: ESP_GATTC_REG_FOR_NOTIFY_EVT
[12:21:16][D][text_sensor:064]: 'Mac Address': Sending state '00:1A:22:18:A6:5C'
[12:21:16][D][text_sensor:064]: 'Lock BLE State': Sending state 'CONNECTED'
[12:21:16][D][eqiva_key_ble:063]: ESP_GATTC_REG_FOR_NOTIFY_EVT
[12:21:16][D][text_sensor:064]: 'Mac Address': Sending state '00:1A:22:18:A6:5C'
[12:21:16][D][text_sensor:064]: 'Lock BLE State': Sending state 'CONNECTED'
[12:21:16][D][eqiva_key_ble:063]: ESP_GATTC_REG_FOR_NOTIFY_EVT
[12:21:16][D][text_sensor:064]: 'Mac Address': Sending state '00:1A:22:18:A6:5C'
[12:21:16][D][text_sensor:064]: 'Lock BLE State': Sending state 'CONNECTED'
[12:21:16][D][eqiva_key_ble:063]: ESP_GATTC_REG_FOR_NOTIFY_EVT
[12:21:16][D][text_sensor:064]: 'Mac Address': Sending state '00:1A:22:18:A6:5C'
[12:21:16][D][text_sensor:064]: 'Lock BLE State': Sending state 'CONNECTED'
[12:21:16][D][eqiva_key_ble:063]: ESP_GATTC_REG_FOR_NOTIFY_EVT
[12:21:16][D][text_sensor:064]: 'Mac Address': Sending state '00:1A:22:18:A6:5C'
[12:21:16][D][esp32_ble_client:110]: [0] [00:1A:22:18:A6:5C] ESP_GATTC_SEARCH_CMPL_EVT
[12:21:16][I][esp32_ble_client:227]: [0] [00:1A:22:18:A6:5C] Connected
[12:21:16][D][text_sensor:064]: 'Lock BLE State': Sending state 'ESTABLISHED'
[12:21:16][D][eqiva_key_ble:040]: Write (UUID): 3141DD40-15DB-11E6-A24B-0002A5D5C51B  
[12:21:16][D][eqiva_key_ble:047]: Read (UUID): 359D4820-15DB-11E6-82BD-0002A5D5C51B  
[12:21:16][D][eqiva_key_ble:392]: Check send frag: not-empty, not-sending
[12:21:16][I][eqiva_key_ble:360]: Retaining message...
[12:21:16][I][eqiva_key_ble:377]: Reason: exchanging nonce
[12:21:16][I][eqiva_key_ble:380]: Reason: no remote session
[12:21:16][D][text_sensor:064]: 'Mac Address': Sending state '00:1A:22:18:A6:5C'
[12:21:16][D][esp32_ble_client:188]: [0] [00:1A:22:18:A6:5C] cfg_mtu status 0, mtu 23
[12:21:16][D][text_sensor:064]: 'Lock BLE State': Sending state 'ESTABLISHED'
[12:21:16][D][eqiva_key_ble:231]: OTHER EVENT 18
[12:21:16][D][text_sensor:064]: 'Mac Address': Sending state '00:1A:22:18:A6:5C'
[12:21:16][D][esp32_ble_client:110]: [0] [00:1A:22:18:A6:5C] ESP_GATTC_REG_FOR_NOTIFY_EVT
[12:21:16][D][esp32_ble_client:296]: Wrote notify descriptor 1, properties=26
[12:21:16][D][text_sensor:064]: 'Lock BLE State': Sending state 'ESTABLISHED'
[12:21:16][D][eqiva_key_ble:231]: OTHER EVENT 38
[12:21:16][D][text_sensor:064]: 'Mac Address': Sending state '00:1A:22:18:A6:5C'
[12:21:16][D][esp32_ble_client:110]: [0] [00:1A:22:18:A6:5C] ESP_GATTC_WRITE_CHAR_EVT
[12:21:16][D][text_sensor:064]: 'Lock BLE State': Sending state 'ESTABLISHED'
[12:21:16][D][eqiva_key_ble:067]: ESP_GATTC_WRITE_CHAR_EVT
[12:21:16][I][eqiva_key_ble:069]: Send successfull: 121670 | 121670 | 0
[12:21:16][D][eqiva_key_ble:392]: Check send frag: empty, not-sending
[12:21:16][D][text_sensor:064]: 'Mac Address': Sending state '00:1A:22:18:A6:5C'
[12:21:16][D][esp32_ble_client:110]: [0] [00:1A:22:18:A6:5C] ESP_GATTC_NOTIFY_EVT
[12:21:16][D][text_sensor:064]: 'Lock BLE State': Sending state 'ESTABLISHED'
[12:21:16][D][eqiva_key_ble:079]: ESP_GATTC_NOTIFY_EVT
[12:21:16][D][eqiva_key_ble:086]: LAST
[12:21:16][D][eqiva_key_ble:154]: Case 0x03
[12:21:17][D][eqiva_key_ble:162]: Nonce exchanged: B81EC5B5592403C9
[12:21:17][D][eqiva_key_ble:163]: Remote user_id: 6
[12:21:17][D][text_sensor:064]: 'User Key': Sending state '77696A766D746F68646C713469633566'
[12:21:17][D][text_sensor:064]: 'User ID': Sending state '6'
[12:21:17][D][eqiva_key_ble:392]: Check send frag: not-empty, not-sending
[12:21:17][D][eqiva_key_ble:392]: Check send frag: empty, sending
[12:21:17][D][text_sensor:064]: 'Mac Address': Sending state '00:1A:22:18:A6:5C'
[12:21:17][D][esp32_ble_client:110]: [0] [00:1A:22:18:A6:5C] ESP_GATTC_WRITE_DESCR_EVT
[12:21:17][D][text_sensor:064]: 'Lock BLE State': Sending state 'ESTABLISHED'
[12:21:17][D][eqiva_key_ble:075]: ESP_GATTC_WRITE_DESCR_EVT
[12:21:17][W][component:232]: Component esp32_ble took a long time for an operation (334 ms).
[12:21:17][W][component:233]: Components should block for at most 30 ms.
[12:21:17][D][text_sensor:064]: 'Mac Address': Sending state '00:1A:22:18:A6:5C'
[12:21:17][D][esp32_ble_client:110]: [0] [00:1A:22:18:A6:5C] ESP_GATTC_NOTIFY_EVT
[12:21:17][D][text_sensor:064]: 'Lock BLE State': Sending state 'ESTABLISHED'
[12:21:17][D][eqiva_key_ble:079]: ESP_GATTC_NOTIFY_EVT
[12:21:17][D][eqiva_key_ble:086]: LAST
[12:21:17][D][eqiva_key_ble:107]: # Auth value: 
[12:21:17][D][eqiva_key_ble:108]: 8015CC5E
[12:21:17][D][eqiva_key_ble:111]: # Crypted data: 
[12:21:17][D][eqiva_key_ble:112]: 2BB336C6DB72F65E
[12:21:17][D][eqiva_key_ble:125]: # Decrypted: 
[12:21:17][D][eqiva_key_ble:126]: 76810A0010170000
[12:21:17][D][eqiva_key_ble:185]: Case 0x83
[12:21:17][D][text_sensor:064]: 'Lock Status': Sending state 'UNLOCKED'
[12:21:17][D][text_sensor:064]: 'Low Battery': Sending state 'true'
[12:21:17][D][eqiva_key_ble:215]: # Lock state: 2
[12:21:17][D][eqiva_key_ble:216]: # Battery low: true
[12:21:17][D][eqiva_key_ble:392]: Check send frag: empty, sending
[12:21:17][D][text_sensor:064]: 'Mac Address': Sending state '00:1A:22:18:A6:5C'
[12:21:17][D][esp32_ble_client:110]: [0] [00:1A:22:18:A6:5C] ESP_GATTC_WRITE_CHAR_EVT
[12:21:17][D][text_sensor:064]: 'Lock BLE State': Sending state 'ESTABLISHED'
[12:21:17][D][eqiva_key_ble:067]: ESP_GATTC_WRITE_CHAR_EVT
[12:21:17][I][eqiva_key_ble:069]: Send successfull: 121670 | 121671 | 1
[12:21:17][D][eqiva_key_ble:392]: Check send frag: empty, not-sending
[12:21:17][W][component:232]: Component esp32_ble took a long time for an operation (102 ms).
[12:21:17][W][component:233]: Components should block for at most 30 ms.
[12:21:17][D][lock:054]: 'Eqiva Lock': Sending state UNLOCKED

My full yaml is

substitutions:
  devicename: nspanel-lock
  upper_devicename: "NSPanel Lock"

esphome:
  name: ${devicename}
  friendly_name: ${upper_devicename}

esp32:
  board: esp32dev
  framework:
    type: esp-idf
    # Uncomment below for ESP32-C3 if you have unexpected reboots when encrypting data
    # sdkconfig_options:
      # CONFIG_BOOTLOADER_WDT_TIME_MS: "60000"

external_components:
  - source: github://digaus/esphome-components-eqiva
#  # use esp32_ble components from ESPHome 2023.12.9
#  - source: github://esphome/esphome@2023.12.9
#    components: [ esp32_ble, esp32_ble_client, esp32_ble_tracker ]

  # use refresh when you do not get latest version
  # refresh: 0s
#external_components:
#  - source: github://paul-leitner/esphome-components-eqiva

api:
  encryption: 
    key: !secret nspanel_key
  services:
    - service: update_nextion
      then:
        - lambda: 'id(disp1)->upload_tft();'

ota:
  password: !secret api_password

# Enable Web server.
#web_server:
#  port: 80

# Enable logging
logger:

# WIFI
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  on_connect:
    - esp32_ble_tracker.start_scan:
        continuous: true
    - if:
        condition:
          lambda: 'return (strcmp(ESPHOME_VERSION, "2023.12.9") > 0);'
        then:
          lambda: 'id(key_ble)->set_auto_connect(true);'
    - eqiva_key_ble.connect:
       mac_address: !lambda 'return id(mac_address).state;' 
       user_id: !lambda 'return id(user_id).state;'
       user_key: !lambda 'return id(user_key).state;'
  on_disconnect:
    - esp32_ble_tracker.stop_scan:
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: ${upper_devicename} Fallback Hotspot
    password: !secret ap_password

#button, number and text input for pairing and setting mac/user_id/user-key via UI
button:
  - platform: template
    id: ble_settings
    name: BLE Settings _Save_
    icon: "mdi:content-save"
    on_press:
      - eqiva_key_ble.connect:
          mac_address: !lambda 'return id(mac_address).state;' 
          user_id: !lambda 'return id(user_id).state;'
          user_key: !lambda 'return id(user_key).state;'

  - platform: template
    id: ble_disconnect
    name: BLE Settings _Disconnect_
    icon: "mdi:clear"
    on_press:
      - eqiva_key_ble.disconnect

  - platform: template
    id: ble_pair
    name: BLE Pair _Start_
    icon: "mdi:check-underline"
    on_press:
      - eqiva_key_ble.pair:
          card_key: !lambda 'return id(card_key).state;' 

  - platform: template
    id: ble_open
    name: Open Door
    icon: "mdi:door-open"
    on_press:
      - eqiva_key_ble.connect:
          mac_address: !lambda 'return id(mac_address).state;' 
          user_id: !lambda 'return id(user_id).state;'
          user_key: !lambda 'return id(user_key).state;'
      - logger.log: "Waiting for connection..."
      - wait_until:
            lambda: 'return id(key_ble)->connected();'
      - eqiva_key_ble.open

  - platform: template
    id: lock_settings
    name: Lock Settings _Apply_
    icon: "mdi:check-underline"
    on_press:
      - eqiva_key_ble.settings:
          turn_left: !lambda 'return id(direction).state == "Left";' 
          key_horizontal: !lambda 'return id(position).state == "Horizontal";'
          lock_turns: !lambda 'return atoi(id(turns).state.c_str());'

  - platform: template
    id: upload_tft_exit_reparse
    on_press:
      - logger.log: "Button pressed: Update TFT display"
      - lambda: |-
          id(disp1).set_protocol_reparse_mode(false); // Force passive reparse protocol
          static const char *const TAG = "CUSTOM.script.upload_tft";
          ble_tracker->dump_config();
          ESP_LOGI(TAG, "Stopping BLE Tracker scan...");
          ble_tracker->stop_scan();
          ESP_LOGI(TAG, "Disabling BLE Tracker scan...");
          ble_tracker->set_scan_active(false);
          id(disp1).upload_tft(); // Upload TFT file
          ESP_LOGI(TAG, "Enabling BLE Tracker scan...");
          ble_tracker->set_scan_active(true);
          ESP_LOGI(TAG, "Starting BLE Tracker scan...");
          ble_tracker->start_scan();

  - platform: template
    name: ${upper_devicename} TFT Upload
    device_class: update
    entity_category: config
    on_press:
      - lambda: 'id(disp1)->upload_tft();'

number:
  - platform: template
    mode: box
    name: BLE Settings User ID
    id: user_id
    max_value: 7
    min_value: 0
    step: 1
    optimistic: true
    restore_value: true

text:
  - platform: template
    mode: text
    name: BLE Settings Mac Address
    id: mac_address
    optimistic: true
    restore_value: true
  - platform: template
    mode: text
    name: BLE Settings User Key
    id: user_key
    optimistic: true
    restore_value: true
  - platform: template
    mode: text
    name: BLE Pair Card Key
    id: card_key
    optimistic: true

select:
  - platform: template
    name: Lock Settings Close Direction
    id: direction
    options:
     - "Left"
     - "Right"
    optimistic: true
  - platform: template
    name: Lock Settings Key Position
    id: position
    options:
     - "Vertical"
     - "Horizontal"
    optimistic: true
  - platform: template
    name: Lock Settings Turns
    id: turns
    options:
     - "1"
     - "2"
     - "3"
     - "4"
    optimistic: true

esp32_ble_tracker:
  id: ble_tracker
  scan_parameters:
    window: 300ms
    # Activate scan only after wifi connect, see https://github.com/esphome/issues/issues/2941#issuecomment-1842369092
    continuous: false

eqiva_ble:
  id: key_lock

eqiva_key_ble:
  id: key_ble
  # Below can left empty because we connect in wifi on_connect
  # mac_address: 00:12:34:56:42:88
  # user_id: 0
  # user_key: 12345678636F6763386A726E33746F35

sensor:
  - platform: uptime
    name: "NSPanel Uptime"

text_sensor: 
  - platform: eqiva_key_ble
    mac_address: 
      name: "Mac Address"
    lock_status: 
      name: "Lock Status"
      id: lock_status
    low_battery:
      name: "Low Battery"
    lock_ble_state:
      name: "Lock BLE State"
    user_id:
      name: "User ID"
    user_key:
      name: "User Key"
    # on_raw_value:
            # then:   Do stuff on state change (!lambda "return x")
  - platform: version
    id: esphome_version
    name: "ESPHome Version"

# Call status every 4 minutes because lock seems to disconnect after 5 minutes of inactivity
# need to watch battery consumption, could also do some other time or present based approaches
time:
  - platform: sntp
    id: sntp_time
    timezone: Europe/Lisbon
    servers:
     - 192.168.10.1
    on_time:
      # Every 4 minutes
      - seconds: 0
        minutes: /10
        then:
          - logger.log: "Waiting for connection..."
          - eqiva_key_ble.connect:
              mac_address: !lambda 'return id(mac_address).state;' 
              user_id: !lambda 'return id(user_id).state;'
              user_key: !lambda 'return id(user_key).state;'
          - wait_until:
              condition:
                - lambda: 'return id(key_ble)->connected();'
              timeout: 10s
          - eqiva_key_ble.status:

# Lock component for HA, can also create two locks and use connect service to connect/control two different locks
# One ESP per lock is still recommended
lock:
  - platform: template
    id: eqiva_lock
    name: "Eqiva Lock"
    lambda: |-
      if (id(lock_status).state == "LOCKED") {
        return LOCK_STATE_LOCKED;
      } else if (id(lock_status).state == "UNLOCKED" || id(lock_status).state == "OPENED") {
        return LOCK_STATE_UNLOCKED;
      } else if(id(lock_status).state == "MOVING") {
        return {};
      } else if (id(lock_status).state == "UNKNOWN") {
        return LOCK_STATE_JAMMED;
      } 
      return LOCK_STATE_LOCKED;
    lock_action:
      - eqiva_key_ble.lock:
    unlock_action:
      - eqiva_key_ble.unlock:
    open_action:
      - eqiva_key_ble.open:

uart:
  id: tf_uart
  tx_pin: 16
  rx_pin: 17
  baud_rate: 115200

switch:
  - platform: gpio
    name: ${upper_devicename} relay 1
    id: relay_1
    pin:
      number: 22
    icon: mdi:electric-switch

  - platform: gpio
    name: ${upper_devicename} relay 2
    id: relay_2
    pin:
      number: 19
    icon: mdi:ceiling-light

  - platform: gpio
    name: ${upper_devicename} screen power
    id: screen_power
    entity_category: config
    pin:
      number: 4
      inverted: true
    restore_mode: ALWAYS_ON
    icon: mdi:television

binary_sensor:
  - platform: nextion
    name: ${upper_devicename} Action
    page_id: 0
    component_id: 1
    on_press:
      then:
        - eqiva_key_ble.connect:
            mac_address: !lambda 'return id(mac_address).state;' 
            user_id: !lambda 'return id(user_id).state;'
            user_key: !lambda 'return id(user_key).state;'
    on_release:
      then:
        - if:
            condition:
              lambda: 'return id(lock_status).state == "LOCKED";'
            then:
              - eqiva_key_ble.unlock
        - if:
            condition:
              lambda: 'return id(lock_status).state == "UNLOCKED";'
            then:
              - eqiva_key_ble.lock

  - platform: status
    name: "${upper_devicename} status"

  - platform: gpio
    name: "${upper_devicename} left button"
    pin:
      number: 14
      inverted: true
    icon: mdi:light-switch
    on_press:
      - lambda: 'id(disp1).send_command_printf("click screensaver,0");' # this wakes up the screen

  - platform: gpio
    name: "${upper_devicename} right button"
    pin:
      number: 27
      inverted: true
    icon: mdi:light-switch
    on_press:
      - switch.toggle: relay_2
      - lambda: 'id(disp1).send_command_printf("click screensaver,0");'

# Configure the screen itself
display:
  - platform: nextion
    id: disp1
    uart_id: tf_uart
    tft_url: http://172.16.3.20/nspanel/hmi.tft

script:
  - id: page_confirm
    mode: restart
    then:
      - lambda: 'display_wrapped_text->execute("confirm.title", "Confirme, Por Favor", 19);'

  - id: display_wrapped_text
    mode: parallel
    max_runs: 5
    parameters:
      component: string
      text_to_display: string
      line_length_limit: uint
    then:
      - lambda: |-
          int startPos = 0;
          int endPos = 0;
          std::string wrappedText = "";
          if (text_to_display.find("\\r") != std::string::npos) {
            wrappedText = text_to_display;
          } else {
            while (startPos < text_to_display.length()) {
              while (text_to_display[startPos] == ' ' and startPos < text_to_display.length()) { startPos++; }
              int endPos = startPos + line_length_limit;
              if (endPos >= text_to_display.length()) endPos = text_to_display.length();
              else
                {
                  while (endPos > startPos && text_to_display[endPos] != ' ') { endPos--; }
                  if (endPos == startPos) endPos = startPos + line_length_limit; // Handle case of long word
                }
              wrappedText += text_to_display.substr(startPos, endPos-startPos);
              if (endPos < text_to_display.length())
                {
                  while (text_to_display[endPos] == ' ') { endPos--; }
                  if (endPos >= startPos) wrappedText += "\\r";
                }
              startPos = endPos + 1; // Skip the space
              while (text_to_display[startPos] == ' ' and startPos < text_to_display.length()) { startPos++; }
            }
          }
          disp1->set_component_text(component.c_str(), wrappedText.c_str());

Wonder if that's hw related... I'll try this later on my old NodeMCU.

hmmmonteiro commented 2 months ago

NodeMCU

[16:23:17][I][eqiva_ble:015]: Found Eqiva device (MAC: 00:1A:22:18:A6:5C) (UUID): 0x1A00  (Name): KEY-BLE
[16:23:17][D][esp32_ble_client:110]: [0] [00:1A:22:18:A6:5C] Found device
[16:23:17][D][esp32_ble_tracker:661]: Found device 00:1A:22:18:A6:5C RSSI=-73
[16:23:17][D][esp32_ble_tracker:682]:   Address Type: PUBLIC
[16:23:17][D][esp32_ble_tracker:684]:   Name: 'KEY-BLE'
[16:23:17][D][text_sensor:064]: 'Lock BLE State': Sending state 'DISCOVERED'
[16:23:17][D][esp32_ble_tracker:215]: Pausing scan to make connection...
[16:23:17][D][text_sensor:064]: 'Lock BLE State': Sending state 'READY_TO_CONNECT'
[16:23:17][I][esp32_ble_client:067]: [0] [00:1A:22:18:A6:5C] 0x00 Attempting BLE connection
[16:23:17][D][text_sensor:064]: 'Lock BLE State': Sending state 'CONNECTING'
[16:23:18][D][text_sensor:064]: 'Mac Address': Sending state '00:1A:22:18:A6:5C'
[16:23:18][D][esp32_ble_client:110]: [0] [00:1A:22:18:A6:5C] ESP_GATTC_CONNECT_EVT
[16:23:18][D][text_sensor:064]: 'Lock BLE State': Sending state 'CONNECTING'
[16:23:18][D][eqiva_key_ble:231]: OTHER EVENT 40
[16:23:18][D][text_sensor:064]: 'Mac Address': Sending state '00:1A:22:18:A6:5C'
[16:23:18][D][esp32_ble_client:110]: [0] [00:1A:22:18:A6:5C] ESP_GATTC_OPEN_EVT
[16:23:18][D][text_sensor:064]: 'Lock BLE State': Sending state 'CONNECTED'
[16:23:18][D][text_sensor:064]: 'Lock BLE State': Sending state 'CONNECTED'
[16:23:18][D][eqiva_key_ble:231]: OTHER EVENT 2
[16:23:18][D][esp32_ble_tracker:266]: Starting scan...
[16:23:19][D][text_sensor:064]: 'Mac Address': Sending state '00:1A:22:18:A6:5C'
[16:23:19][D][esp32_ble_client:306]: [0] [00:1A:22:18:A6:5C] Event 46
[16:23:19][D][text_sensor:064]: 'Lock BLE State': Sending state 'CONNECTED'
[16:23:19][D][eqiva_key_ble:231]: OTHER EVENT 46
[16:23:20][D][text_sensor:064]: 'Mac Address': Sending state '00:1A:22:18:A6:5C'
[16:23:20][D][text_sensor:064]: 'Lock BLE State': Sending state 'CONNECTED'
[16:23:20][D][eqiva_key_ble:063]: ESP_GATTC_REG_FOR_NOTIFY_EVT
[16:23:20][D][text_sensor:064]: 'Mac Address': Sending state '00:1A:22:18:A6:5C'
[16:23:20][D][text_sensor:064]: 'Lock BLE State': Sending state 'CONNECTED'
[16:23:20][D][eqiva_key_ble:063]: ESP_GATTC_REG_FOR_NOTIFY_EVT
[16:23:20][D][text_sensor:064]: 'Mac Address': Sending state '00:1A:22:18:A6:5C'
[16:23:20][D][text_sensor:064]: 'Lock BLE State': Sending state 'CONNECTED'
[16:23:20][D][eqiva_key_ble:063]: ESP_GATTC_REG_FOR_NOTIFY_EVT
[16:23:20][D][text_sensor:064]: 'Mac Address': Sending state '00:1A:22:18:A6:5C'
[16:23:20][D][text_sensor:064]: 'Lock BLE State': Sending state 'CONNECTED'
[16:23:20][D][eqiva_key_ble:063]: ESP_GATTC_REG_FOR_NOTIFY_EVT
[16:23:20][D][text_sensor:064]: 'Mac Address': Sending state '00:1A:22:18:A6:5C'
[16:23:20][D][text_sensor:064]: 'Lock BLE State': Sending state 'CONNECTED'
[16:23:20][D][eqiva_key_ble:063]: ESP_GATTC_REG_FOR_NOTIFY_EVT
[16:23:20][D][text_sensor:064]: 'Mac Address': Sending state '00:1A:22:18:A6:5C'
[16:23:20][D][esp32_ble_client:110]: [0] [00:1A:22:18:A6:5C] ESP_GATTC_SEARCH_CMPL_EVT
[16:23:20][I][esp32_ble_client:227]: [0] [00:1A:22:18:A6:5C] Connected
[16:23:20][D][text_sensor:064]: 'Lock BLE State': Sending state 'ESTABLISHED'
[16:23:20][D][eqiva_key_ble:040]: Write (UUID): 3141DD40-15DB-11E6-A24B-0002A5D5C51B  
[16:23:20][D][eqiva_key_ble:047]: Read (UUID): 359D4820-15DB-11E6-82BD-0002A5D5C51B  
[16:23:20][D][eqiva_key_ble:392]: Check send frag: not-empty, not-sending
[16:23:20][I][eqiva_key_ble:360]: Retaining message...
[16:23:20][I][eqiva_key_ble:377]: Reason: exchanging nonce
[16:23:20][I][eqiva_key_ble:380]: Reason: no remote session
[16:23:20][D][text_sensor:064]: 'Mac Address': Sending state '00:1A:22:18:A6:5C'
[16:23:20][D][esp32_ble_client:188]: [0] [00:1A:22:18:A6:5C] cfg_mtu status 0, mtu 23
[16:23:20][D][text_sensor:064]: 'Lock BLE State': Sending state 'ESTABLISHED'
[16:23:20][D][eqiva_key_ble:231]: OTHER EVENT 18
[16:23:20][D][text_sensor:064]: 'Mac Address': Sending state '00:1A:22:18:A6:5C'
[16:23:20][D][esp32_ble_client:110]: [0] [00:1A:22:18:A6:5C] ESP_GATTC_REG_FOR_NOTIFY_EVT
[16:23:20][D][esp32_ble_client:296]: Wrote notify descriptor 1, properties=26
[16:23:20][D][text_sensor:064]: 'Lock BLE State': Sending state 'ESTABLISHED'
[16:23:20][D][eqiva_key_ble:231]: OTHER EVENT 38
[16:23:20][W][component:232]: Component esp32_ble took a long time for an operation (203 ms).
[16:23:20][W][component:233]: Components should block for at most 30 ms.
[16:23:20][D][text_sensor:064]: 'Mac Address': Sending state '00:1A:22:18:A6:5C'
[16:23:20][D][esp32_ble_client:110]: [0] [00:1A:22:18:A6:5C] ESP_GATTC_WRITE_CHAR_EVT
[16:23:20][D][text_sensor:064]: 'Lock BLE State': Sending state 'ESTABLISHED'
[16:23:20][D][eqiva_key_ble:067]: ESP_GATTC_WRITE_CHAR_EVT
[16:23:20][I][eqiva_key_ble:069]: Send successfull: 9 | 9 | 0
[16:23:20][D][eqiva_key_ble:392]: Check send frag: empty, not-sending
[16:23:20][D][text_sensor:064]: 'Mac Address': Sending state '00:1A:22:18:A6:5C'
[16:23:20][D][esp32_ble_client:110]: [0] [00:1A:22:18:A6:5C] ESP_GATTC_NOTIFY_EVT
[16:23:20][D][text_sensor:064]: 'Lock BLE State': Sending state 'ESTABLISHED'
[16:23:20][D][eqiva_key_ble:079]: ESP_GATTC_NOTIFY_EVT
[16:23:20][D][eqiva_key_ble:086]: LAST
[16:23:20][D][eqiva_key_ble:154]: Case 0x03
[16:23:20][D][eqiva_key_ble:162]: Nonce exchanged: 841CA0454D7CF1D4
[16:23:20][D][eqiva_key_ble:163]: Remote user_id: 0
[16:23:20][D][text_sensor:064]: 'User Key': Sending state '316670796E706C35726A656F70703731'
[16:23:20][D][text_sensor:064]: 'User ID': Sending state '0'
[16:23:20][D][eqiva_key_ble:392]: Check send frag: not-empty, not-sending
[16:23:20][D][eqiva_key_ble:392]: Check send frag: empty, sending
[16:23:20][W][component:232]: Component esp32_ble took a long time for an operation (53 ms).
[16:23:20][W][component:233]: Components should block for at most 30 ms.
[16:23:20][D][text_sensor:064]: 'Mac Address': Sending state '00:1A:22:18:A6:5C'
[16:23:20][D][esp32_ble_client:110]: [0] [00:1A:22:18:A6:5C] ESP_GATTC_WRITE_DESCR_EVT
[16:23:20][D][text_sensor:064]: 'Lock BLE State': Sending state 'ESTABLISHED'
[16:23:20][D][eqiva_key_ble:075]: ESP_GATTC_WRITE_DESCR_EVT
[16:23:20][D][text_sensor:064]: 'Mac Address': Sending state '00:1A:22:18:A6:5C'
[16:23:20][D][esp32_ble_client:110]: [0] [00:1A:22:18:A6:5C] ESP_GATTC_NOTIFY_EVT
[16:23:20][D][text_sensor:064]: 'Lock BLE State': Sending state 'ESTABLISHED'
[16:23:20][D][eqiva_key_ble:079]: ESP_GATTC_NOTIFY_EVT
[16:23:20][D][eqiva_key_ble:086]: LAST
[16:23:20][D][eqiva_key_ble:107]: # Auth value: 
[16:23:20][D][eqiva_key_ble:108]: 0348FAC9
[16:23:20][D][eqiva_key_ble:111]: # Crypted data: 
[16:23:20][D][eqiva_key_ble:112]: BC81589328FB8C1C
[16:23:20][D][eqiva_key_ble:125]: # Decrypted: 
[16:23:20][D][eqiva_key_ble:126]: 70810B0010170000
[16:23:20][D][eqiva_key_ble:185]: Case 0x83
[16:23:20][D][text_sensor:064]: 'Lock Status': Sending state 'LOCKED'
[16:23:20][D][text_sensor:064]: 'Low Battery': Sending state 'true'
[16:23:20][D][eqiva_key_ble:215]: # Lock state: 3
[16:23:20][D][eqiva_key_ble:216]: # Battery low: true
[16:23:20][D][eqiva_key_ble:392]: Check send frag: empty, sending
[16:23:20][D][text_sensor:064]: 'Mac Address': Sending state '00:1A:22:18:A6:5C'
[16:23:20][D][esp32_ble_client:110]: [0] [00:1A:22:18:A6:5C] ESP_GATTC_WRITE_CHAR_EVT
[16:23:20][D][text_sensor:064]: 'Lock BLE State': Sending state 'ESTABLISHED'
[16:23:20][D][eqiva_key_ble:067]: ESP_GATTC_WRITE_CHAR_EVT
[16:23:20][I][eqiva_key_ble:069]: Send successfull: 9 | 9 | 0
[16:23:20][D][eqiva_key_ble:392]: Check send frag: empty, not-sending

and respective yaml

substitutions:
  devicename: equiva-test
  upper_devicename: "Eqiva Test"

esphome:
  name: ${devicename}
  friendly_name: ${upper_devicename}

esp32:
  board: esp32dev
  framework:
    type: esp-idf

external_components:
  - source: github://digaus/esphome-components-eqiva

api:
  encryption: 
    key: !secret nspanel_key

ota:
  password: !secret ota_password

# Enable logging
logger:

# WIFI
wifi:
  ssid: !secret iot_wifi_ssid
  password: !secret iot_wifi_password
  on_connect:
    - esp32_ble_tracker.start_scan:
        continuous: true
    - if:
        condition:
          lambda: 'return (strcmp(ESPHOME_VERSION, "2023.12.9") > 0);'
        then:
          lambda: 'id(key_ble)->set_auto_connect(true);'
    - eqiva_key_ble.connect:
       mac_address: !lambda 'return id(mac_address).state;' 
       user_id: !lambda 'return id(user_id).state;'
       user_key: !lambda 'return id(user_key).state;'
  on_disconnect:
    - esp32_ble_tracker.stop_scan:
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: ${upper_devicename} Fallback Hotspot
    password: !secret iot_wifi_password

#button, number and text input for pairing and setting mac/user_id/user-key via UI
button:
  - platform: template
    id: ble_settings
    name: BLE Settings _Save_
    icon: "mdi:content-save"
    on_press:
      - eqiva_key_ble.connect:
          mac_address: !lambda 'return id(mac_address).state;' 
          user_id: !lambda 'return id(user_id).state;'
          user_key: !lambda 'return id(user_key).state;'

  - platform: template
    id: ble_disconnect
    name: BLE Settings _Disconnect_
    icon: "mdi:clear"
    on_press:
      - eqiva_key_ble.disconnect

  - platform: template
    id: ble_pair
    name: BLE Pair _Start_
    icon: "mdi:check-underline"
    on_press:
      - eqiva_key_ble.pair:
          card_key: !lambda 'return id(card_key).state;' 

  - platform: template
    id: ble_open
    name: Open Door
    icon: "mdi:door-open"
    on_press:
      - eqiva_key_ble.connect:
          mac_address: !lambda 'return id(mac_address).state;' 
          user_id: !lambda 'return id(user_id).state;'
          user_key: !lambda 'return id(user_key).state;'
      - logger.log: "Waiting for connection..."
      - wait_until:
            lambda: 'return id(key_ble)->connected();'
      - eqiva_key_ble.open

  - platform: template
    id: lock_settings
    name: Lock Settings _Apply_
    icon: "mdi:check-underline"
    on_press:
      - eqiva_key_ble.settings:
          turn_left: !lambda 'return id(direction).state == "Left";' 
          key_horizontal: !lambda 'return id(position).state == "Horizontal";'
          lock_turns: !lambda 'return atoi(id(turns).state.c_str());'

number:
  - platform: template
    mode: box
    name: BLE Settings User ID
    id: user_id
    max_value: 7
    min_value: 0
    step: 1
    optimistic: true
    restore_value: true

text:
  - platform: template
    mode: text
    name: BLE Settings Mac Address
    id: mac_address
    optimistic: true
    restore_value: true
  - platform: template
    mode: text
    name: BLE Settings User Key
    id: user_key
    optimistic: true
    restore_value: true
  - platform: template
    mode: text
    name: BLE Pair Card Key
    id: card_key
    optimistic: true

select:
  - platform: template
    name: Lock Settings Close Direction
    id: direction
    options:
     - "Left"
     - "Right"
    optimistic: true
  - platform: template
    name: Lock Settings Key Position
    id: position
    options:
     - "Vertical"
     - "Horizontal"
    optimistic: true
  - platform: template
    name: Lock Settings Turns
    id: turns
    options:
     - "1"
     - "2"
     - "3"
     - "4"
    optimistic: true

esp32_ble_tracker:
  id: ble_tracker
  scan_parameters:
    window: 300ms
    # Activate scan only after wifi connect, see https://github.com/esphome/issues/issues/2941#issuecomment-1842369092
    continuous: false

eqiva_ble:
  id: key_lock

eqiva_key_ble:
  id: key_ble
  # Below can left empty because we connect in wifi on_connect
  # mac_address: 00:12:34:56:42:88
  # user_id: 0
  # user_key: 12345678636F6763386A726E33746F35

sensor:
  - platform: uptime
    name: "Eqiva Test Uptime"

text_sensor: 
  - platform: eqiva_key_ble
    mac_address: 
      name: "Mac Address"
    lock_status: 
      name: "Lock Status"
      id: lock_status
    low_battery:
      name: "Low Battery"
    lock_ble_state:
      name: "Lock BLE State"
    user_id:
      name: "User ID"
    user_key:
      name: "User Key"
    # on_raw_value:
            # then:   Do stuff on state change (!lambda "return x")
  - platform: version
    id: esphome_version
    name: "ESPHome Version"

# Call status every 4 minutes because lock seems to disconnect after 5 minutes of inactivity
# need to watch battery consumption, could also do some other time or present based approaches
time:
  - platform: sntp
    id: sntp_time
    timezone: Europe/Lisbon
    servers:
     - 192.168.10.1
    on_time:
      # Every 4 minutes
      - seconds: 0
        minutes: /10
        then:
          - logger.log: "Waiting for connection..."
          - eqiva_key_ble.connect:
              mac_address: !lambda 'return id(mac_address).state;' 
              user_id: !lambda 'return id(user_id).state;'
              user_key: !lambda 'return id(user_key).state;'
          - wait_until:
              condition:
                - lambda: 'return id(key_ble)->connected();'
              timeout: 10s
          - eqiva_key_ble.status:

# Lock component for HA, can also create two locks and use connect service to connect/control two different locks
# One ESP per lock is still recommended
lock:
  - platform: template
    id: eqiva_lock
    name: "Eqiva Lock"
    lambda: |-
      if (id(lock_status).state == "LOCKED") {
        return LOCK_STATE_LOCKED;
      } else if (id(lock_status).state == "UNLOCKED" || id(lock_status).state == "OPENED") {
        return LOCK_STATE_UNLOCKED;
      } else if(id(lock_status).state == "MOVING") {
        return {};
      } else if (id(lock_status).state == "UNKNOWN") {
        return LOCK_STATE_JAMMED;
      } 
      return LOCK_STATE_LOCKED;
    lock_action:
      - eqiva_key_ble.lock:
    unlock_action:
      - eqiva_key_ble.unlock:
    open_action:
      - eqiva_key_ble.open:

I did get a bit of trouble getting it going but after a factory reset on the lock things started working properly.

sobrarbe commented 2 months ago

Hello. First of all, thank you for the work you are doing with this lock. I have the same problem. In ESPHome Eqiva Lock when I put the mac on and the eqiva lock does not connect. This comes up all the time. Do you know of any solution to connect it? Thank you very much for the help ![Uploading IMG_20240406_174235.jpg…]()

digaus commented 2 months ago

Please use this workaround until I find time to update:

https://github.com/digaus/esphome-components-eqiva/issues/11#issuecomment-1965232328

digaus commented 2 months ago

Updated and should work again with latest esphome version.

user45876 commented 2 months ago

Thanks a lot, dear digaus. Tested it by removing the workaround and can confirm that it is working as intended with ESPHome 2024.4.1.

Thank you very much!

RoP09 commented 2 months ago

For me it is not working instantly, but also last time I had to flash the ESP32 again using USB (and remove pairing, redo everything, have to try that later...):

12:23:12 | [D] | [text_sensor:064] | 'Lock BLE State': Sending state 'IDLE' 12:23:12 | [D] | [eqiva_key_ble:058] | ESP_GATTC_DISCONNECT_EVT 12:23:12 | [D] | [text_sensor:064] | 'Mac Address': Sending state '00:1A:22:13:6F:77' 12:23:12 | [D] | [esp32_ble_client:110] | [0] [00:1A:22:13:6F:77] ESP_GATTC_OPEN_EVT 12:23:12 | [W] | [esp32_ble_client:143] | [0] [00:1A:22:13:6F:77] Connection failed, status=133 12:23:12 | [D] | [text_sensor:064] | 'Lock BLE State': Sending state 'IDLE' 12:23:12 | [D] | [esp32_ble_tracker:266] | Starting scan... 12:23:13 | [D] | [esp32_ble_client:110] | [0] [00:1A:22:13:6F:77] Found device 12:23:13 | [D] | [esp32_ble_tracker:665] | Found device 00:1A:22:13:6F:77 RSSI=-85 12:23:13 | [D] | [esp32_ble_tracker:686] | Address Type: PUBLIC 12:23:13 | [D] | [esp32_ble_tracker:688] | Name: 'KEY-BLE' 12:23:13 | [D] | [text_sensor:064] | 'Lock BLE State': Sending state 'DISCOVERED' 12:23:13 | [D] | [esp32_ble_tracker:215] | Pausing scan to make connection... 12:23:13 | [D] | [esp32_ble_tracker:215] | Pausing scan to make connection... 12:23:13 | [D] | [esp-idf:000] | E (168585) BT_BTM: BTM_BleScan scan not ac 12:23:13 | [D] | [esp-idf:000] | W (168588) BT_APPL: bta_dm_ble_scan stop scan failed, status 12:23:13 | [D] | [text_sensor:064] | 'Lock BLE State': Sending state 'READY_TO_CONNECT' 12:23:13 | [I] | [esp32_ble_client:067] | [0] [00:1A:22:13:6F:77] 0x00 Attempting BLE connection 12:23:13 | [D] | [text_sensor:064] | 'Lock BLE State': Sending state 'CONNECTING' 12:23:15 | [D] | [esp-idf:000] | W (170571) BT_HCI: hcif disc complete: hdl 0x0, rsn 0x3e 12:23:16 | [D] | [esp-idf:000] | W (171580) BT_HCI: hcif disc complete: hdl 0x0, rsn 0x3e 12:23:16 | [D] | [sensor:094] | 'Eqiva Test Uptime': Sending state 170.68600 s with 0 decimals of accuracy 12:23:17 | [D] | [esp-idf:000] | W (172574) BT_HCI: hcif disc complete: hdl 0x0, rsn 0x3e 12:23:18 | [D] | [esp-idf:000] | W (173590) BT_APPL: gattc_conn_cb: if=3 st=0 id=3 rsn=0x3e 12:23:18 | [D] | [esp-idf:000] | W (173599) BT_HCI: hcif disc complete: hdl 0x0, rsn 0x3e 12:23:18 | [D] | [text_sensor:064] | 'Mac Address': Sending state '00:1A:22:13:6F:77' 12:23:18 | [D] | [esp32_ble_client:172] | [0] [00:1A:22:13:6F:77] ESP_GATTC_DISCONNECT_EVT, reason 62 12:23:18 | [D] | [text_sensor:064] | 'Lock BLE State': Sending state 'IDLE' 12:23:18 | [D] | [text_sensor:064] | 'Lock BLE State': Sending state 'IDLE' 12:23:18 | [D] | [eqiva_key_ble:058] | ESP_GATTC_DISCONNECT_EVT 12:23:18 | [D] | [text_sensor:064] | 'Mac Address': Sending state '00:1A:22:13:6F:77' 12:23:18 | [D] | [esp32_ble_client:110] | [0] [00:1A:22:13:6F:77] ESP_GATTC_OPEN_EVT 12:23:18 | [W] | [esp32_ble_client:143] |  

sobrarbe commented 1 month ago

Hello, it works for me but now in HA I have to update to version 2024.4.1 and it won't let me and I get this when trying to look.

Compiling .pioenvs/esphome-eqiva-lock/src/main.o src/main.cpp: In function 'void setup()': src/main.cpp:636:12: error: 'class esphome::eqiva_key_ble::EqivaKeyBle' has no member named 'set_auto_connect'; did you mean 'set_turn_left'? key_ble->set_auto_connect(true); ^~~~ set_turn_left Compiling .pioenvs/esphome-eqiva-lock/bt/host/bluedroid/stack/btm/btm_sec.o Compiling .pioenvs/esphome-eqiva-lock/bt/host/bluedroid/stack/btu/btu_hcif.o Compiling .pioenvs/esphome-eqiva-lock/bt/host/bluedroid/stack/btu/btu_init.o Compiling .pioenvs/esphome-eqiva-lock/bt/host/bluedroid/stack/btu/btu_task.o *** [.pioenvs/esphome-eqiva-lock/src/main.o] Error 1 ========================== [FAILED] Took 6.69 seconds ==================== ========

Any solution for this? thank you

user45876 commented 1 month ago

Have you removed the workaround from post #11?

hmmmonteiro commented 1 month ago

Hi.

Make sure that you're no longer using the workaround, and that you clean your build files before building again.

On Mon, Apr 29, 2024, 21:09 sobrarbe @.***> wrote:

Hello, it works for me but now in HA I have to update to version 2024.4.1 and it won't let me and I get this when trying to look.

Compiling .pioenvs/esphome-eqiva-lock/src/main.o src/main.cpp: In function 'void setup()': src/main.cpp:636:12: error: 'class esphome::eqiva_key_ble::EqivaKeyBle' has no member named 'set_auto_connect'; did you mean 'set_turn_left'? key_ble->set_auto_connect(true); ^~~~ set_turn_left Compiling .pioenvs/esphome-eqiva-lock/bt/host/bluedroid/stack/btm/btm_sec.o Compiling .pioenvs/esphome-eqiva-lock/bt/host/bluedroid/stack/btu/btu_hcif.o Compiling .pioenvs/esphome-eqiva-lock/bt/host/bluedroid/stack/btu/btu_init.o Compiling .pioenvs/esphome-eqiva-lock/bt/host/bluedroid/stack/btu/btu_task.o *** [.pioenvs/esphome-eqiva-lock/src/main.o] Error 1 ========================== [FAILED] Took 6.69 seconds ====================

Any solution for this? thank you

— Reply to this email directly, view it on GitHub https://github.com/digaus/esphome-components-eqiva/issues/11#issuecomment-2083572356, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACFPFVJ3UV4RQQWESTSWJXTY72SHRAVCNFSM6AAAAABDTF7RJSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAOBTGU3TEMZVGY . You are receiving this because you commented.Message ID: @.***>

hmmmonteiro commented 1 month ago

Working like a charm on my NSPanel. @digaus , thank you for the elegant solution.

Cheers!

RoP09 commented 1 month ago

It works, but only for some days, then I get the same issues again as before (cannot connect to the lock). Requires to wipe the ESP.

Never had this issue with the "old" implementation before:

` 13:55:17 [D] [esp32_ble_tracker:215] Pausing scan to make connection...
13:55:17 [D] [esp-idf:000] E (57550348) BT_BTM: BTM_BleScan scan not ac
13:55:17 [D] [text_sensor:064] 'Lock BLE State': Sending state 'READY_TO_CONNECT'
13:55:17 [I] [esp32_ble_client:067] [0] [00:1A:22:13:6F:77] 0x00 Attempting BLE connection
13:55:18 [D] [text_sensor:064] 'Lock BLE State': Sending state 'CONNECTING'
13:55:20 [D] [esp-idf:000] W (57552394) BT_HCI: hcif disc complete: hdl 0x0, rsn 0x3e
13:55:21 [D] [esp-idf:000] W (57553399) BT_HCI: hcif disc complete: hdl 0x0, rsn 0x3e
13:55:22 [D] [esp-idf:000] W (57554404) BT_HCI: hcif disc complete: hdl 0x0, rsn 0x3e
13:55:23 [D] [esp-idf:000] W (57555409) BT_APPL: gattc_conn_cb: if=3 st=0 id=3 rsn=0x3e
13:55:23 [D] [esp-idf:000] W (57555415) BT_HCI: hcif disc complete: hdl 0x0, rsn 0x3e
13:55:23 [D] [text_sensor:064] 'Mac Address': Sending state '00:1A:22:13:6F:77'
13:55:23 [D] [esp32_ble_client:172] [0] [00:1A:22:13:6F:77] ESP_GATTC_DISCONNECT_EVT, reason 62
13:55:23 [D] [text_sensor:064] 'Lock BLE State': Sending state 'IDLE'
13:55:23 [D] [text_sensor:064] 'Lock BLE State': Sending state 'IDLE'
13:55:23 [D] [eqiva_key_ble:058] ESP_GATTC_DISCONNECT_EVT
13:55:23 [D] [text_sensor:064] 'Mac Address': Sending state '00:1A:22:13:6F:77'
13:55:23 [D] [esp32_ble_client:110] [0] [00:1A:22:13:6F:77] ESP_GATTC_OPEN_EVT
13:55:23 [W] [esp32_ble_client:143] [0] [00:1A:22:13:6F:77] Connection failed, status=133
13:55:23 [D] [text_sensor:064] 'Lock BLE State': Sending state 'IDLE'

`

digaus commented 1 month ago

Are you staying connected to the lock? Or are you always connecting/disconnecting ?

Found a related issue: https://github.com/espressif/esp-idf/issues/2810

Maybe the reception is not good enough to connect with 100% reliability? What is the RSSI value ?

RoP09 commented 1 month ago

Usually I stay connected all the time and it works, but after some time (days/weeks) it seems it is no longer able to connect at all.

I am behind a wall with the ESP32, but only about <1.5m away from the lock. I will check if it improves if I put it directly next to the lock, thanks!