Merge branch 'optimizations' into v1.4.0

This commit is contained in:
Nicu Hodos 2024-10-28 11:55:37 +01:00
commit 50e6c2586d
2 changed files with 35 additions and 26 deletions

View File

@ -12,9 +12,16 @@ namespace HaESP {
} activeSensors;
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());
uint32_t hfree;
uint32_t hmax;
uint8_t hfrag;
ESP.getHeapStats(&hfree, &hmax, &hfrag);
char value[128];
snprintf_P(value, sizeof(value), PSTR("{\"fragmentation\":%d,\"heap\":{\"Heap free\":\"%d B\",\"Heap max free block\":\"%d B\"}}"), hfrag, hfree, hmax);
Sensor::mapSensors["heap_fragmentation"]->updateState(value);
Sensor::mapSensors["heap_free"]->updateState(to_string(hfree).c_str());
Sensor::mapSensors["heap_max_free_block"]->updateState(to_string(hmax).c_str());
}, &ts);
Task tRestartInfo(TASK_IMMEDIATE, TASK_ONCE, []() {
@ -23,9 +30,13 @@ namespace HaESP {
template <class T>
Builder<T>& heapStats(Builder<T>& builder) {
builder.addDiagnostic(Builder<Sensor>::instance(new Sensor{ "Heap fragmentation", "heap_fragmentation" })
auto heap = new Sensor{ "Heap fragmentation", "heap_fragmentation" };
builder.addDiagnostic(Builder<Sensor>::instance(heap)
.withUnitMeasure("%")
.withPrecision(0)
.withValueTemplate("{{ value_json.fragmentation }}")
.overrideConfig("json_attributes_topic", heap->stateTopic)
.overrideConfig("json_attributes_template", "{{ value_json.heap | tojson }}")
.build());
builder.addDiagnostic(Builder<Sensor>::instance(new Sensor{ "Heap free", "heap_free" })
.withDeviceClass("data_size")

View File

@ -7,6 +7,7 @@ using namespace std;
#define JSON_SIZE 512
#define TOPIC_SIZE 255
#define BASE_TOPIC "homeassistant/%s/%s/%s"
namespace Ha {
uint16_t(*publisher)(const char* topic, const char* message);
@ -87,7 +88,7 @@ namespace Ha {
const char* entityCategory = nullptr;
const char* deviceClass = nullptr;
const char* name = nullptr;
char* id = nullptr;
const char* id = nullptr;
const char* type = nullptr;
char configTopic[TOPIC_SIZE];
JsonPairs<bool> jsonBooleans;
@ -97,7 +98,7 @@ namespace Ha {
static List<Component> components;
bool multiValueComponent = false;
Component(const char* name, const char* id, const char* type) : name(name), id((char*)id), type(type) {
Component(const char* name, const char* id, const char* type) : name(name), id(id), type(type) {
components.add(this);
}
@ -116,12 +117,13 @@ namespace Ha {
}
protected:
virtual void buildUniqueId(char* uniqueId) {
sprintf(uniqueId, "%s_%s", MAIN_DEVICE_ID, id);
char uniqueId[50];
virtual void buildUniqueId() {
snprintf(uniqueId, sizeof(uniqueId), "%s_%s", MAIN_DEVICE_ID, id);
}
virtual void buildConfigTopic() {
sprintf(configTopic, "homeassistant/%s/%s/%s/config", type, MAIN_DEVICE_ID, id);
snprintf(configTopic, sizeof(configTopic), BASE_TOPIC"/config", type, MAIN_DEVICE_ID, id);
}
virtual void buildComponentConfig(JsonDocument& jsonDoc) = 0;
@ -131,9 +133,8 @@ namespace Ha {
if (entityCategory) jsonDoc["entity_category"] = entityCategory;
if (deviceClass) jsonDoc["device_class"] = deviceClass;
jsonDoc["name"] = name;
char uniqueId[50];
buildUniqueId(uniqueId);
jsonDoc["unique_id"] = uniqueId;
buildUniqueId();
jsonDoc["unique_id"] = (const char*)uniqueId;
buildConfigTopic();
buildComponentConfig(jsonDoc);
@ -205,8 +206,7 @@ namespace Ha {
return *this;
}
template <typename V>
Builder& overrideConfig(const char* key, V value) {
Builder& overrideConfig(const char* key, float value) {
cmp->jsonNumbers.add({key, value});
return *this;
}
@ -262,7 +262,7 @@ namespace Ha {
static unordered_map<string, Command*> mapCommands;
Command(const char* name, const char* id, const char* type, onMessage f) : Component(name, id, type), f(f) {
sprintf(commandTopic, "homeassistant/%s/%s/%s/set", type, MAIN_DEVICE_ID, id);
snprintf(commandTopic, sizeof(commandTopic), BASE_TOPIC"/set", type, MAIN_DEVICE_ID, id);
mapCommands.insert({ string(commandTopic), this });
}
@ -276,7 +276,7 @@ namespace Ha {
virtual void buildCommandConfig(JsonDocument& jsonDoc) = 0;
void buildComponentConfig(JsonDocument& jsonDoc) override {
jsonDoc["command_topic"] = commandTopic;
jsonDoc["command_topic"] = (const char*)commandTopic;
if (retain) jsonDoc["retain"] = true;
buildCommandConfig(jsonDoc);
}
@ -293,11 +293,12 @@ namespace Ha {
struct StateConfig {
static unordered_map<string, Command*> mapStateTopics;
char stateTopic[TOPIC_SIZE];
StateConfig(Component* cmp) : cmp(cmp) {}
void withStateTopic() {
sprintf(stateTopic, "homeassistant/%s/%s/%s/state", cmp->type, MAIN_DEVICE_ID, cmp->id);
snprintf(stateTopic, sizeof(stateTopic), BASE_TOPIC"/state", cmp->type, MAIN_DEVICE_ID, cmp->id);
}
void updateState(const char* message) {
@ -305,10 +306,9 @@ namespace Ha {
}
protected:
char stateTopic[TOPIC_SIZE];
Component* cmp;
void buildConfig(JsonDocument& jsonDoc) {
if (stateTopic[0]) jsonDoc["state_topic"] = stateTopic;
if (stateTopic[0]) jsonDoc["state_topic"] = (const char*)stateTopic;
}
};
@ -337,9 +337,7 @@ namespace Ha {
: Command(name, id, "number", f), StateConfig(this), min(min), max(max), step(step) {}
void updateState(unsigned int value) {
char message[32];
sprintf(message, "%u", value);
StateConfig::updateState(message);
StateConfig::updateState(to_string(value).c_str());
}
void restoreFromState() {
@ -367,17 +365,17 @@ namespace Ha {
}
protected:
void buildUniqueId(char* uniqueId) override {
void buildUniqueId() override {
if (multiValueComponent && deviceClass) {
sprintf(uniqueId, "%s_%s_%s", MAIN_DEVICE_ID, deviceClass, id);
snprintf(uniqueId, sizeof(uniqueId), "%s_%s_%s", MAIN_DEVICE_ID, deviceClass, id);
} else {
Component::buildUniqueId(uniqueId);
Component::buildUniqueId();
}
}
void buildConfigTopic() override {
if (multiValueComponent && deviceClass) {
sprintf(configTopic, "homeassistant/%s/%s/%s_%s/config", type, MAIN_DEVICE_ID, deviceClass, id);
snprintf(configTopic, sizeof(configTopic), BASE_TOPIC"_%s""/config", type, MAIN_DEVICE_ID, deviceClass, id);
} else {
Component::buildConfigTopic();
}