diff --git a/src/mqtt/MQTT.cpp b/src/mqtt/MQTT.cpp index 97636b601..3e477cae5 100644 --- a/src/mqtt/MQTT.cpp +++ b/src/mqtt/MQTT.cpp @@ -409,6 +409,9 @@ void MQTT::onReceive(char *topic, byte *payload, size_t length) void mqttInit() { + if (!moduleConfig.mqtt.enabled) + return; + new MQTT(); } diff --git a/test/test_mqtt/MQTT.cpp b/test/test_mqtt/MQTT.cpp index edf9a3983..523e1d4ac 100644 --- a/test/test_mqtt/MQTT.cpp +++ b/test/test_mqtt/MQTT.cpp @@ -800,6 +800,17 @@ void test_disabled(void) TEST_ASSERT_FALSE(mqtt->isEnabled()); } +void test_mqttInitSkipsAllocationWhenDisabled(void) +{ + delete unitTest; + mqtt = unitTest = NULL; + + moduleConfig.mqtt.enabled = false; + mqttInit(); + + TEST_ASSERT_NULL(mqtt); +} + // Subscriptions contain the moduleConfig.mqtt.root prefix. void test_customMqttRoot(void) { @@ -912,6 +923,7 @@ void setup() RUN_TEST(test_usingCustomServer); RUN_TEST(test_enabled); RUN_TEST(test_disabled); + RUN_TEST(test_mqttInitSkipsAllocationWhenDisabled); RUN_TEST(test_customMqttRoot); RUN_TEST(test_configEmptyIsValid); RUN_TEST(test_configEnabledEmptyIsValid);