mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 15:59:52 +03:00
Expose EMS-ESP commands (like reset) via MQTT Discovery #1605
This commit is contained in:
@@ -1561,7 +1561,7 @@ void EMSdevice::publish_all_values() {
|
|||||||
|
|
||||||
// For each value in the device create the json object pair and add it to given json
|
// For each value in the device create the json object pair and add it to given json
|
||||||
// return false if empty
|
// return false if empty
|
||||||
// this is used to create the MQTT payloads, Console messages and Web API calls
|
// this is used to create the MQTT payloads, Console messages and Web API call responses
|
||||||
bool EMSdevice::generate_values(JsonObject output, const uint8_t tag_filter, const bool nested, const uint8_t output_target) {
|
bool EMSdevice::generate_values(JsonObject output, const uint8_t tag_filter, const bool nested, const uint8_t output_target) {
|
||||||
bool has_values = false; // to see if we've added a value. it's faster than doing a json.size() at the end
|
bool has_values = false; // to see if we've added a value. it's faster than doing a json.size() at the end
|
||||||
uint8_t old_tag = 255; // NAN
|
uint8_t old_tag = 255; // NAN
|
||||||
@@ -1697,8 +1697,8 @@ bool EMSdevice::generate_values(JsonObject output, const uint8_t tag_filter, con
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// commenting out - we don't want Commands in MQTT or Console
|
// commenting out as we don't want Commands in Console ('show values')
|
||||||
// else if (dv.type == DeviceValueType::CMD && output_target != EMSdevice::OUTPUT_TARGET::MQTT) {
|
// else if (dv.type == DeviceValueType::CMD && output_target != EMSdevice::OUTPUT_TARGET::MQTT) {
|
||||||
// json[name] = "";
|
// json[name] = "";
|
||||||
// }
|
// }
|
||||||
|
|
||||||
@@ -1748,7 +1748,7 @@ void EMSdevice::mqtt_ha_entity_config_create() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!dv.has_state(DeviceValueState::DV_HA_CONFIG_CREATED) && (dv.type != DeviceValueType::CMD) && dv.has_state(DeviceValueState::DV_ACTIVE)
|
if (!dv.has_state(DeviceValueState::DV_HA_CONFIG_CREATED) && dv.has_state(DeviceValueState::DV_ACTIVE)
|
||||||
&& !dv.has_state(DeviceValueState::DV_API_MQTT_EXCLUDE)) {
|
&& !dv.has_state(DeviceValueState::DV_API_MQTT_EXCLUDE)) {
|
||||||
// create_device_config is only done once for the EMS device. It can added to any entity, so we take the first
|
// create_device_config is only done once for the EMS device. It can added to any entity, so we take the first
|
||||||
if (Mqtt::publish_ha_sensor_config(dv, name(), brand_to_char(), false, create_device_config)) {
|
if (Mqtt::publish_ha_sensor_config(dv, name(), brand_to_char(), false, create_device_config)) {
|
||||||
|
|||||||
82
src/mqtt.cpp
82
src/mqtt.cpp
@@ -878,6 +878,7 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
|
|||||||
readonly_sensors = false;
|
readonly_sensors = false;
|
||||||
break;
|
break;
|
||||||
case DeviceValueType::ENUM:
|
case DeviceValueType::ENUM:
|
||||||
|
case DeviceValueType::CMD: // hardcoded commands are always ENUMS
|
||||||
// select - https://www.home-assistant.io/integrations/select.mqtt
|
// select - https://www.home-assistant.io/integrations/select.mqtt
|
||||||
snprintf(topic, sizeof(topic), "select/%s", config_topic);
|
snprintf(topic, sizeof(topic), "select/%s", config_topic);
|
||||||
readonly_sensors = false;
|
readonly_sensors = false;
|
||||||
@@ -927,8 +928,8 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
|
|||||||
}
|
}
|
||||||
doc["cmd_t"] = command_topic;
|
doc["cmd_t"] = command_topic;
|
||||||
|
|
||||||
// extend for enums, add options
|
// extend for enums and also commands, add options
|
||||||
if (type == DeviceValueType::ENUM) {
|
if ((type == DeviceValueType::ENUM) || (type == DeviceValueType::CMD)) {
|
||||||
JsonArray option_list = doc["ops"].to<JsonArray>();
|
JsonArray option_list = doc["ops"].to<JsonArray>();
|
||||||
if (EMSESP::system_.enum_format() == ENUM_FORMAT_INDEX) {
|
if (EMSESP::system_.enum_format() == ENUM_FORMAT_INDEX) {
|
||||||
// use index numbers
|
// use index numbers
|
||||||
@@ -943,7 +944,6 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
|
|||||||
}
|
}
|
||||||
snprintf(sample_val, sizeof(sample_val), "'%s'", Helpers::translated_word(options[0]));
|
snprintf(sample_val, sizeof(sample_val), "'%s'", Helpers::translated_word(options[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (type != DeviceValueType::STRING && type != DeviceValueType::BOOL) {
|
} else if (type != DeviceValueType::STRING && type != DeviceValueType::BOOL) {
|
||||||
// For numeric's add the range
|
// For numeric's add the range
|
||||||
doc["mode"] = "box"; // auto, slider or box
|
doc["mode"] = "box"; // auto, slider or box
|
||||||
@@ -979,11 +979,6 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// state topic
|
|
||||||
char stat_t[MQTT_TOPIC_MAX_SIZE];
|
|
||||||
snprintf(stat_t, sizeof(stat_t), "%s/%s", Mqtt::base().c_str(), tag_to_topic(device_type, tag).c_str());
|
|
||||||
doc["stat_t"] = stat_t;
|
|
||||||
|
|
||||||
// friendly name = <tag> <name>
|
// friendly name = <tag> <name>
|
||||||
char ha_name[70];
|
char ha_name[70];
|
||||||
char * F_name = strdup(fullname);
|
char * F_name = strdup(fullname);
|
||||||
@@ -997,38 +992,49 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
|
|||||||
free(F_name); // very important!
|
free(F_name); // very important!
|
||||||
doc["name"] = ha_name;
|
doc["name"] = ha_name;
|
||||||
|
|
||||||
// value template
|
|
||||||
// if its nested mqtt format then use the appended entity name, otherwise take the original name
|
|
||||||
char val_obj[100];
|
|
||||||
char val_cond[200];
|
|
||||||
if (is_nested() && tag >= DeviceValueTAG::TAG_HC1) {
|
|
||||||
snprintf(val_obj, sizeof(val_obj), "value_json.%s.%s", EMSdevice::tag_to_mqtt(tag), entity);
|
|
||||||
snprintf(val_cond, sizeof(val_cond), "value_json.%s is defined and %s is defined", EMSdevice::tag_to_mqtt(tag), val_obj);
|
|
||||||
} else {
|
|
||||||
snprintf(val_obj, sizeof(val_obj), "value_json.%s", entity);
|
|
||||||
snprintf(val_cond, sizeof(val_cond), "%s is defined", val_obj);
|
|
||||||
}
|
|
||||||
|
|
||||||
// special case to handle booleans
|
// not needed for commands
|
||||||
// applies to both Binary Sensor (read only) and a Switch (for a command)
|
if (type != DeviceValueType::CMD) {
|
||||||
// has no unit of measure or icon
|
// state topic, except for commands
|
||||||
if (type == DeviceValueType::BOOL) {
|
char stat_t[MQTT_TOPIC_MAX_SIZE];
|
||||||
if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) {
|
snprintf(stat_t, sizeof(stat_t), "%s/%s", Mqtt::base().c_str(), tag_to_topic(device_type, tag).c_str());
|
||||||
doc["pl_on"] = true;
|
doc["stat_t"] = stat_t;
|
||||||
doc["pl_off"] = false;
|
|
||||||
snprintf(sample_val, sizeof(sample_val), "false");
|
// value template
|
||||||
} else if (EMSESP::system_.bool_format() == BOOL_FORMAT_10) {
|
// if its nested mqtt format then use the appended entity name, otherwise take the original name
|
||||||
doc["pl_on"] = 1;
|
char val_obj[100];
|
||||||
doc["pl_off"] = 0;
|
char val_cond[200];
|
||||||
|
if (is_nested() && tag >= DeviceValueTAG::TAG_HC1) {
|
||||||
|
snprintf(val_obj, sizeof(val_obj), "value_json.%s.%s", EMSdevice::tag_to_mqtt(tag), entity);
|
||||||
|
snprintf(val_cond, sizeof(val_cond), "value_json.%s is defined and %s is defined", EMSdevice::tag_to_mqtt(tag), val_obj);
|
||||||
} else {
|
} else {
|
||||||
char result[12];
|
snprintf(val_obj, sizeof(val_obj), "value_json.%s", entity);
|
||||||
doc["pl_on"] = Helpers::render_boolean(result, true);
|
snprintf(val_cond, sizeof(val_cond), "%s is defined", val_obj);
|
||||||
doc["pl_off"] = Helpers::render_boolean(result, false);
|
|
||||||
snprintf(sample_val, sizeof(sample_val), "'%s'", Helpers::render_boolean(result, false));
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
doc["val_tpl"] = (std::string) "{{" + val_obj + " if " + val_cond + " else " + sample_val + "}}";
|
// special case to handle booleans
|
||||||
|
// applies to both Binary Sensor (read only) and a Switch (for a command)
|
||||||
|
// has no unit of measure or icon
|
||||||
|
if (type == DeviceValueType::BOOL) {
|
||||||
|
if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) {
|
||||||
|
doc["pl_on"] = true;
|
||||||
|
doc["pl_off"] = false;
|
||||||
|
snprintf(sample_val, sizeof(sample_val), "false");
|
||||||
|
} else if (EMSESP::system_.bool_format() == BOOL_FORMAT_10) {
|
||||||
|
doc["pl_on"] = 1;
|
||||||
|
doc["pl_off"] = 0;
|
||||||
|
} else {
|
||||||
|
char result[12];
|
||||||
|
doc["pl_on"] = Helpers::render_boolean(result, true);
|
||||||
|
doc["pl_off"] = Helpers::render_boolean(result, false);
|
||||||
|
snprintf(sample_val, sizeof(sample_val), "'%s'", Helpers::render_boolean(result, false));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
doc["val_tpl"] = (std::string) "{{" + val_obj + " if " + val_cond + " else " + sample_val + "}}";
|
||||||
|
|
||||||
|
// add the dev json object to the end, not for commands
|
||||||
|
add_ha_sections_to_doc(nullptr, stat_t, doc, false, val_cond); // no name, since the "dev" has already been adde
|
||||||
|
}
|
||||||
|
|
||||||
// Add the state class, device class and sometimes the icon. Used only for read-only sensors like Sensor and Binary Sensor
|
// Add the state class, device class and sometimes the icon. Used only for read-only sensors like Sensor and Binary Sensor
|
||||||
if (readonly_sensors) {
|
if (readonly_sensors) {
|
||||||
@@ -1037,12 +1043,10 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
|
|||||||
if (device_type == EMSdevice::DeviceType::SYSTEM) {
|
if (device_type == EMSdevice::DeviceType::SYSTEM) {
|
||||||
doc["ent_cat"] = "diagnostic";
|
doc["ent_cat"] = "diagnostic";
|
||||||
}
|
}
|
||||||
|
|
||||||
add_ha_uom(doc.as<JsonObject>(), type, uom, entity); // add the UoM, device and state class
|
add_ha_uom(doc.as<JsonObject>(), type, uom, entity); // add the UoM, device and state class
|
||||||
}
|
}
|
||||||
|
|
||||||
doc["dev"] = dev_json; // add the dev json object to the end
|
doc["dev"] = dev_json;
|
||||||
add_ha_sections_to_doc(nullptr, stat_t, doc, false, val_cond); // no name, since the "dev" has already been added
|
|
||||||
|
|
||||||
return queue_ha(topic, doc.as<JsonObject>());
|
return queue_ha(topic, doc.as<JsonObject>());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user