Skip to content

Commit 37dca9d

Browse files
committed
Refactor update handling and timeouts
1 parent da77885 commit 37dca9d

File tree

2 files changed

+54
-49
lines changed

2 files changed

+54
-49
lines changed

components/nuki_lock/nuki_lock.cpp

+47-45
Original file line numberDiff line numberDiff line change
@@ -989,13 +989,10 @@ void NukiLockComponent::setup() {
989989
if (this->nuki_lock_.isPairedWithLock()) {
990990
this->status_update_ = true;
991991

992+
// First boot: Request config and auth data
992993
this->config_update_ = true;
993994
this->advanced_config_update_ = true;
994-
995-
// First auth data request, then every 2nd time
996-
// Requesting only when events are enabled
997995
if (this->send_events_) {
998-
this->auth_data_required_ = true;
999996
this->auth_data_update_ = true;
1000997
}
1001998

@@ -1006,6 +1003,9 @@ void NukiLockComponent::setup() {
10061003
this->is_paired_binary_sensor_->publish_initial_state(true);
10071004
}
10081005
#endif
1006+
1007+
this->setup_intervals();
1008+
10091009
} else {
10101010
ESP_LOGW(TAG, "%s Nuki is not paired", this->deviceName_.c_str());
10111011
#ifdef USE_BINARY_SENSOR
@@ -1027,6 +1027,22 @@ void NukiLockComponent::setup() {
10271027
#endif
10281028
}
10291029

1030+
void NukiLockComponent::setup_intervals(bool setup) {
1031+
this->cancel_interval("update_config");
1032+
this->cancel_interval("update_auth_data");
1033+
1034+
if(setup) {
1035+
this->set_interval("update_config", CONFIG_UPDATE_INTERVAL_SEC * 1000, [this]() {
1036+
this->config_update_ = true;
1037+
this->advanced_config_update_ = true;
1038+
});
1039+
1040+
this->set_interval("update_auth_data", AUTH_DATA_UPDATE_INTERVAL_SEC * 1000, [this]() {
1041+
this->auth_data_update_ = true;
1042+
});
1043+
}
1044+
}
1045+
10301046
void NukiLockComponent::update() {
10311047
// Check for new advertisements
10321048
this->scanner_.update();
@@ -1036,13 +1052,6 @@ void NukiLockComponent::update() {
10361052
// Terminate stale Bluetooth connections
10371053
this->nuki_lock_.updateConnectionState();
10381054

1039-
if (this->pairing_mode_ && this->pairing_mode_timer_ != 0) {
1040-
if (millis() > this->pairing_mode_timer_) {
1041-
ESP_LOGV(TAG, "Pairing timed out, turning off pairing mode");
1042-
this->set_pairing_mode(false);
1043-
}
1044-
}
1045-
10461055
if (millis() - last_command_executed_time_ < command_cooldown_millis) {
10471056
// Give the lock time to terminate the previous command
10481057
uint32_t millisSinceLastExecution = millis() - last_command_executed_time_;
@@ -1080,54 +1089,46 @@ void NukiLockComponent::update() {
10801089
}
10811090
} else if (this->action_attempts_ == 0) {
10821091
// Publish failed state only when no attempts are left
1092+
this->publish_state(lock::LOCK_STATE_NONE);
1093+
10831094
#ifdef USE_BINARY_SENSOR
10841095
if (this->is_connected_binary_sensor_ != nullptr)
10851096
{
10861097
this->is_connected_binary_sensor_->publish_state(false);
10871098
}
10881099
#endif
1089-
this->publish_state(lock::LOCK_STATE_NONE);
10901100
}
10911101

10921102
// Schedule a status update without waiting for the next advertisement for a faster feedback
10931103
this->status_update_ = true;
10941104

10951105
// Give the lock extra time when successful in order to account for time to turn the key
10961106
command_cooldown_millis = isExecutionSuccessful ? COOLDOWN_COMMANDS_EXTENDED_MILLIS : COOLDOWN_COMMANDS_MILLIS;
1097-
last_command_executed_time_ = millis();
10981107

10991108
} else if (this->status_update_) {
11001109
ESP_LOGD(TAG, "Update present, getting data...");
11011110
this->update_status();
1102-
11031111
command_cooldown_millis = COOLDOWN_COMMANDS_MILLIS;
1104-
last_command_executed_time_ = millis();
1105-
1112+
} else if (this->auth_data_update_) {
1113+
ESP_LOGD(TAG, "Update present, getting auth data...");
1114+
this->update_auth_data();
1115+
command_cooldown_millis = COOLDOWN_COMMANDS_MILLIS;
1116+
} else if (this->event_log_update_) {
1117+
ESP_LOGD(TAG, "Update present, getting event logs...");
1118+
this->update_event_logs();
1119+
command_cooldown_millis = COOLDOWN_COMMANDS_MILLIS;
11061120
} else if (this->config_update_) {
11071121
ESP_LOGD(TAG, "Update present, getting config...");
11081122
this->update_config();
1109-
11101123
command_cooldown_millis = COOLDOWN_COMMANDS_MILLIS;
1111-
last_command_executed_time_ = millis();
11121124
} else if (this->advanced_config_update_) {
11131125
ESP_LOGD(TAG, "Update present, getting advanced config...");
11141126
this->update_advanced_config();
1115-
11161127
command_cooldown_millis = COOLDOWN_COMMANDS_MILLIS;
1117-
last_command_executed_time_ = millis();
1118-
} else if (this->auth_data_update_) {
1119-
ESP_LOGD(TAG, "Update present, getting auth data...");
1120-
this->update_auth_data();
1128+
}
11211129

1122-
command_cooldown_millis = COOLDOWN_COMMANDS_MILLIS;
1123-
last_command_executed_time_ = millis();
1124-
} else if (this->event_log_update_) {
1125-
ESP_LOGD(TAG, "Update present, getting event logs...");
1126-
this->update_event_logs();
1130+
last_command_executed_time_ = millis();
11271131

1128-
command_cooldown_millis = COOLDOWN_COMMANDS_MILLIS;
1129-
last_command_executed_time_ = millis();
1130-
}
11311132
} else {
11321133
#ifdef USE_BINARY_SENSOR
11331134
if (this->is_paired_binary_sensor_ != nullptr) {
@@ -1148,8 +1149,12 @@ void NukiLockComponent::update() {
11481149
ESP_LOGI(TAG, "Nuki paired successfuly as %s!", this->pairing_as_app_ ? "App" : "Bridge");
11491150
this->update_status();
11501151
this->paired_callback_.call();
1152+
11511153
this->set_pairing_mode(false);
1154+
1155+
this->setup_intervals();
11521156
}
1157+
11531158
#ifdef USE_BINARY_SENSOR
11541159
if (this->is_paired_binary_sensor_ != nullptr) {
11551160
this->is_paired_binary_sensor_->publish_state(paired);
@@ -1382,18 +1387,10 @@ void NukiLockComponent::notify(Nuki::EventType event_type) {
13821387
}
13831388

13841389
this->status_update_ = true;
1385-
this->config_update_ = true;
1386-
this->advanced_config_update_ = true;
13871390

1388-
// Request Auth Data on every second notify, otherwise just event logs
1389-
// Event logs are always requested after Auth Data requests
1391+
// Request event logs after every notify
13901392
if (this->send_events_) {
1391-
this->auth_data_required_ = !this->auth_data_required_;
1392-
if (this->auth_data_required_) {
1393-
this->auth_data_update_ = true;
1394-
} else {
1395-
this->event_log_update_ = true;
1396-
}
1393+
this->event_log_update_ = true;
13971394
}
13981395

13991396
ESP_LOGI(TAG, "event notified %d", event_type);
@@ -1403,6 +1400,8 @@ void NukiLockComponent::unpair() {
14031400
if (this->nuki_lock_.isPairedWithLock()) {
14041401
this->nuki_lock_.unPairNuki();
14051402
ESP_LOGI(TAG, "Unpaired Nuki! Turn on Pairing Mode to pair a new Nuki.");
1403+
1404+
this->setup_intervals(false);
14061405
} else {
14071406
ESP_LOGE(TAG, "Unpair action called for unpaired Nuki");
14081407
}
@@ -1417,18 +1416,21 @@ void NukiLockComponent::set_pairing_mode(bool enabled) {
14171416
}
14181417
#endif
14191418

1419+
cancel_timeout("pairing_mode_timeout");
1420+
14201421
if (enabled) {
14211422
ESP_LOGI(TAG, "Pairing Mode turned on for %d seconds", this->pairing_mode_timeout_);
14221423
this->pairing_mode_on_callback_.call();
14231424

14241425
ESP_LOGI(TAG, "Waiting for Nuki to enter pairing mode...");
14251426

1426-
// Turn on for ... seconds
1427-
uint32_t now_millis = millis();
1428-
this->pairing_mode_timer_ = now_millis + (this->pairing_mode_timeout_ * 1000);
1427+
this->set_timeout("pairing_mode_timeout", this->pairing_mode_timeout_ * 1000, [this]()
1428+
{
1429+
ESP_LOGV(TAG, "Pairing timed out, turning off pairing mode");
1430+
this->set_pairing_mode(false);
1431+
});
14291432
} else {
14301433
ESP_LOGI(TAG, "Pairing Mode turned off");
1431-
this->pairing_mode_timer_ = 0;
14321434
this->pairing_mode_off_callback_.call();
14331435
}
14341436
}

components/nuki_lock/nuki_lock.h

+7-4
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,14 @@ class NukiLockComponent : public lock::Lock, public PollingComponent, public Nuk
100100
static const uint8_t MAX_ACTION_ATTEMPTS = 5;
101101
static const uint8_t MAX_TOLERATED_UPDATES_ERRORS = 5;
102102

103-
static const uint8_t MAX_AUTH_DATA_ENTRIES = 10;
104-
static const uint8_t MAX_EVENT_LOG_ENTRIES = 3;
105-
106103
static const uint32_t COOLDOWN_COMMANDS_MILLIS = 1000;
107104
static const uint32_t COOLDOWN_COMMANDS_EXTENDED_MILLIS = 3000;
108105

106+
static const uint8_t CONFIG_UPDATE_INTERVAL_SEC = 60;
107+
static const uint8_t AUTH_DATA_UPDATE_INTERVAL_SEC = 120;
108+
static const uint8_t MAX_AUTH_DATA_ENTRIES = 10;
109+
static const uint8_t MAX_EVENT_LOG_ENTRIES = 3;
110+
109111
public:
110112
const uint32_t deviceId_ = 2020002;
111113
const std::string deviceName_ = "Nuki ESPHome";
@@ -185,6 +187,8 @@ class NukiLockComponent : public lock::Lock, public PollingComponent, public Nuk
185187
void update_auth_data();
186188
void process_log_entries(const std::list<NukiLock::LogEntry>& log_entries);
187189

190+
void setup_intervals(bool setup = true);
191+
188192
bool execute_lock_action(NukiLock::LockAction lock_action);
189193

190194
BleScanner::Scanner scanner_;
@@ -209,7 +213,6 @@ class NukiLockComponent : public lock::Lock, public PollingComponent, public Nuk
209213
bool advanced_config_update_;
210214
bool auth_data_update_;
211215
bool event_log_update_;
212-
bool auth_data_required_;
213216
bool open_latch_;
214217
bool lock_n_go_;
215218

0 commit comments

Comments
 (0)