mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2026-01-31 11:09:10 +03:00
added back CUSTOM MQTT format
This commit is contained in:
@@ -116,6 +116,7 @@ class MqttSettingsForm extends React.Component<MqttSettingsFormProps> {
|
|||||||
<MenuItem value={1}>Single</MenuItem>
|
<MenuItem value={1}>Single</MenuItem>
|
||||||
<MenuItem value={2}>Nested</MenuItem>
|
<MenuItem value={2}>Nested</MenuItem>
|
||||||
<MenuItem value={3}>Home Assistant</MenuItem>
|
<MenuItem value={3}>Home Assistant</MenuItem>
|
||||||
|
<MenuItem value={4}>Custom</MenuItem>
|
||||||
</SelectValidator>
|
</SelectValidator>
|
||||||
<SelectValidator name="mqtt_qos"
|
<SelectValidator name="mqtt_qos"
|
||||||
label="QoS"
|
label="QoS"
|
||||||
|
|||||||
@@ -325,7 +325,10 @@ void Sensor::publish_values() {
|
|||||||
for (const auto & device : devices_) {
|
for (const auto & device : devices_) {
|
||||||
char s[7];
|
char s[7];
|
||||||
|
|
||||||
if ((mqtt_format_ == Mqtt::Format::NESTED) || (mqtt_format_ == Mqtt::Format::HA)) {
|
if (mqtt_format_ == Mqtt::Format::CUSTOM) {
|
||||||
|
// e.g. sensor_data = {28-EA41-9497-0E03-5F":23.30,"28-233D-9497-0C03-8B":24.0}
|
||||||
|
doc[device.to_string()] = Helpers::render_value(s, device.temperature_c, 1);
|
||||||
|
} else if ((mqtt_format_ == Mqtt::Format::NESTED) || (mqtt_format_ == Mqtt::Format::HA)) {
|
||||||
// e.g. sensor_data = {"sensor1":{"id":"28-EA41-9497-0E03-5F","temp":"23.30"},"sensor2":{"id":"28-233D-9497-0C03-8B","temp":"24.0"}}
|
// e.g. sensor_data = {"sensor1":{"id":"28-EA41-9497-0E03-5F","temp":"23.30"},"sensor2":{"id":"28-233D-9497-0C03-8B","temp":"24.0"}}
|
||||||
char sensorID[20]; // sensor{1-n}
|
char sensorID[20]; // sensor{1-n}
|
||||||
strlcpy(sensorID, "sensor", 20);
|
strlcpy(sensorID, "sensor", 20);
|
||||||
@@ -337,7 +340,6 @@ void Sensor::publish_values() {
|
|||||||
|
|
||||||
// special for HA
|
// special for HA
|
||||||
if (mqtt_format_ == Mqtt::Format::HA) {
|
if (mqtt_format_ == Mqtt::Format::HA) {
|
||||||
std::string topic(100, '\0');
|
|
||||||
// create the config if this hasn't already been done
|
// create the config if this hasn't already been done
|
||||||
// to e.g. homeassistant/sensor/ems-esp/dallas_sensor1/config
|
// to e.g. homeassistant/sensor/ems-esp/dallas_sensor1/config
|
||||||
if (!(registered_ha_[i])) {
|
if (!(registered_ha_[i])) {
|
||||||
@@ -346,21 +348,22 @@ void Sensor::publish_values() {
|
|||||||
config["stat_t"] = F("ems-esp/sensor_data");
|
config["stat_t"] = F("ems-esp/sensor_data");
|
||||||
config["unit_of_meas"] = F("°C");
|
config["unit_of_meas"] = F("°C");
|
||||||
|
|
||||||
std::string str(50, '\0');
|
char str[50];
|
||||||
snprintf_P(&str[0], 50, PSTR("{{value_json.sensor%d.temp}}"), i);
|
snprintf_P(str, sizeof(str), PSTR("{{value_json.sensor%d.temp}}"), i);
|
||||||
config["val_tpl"] = str;
|
config["val_tpl"] = str;
|
||||||
|
|
||||||
snprintf_P(&str[0], 50, PSTR("Dallas sensor%d"), i);
|
snprintf_P(str, sizeof(str), PSTR("Dallas sensor%d"), i);
|
||||||
config["name"] = str;
|
config["name"] = str;
|
||||||
|
|
||||||
snprintf_P(&str[0], 50, PSTR("dalas_sensor%d"), i);
|
snprintf_P(str, sizeof(str), PSTR("dalas_sensor%d"), i);
|
||||||
config["uniq_id"] = str;
|
config["uniq_id"] = str;
|
||||||
|
|
||||||
JsonObject dev = config.createNestedObject("dev");
|
JsonObject dev = config.createNestedObject("dev");
|
||||||
JsonArray ids = dev.createNestedArray("ids");
|
JsonArray ids = dev.createNestedArray("ids");
|
||||||
ids.add("ems-esp");
|
ids.add("ems-esp");
|
||||||
|
|
||||||
snprintf_P(&topic[0], 60, PSTR("homeassistant/sensor/ems-esp/dallas_sensor%d/config"), i);
|
std::string topic(100, '\0');
|
||||||
|
snprintf_P(&topic[0], 100, PSTR("homeassistant/sensor/ems-esp/dallas_sensor%d/config"), i);
|
||||||
Mqtt::publish_retain(topic, config.as<JsonObject>(), true); // publish the config payload with retain flag
|
Mqtt::publish_retain(topic, config.as<JsonObject>(), true); // publish the config payload with retain flag
|
||||||
|
|
||||||
registered_ha_[i] = true;
|
registered_ha_[i] = true;
|
||||||
|
|||||||
Reference in New Issue
Block a user