diff --git a/src/list.h b/src/list.h index e4c000f..bf87503 100644 --- a/src/list.h +++ b/src/list.h @@ -13,7 +13,7 @@ struct List { void add(T* t) { Container* c = new Container{t}; - first == nullptr ? first = c : last->next = c; + isEmpty() ? first = c : last->next = c; last = c; } @@ -33,4 +33,8 @@ struct List { first = last = nullptr; } + bool isEmpty() { + return first == nullptr; + } + }; diff --git a/src/mqtt.h b/src/mqtt.h index 9aa6886..725f162 100644 --- a/src/mqtt.h +++ b/src/mqtt.h @@ -27,11 +27,10 @@ namespace Mqtt { } void publishInit() { - static bool firstTime = true; - if (firstTime) { + if (!Component::components.isEmpty()) { Component::components.forEach([](Component* c) { c->publishConfig(); }); + Component::components.empty(); HaESP::enableSensors(); - firstTime = false; } } diff --git a/test/native/test_list/main.cpp b/test/native/test_list/main.cpp index d80b4b1..4e4bb15 100644 --- a/test/native/test_list/main.cpp +++ b/test/native/test_list/main.cpp @@ -30,6 +30,7 @@ void test_empty(void) { list.empty(); TEST_ASSERT_EQUAL_PTR(nullptr, list.first); TEST_ASSERT_EQUAL_PTR(list.first, list.last); + TEST_ASSERT_TRUE(list.isEmpty()); } int main(int argc, char **argv) {