Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
AzonInc committed Apr 3, 2024
1 parent 83b4ef6 commit c992395
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
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
42 changes: 24 additions & 18 deletions components/nuki_lock/nuki_lock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,8 @@ namespace esphome
{
ESP_LOGI(TAG, "Starting NUKI Lock...");

/*esp_task_wdt_config_t wdtc;
wdtc.idle_core_mask = 0;
wdtc.timeout_ms = 15000;
wdtc.trigger_panic = false;
esp_task_wdt_reconfigure(&wdtc);*/

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

this->traits.set_supported_states(
Expand All @@ -211,14 +207,19 @@ namespace esphome
this->nukiLock_.setConnectTimeout(BLE_CONNECT_TIMEOUT_SEC);
this->nukiLock_.setConnectRetries(BLE_CONNECT_TIMEOUT_RETRIES);

bool result = this->nukiLock_.saveSecurityPincode(1234);
if (result) {
ESP_LOGI(TAG, "Set pincode done");

} else {
ESP_LOGE(TAG, "Set pincode failed!");
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 @@ -245,19 +246,21 @@ namespace esphome
// Check for new advertisements
this->scanner_.update();
App.feed_wdt();
yield();
delay(20);

// Terminate stale Bluetooth connections
this->nukiLock_.updateConnectionState();

/*if(this->pairing_mode_ && this->pairing_mode_timer_ != 0)
if(this->pairing_mode_ && this->pairing_mode_timer_ != 0)
{
if(millis() > this->pairing_mode_timer_)
ESP_LOGI(TAG, "Pairing timed out. Timeout: %d", this->pairing_mode_timer_);

/*if(millis() > this->pairing_mode_timer_)
{
ESP_LOGV(TAG, "Pairing timed out, turning off pairing mode");
this->set_pairing_mode(false);
}
}*/
}*/
}

if (millis() - lastCommandExecutedTime_ < command_cooldown_millis)
{
Expand Down Expand Up @@ -619,12 +622,15 @@ namespace esphome

// Turn on for ... seconds
this->pairing_mode_timer_ = millis() + (this->pairing_timeout_ * 1000);
ESP_LOGI(TAG, "Pairing Timeout set to: %d", this->pairing_mode_timer_);
}
else
{
ESP_LOGI(TAG, "Pairing Mode turned off");
this->pairing_mode_timer_ = 0;
this->pairing_mode_off_callback_.call();

ESP_LOGI(TAG, "Pairing Timeout set to: %d", this->pairing_mode_timer_);
}
}

Expand Down
4 changes: 4 additions & 0 deletions components/nuki_lock/nuki_lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,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 @@ -111,6 +113,8 @@ namespace esphome {
bool open_latch_;
bool lock_n_go_;

uint16_t security_pin = 0;

uint16_t pairing_timeout_ = 0;

bool pairing_mode_ = false;
Expand Down

0 comments on commit c992395

Please sign in to comment.