From 62742549a79d2fc7742f32bc0a7028b75fcb4535 Mon Sep 17 00:00:00 2001 From: iranl Date: Sat, 2 Nov 2024 21:54:06 +0100 Subject: [PATCH] Enable/Disable HA Discovery using a checkbox --- README.md | 3 ++- src/NukiNetwork.cpp | 6 ++++++ src/PreferencesKeys.h | 5 +++-- src/WebCfgServer.cpp | 44 ++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 52 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e251f85..a0202a0 100644 --- a/README.md +++ b/README.md @@ -203,10 +203,11 @@ In a browser navigate to the IP address assigned to the ESP32. - MQTT User: If using authentication on the MQTT broker set to a username with read/write rights on the MQTT broker, set to # to clear - MQTT Password : If using authentication on the MQTT broker set to the password belonging to a username with read/write rights on the MQTT broker, set to # to clear - MQTT NukiHub Path: Set to the preferred MQTT root topic for NukiHub, defaults to "nukihub". Make sure this topic is unique when using multiple ESP32 NukiHub devices +- Enable Home Assistant auto discovery: Enable Home Assistant MQTT auto discovery. Will automatically create entities in Home Assistant for NukiHub and connected Nuki Lock and/or Opener when enabled. #### Advanced MQTT Configuration -- Home Assistant discovery topic: Set to the Home Assistant auto discovery topic, leave empty to disable auto discovery. Usually "homeassistant" unless you manually changed this setting on the Home Assistant side. +- Home Assistant discovery topic: Set to the Home Assistant auto discovery topic. Usually "homeassistant" unless you manually changed this setting on the Home Assistant side. - Set Nuki Opener Lock/Unlock action in Home Assistant to Continuous mode (Opener only): By default the lock entity in Home Assistant will enable Ring-to-Open (RTO) when unlocking and disable RTO when locking. By enabling this setting this behaviour will change and now unlocking will enable Continuous Mode and locking will disable Continuous Mode, for more information see the "[Home Assistant Discovery](#home-assistant-discovery-optional)" section of this README. - MQTT SSL CA Certificate: Optionally set to the CA SSL certificate of the MQTT broker, see the "[MQTT Encryption](#mqtt-encryption-optional)" section of this README. - MQTT SSL Client Certificate: Optionally set to the Client SSL certificate of the MQTT broker, see the "[MQTT Encryption](#mqtt-encryption-optional)" section of this README. diff --git a/src/NukiNetwork.cpp b/src/NukiNetwork.cpp index 9ae5d7a..88c7b08 100644 --- a/src/NukiNetwork.cpp +++ b/src/NukiNetwork.cpp @@ -61,6 +61,12 @@ NukiNetwork::NukiNetwork(Preferences *preferences) { _mqttConnectionStateTopic[i] = connectionStateTopic.charAt(i); } + + if(_preferences->getString(preference_mqtt_hass_discovery, "") != "" && !_preferences->getBool(preference_mqtt_hass_enabled, false)) + { + _preferences->putBool(preference_mqtt_hass_enabled, true); + } + #endif setupDevice(); diff --git a/src/PreferencesKeys.h b/src/PreferencesKeys.h index b01a959..8390e0f 100644 --- a/src/PreferencesKeys.h +++ b/src/PreferencesKeys.h @@ -46,6 +46,7 @@ #define preference_cred_user (char*)"crdusr" #define preference_cred_password (char*)"crdpass" #define preference_gpio_configuration (char*)"gpiocfg" +#define preference_mqtt_hass_enabled (char*)"hassena" #define preference_mqtt_hass_discovery (char*)"hassdiscovery" #define preference_webserver_enabled (char*)"websrvena" #define preference_update_from_mqtt (char*)"updMqtt" @@ -293,7 +294,7 @@ private: preference_network_custom_rst, preference_network_custom_cs, preference_network_custom_sck, preference_network_custom_miso, preference_network_custom_mosi, preference_network_custom_pwr, preference_network_custom_mdio, preference_ntw_reconfigure, preference_lock_max_auth_entry_count, preference_opener_max_auth_entry_count, preference_auth_control_enabled, preference_auth_topic_per_entry, preference_auth_info_enabled, preference_auth_max_entries, preference_wifi_ssid, preference_wifi_pass, - preference_keypad_check_code_enabled, preference_disable_network_not_connected + preference_keypad_check_code_enabled, preference_disable_network_not_connected, preference_mqtt_hass_enabled }; std::vector _redact = { @@ -306,7 +307,7 @@ private: preference_timecontrol_topic_per_entry, preference_keypad_topic_per_entry, preference_enable_bootloop_reset, preference_webserver_enabled, preference_restart_on_disconnect, preference_keypad_control_enabled, preference_keypad_info_enabled, preference_keypad_publish_code, preference_show_secrets, preference_timecontrol_control_enabled, preference_timecontrol_info_enabled, preference_register_as_app, preference_register_opener_as_app, preference_ip_dhcp_enabled, - preference_publish_authdata, preference_publish_debug_info, preference_official_hybrid_enabled, + preference_publish_authdata, preference_publish_debug_info, preference_official_hybrid_enabled, preference_mqtt_hass_enabled, preference_official_hybrid_actions, preference_official_hybrid_retry, preference_conf_info_enabled, preference_disable_non_json, preference_update_from_mqtt, preference_auth_control_enabled, preference_auth_topic_per_entry, preference_auth_info_enabled, preference_webserial_enabled, preference_ntw_reconfigure, preference_keypad_check_code_enabled, preference_disable_network_not_connected diff --git a/src/WebCfgServer.cpp b/src/WebCfgServer.cpp index 7bbaa7f..a94ce81 100644 --- a/src/WebCfgServer.cpp +++ b/src/WebCfgServer.cpp @@ -1726,6 +1726,33 @@ bool WebCfgServer::processArgs(PsychicRequest *request, String& message) //configChanged = true; } } + else if(key == "ENHADISC") + { + if(_preferences->getBool(preference_mqtt_hass_enabled, false) != (value == "1")) + { + if(!_preferences->getBool(preference_mqtt_hass_enabled, false)) + { + if (_nuki != nullptr) + { + _nuki->disableHASS(); + } + if (_nukiOpener != nullptr) + { + _nukiOpener->disableHASS(); + } + + _preferences->putString(preference_mqtt_hass_discovery, ""); + } + else if(_preferences->getString(preference_mqtt_hass_discovery, "") == "") + { + _preferences->putString(preference_mqtt_hass_discovery, "homeassistant"); + } + _preferences->putBool(preference_mqtt_hass_enabled, (value == "1")); + Log->print(F("Setting changed: ")); + Log->println(key); + configChanged = true; + } + } else if(key == "HASSDISCOVERY") { if(_preferences->getString(preference_mqtt_hass_discovery, "") != value) @@ -1739,6 +1766,16 @@ bool WebCfgServer::processArgs(PsychicRequest *request, String& message) _nukiOpener->disableHASS(); } _preferences->putString(preference_mqtt_hass_discovery, value); + + if(value != "" && !_preferences->getBool(preference_mqtt_hass_enabled, false)) + { + _preferences->putBool(preference_mqtt_hass_enabled, true); + } + else if(value == "" && _preferences->getBool(preference_mqtt_hass_enabled, false)) + { + _preferences->putBool(preference_mqtt_hass_enabled, false); + } + Log->print(F("Setting changed: ")); Log->println(key); configChanged = true; @@ -3507,11 +3544,12 @@ esp_err_t WebCfgServer::buildMqttConfigHtml(PsychicRequest *request) printInputField(&response, "MQTTUSER", "MQTT User (# to clear)", _preferences->getString(preference_mqtt_user).c_str(), 30, "", false, true); printInputField(&response, "MQTTPASS", "MQTT Password", "*", 30, "", true, true); printInputField(&response, "MQTTPATH", "MQTT NukiHub Path", _preferences->getString(preference_mqtt_lock_path).c_str(), 180, ""); + printCheckBox(&response, "ENHADISC", "Enable Home Assistant auto discovery", _preferences->getBool(preference_mqtt_hass_enabled), ""); response.print("
"); response.print("

Advanced MQTT Configuration

"); response.print(""); - printInputField(&response, "HASSDISCOVERY", "Home Assistant discovery topic (empty to disable; usually homeassistant)", _preferences->getString(preference_mqtt_hass_discovery).c_str(), 30, ""); + printInputField(&response, "HASSDISCOVERY", "Home Assistant discovery topic (usually \"homeassistant\")", _preferences->getString(preference_mqtt_hass_discovery).c_str(), 30, ""); if(_preferences->getBool(preference_opener_enabled, false)) { printCheckBox(&response, "OPENERCONT", "Set Nuki Opener Lock/Unlock action in Home Assistant to Continuous mode", _preferences->getBool(preference_opener_continuous_mode), ""); @@ -3547,7 +3585,7 @@ esp_err_t WebCfgServer::buildAdvancedConfigHtml(PsychicRequest *request) response.print(""); - printCheckBox(&response, "DISNTWNOCON", "Disable Network if not connected within 60s", _preferences->getBool(preference_disable_network_not_connected, false), ""); + printCheckBox(&response, "DISNTWNOCON", "Disable Network if not connected within 60s", _preferences->getBool(preference_disable_network_not_connected, false), ""); printCheckBox(&response, "WEBLOG", "Enable WebSerial logging", _preferences->getBool(preference_webserial_enabled), ""); printCheckBox(&response, "BTLPRST", "Enable Bootloop prevention (Try to reset these settings to default on bootloop)", true, ""); printInputField(&response, "BUFFSIZE", "Char buffer size (min 4096, max 32768)", _preferences->getInt(preference_buffer_size, CHAR_BUFFER_SIZE), 6, ""); @@ -3721,7 +3759,7 @@ esp_err_t WebCfgServer::buildAccLvlHtml(PsychicRequest *request) printCheckBox(&response, "KPPER", "Publish a topic per keypad entry and create HA sensor", _preferences->getBool(preference_keypad_topic_per_entry), ""); printCheckBox(&response, "KPCODE", "Also publish keypad codes (Disadvised for security reasons)", _preferences->getBool(preference_keypad_publish_code, false), ""); printCheckBox(&response, "KPENA", "Add, modify and delete keypad codes", _preferences->getBool(preference_keypad_control_enabled), ""); - printCheckBox(&response, "KPCHECK", "Allow checking if keypad codes are valid (Disadvised for security reasons)", _preferences->getBool(preference_keypad_check_code_enabled, false), ""); + printCheckBox(&response, "KPCHECK", "Allow checking if keypad codes are valid (Disadvised for security reasons)", _preferences->getBool(preference_keypad_check_code_enabled, false), ""); } printCheckBox(&response, "TCPUB", "Publish time control entries information", _preferences->getBool(preference_timecontrol_info_enabled), ""); printCheckBox(&response, "TCPER", "Publish a topic per time control entry and create HA sensor", _preferences->getBool(preference_timecontrol_topic_per_entry), "");
Current bootloop prevention state"); response.print(_preferences->getBool(preference_enable_bootloop_reset, false) ? "Enabled" : "Disabled"); response.print("