diff --git a/include/MqttHandleHass.h b/include/MqttHandleHass.h index 5bf7e71eb..27f6e911b 100644 --- a/include/MqttHandleHass.h +++ b/include/MqttHandleHass.h @@ -3,6 +3,7 @@ #include #include +#include // mqtt discovery device classes enum { @@ -50,12 +51,12 @@ const byteAssign_fieldDeviceClass_t deviceFieldAssignment[] = { class MqttHandleHassClass { public: - void init(); - void loop(); + void init(Scheduler* scheduler); void publishConfig(); void forceUpdate(); private: + void loop(); void publish(const String& subtopic, const String& payload); void publishField(std::shared_ptr inv, ChannelType_t type, ChannelNum_t channel, byteAssign_fieldDeviceClass_t fieldType, bool clear = false); void publishInverterButton(std::shared_ptr inv, const char* caption, const char* icon, const char* category, const char* deviceClass, const char* subTopic, const char* payload); @@ -63,6 +64,8 @@ class MqttHandleHassClass { void publishInverterBinarySensor(std::shared_ptr inv, const char* caption, const char* subTopic, const char* payload_on, const char* payload_off); void createDeviceInfo(JsonObject& object, std::shared_ptr inv); + Task _loopTask; + bool _wasConnected = false; bool _updateForced = false; }; diff --git a/src/MqttHandleHass.cpp b/src/MqttHandleHass.cpp index e5842240c..5880fd4e9 100644 --- a/src/MqttHandleHass.cpp +++ b/src/MqttHandleHass.cpp @@ -9,8 +9,12 @@ MqttHandleHassClass MqttHandleHass; -void MqttHandleHassClass::init() +void MqttHandleHassClass::init(Scheduler* scheduler) { + scheduler->addTask(_loopTask); + _loopTask.setCallback(std::bind(&MqttHandleHassClass::loop, this)); + _loopTask.setIterations(TASK_FOREVER); + _loopTask.enable(); } void MqttHandleHassClass::loop() diff --git a/src/main.cpp b/src/main.cpp index b2072eecc..793a4850e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -102,7 +102,7 @@ void setup() MqttHandleDtu.init(); MqttHandleInverter.init(); MqttHandleInverterTotal.init(&scheduler); - MqttHandleHass.init(); + MqttHandleHass.init(&scheduler); MessageOutput.println("done"); // Initialize WebApi @@ -161,8 +161,6 @@ void loop() yield(); MqttHandleInverter.loop(); yield(); - MqttHandleHass.loop(); - yield(); WebApi.loop(); yield(); Display.loop();