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
|
#pragma once
|
||||||
|
|
||||||
#include "TaskScheduler.h"
|
#include "TaskScheduler.h"
|
||||||
|
#include "mqtt.h"
|
||||||
#include "ha.h"
|
#include "ha.h"
|
||||||
|
|
||||||
using namespace Ha;
|
using namespace Ha;
|
||||||
|
|
||||||
namespace HaESP {
|
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_fragmentation"]->updateState(to_string(ESP.getHeapFragmentation()).c_str());
|
||||||
Sensor::mapSensors["heap_free"]->updateState(to_string(ESP.getFreeHeap()).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());
|
Sensor::mapSensors["heap_max_free_block"]->updateState(to_string(ESP.getMaxFreeBlockSize()).c_str());
|
||||||
}, &ts);
|
}, &ts);
|
||||||
|
|
||||||
Task tRestartInfo(TASK_IMMEDIATE, TASK_ONCE, [](){
|
Task tRestartInfo(TASK_IMMEDIATE, TASK_ONCE, []() {
|
||||||
Sensor::mapSensors["restart_reason"]->updateState(ESP.getResetReason().c_str());
|
Sensor::mapSensors["restart_reason"]->updateState(ESP.getResetReason().c_str());
|
||||||
}, &ts);
|
}, &ts);
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
Builder<T>& heapStats(Builder<T>& builder) {
|
Builder<T>& heapStats(Builder<T>& builder) {
|
||||||
@ -34,14 +35,16 @@ namespace HaESP {
|
|||||||
.withPrecision(0)
|
.withPrecision(0)
|
||||||
.build()
|
.build()
|
||||||
);
|
);
|
||||||
|
Mqtt::connectedStatus.setWaiting();
|
||||||
tHeap.enable();
|
tHeap.enable();
|
||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
Builder<T>& restartInfo(Builder<T>& builder) {
|
Builder<T>& restartInfo(Builder<T>& builder) {
|
||||||
builder.addDiagnostic(Builder<Sensor>::instance((new Sensor{"Restart reason", "restart_reason"})).build());
|
builder.addDiagnostic(Builder<Sensor>::instance((new Sensor{ "Restart reason", "restart_reason" })).build());
|
||||||
tRestartInfo.enable();
|
Mqtt::connectedStatus.setWaiting();
|
||||||
|
tRestartInfo.waitFor(&Mqtt::connectedStatus);
|
||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2,10 +2,15 @@
|
|||||||
|
|
||||||
#include <AsyncMqttClient.h>
|
#include <AsyncMqttClient.h>
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
|
#include <TaskScheduler.h>
|
||||||
|
#include "ha.h"
|
||||||
|
|
||||||
#define MAIN_TOPIC "homeassistant/+/" MAIN_DEVICE_ID "/#"
|
#define MAIN_TOPIC "homeassistant/+/" MAIN_DEVICE_ID "/#"
|
||||||
|
|
||||||
|
using namespace Ha;
|
||||||
|
|
||||||
namespace Mqtt {
|
namespace Mqtt {
|
||||||
|
StatusRequest connectedStatus;
|
||||||
|
|
||||||
AsyncMqttClient client;
|
AsyncMqttClient client;
|
||||||
|
|
||||||
@ -61,6 +66,7 @@ namespace Mqtt {
|
|||||||
client.subscribe(MAIN_TOPIC, 0);
|
client.subscribe(MAIN_TOPIC, 0);
|
||||||
tReConnect.disable();
|
tReConnect.disable();
|
||||||
Serial.println("Connected to MQTT");
|
Serial.println("Connected to MQTT");
|
||||||
|
connectedStatus.signal();
|
||||||
if (onConnected) onConnected();
|
if (onConnected) onConnected();
|
||||||
});
|
});
|
||||||
client.onDisconnect([onDisconnected](AsyncMqttClientDisconnectReason reason) {
|
client.onDisconnect([onDisconnected](AsyncMqttClientDisconnectReason reason) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user