diff --git a/docs/use/gateway.md b/docs/use/gateway.md index d88c65b..bdf1db8 100644 --- a/docs/use/gateway.md +++ b/docs/use/gateway.md @@ -85,11 +85,12 @@ build_flags = '-UMQTTsetMQTT' If the new connection fails the gateway will fallback to the previous connection. ::: -## Change the MQTT main topic and or gateway name +## Change the MQTT main topic, discovery prefix, and or gateway name ``` mosquitto_pub -t "home/OpenMQTTGateway/commands/MQTTtoSYS/config" -m '{ "mqtt_topic": "topic/", + "discovery_prefix": "prefix", "gateway_name": "name" }' ``` diff --git a/main/ZmqttDiscovery.ino b/main/ZmqttDiscovery.ino index 787589d..d91ff9e 100644 --- a/main/ZmqttDiscovery.ino +++ b/main/ZmqttDiscovery.ino @@ -187,7 +187,7 @@ void announceDeviceTrigger(bool use_gateway_info, char* topic, char* type, char* sensor["device"] = device; //device representing the board /* Publish on the topic */ - String topic_to_publish = String(discovery_Topic) + "/device_automation/" + String(unique_id) + "/config"; + String topic_to_publish = String(discovery_prefix) + "/device_automation/" + String(unique_id) + "/config"; Log.trace(F("Announce Device Trigger %s" CR), topic_to_publish.c_str()); pub_custom_topic((char*)topic_to_publish.c_str(), sensor, true); } @@ -434,7 +434,7 @@ void createDiscovery(const char* sensor_type, sensor["device"] = device; - String topic = String(discovery_Topic) + "/" + String(sensor_type) + "/" + String(unique_id) + "/config"; + String topic = String(discovery_prefix) + "/" + String(sensor_type) + "/" + String(unique_id) + "/config"; Log.trace(F("Announce Device %s on %s" CR), String(sensor_type).c_str(), topic.c_str()); pub_custom_topic((char*)topic.c_str(), sensor, true); } @@ -443,7 +443,7 @@ void eraseTopic(const char* sensor_type, const char* unique_id) { if (sensor_type == NULL || unique_id == NULL) { return; } - String topic = String(discovery_Topic) + "/" + String(sensor_type) + "/" + String(unique_id) + "/config"; + String topic = String(discovery_prefix) + "/" + String(sensor_type) + "/" + String(unique_id) + "/config"; Log.trace(F("Erase entity discovery %s on %s" CR), String(sensor_type).c_str(), topic.c_str()); pubMQTT((char*)topic.c_str(), "", true); } diff --git a/main/ZwebUI.ino b/main/ZwebUI.ino index 3204170..cd7fd9f 100644 --- a/main/ZwebUI.ino +++ b/main/ZwebUI.ino @@ -692,7 +692,8 @@ void handleWI() { * T: handleMQ Arg: 4, sc=on * T: handleMQ Arg: 5, h= * T: handleMQ Arg: 6, mt=home/ - * T: handleMQ Arg: 7, save= + * T: handleMQ Arg: 7 dp=homeassistant (#ifdef ZmqttDiscovery) + * T: handleMQ Arg: 8, save= */ void handleMQ() { WEBUI_TRACE_LOG(F("handleMQ: uri: %s, args: %d, method: %d" CR), server.uri(), server.args(), server.method()); @@ -762,6 +763,14 @@ void handleMQ() { update = true; } } +# ifdef ZmqttDiscovery + if (server.hasArg("dp")) { + WEBtoSYS["discovery_prefix"] = server.arg("dp"); + if (strncmp(discovery_prefix, server.arg("dp").c_str(), parameters_size)) { + update = true; + } + } +# endif # ifndef ESPWifiManualSetup if (update) { @@ -803,11 +812,19 @@ void handleMQ() { String response = String(buffer); response += String(script); response += String(style); - // mqtt server (mh), mqtt port (ml), mqtt username (mu), mqtt password (mp), secure connection (sc), server certificate (msc), topic (mt) + // mqtt server (mh), mqtt port (ml), mqtt username (mu), mqtt password (mp), secure connection (sc), server certificate (msc), mqtt topic (mt), discovery prefix (dp) (last one only #ifdef ZmqttDiscovery) # if MQTT_BROKER_MODE +# ifdef ZmqttDiscovery + snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, config_mqtt_body, jsonChar, gateway_name, "", "1883", "", "", gateway_name, mqtt_topic, discovery_prefix); +# else snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, config_mqtt_body, jsonChar, gateway_name, "", "1883", "", "", gateway_name, mqtt_topic); +# endif # else +# ifdef ZmqttDiscovery + snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, config_mqtt_body, jsonChar, gateway_name, cnt_parameters_array[CNT_DEFAULT_INDEX].mqtt_server, cnt_parameters_array[CNT_DEFAULT_INDEX].mqtt_port, cnt_parameters_array[CNT_DEFAULT_INDEX].mqtt_user, (cnt_parameters_array[CNT_DEFAULT_INDEX].isConnectionSecure ? "checked" : ""), gateway_name, mqtt_topic, discovery_prefix); +# else snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, config_mqtt_body, jsonChar, gateway_name, cnt_parameters_array[CNT_DEFAULT_INDEX].mqtt_server, cnt_parameters_array[CNT_DEFAULT_INDEX].mqtt_port, cnt_parameters_array[CNT_DEFAULT_INDEX].mqtt_user, (cnt_parameters_array[CNT_DEFAULT_INDEX].isConnectionSecure ? "checked" : ""), gateway_name, mqtt_topic); +# endif # endif response += String(buffer); snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, footer, OMG_VERSION); diff --git a/main/config_WebContent.h b/main/config_WebContent.h index 323663d..1cb64d4 100644 --- a/main/config_WebContent.h +++ b/main/config_WebContent.h @@ -100,11 +100,13 @@ const char information_body[] = body_header "