Skip to content

Commit

Permalink
support HA writable text fields
Browse files Browse the repository at this point in the history
  • Loading branch information
proddy committed Oct 31, 2023
1 parent 9bf56b2 commit 89d5915
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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);
Expand Down Expand Up @@ -910,20 +916,18 @@ 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 {
snprintf(command_topic, sizeof(command_topic), "%s/%s/%s", mqtt_basename_.c_str(), device_name, entity);
}
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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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

0 comments on commit 89d5915

Please sign in to comment.