Merge branch 'optimizations' into v1.4.0
This commit is contained in:
commit
50e6c2586d
19
src/esp.h
19
src/esp.h
@ -12,9 +12,16 @@ namespace HaESP {
|
|||||||
} activeSensors;
|
} activeSensors;
|
||||||
|
|
||||||
Task tHeap(5 * TASK_MINUTE, TASK_FOREVER, []() {
|
Task tHeap(5 * TASK_MINUTE, TASK_FOREVER, []() {
|
||||||
Sensor::mapSensors["heap_fragmentation"]->updateState(to_string(ESP.getHeapFragmentation()).c_str());
|
uint32_t hfree;
|
||||||
Sensor::mapSensors["heap_free"]->updateState(to_string(ESP.getFreeHeap()).c_str());
|
uint32_t hmax;
|
||||||
Sensor::mapSensors["heap_max_free_block"]->updateState(to_string(ESP.getMaxFreeBlockSize()).c_str());
|
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);
|
}, &ts);
|
||||||
|
|
||||||
Task tRestartInfo(TASK_IMMEDIATE, TASK_ONCE, []() {
|
Task tRestartInfo(TASK_IMMEDIATE, TASK_ONCE, []() {
|
||||||
@ -23,9 +30,13 @@ namespace HaESP {
|
|||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
Builder<T>& heapStats(Builder<T>& builder) {
|
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("%")
|
.withUnitMeasure("%")
|
||||||
.withPrecision(0)
|
.withPrecision(0)
|
||||||
|
.withValueTemplate("{{ value_json.fragmentation }}")
|
||||||
|
.overrideConfig("json_attributes_topic", heap->stateTopic)
|
||||||
|
.overrideConfig("json_attributes_template", "{{ value_json.heap | tojson }}")
|
||||||
.build());
|
.build());
|
||||||
builder.addDiagnostic(Builder<Sensor>::instance(new Sensor{ "Heap free", "heap_free" })
|
builder.addDiagnostic(Builder<Sensor>::instance(new Sensor{ "Heap free", "heap_free" })
|
||||||
.withDeviceClass("data_size")
|
.withDeviceClass("data_size")
|
||||||
|
|||||||
42
src/ha.h
42
src/ha.h
@ -7,6 +7,7 @@ using namespace std;
|
|||||||
|
|
||||||
#define JSON_SIZE 512
|
#define JSON_SIZE 512
|
||||||
#define TOPIC_SIZE 255
|
#define TOPIC_SIZE 255
|
||||||
|
#define BASE_TOPIC "homeassistant/%s/%s/%s"
|
||||||
|
|
||||||
namespace Ha {
|
namespace Ha {
|
||||||
uint16_t(*publisher)(const char* topic, const char* message);
|
uint16_t(*publisher)(const char* topic, const char* message);
|
||||||
@ -87,7 +88,7 @@ namespace Ha {
|
|||||||
const char* entityCategory = nullptr;
|
const char* entityCategory = nullptr;
|
||||||
const char* deviceClass = nullptr;
|
const char* deviceClass = nullptr;
|
||||||
const char* name = nullptr;
|
const char* name = nullptr;
|
||||||
char* id = nullptr;
|
const char* id = nullptr;
|
||||||
const char* type = nullptr;
|
const char* type = nullptr;
|
||||||
char configTopic[TOPIC_SIZE];
|
char configTopic[TOPIC_SIZE];
|
||||||
JsonPairs<bool> jsonBooleans;
|
JsonPairs<bool> jsonBooleans;
|
||||||
@ -97,7 +98,7 @@ namespace Ha {
|
|||||||
static List<Component> components;
|
static List<Component> components;
|
||||||
bool multiValueComponent = false;
|
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);
|
components.add(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,12 +117,13 @@ namespace Ha {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void buildUniqueId(char* uniqueId) {
|
char uniqueId[50];
|
||||||
sprintf(uniqueId, "%s_%s", MAIN_DEVICE_ID, id);
|
virtual void buildUniqueId() {
|
||||||
|
snprintf(uniqueId, sizeof(uniqueId), "%s_%s", MAIN_DEVICE_ID, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void buildConfigTopic() {
|
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;
|
virtual void buildComponentConfig(JsonDocument& jsonDoc) = 0;
|
||||||
@ -131,9 +133,8 @@ namespace Ha {
|
|||||||
if (entityCategory) jsonDoc["entity_category"] = entityCategory;
|
if (entityCategory) jsonDoc["entity_category"] = entityCategory;
|
||||||
if (deviceClass) jsonDoc["device_class"] = deviceClass;
|
if (deviceClass) jsonDoc["device_class"] = deviceClass;
|
||||||
jsonDoc["name"] = name;
|
jsonDoc["name"] = name;
|
||||||
char uniqueId[50];
|
buildUniqueId();
|
||||||
buildUniqueId(uniqueId);
|
jsonDoc["unique_id"] = (const char*)uniqueId;
|
||||||
jsonDoc["unique_id"] = uniqueId;
|
|
||||||
buildConfigTopic();
|
buildConfigTopic();
|
||||||
|
|
||||||
buildComponentConfig(jsonDoc);
|
buildComponentConfig(jsonDoc);
|
||||||
@ -205,8 +206,7 @@ namespace Ha {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename V>
|
Builder& overrideConfig(const char* key, float value) {
|
||||||
Builder& overrideConfig(const char* key, V value) {
|
|
||||||
cmp->jsonNumbers.add({key, value});
|
cmp->jsonNumbers.add({key, value});
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@ -262,7 +262,7 @@ namespace Ha {
|
|||||||
static unordered_map<string, Command*> mapCommands;
|
static unordered_map<string, Command*> mapCommands;
|
||||||
|
|
||||||
Command(const char* name, const char* id, const char* type, onMessage f) : Component(name, id, type), f(f) {
|
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 });
|
mapCommands.insert({ string(commandTopic), this });
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -276,7 +276,7 @@ namespace Ha {
|
|||||||
virtual void buildCommandConfig(JsonDocument& jsonDoc) = 0;
|
virtual void buildCommandConfig(JsonDocument& jsonDoc) = 0;
|
||||||
|
|
||||||
void buildComponentConfig(JsonDocument& jsonDoc) override {
|
void buildComponentConfig(JsonDocument& jsonDoc) override {
|
||||||
jsonDoc["command_topic"] = commandTopic;
|
jsonDoc["command_topic"] = (const char*)commandTopic;
|
||||||
if (retain) jsonDoc["retain"] = true;
|
if (retain) jsonDoc["retain"] = true;
|
||||||
buildCommandConfig(jsonDoc);
|
buildCommandConfig(jsonDoc);
|
||||||
}
|
}
|
||||||
@ -293,11 +293,12 @@ namespace Ha {
|
|||||||
|
|
||||||
struct StateConfig {
|
struct StateConfig {
|
||||||
static unordered_map<string, Command*> mapStateTopics;
|
static unordered_map<string, Command*> mapStateTopics;
|
||||||
|
char stateTopic[TOPIC_SIZE];
|
||||||
|
|
||||||
StateConfig(Component* cmp) : cmp(cmp) {}
|
StateConfig(Component* cmp) : cmp(cmp) {}
|
||||||
|
|
||||||
void withStateTopic() {
|
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) {
|
void updateState(const char* message) {
|
||||||
@ -305,10 +306,9 @@ namespace Ha {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
char stateTopic[TOPIC_SIZE];
|
|
||||||
Component* cmp;
|
Component* cmp;
|
||||||
void buildConfig(JsonDocument& jsonDoc) {
|
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) {}
|
: Command(name, id, "number", f), StateConfig(this), min(min), max(max), step(step) {}
|
||||||
|
|
||||||
void updateState(unsigned int value) {
|
void updateState(unsigned int value) {
|
||||||
char message[32];
|
StateConfig::updateState(to_string(value).c_str());
|
||||||
sprintf(message, "%u", value);
|
|
||||||
StateConfig::updateState(message);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void restoreFromState() {
|
void restoreFromState() {
|
||||||
@ -367,17 +365,17 @@ namespace Ha {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void buildUniqueId(char* uniqueId) override {
|
void buildUniqueId() override {
|
||||||
if (multiValueComponent && deviceClass) {
|
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 {
|
} else {
|
||||||
Component::buildUniqueId(uniqueId);
|
Component::buildUniqueId();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void buildConfigTopic() override {
|
void buildConfigTopic() override {
|
||||||
if (multiValueComponent && deviceClass) {
|
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 {
|
} else {
|
||||||
Component::buildConfigTopic();
|
Component::buildConfigTopic();
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user