Skip to content

Commit

Permalink
Fix Watchdog Bootcycle, Add Security Pin
Browse files Browse the repository at this point in the history
  • Loading branch information
AzonInc committed Apr 3, 2024
1 parent 32cfb98 commit d7a329a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ lock:
name: "Nuki Unpair"
pairing_mode:
name: "Nuki Pairing Mode"
security_pin: 1234

# Optional: Callbacks
on_pairing_mode_on_action:
Expand Down Expand Up @@ -110,8 +111,8 @@ The module is a Fork of [ESPHome_nuki_lock](https://github.com/uriyacovy/ESPHome
## Tested Hardware
- ESP32 wroom
- Nuki smart lock v3
- Nuki smart lock v2
- Nuki door sensor
- ESP32 WROOM
- Nuki Smart Lock v4
- Nuki Smart Lock v3
- Nuki Smart Lock v2
- Nuki Door Sensor
5 changes: 5 additions & 0 deletions components/nuki_lock/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

CONF_IS_CONNECTED = "is_connected"
CONF_IS_PAIRED = "is_paired"
CONF_SECURITY_PIN = "security_pin"
CONF_UNPAIR_BUTTON = "unpair"
CONF_PAIRING_MODE_SWITCH = "pairing_mode"
CONF_BATTERY_CRITICAL = "battery_critical"
Expand Down Expand Up @@ -60,6 +61,7 @@
device_class=DEVICE_CLASS_CONNECTIVITY,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
cv.Optional(CONF_SECURITY_PIN, default=0): cv.uint16_t,
cv.Optional(CONF_BATTERY_CRITICAL): binary_sensor.binary_sensor_schema(
device_class=DEVICE_CLASS_BATTERY,
),
Expand Down Expand Up @@ -113,6 +115,9 @@ async def to_code(config):
sens = await binary_sensor.new_binary_sensor(config[CONF_IS_PAIRED])
cg.add(var.set_is_paired(sens))

if CONF_SECURITY_PIN in config:
cg.add(var.set_security_pin(config[CONF_SECURITY_PIN]))

if CONF_BATTERY_CRITICAL in config:
sens = await binary_sensor.new_binary_sensor(config[CONF_BATTERY_CRITICAL])
cg.add(var.set_battery_critical(sens))
Expand Down
28 changes: 25 additions & 3 deletions components/nuki_lock/nuki_lock.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "esphome/core/log.h"
#include "esphome/core/application.h"
#include "nuki_lock.h"

namespace esphome
Expand Down Expand Up @@ -180,9 +181,12 @@ namespace esphome

void NukiLockComponent::setup()
{

ESP_LOGI(TAG, "Starting NUKI Lock...");

// Increase Watchdog Timeout
// Fixes Pairing Crash
esp_task_wdt_init(15, false);

this->traits.set_supported_states(
std::set<lock::LockState>
{
Expand All @@ -203,6 +207,19 @@ namespace esphome
this->nukiLock_.setConnectTimeout(BLE_CONNECT_TIMEOUT_SEC);
this->nukiLock_.setConnectRetries(BLE_CONNECT_TIMEOUT_RETRIES);

if(this->security_pin_ > 0)
{
bool result = this->nukiLock_.saveSecurityPincode(this->security_pin_);
if (result)
{
ESP_LOGI(TAG, "Set pincode done");
}
else
{
ESP_LOGE(TAG, "Set pincode failed!");
}
}

if (this->nukiLock_.isPairedWithLock())
{
this->status_update_ = true;
Expand All @@ -228,6 +245,7 @@ namespace esphome
{
// Check for new advertisements
this->scanner_.update();
App.feed_wdt();
delay(20);

// Terminate stale Bluetooth connections
Expand Down Expand Up @@ -312,6 +330,9 @@ namespace esphome
}
else
{
this->is_paired_->publish_state(false);
this->is_connected_->publish_state(false);

// Pairing Mode is active
if(this->pairing_mode_)
{
Expand Down Expand Up @@ -563,8 +584,8 @@ namespace esphome
{
if(this->nukiLock_.isPairedWithLock())
{
ESP_LOGW(TAG, "Unpair requested");
this->nukiLock_.unPairNuki();
ESP_LOGI(TAG, "Unpaired Nuki! Turn on Pairing Mode to pair a new Nuki.");
}
else
{
Expand Down Expand Up @@ -601,7 +622,8 @@ namespace esphome
ESP_LOGI(TAG, "Waiting for Nuki to enter pairing mode...");

// Turn on for ... seconds
this->pairing_mode_timer_ = millis() + (this->pairing_timeout_ * 1000);
uint32_t now_millis = millis();
this->pairing_mode_timer_ = now_millis + (this->pairing_timeout_ * 1000);
}
else
{
Expand Down
7 changes: 6 additions & 1 deletion components/nuki_lock/nuki_lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "NukiConstants.h"
#include "BleScanner.h"


namespace esphome {
namespace nuki_lock {

Expand Down Expand Up @@ -50,6 +51,8 @@ namespace esphome {
void set_unpair_button(button::Button *unpair_button) { this->unpair_button_ = unpair_button; }
void set_pairing_mode_switch(switch_::Switch *pairing_mode_switch) { this->pairing_mode_switch_ = pairing_mode_switch; }

void set_security_pin(uint16_t security_pin) { this->security_pin_ = security_pin; }

void set_pairing_timeout(uint16_t pairing_timeout) { this->pairing_timeout_ = pairing_timeout; }

void add_pairing_mode_on_callback(std::function<void()> &&callback);
Expand Down Expand Up @@ -104,10 +107,12 @@ namespace esphome {
bool open_latch_;
bool lock_n_go_;

uint16_t security_pin_ = 0;

uint16_t pairing_timeout_ = 0;

bool pairing_mode_ = false;
uint16_t pairing_mode_timer_ = 0;
uint32_t pairing_mode_timer_ = 0;

private:
NukiLock::NukiLock nukiLock_;
Expand Down

0 comments on commit d7a329a

Please sign in to comment.