mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 15:59:52 +03:00
HA binary sensors put on MQTT queue
This commit is contained in:
31
src/mqtt.cpp
31
src/mqtt.cpp
@@ -687,8 +687,7 @@ void Mqtt::register_mqtt_ha_binary_sensor(const __FlashStringHelper * name, cons
|
||||
return;
|
||||
}
|
||||
|
||||
DynamicJsonDocument doc(EMSESP_MAX_JSON_SIZE_SMALL);
|
||||
// StaticJsonDocument<EMSESP_MAX_JSON_SIZE_SMALL> doc;
|
||||
StaticJsonDocument<EMSESP_MAX_JSON_SIZE_SMALL> doc;
|
||||
|
||||
doc["name"] = name;
|
||||
doc["uniq_id"] = entity;
|
||||
@@ -716,31 +715,16 @@ void Mqtt::register_mqtt_ha_binary_sensor(const __FlashStringHelper * name, cons
|
||||
snprintf_P(ha_device, sizeof(ha_device), PSTR("ems-esp-%s"), EMSdevice::device_type_2_device_name(device_type).c_str());
|
||||
ids.add(ha_device);
|
||||
|
||||
doc.shrinkToFit();
|
||||
|
||||
char topic[MQTT_TOPIC_MAX_SIZE];
|
||||
snprintf_P(topic, sizeof(topic), PSTR("homeassistant/binary_sensor/ems-esp/%s/config"), entity);
|
||||
|
||||
// convert json to string and publish immediately with retain forced to true
|
||||
char payload_text[256];
|
||||
serializeJson(doc, payload_text); // convert json to string
|
||||
uint16_t packet_id = mqttClient_->publish(topic, 0, true, payload_text);
|
||||
#if defined(EMSESP_STANDALONE)
|
||||
LOG_DEBUG(F("Publishing topic %s"), topic);
|
||||
#else
|
||||
if (!packet_id) {
|
||||
LOG_ERROR(F("Failed to publish topic %s"), topic);
|
||||
} else {
|
||||
LOG_DEBUG(F("Publishing topic %s"), topic);
|
||||
}
|
||||
#endif
|
||||
|
||||
// delay(MQTT_PUBLISH_WAIT);
|
||||
delay(50);
|
||||
// queue MQTT publish
|
||||
publish(topic, doc.as<JsonObject>());
|
||||
}
|
||||
|
||||
// HA config for a normal 'sensor' type
|
||||
// entity must match the key/value pair in the _data topic
|
||||
// some string copying here into chars, it looks messy but does help with heap fragmentation issues
|
||||
void Mqtt::register_mqtt_ha_sensor(const char * prefix,
|
||||
const __FlashStringHelper * suffix,
|
||||
const __FlashStringHelper * name,
|
||||
@@ -797,8 +781,7 @@ void Mqtt::register_mqtt_ha_sensor(const char * prefix,
|
||||
}
|
||||
new_name[0] = toupper(new_name[0]); // capitalize first letter
|
||||
|
||||
DynamicJsonDocument doc(EMSESP_MAX_JSON_SIZE_SMALL);
|
||||
// StaticJsonDocument<EMSESP_MAX_JSON_SIZE_SMALL> doc;
|
||||
StaticJsonDocument<EMSESP_MAX_JSON_SIZE_HA_CONFIG> doc;
|
||||
|
||||
doc["name"] = new_name;
|
||||
doc["uniq_id"] = uniq.c_str();
|
||||
@@ -814,9 +797,8 @@ void Mqtt::register_mqtt_ha_sensor(const char * prefix,
|
||||
JsonArray ids = dev.createNestedArray("ids");
|
||||
ids.add(ha_device);
|
||||
|
||||
doc.shrinkToFit();
|
||||
// convert json to string and publish immediately with retain forced to true
|
||||
char payload_text[256];
|
||||
char payload_text[EMSESP_MAX_JSON_SIZE_HA_CONFIG];
|
||||
serializeJson(doc, payload_text); // convert json to string
|
||||
|
||||
uint16_t packet_id = mqttClient_->publish(topic, 0, true, payload_text);
|
||||
@@ -830,7 +812,6 @@ void Mqtt::register_mqtt_ha_sensor(const char * prefix,
|
||||
#endif
|
||||
}
|
||||
|
||||
// delay(MQTT_PUBLISH_WAIT); // don't flood asynctcp
|
||||
delay(50); // enough time to send the short message out
|
||||
}
|
||||
} // namespace emsesp
|
||||
|
||||
Reference in New Issue
Block a user