use StatusRequest to signal mqtt connectivity - fixes updates being published only after mqtt has been connected
This commit is contained in:
parent
6468db5565
commit
2456ae1dc8
15
src/esp.h
15
src/esp.h
@ -1,20 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include "TaskScheduler.h"
|
||||
#include "mqtt.h"
|
||||
#include "ha.h"
|
||||
|
||||
using namespace Ha;
|
||||
|
||||
namespace HaESP {
|
||||
Task tHeap(1 * TASK_MINUTE, TASK_FOREVER, []() {
|
||||
Task tHeap(5 * TASK_MINUTE, TASK_FOREVER, []() {
|
||||
Sensor::mapSensors["heap_fragmentation"]->updateState(to_string(ESP.getHeapFragmentation()).c_str());
|
||||
Sensor::mapSensors["heap_free"]->updateState(to_string(ESP.getFreeHeap()).c_str());
|
||||
Sensor::mapSensors["heap_max_free_block"]->updateState(to_string(ESP.getMaxFreeBlockSize()).c_str());
|
||||
}, &ts);
|
||||
|
||||
Task tRestartInfo(TASK_IMMEDIATE, TASK_ONCE, [](){
|
||||
Sensor::mapSensors["restart_reason"]->updateState(ESP.getResetReason().c_str());
|
||||
}, &ts);
|
||||
Task tRestartInfo(TASK_IMMEDIATE, TASK_ONCE, []() {
|
||||
Sensor::mapSensors["restart_reason"]->updateState(ESP.getResetReason().c_str());
|
||||
}, &ts);
|
||||
|
||||
template <class T>
|
||||
Builder<T>& heapStats(Builder<T>& builder) {
|
||||
@ -34,14 +35,16 @@ namespace HaESP {
|
||||
.withPrecision(0)
|
||||
.build()
|
||||
);
|
||||
Mqtt::connectedStatus.setWaiting();
|
||||
tHeap.enable();
|
||||
return builder;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
Builder<T>& restartInfo(Builder<T>& builder) {
|
||||
builder.addDiagnostic(Builder<Sensor>::instance((new Sensor{"Restart reason", "restart_reason"})).build());
|
||||
tRestartInfo.enable();
|
||||
builder.addDiagnostic(Builder<Sensor>::instance((new Sensor{ "Restart reason", "restart_reason" })).build());
|
||||
Mqtt::connectedStatus.setWaiting();
|
||||
tRestartInfo.waitFor(&Mqtt::connectedStatus);
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
@ -2,10 +2,15 @@
|
||||
|
||||
#include <AsyncMqttClient.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <TaskScheduler.h>
|
||||
#include "ha.h"
|
||||
|
||||
#define MAIN_TOPIC "homeassistant/+/" MAIN_DEVICE_ID "/#"
|
||||
|
||||
using namespace Ha;
|
||||
|
||||
namespace Mqtt {
|
||||
StatusRequest connectedStatus;
|
||||
|
||||
AsyncMqttClient client;
|
||||
|
||||
@ -61,6 +66,7 @@ namespace Mqtt {
|
||||
client.subscribe(MAIN_TOPIC, 0);
|
||||
tReConnect.disable();
|
||||
Serial.println("Connected to MQTT");
|
||||
connectedStatus.signal();
|
||||
if (onConnected) onConnected();
|
||||
});
|
||||
client.onDisconnect([onDisconnected](AsyncMqttClientDisconnectReason reason) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user