mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 15:59:52 +03:00
support HA writable text fields
This commit is contained in:
25
src/mqtt.cpp
25
src/mqtt.cpp
@@ -472,6 +472,7 @@ void Mqtt::on_disconnect(espMqttClientTypes::DisconnectReason reason) {
|
||||
} else {
|
||||
LOG_WARNING("MQTT disconnected: code %d", reason);
|
||||
}
|
||||
mqttClient_->clearQueue(true);
|
||||
}
|
||||
|
||||
// MQTT on_connect - when an MQTT connect is established
|
||||
@@ -589,7 +590,7 @@ bool Mqtt::queue_message(const uint8_t operation, const std::string & topic, con
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (!mqtt_enabled_ || topic.empty()) {
|
||||
if (!mqtt_enabled_ || topic.empty() || !connected()) {
|
||||
return false; // quit, not using MQTT
|
||||
}
|
||||
// check free mem
|
||||
@@ -847,10 +848,11 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
|
||||
|
||||
bool set_ha_classes = false; // set to true if we want to set the state class and device class
|
||||
|
||||
// create the topic, depending on the type and whether the device entity is writable (a command)
|
||||
// create the topic
|
||||
// depending on the type and whether the device entity is writable (a command)
|
||||
// https://developers.home-assistant.io/docs/core/entity
|
||||
char topic[MQTT_TOPIC_MAX_SIZE];
|
||||
// if it's a command then we can use Number, Switch, Select. Otherwise stick to Sensor
|
||||
// if it's a command then we can use Number, Switch, Select or Text. Otherwise stick to Sensor
|
||||
if (has_cmd) {
|
||||
switch (type) {
|
||||
case DeviceValueType::INT:
|
||||
@@ -877,6 +879,10 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
|
||||
snprintf(topic, sizeof(topic), "sensor/%s", config_topic);
|
||||
set_ha_classes = true;
|
||||
break;
|
||||
case DeviceValueType::STRING:
|
||||
snprintf(topic, sizeof(topic), "text/%s", config_topic); // e.g. set_datetime, set_holiday, set_wwswitchtime
|
||||
set_ha_classes = true;
|
||||
break;
|
||||
default:
|
||||
// plain old sensor
|
||||
snprintf(topic, sizeof(topic), "sensor/%s", config_topic);
|
||||
@@ -910,12 +916,10 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
|
||||
|
||||
char sample_val[30] = "0"; // sample, correct(!) entity value, used only to prevent warning/error in HA if real value is not published yet
|
||||
|
||||
// handle commands, which are device entities that are writable
|
||||
// we add the command topic parameter
|
||||
// note: there is no way to handle strings in HA so datetimes (e.g. set_datetime, set_holiday, set_wwswitchtime etc) are excluded
|
||||
// we add the command topic parameter for commands
|
||||
if (has_cmd) {
|
||||
// command topic back to EMS-ESP
|
||||
char command_topic[MQTT_TOPIC_MAX_SIZE];
|
||||
// add command topic
|
||||
if (tag >= DeviceValueTAG::TAG_HC1) {
|
||||
snprintf(command_topic, sizeof(command_topic), "%s/%s/%s/%s", mqtt_basename_.c_str(), device_name, EMSdevice::tag_to_mqtt(tag), entity);
|
||||
} else {
|
||||
@@ -923,7 +927,7 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
|
||||
}
|
||||
doc["cmd_t"] = command_topic;
|
||||
|
||||
// for enums, add options
|
||||
// extend for enums, add options
|
||||
if (type == DeviceValueType::ENUM) {
|
||||
JsonArray option_list = doc.createNestedArray("ops"); // options
|
||||
if (EMSESP::system_.enum_format() == ENUM_FORMAT_INDEX) {
|
||||
@@ -941,7 +945,7 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
|
||||
}
|
||||
|
||||
} else if (type != DeviceValueType::STRING && type != DeviceValueType::BOOL) {
|
||||
// Must be Numeric....
|
||||
// For numeric's add the range
|
||||
doc["mode"] = "box"; // auto, slider or box
|
||||
if (num_op > 0) {
|
||||
doc["step"] = 1.0 / num_op;
|
||||
@@ -1306,5 +1310,4 @@ void Mqtt::add_avty_to_doc(const char * state_t, const JsonObject & doc, const c
|
||||
doc["avty_mode"] = "all";
|
||||
}
|
||||
|
||||
|
||||
} // namespace emsesp
|
||||
} // namespace emsesp
|
||||
Reference in New Issue
Block a user