@@ -989,13 +989,10 @@ void NukiLockComponent::setup() {
989
989
if (this ->nuki_lock_ .isPairedWithLock ()) {
990
990
this ->status_update_ = true ;
991
991
992
+ // First boot: Request config and auth data
992
993
this ->config_update_ = true ;
993
994
this ->advanced_config_update_ = true ;
994
-
995
- // First auth data request, then every 2nd time
996
- // Requesting only when events are enabled
997
995
if (this ->send_events_ ) {
998
- this ->auth_data_required_ = true ;
999
996
this ->auth_data_update_ = true ;
1000
997
}
1001
998
@@ -1006,6 +1003,9 @@ void NukiLockComponent::setup() {
1006
1003
this ->is_paired_binary_sensor_ ->publish_initial_state (true );
1007
1004
}
1008
1005
#endif
1006
+
1007
+ this ->setup_intervals ();
1008
+
1009
1009
} else {
1010
1010
ESP_LOGW (TAG, " %s Nuki is not paired" , this ->deviceName_ .c_str ());
1011
1011
#ifdef USE_BINARY_SENSOR
@@ -1027,6 +1027,22 @@ void NukiLockComponent::setup() {
1027
1027
#endif
1028
1028
}
1029
1029
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
+
1030
1046
void NukiLockComponent::update () {
1031
1047
// Check for new advertisements
1032
1048
this ->scanner_ .update ();
@@ -1036,13 +1052,6 @@ void NukiLockComponent::update() {
1036
1052
// Terminate stale Bluetooth connections
1037
1053
this ->nuki_lock_ .updateConnectionState ();
1038
1054
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
-
1046
1055
if (millis () - last_command_executed_time_ < command_cooldown_millis) {
1047
1056
// Give the lock time to terminate the previous command
1048
1057
uint32_t millisSinceLastExecution = millis () - last_command_executed_time_;
@@ -1080,54 +1089,46 @@ void NukiLockComponent::update() {
1080
1089
}
1081
1090
} else if (this ->action_attempts_ == 0 ) {
1082
1091
// Publish failed state only when no attempts are left
1092
+ this ->publish_state (lock::LOCK_STATE_NONE);
1093
+
1083
1094
#ifdef USE_BINARY_SENSOR
1084
1095
if (this ->is_connected_binary_sensor_ != nullptr )
1085
1096
{
1086
1097
this ->is_connected_binary_sensor_ ->publish_state (false );
1087
1098
}
1088
1099
#endif
1089
- this ->publish_state (lock::LOCK_STATE_NONE);
1090
1100
}
1091
1101
1092
1102
// Schedule a status update without waiting for the next advertisement for a faster feedback
1093
1103
this ->status_update_ = true ;
1094
1104
1095
1105
// Give the lock extra time when successful in order to account for time to turn the key
1096
1106
command_cooldown_millis = isExecutionSuccessful ? COOLDOWN_COMMANDS_EXTENDED_MILLIS : COOLDOWN_COMMANDS_MILLIS;
1097
- last_command_executed_time_ = millis ();
1098
1107
1099
1108
} else if (this ->status_update_ ) {
1100
1109
ESP_LOGD (TAG, " Update present, getting data..." );
1101
1110
this ->update_status ();
1102
-
1103
1111
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;
1106
1120
} else if (this ->config_update_ ) {
1107
1121
ESP_LOGD (TAG, " Update present, getting config..." );
1108
1122
this ->update_config ();
1109
-
1110
1123
command_cooldown_millis = COOLDOWN_COMMANDS_MILLIS;
1111
- last_command_executed_time_ = millis ();
1112
1124
} else if (this ->advanced_config_update_ ) {
1113
1125
ESP_LOGD (TAG, " Update present, getting advanced config..." );
1114
1126
this ->update_advanced_config ();
1115
-
1116
1127
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
+ }
1121
1129
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 ();
1127
1131
1128
- command_cooldown_millis = COOLDOWN_COMMANDS_MILLIS;
1129
- last_command_executed_time_ = millis ();
1130
- }
1131
1132
} else {
1132
1133
#ifdef USE_BINARY_SENSOR
1133
1134
if (this ->is_paired_binary_sensor_ != nullptr ) {
@@ -1148,8 +1149,12 @@ void NukiLockComponent::update() {
1148
1149
ESP_LOGI (TAG, " Nuki paired successfuly as %s!" , this ->pairing_as_app_ ? " App" : " Bridge" );
1149
1150
this ->update_status ();
1150
1151
this ->paired_callback_ .call ();
1152
+
1151
1153
this ->set_pairing_mode (false );
1154
+
1155
+ this ->setup_intervals ();
1152
1156
}
1157
+
1153
1158
#ifdef USE_BINARY_SENSOR
1154
1159
if (this ->is_paired_binary_sensor_ != nullptr ) {
1155
1160
this ->is_paired_binary_sensor_ ->publish_state (paired);
@@ -1382,18 +1387,10 @@ void NukiLockComponent::notify(Nuki::EventType event_type) {
1382
1387
}
1383
1388
1384
1389
this ->status_update_ = true ;
1385
- this ->config_update_ = true ;
1386
- this ->advanced_config_update_ = true ;
1387
1390
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
1390
1392
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 ;
1397
1394
}
1398
1395
1399
1396
ESP_LOGI (TAG, " event notified %d" , event_type);
@@ -1403,6 +1400,8 @@ void NukiLockComponent::unpair() {
1403
1400
if (this ->nuki_lock_ .isPairedWithLock ()) {
1404
1401
this ->nuki_lock_ .unPairNuki ();
1405
1402
ESP_LOGI (TAG, " Unpaired Nuki! Turn on Pairing Mode to pair a new Nuki." );
1403
+
1404
+ this ->setup_intervals (false );
1406
1405
} else {
1407
1406
ESP_LOGE (TAG, " Unpair action called for unpaired Nuki" );
1408
1407
}
@@ -1417,18 +1416,21 @@ void NukiLockComponent::set_pairing_mode(bool enabled) {
1417
1416
}
1418
1417
#endif
1419
1418
1419
+ cancel_timeout (" pairing_mode_timeout" );
1420
+
1420
1421
if (enabled) {
1421
1422
ESP_LOGI (TAG, " Pairing Mode turned on for %d seconds" , this ->pairing_mode_timeout_ );
1422
1423
this ->pairing_mode_on_callback_ .call ();
1423
1424
1424
1425
ESP_LOGI (TAG, " Waiting for Nuki to enter pairing mode..." );
1425
1426
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
+ });
1429
1432
} else {
1430
1433
ESP_LOGI (TAG, " Pairing Mode turned off" );
1431
- this ->pairing_mode_timer_ = 0 ;
1432
1434
this ->pairing_mode_off_callback_ .call ();
1433
1435
}
1434
1436
}
0 commit comments