improvements to #1382

This commit is contained in:
Proddy
2023-11-04 13:13:52 +01:00
parent cd564f0c54
commit a449ebd0ea

View File

@@ -847,7 +847,7 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
char config_topic[70]; char config_topic[70];
snprintf(config_topic, sizeof(config_topic), "%s/%s_%s/config", mqtt_basename_.c_str(), device_name, entity_with_tag); snprintf(config_topic, sizeof(config_topic), "%s/%s_%s/config", mqtt_basename_.c_str(), device_name, entity_with_tag);
bool set_ha_classes = false; // set to true if we want to set the state class and device class bool readonly_sensors = true;
// create the topic // create the topic
// depending on the type and whether the device entity is writable (a command) // depending on the type and whether the device entity is writable (a command)
@@ -860,43 +860,41 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
case DeviceValueType::UINT: case DeviceValueType::UINT:
case DeviceValueType::SHORT: case DeviceValueType::SHORT:
case DeviceValueType::USHORT: case DeviceValueType::USHORT:
case DeviceValueType::ULONG:
// number - https://www.home-assistant.io/integrations/number.mqtt
// Domoticz does not support number, use sensor
if (discovery_type() == discoveryType::HOMEASSISTANT) { if (discovery_type() == discoveryType::HOMEASSISTANT) {
// number - https://www.home-assistant.io/integrations/number.mqtt
snprintf(topic, sizeof(topic), "number/%s", config_topic); snprintf(topic, sizeof(topic), "number/%s", config_topic);
readonly_sensors = false;
} else { } else {
// Domoticz does not support number, use sensor
snprintf(topic, sizeof(topic), "sensor/%s", config_topic); snprintf(topic, sizeof(topic), "sensor/%s", config_topic);
} }
break; break;
case DeviceValueType::BOOL: case DeviceValueType::BOOL:
// switch - https://www.home-assistant.io/integrations/switch.mqtt // switch - https://www.home-assistant.io/integrations/switch.mqtt
snprintf(topic, sizeof(topic), "switch/%s", config_topic); snprintf(topic, sizeof(topic), "switch/%s", config_topic);
readonly_sensors = false;
break; break;
case DeviceValueType::ENUM: case DeviceValueType::ENUM:
// 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);
break; readonly_sensors = false;
case DeviceValueType::ULONG:
snprintf(topic, sizeof(topic), "sensor/%s", config_topic);
set_ha_classes = true;
break; break;
case DeviceValueType::STRING: case DeviceValueType::STRING:
// text - https://www.home-assistant.io/integrations/text.mqtt
snprintf(topic, sizeof(topic), "text/%s", config_topic); // e.g. set_datetime, set_holiday, set_wwswitchtime snprintf(topic, sizeof(topic), "text/%s", config_topic); // e.g. set_datetime, set_holiday, set_wwswitchtime
set_ha_classes = true; readonly_sensors = false;
break; break;
default: default:
// plain old sensor // plain old sensor, and make read-only
snprintf(topic, sizeof(topic), "sensor/%s", config_topic);
break; break;
} }
} else { }
set_ha_classes = true; // these are Read only sensors. We can set the device class and state class
// plain old read only device entity // For read-only sensors there are either sensor or binary_sensor
if (type == DeviceValueType::BOOL) { // for both we also set the device class and state class
snprintf(topic, sizeof(topic), "binary_sensor/%s", config_topic); // binary sensor (for booleans) if (readonly_sensors) {
} else { snprintf(topic, sizeof(topic), (type == DeviceValueType::BOOL) ? "binary_sensor/%s" : "sensor/%s", config_topic); // binary sensor (for booleans)
snprintf(topic, sizeof(topic), "sensor/%s", config_topic); // normal HA sensor
}
} }
// if we're asking to remove this topic, send an empty payload and exit // if we're asking to remove this topic, send an empty payload and exit
@@ -912,13 +910,15 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
doc["obj_id"] = uniq_id; // same as unique_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 * ic_ha = "ic"; // icon - only set this if there is no device class
const char * sc_ha = "stat_cla"; // state class
const char * uom_ha = "unit_of_meas"; // unit of measure const char * uom_ha = "unit_of_meas"; // unit of measure
char sample_val[30] = "0"; // sample, correct(!) entity value, used only to prevent warning/error in HA if real value is not published yet char sample_val[30] = "0"; // sample, correct(!) entity value, used only to prevent warning/error in HA if real value is not published yet
// we add the command topic parameter for commands // we add the command topic parameter for commands
if (has_cmd) { if (has_cmd) {
// add category
doc["ent_cat"] = "config"; // for writeable entities, like switch, number, text, select
char command_topic[MQTT_TOPIC_MAX_SIZE]; char command_topic[MQTT_TOPIC_MAX_SIZE];
// add command topic // add command topic
if (tag >= DeviceValueTAG::TAG_HC1) { if (tag >= DeviceValueTAG::TAG_HC1) {
@@ -1040,12 +1040,16 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
doc[uom_ha] = EMSdevice::uom_to_string(uom); // default doc[uom_ha] = EMSdevice::uom_to_string(uom); // default
} }
} }
doc["val_tpl"] = (std::string) "{{" + val_obj + " if " + val_cond + " else " + sample_val + "}}"; doc["val_tpl"] = (std::string) "{{" + val_obj + " if " + val_cond + " else " + sample_val + "}}";
// this next section is adding the state class, device class and sometimes the icon // Add the state class, device class and sometimes the icon. Used only for read-only sensors Sensor and Binary Sensor
// used for Sensor and Binary Sensor Entities in HA if (readonly_sensors) {
if (set_ha_classes) { // first set the catagory
const char * dc_ha = "dev_cla"; // device class doc["ent_cat"] = "diagnostic";
const char * dc_ha = "dev_cla"; // device class
const char * sc_ha = "stat_cla"; // state class
switch (uom) { switch (uom) {
case DeviceValueUOM::DEGREES: case DeviceValueUOM::DEGREES:
@@ -1129,16 +1133,8 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
} }
} }
// add category doc["dev"] = dev_json; // add the dev json object to the end
// "config" for writeable entities, like switch, number, text, select add_avty_to_doc(stat_t, doc.as<JsonObject>(), val_cond); // add "availability" section
// "diagnostic" for read only sensors
doc["ent_cat"] = (has_cmd) ? "config" : "diagnostic";
// add the dev json object to the end
doc["dev"] = dev_json;
// add "availability" section
add_avty_to_doc(stat_t, doc.as<JsonObject>(), val_cond);
return queue_ha(topic, doc.as<JsonObject>()); return queue_ha(topic, doc.as<JsonObject>());
} }