Skip to content

Commit

Permalink
MQTT Hass: Move serialization and allocation check into own method
Browse files Browse the repository at this point in the history
  • Loading branch information
tbnobody committed Sep 24, 2024
1 parent 9a318d5 commit 2213ad7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 35 deletions.
1 change: 1 addition & 0 deletions include/MqttHandleHass.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class MqttHandleHassClass {
private:
void loop();
static void publish(const String& subtopic, const String& payload);
static void publish(const String& subtopic, const JsonDocument& doc);

// Binary Sensor
static void publishBinarySensor(JsonDocument& doc, const String& root_device, const String& unique_id_prefix, const String& name, const String& state_topic, const String& payload_on, const String& payload_off, const String& device_class, const String& category);
Expand Down
50 changes: 15 additions & 35 deletions src/MqttHandleHass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,7 @@ void MqttHandleHassClass::publishInverterField(std::shared_ptr<InverterAbstract>
root["stat_cla"] = stateCls;
}

if (!Utils::checkJsonAlloc(root, __FUNCTION__, __LINE__)) {
return;
}

String buffer;
serializeJson(root, buffer);
publish(configTopic, buffer);
publish(configTopic, root);
} else {
publish(configTopic, "");
}
Expand Down Expand Up @@ -213,13 +207,7 @@ void MqttHandleHassClass::publishInverterButton(std::shared_ptr<InverterAbstract

createInverterInfo(root, inv);

if (!Utils::checkJsonAlloc(root, __FUNCTION__, __LINE__)) {
return;
}

String buffer;
serializeJson(root, buffer);
publish(configTopic, buffer);
publish(configTopic, root);
}

void MqttHandleHassClass::publishInverterNumber(
Expand Down Expand Up @@ -258,13 +246,7 @@ void MqttHandleHassClass::publishInverterNumber(

createInverterInfo(root, inv);

if (!Utils::checkJsonAlloc(root, __FUNCTION__, __LINE__)) {
return;
}

String buffer;
serializeJson(root, buffer);
publish(configTopic, buffer);
publish(configTopic, root);
}

void MqttHandleHassClass::createInverterInfo(JsonDocument& root, std::shared_ptr<InverterAbstract> inv)
Expand Down Expand Up @@ -330,6 +312,16 @@ void MqttHandleHassClass::publish(const String& subtopic, const String& payload)
yield();
}

void MqttHandleHassClass::publish(const String& subtopic, const JsonDocument& doc)
{
if (!Utils::checkJsonAlloc(doc, __FUNCTION__, __LINE__)) {
return;
}
String buffer;
serializeJson(doc, buffer);
publish(subtopic, buffer);
}

void MqttHandleHassClass::publishBinarySensor(JsonDocument& doc, const String& root_device, const String& unique_id_prefix, const String& name, const String& state_topic, const String& payload_on, const String& payload_off, const String& device_class, const String& category)
{
String sensor_id = name;
Expand All @@ -349,14 +341,8 @@ void MqttHandleHassClass::publishBinarySensor(JsonDocument& doc, const String& r
doc["ent_cat"] = category;
}

if (!Utils::checkJsonAlloc(doc, __FUNCTION__, __LINE__)) {
return;
}

String buffer;
const String configTopic = "binary_sensor/" + root_device + "/" + sensor_id + "/config";
serializeJson(doc, buffer);
publish(configTopic, buffer);
publish(configTopic, doc);
}

void MqttHandleHassClass::publishDtuBinarySensor(const String& name, const String& state_topic, const String& payload_on, const String& payload_off, const String& device_class, const String& category)
Expand Down Expand Up @@ -405,14 +391,8 @@ void MqttHandleHassClass::publishSensor(JsonDocument& doc, const String& root_de
doc["pl_avail"] = config.Mqtt.Lwt.Value_Online;
doc["pl_not_avail"] = config.Mqtt.Lwt.Value_Offline;

if (!Utils::checkJsonAlloc(doc, __FUNCTION__, __LINE__)) {
return;
}

String buffer;
const String configTopic = "sensor/" + root_device + "/" + sensor_id + "/config";
serializeJson(doc, buffer);
publish(configTopic, buffer);
publish(configTopic, doc);
}

void MqttHandleHassClass::publishDtuSensor(const String& name, const String& state_topic, const String& unit_of_measure, const String& icon, const String& device_class, const String& category)
Expand Down

0 comments on commit 2213ad7

Please sign in to comment.