This commit is contained in:
Proddy
2023-01-30 17:14:53 +01:00
parent 7c44f22c45
commit 77a6304fc3
4 changed files with 57 additions and 36 deletions

View File

@@ -615,15 +615,15 @@ void Mqtt::ha_status() {
strcpy(uniq, "system_status");
}
doc["uniq_id"] = uniq;
doc["object_id"] = uniq;
doc["uniq_id"] = uniq;
doc["obj_id"] = uniq;
doc["stat_t"] = mqtt_base_ + "/status";
doc["name"] = "EMS-ESP status";
doc["payload_on"] = "online";
doc["payload_off"] = "offline";
doc["state_class"] = "measurement";
doc["device_class"] = "connectivity";
doc["stat_t"] = mqtt_base_ + "/status";
doc["name"] = "EMS-ESP status";
doc["pl_on"] = "online";
doc["pl_off"] = "offline";
doc["stat_cla"] = "measurement";
doc["dev_cla"] = "connectivity";
// doc["avty_t"] = "~/status"; // commented out, as it causes errors in HA sometimes
// doc["json_attr_t"] = "~/heartbeat"; // store also as HA attributes
@@ -1070,11 +1070,11 @@ void Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
// build the payload
StaticJsonDocument<EMSESP_JSON_SIZE_LARGE> doc;
doc["uniq_id"] = uniq_id;
doc["object_id"] = uniq_id; // same as unique_id
doc["uniq_id"] = uniq_id;
doc["obj_id"] = uniq_id; // same as unique_id
const char * ic_ha = "ic"; // icon - only set this if there is no device class
const char * sc_ha = "state_class"; // state class
const char * sc_ha = "stat_cla"; // state class
const char * uom_ha = "unit_of_meas"; // unit of measure
// handle commands, which are device entities that are writable
@@ -1088,7 +1088,7 @@ void Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
} else {
snprintf(command_topic, sizeof(command_topic), "%s/%s/%s", mqtt_basename_.c_str(), device_name, entity);
}
doc["command_topic"] = command_topic;
doc["cmd_t"] = command_topic;
// for enums, add options
if (type == DeviceValueType::ENUM) {
@@ -1096,7 +1096,7 @@ void Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
for (uint8_t i = 0; i < options_size; i++) {
option_list.add(Helpers::translated_word(options[i]));
}
} else if (type != DeviceValueType::STRING) {
} else if (type != DeviceValueType::STRING && type != DeviceValueType::BOOL) {
// Must be Numeric....
doc["mode"] = "box"; // auto, slider or box
if (num_op > 0) {
@@ -1163,13 +1163,20 @@ void Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
// special case to handle booleans
// applies to both Binary Sensor (read only) and a Switch (for a command)
// always render boolean as strings true & false
// and has no unit of measure or icon
// has no unit of measure or icon
if (type == DeviceValueType::BOOL) {
char result[12];
doc["payload_on"] = Helpers::render_boolean(result, true);
doc["payload_off"] = Helpers::render_boolean(result, false);
doc[sc_ha] = F_(measurement);
if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) {
doc["pl_on"] = true;
doc["pl_off"] = 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);
}
// doc[sc_ha] = F_(measurement); // TODO do we want this???
} else {
// always set the uom, using the standards except for hours/minutes/seconds
// using HA specific codes from https://github.com/home-assistant/core/blob/dev/homeassistant/const.py
@@ -1180,8 +1187,7 @@ void Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
} else if (uom == DeviceValueUOM::SECONDS) {
doc[uom_ha] = "s";
} else if (uom != DeviceValueUOM::NONE) {
// default
doc[uom_ha] = EMSdevice::uom_to_string(uom);
doc[uom_ha] = EMSdevice::uom_to_string(uom); // default
}
}
@@ -1341,7 +1347,7 @@ void Mqtt::publish_ha_climate_config(const uint8_t tag, const bool has_roomtemp,
doc["~"] = mqtt_base_;
doc["uniq_id"] = uniq_id_s;
doc["object_id"] = uniq_id_s; // same as uniq_id
doc["obj_id"] = uniq_id_s; // same as uniq_id
doc["name"] = name_s;
doc["mode_stat_t"] = topic_t;
doc["mode_stat_tpl"] = mode_str_tpl;