diff --git a/README.md b/README.md index 0e441b0d..83e6817d 100644 --- a/README.md +++ b/README.md @@ -447,7 +447,7 @@ To change Nuki Lock/Opener keypad settings set the `keypad/actionJson` topic to |------------------|----------|----------|----------|------------------------------------------------------------------------------------------------------------------|----------------------------------------| | action | Required | Required | Required | The action to execute | "delete", "add", "update" | | codeId | Required | Not used | Required | The code ID of the existing code to delete or update | Integer | -| code | Not used | Required | Required | The code to create or update | 6-digit Integer without zero's, can't start with "12"| +| code | Not used | Required | Optional | The code to create or update | 6-digit Integer without zero's, can't start with "12"| | enabled | Not used | Not used | Optional | Enable or disable the code, always enabled on add, disabled if not set on update | 1 = enabled, 0 = disabled | | name | Not used | Required | Required | The name of the code to create or update | String, max 20 chars | | timeLimited | Not used | Optional | Optional | If this authorization is restricted to access only at certain times, disabled if not set (requires enabled = 1) | 1 = enabled, 0 = disabled | @@ -460,7 +460,7 @@ To change Nuki Lock/Opener keypad settings set the `keypad/actionJson` topic to Examples: - Delete: `{ "action": "delete", "codeId": "1234" }` - Add: `{ "action": "add", "code": "589472", "name": "Test", "timeLimited": "1", "allowedFrom": "2024-04-12 10:00:00", "allowedUntil": "2034-04-12 10:00:00", "allowedWeekdays": [ "wed", "thu", "fri" ], "allowedFromTime": "08:00", "allowedUntilTime": "16:00" }` -- Update: `{ "action": "update", "codeId": "1234", "code": "589472", "enabled": "1", "name": "Test", "timeLimited": "1", "allowedFrom": "2024-04-12 10:00:00", "allowedUntil": "2034-04-12 10:00:00", "allowedWeekdays": [ "mon", "tue", "sat", "sun" ], "allowedFromTime": "08:00", "allowedUntilTime": "16:00" }` +- Update: `{ "action": "update", "codeId": "1234", "enabled": "1", "name": "Test", "timeLimited": "1", "allowedFrom": "2024-04-12 10:00:00", "allowedUntil": "2034-04-12 10:00:00", "allowedWeekdays": [ "mon", "tue", "sat", "sun" ], "allowedFromTime": "08:00", "allowedUntilTime": "16:00" }` ### Result of attempted keypad code changes diff --git a/src/NukiOpenerWrapper.cpp b/src/NukiOpenerWrapper.cpp index 4036cb88..0d670a2d 100644 --- a/src/NukiOpenerWrapper.cpp +++ b/src/NukiOpenerWrapper.cpp @@ -526,9 +526,12 @@ void NukiOpenerWrapper::updateKeypad() _keypadCodeIds.clear(); _keypadCodeIds.reserve(entries.size()); + _keypadCodes.clear(); + _keypadCodes.reserve(entries.size()); for(const auto& entry : entries) { _keypadCodeIds.push_back(entry.codeId); + _keypadCodes.push_back(entry.code); } } @@ -1505,7 +1508,7 @@ void NukiOpenerWrapper::onKeypadJsonCommandReceived(const char *value) return; } } - else + else if (strcmp(action, "update") != 0) { _network->publishKeypadJsonCommandResult("noCodeSet"); return; @@ -1686,7 +1689,14 @@ void NukiOpenerWrapper::onKeypadJsonCommandReceived(const char *value) entry.codeId = codeId; size_t nameLen = strlen(name); memcpy(&entry.name, name, nameLen > 20 ? 20 : nameLen); - entry.code = code; + + if(code) entry.code = code; + else + { + auto it = std::find(_keypadCodeIds.begin(), _keypadCodeIds.end(), codeId); + entry.code = _keypadCodes[(it - _keypadCodeIds.begin())]; + } + entry.enabled = enabled == 0 ? 0 : 1; entry.timeLimited = timeLimited == 1 ? 1 : 0; diff --git a/src/NukiOpenerWrapper.h b/src/NukiOpenerWrapper.h index 6414396e..fb4fd95e 100644 --- a/src/NukiOpenerWrapper.h +++ b/src/NukiOpenerWrapper.h @@ -105,6 +105,7 @@ class NukiOpenerWrapper : public NukiOpener::SmartlockEventHandler int _retryLockstateCount = 0; unsigned long _nextRetryTs = 0; std::vector _keypadCodeIds; + std::vector _keypadCodes; std::vector _timeControlIds; NukiOpener::OpenerState _lastKeyTurnerState; diff --git a/src/NukiWrapper.cpp b/src/NukiWrapper.cpp index 119cb15c..8a81c791 100644 --- a/src/NukiWrapper.cpp +++ b/src/NukiWrapper.cpp @@ -507,9 +507,12 @@ void NukiWrapper::updateKeypad() _keypadCodeIds.clear(); _keypadCodeIds.reserve(entries.size()); + _keypadCodes.clear(); + _keypadCodes.reserve(entries.size()); for(const auto& entry : entries) { _keypadCodeIds.push_back(entry.codeId); + _keypadCodes.push_back(entry.code); } } @@ -1491,7 +1494,7 @@ void NukiWrapper::onKeypadJsonCommandReceived(const char *value) return; } } - else + else if (strcmp(action, "update") != 0) { _network->publishKeypadJsonCommandResult("noCodeSet"); return; @@ -1672,7 +1675,14 @@ void NukiWrapper::onKeypadJsonCommandReceived(const char *value) entry.codeId = codeId; size_t nameLen = strlen(name); memcpy(&entry.name, name, nameLen > 20 ? 20 : nameLen); - entry.code = code; + + if(code) entry.code = code; + else + { + auto it = std::find(_keypadCodeIds.begin(), _keypadCodeIds.end(), codeId); + entry.code = _keypadCodes[(it - _keypadCodeIds.begin())]; + } + entry.enabled = enabled == 0 ? 0 : 1; entry.timeLimited = timeLimited == 1 ? 1 : 0; diff --git a/src/NukiWrapper.h b/src/NukiWrapper.h index f5d02919..9a4663e7 100644 --- a/src/NukiWrapper.h +++ b/src/NukiWrapper.h @@ -94,6 +94,7 @@ class NukiWrapper : public Nuki::SmartlockEventHandler bool _publishAuthData = false; bool _clearAuthData = false; std::vector _keypadCodeIds; + std::vector _keypadCodes; std::vector _timeControlIds; NukiLock::KeyTurnerState _lastKeyTurnerState;