From 7ccaffec7aebbd8dc6547ca20554e599c57ac3dd Mon Sep 17 00:00:00 2001 From: technyon Date: Sat, 13 Aug 2022 08:49:46 +0200 Subject: [PATCH] disable keypad control by default; add more sanity checks --- NetworkLock.cpp | 23 +++++++++++++---------- NukiWrapper.cpp | 15 ++++++++++++++- NukiWrapper.h | 1 + README.md | 3 ++- main.cpp | 1 - 5 files changed, 30 insertions(+), 13 deletions(-) diff --git a/NetworkLock.cpp b/NetworkLock.cpp index 5fb4c55..74d5292 100644 --- a/NetworkLock.cpp +++ b/NetworkLock.cpp @@ -54,16 +54,19 @@ void NetworkLock::initialize() _network->subscribe(_mqttPath, mqtt_topic_reset); _network->initTopic(_mqttPath, mqtt_topic_reset, "0"); - _network->subscribe(_mqttPath, mqtt_topic_keypad_command_action); - _network->subscribe(_mqttPath, mqtt_topic_keypad_command_id); - _network->subscribe(_mqttPath, mqtt_topic_keypad_command_name); - _network->subscribe(_mqttPath, mqtt_topic_keypad_command_code); - _network->subscribe(_mqttPath, mqtt_topic_keypad_command_enabled); - _network->initTopic(_mqttPath, mqtt_topic_keypad_command_action, "--"); - _network->initTopic(_mqttPath, mqtt_topic_keypad_command_id, "0"); - _network->initTopic(_mqttPath, mqtt_topic_keypad_command_name, "--"); - _network->initTopic(_mqttPath, mqtt_topic_keypad_command_code, "000000"); - _network->initTopic(_mqttPath, mqtt_topic_keypad_command_enabled, "1"); + if(_preferences->getBool(preference_keypad_control_enabled)) + { + _network->subscribe(_mqttPath, mqtt_topic_keypad_command_action); + _network->subscribe(_mqttPath, mqtt_topic_keypad_command_id); + _network->subscribe(_mqttPath, mqtt_topic_keypad_command_name); + _network->subscribe(_mqttPath, mqtt_topic_keypad_command_code); + _network->subscribe(_mqttPath, mqtt_topic_keypad_command_enabled); + _network->initTopic(_mqttPath, mqtt_topic_keypad_command_action, "--"); + _network->initTopic(_mqttPath, mqtt_topic_keypad_command_id, "0"); + _network->initTopic(_mqttPath, mqtt_topic_keypad_command_name, "--"); + _network->initTopic(_mqttPath, mqtt_topic_keypad_command_code, "000000"); + _network->initTopic(_mqttPath, mqtt_topic_keypad_command_enabled, "1"); + } } void NetworkLock::update() diff --git a/NukiWrapper.cpp b/NukiWrapper.cpp index 4ddcb3e..8618958 100644 --- a/NukiWrapper.cpp +++ b/NukiWrapper.cpp @@ -203,6 +203,7 @@ void NukiWrapper::updateConfig() { readConfig(); readAdvancedConfig(); + _configRead = true; _hasKeypad = _nukiConfig.hasKeypad > 0; _network->publishConfig(_nukiConfig); _network->publishAdvancedConfig(_nukiAdvancedConfig); @@ -362,7 +363,15 @@ void NukiWrapper::onConfigUpdateReceived(const char *topic, const char *value) void NukiWrapper::onKeypadCommandReceived(const char *command, const uint &id, const String &name, const String &code, const int& enabled) { - if(!_hasKeypad || !_keypadEnabled) + if(!_hasKeypad) + { + if(_configRead) + { + _network->publishKeypadCommandResult("KeypadNotAvailable"); + } + return; + } + if(!_keypadEnabled) { return; } @@ -444,6 +453,10 @@ void NukiWrapper::onKeypadCommandReceived(const char *command, const uint &id, c Serial.print("Update keypad code: "); Serial.println((int)result); updateKeypad(); } + else if(command == "--") + { + return; + } else { _network->publishKeypadCommandResult("UnknownCommand"); diff --git a/NukiWrapper.h b/NukiWrapper.h index 4e7f047..f27e28e 100644 --- a/NukiWrapper.h +++ b/NukiWrapper.h @@ -80,6 +80,7 @@ private: bool _statusUpdated = false; bool _hasKeypad = false; bool _keypadEnabled = false; + bool _configRead = false; uint _maxKeypadCodeCount = 0; unsigned long _nextLockStateUpdateTs = 0; unsigned long _nextBatteryReportTs = 0; diff --git a/README.md b/README.md index 35e33fd..533eb3a 100644 --- a/README.md +++ b/README.md @@ -118,7 +118,8 @@ NOTE: MQTT Discovery uses retained MQTT messages to store devices configurations ## Keypad control (optional) If a keypad is connected to the lock, keypad codes can be added, updated and removed. -Information about codes is published under "keypad/code_x", x starting from 0 up the number of configured codes. +This has to enabled first in the configuration portal. Check "Enabled keypad control via MQTT" and save the configuration. +After enabling keypad control, information about codes is published under "keypad/code_x", x starting from 0 up the number of configured codes.
For security reasons, the code itself is not published. To modify keypad codes, a command structure is setup under keypad/command: diff --git a/main.cpp b/main.cpp index 63b68bc..69e8d25 100644 --- a/main.cpp +++ b/main.cpp @@ -148,7 +148,6 @@ void initPreferences() { preferences->putBool(preference_started_befores, true); preferences->putBool(preference_lock_enabled, true); - preferences->putBool(preference_keypad_control_enabled, true); } if(preferences->getInt(preference_restart_timer) == 0)