mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 15:59:52 +03:00
support domoticz - MQTT autodiscovery in Domoticz not working #1360
This commit is contained in:
27
src/mqtt.cpp
27
src/mqtt.cpp
@@ -863,8 +863,8 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
|
|||||||
case DeviceValueType::USHORT:
|
case DeviceValueType::USHORT:
|
||||||
case DeviceValueType::ULONG:
|
case DeviceValueType::ULONG:
|
||||||
// number - https://www.home-assistant.io/integrations/number.mqtt
|
// number - https://www.home-assistant.io/integrations/number.mqtt
|
||||||
// Domoticz does not support number, use sensor
|
// older Domoticz does not support number, use sensor
|
||||||
if (discovery_type() == discoveryType::HOMEASSISTANT) {
|
if (discovery_type() == discoveryType::HOMEASSISTANT || discovery_type() == discoveryType::DOMOTICZ_LATEST) {
|
||||||
snprintf(topic, sizeof(topic), "number/%s", config_topic);
|
snprintf(topic, sizeof(topic), "number/%s", config_topic);
|
||||||
readonly_sensors = false;
|
readonly_sensors = false;
|
||||||
} else {
|
} else {
|
||||||
@@ -1296,39 +1296,44 @@ void Mqtt::add_ha_sections_to_doc(const std::string & name,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// adds "availability" section to HA Discovery config
|
// adds "availability" section to HA Discovery config
|
||||||
// but not for Domoticz
|
|
||||||
if (discovery_type() == discoveryType::HOMEASSISTANT) {
|
|
||||||
const char * tpl_draft = "{{'online' if %s else 'offline'}}";
|
|
||||||
char tpl[150];
|
|
||||||
JsonArray avty = config.createNestedArray("avty");
|
JsonArray avty = config.createNestedArray("avty");
|
||||||
|
StaticJsonDocument<EMSESP_JSON_SIZE_SMALL> avty_json;
|
||||||
|
|
||||||
StaticJsonDocument<512> avty_json;
|
const char * tpl_draft = "{{'online' if %s else 'offline'}}";
|
||||||
|
|
||||||
|
// EMS-ESP status check
|
||||||
|
char tpl[150];
|
||||||
snprintf(tpl, sizeof(tpl), "%s/status", Mqtt::base().c_str());
|
snprintf(tpl, sizeof(tpl), "%s/status", Mqtt::base().c_str());
|
||||||
avty_json["t"] = tpl;
|
avty_json["t"] = tpl;
|
||||||
snprintf(tpl, sizeof(tpl), tpl_draft, "value == 'online'");
|
snprintf(tpl, sizeof(tpl), tpl_draft, "value == 'online'");
|
||||||
avty_json["val_tpl"] = tpl;
|
avty_json["val_tpl"] = tpl;
|
||||||
avty.add(avty_json);
|
avty.add(avty_json); // returns 0 if no mem
|
||||||
|
|
||||||
|
// skip conditional Jinja2 templates if not home assistant
|
||||||
|
if (discovery_type() == discoveryType::HOMEASSISTANT) {
|
||||||
|
// condition 1
|
||||||
avty_json.clear();
|
avty_json.clear();
|
||||||
avty_json["t"] = state_t;
|
avty_json["t"] = state_t;
|
||||||
snprintf(tpl, sizeof(tpl), tpl_draft, cond1 == nullptr ? "value is defined" : cond1);
|
snprintf(tpl, sizeof(tpl), tpl_draft, cond1 == nullptr ? "value is defined" : cond1);
|
||||||
avty_json["val_tpl"] = tpl;
|
avty_json["val_tpl"] = tpl;
|
||||||
avty.add(avty_json);
|
avty.add(avty_json); // returns 0 if no mem
|
||||||
|
|
||||||
|
// condition 2
|
||||||
if (cond2 != nullptr) {
|
if (cond2 != nullptr) {
|
||||||
avty_json.clear();
|
avty_json.clear();
|
||||||
avty_json["t"] = state_t;
|
avty_json["t"] = state_t;
|
||||||
snprintf(tpl, sizeof(tpl), tpl_draft, cond2);
|
snprintf(tpl, sizeof(tpl), tpl_draft, cond2);
|
||||||
avty_json["val_tpl"] = tpl;
|
avty_json["val_tpl"] = tpl;
|
||||||
avty.add(avty_json);
|
avty.add(avty_json); // returns 0 if no mem
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// negative condition
|
||||||
if (negcond != nullptr) {
|
if (negcond != nullptr) {
|
||||||
avty_json.clear();
|
avty_json.clear();
|
||||||
avty_json["t"] = state_t;
|
avty_json["t"] = state_t;
|
||||||
snprintf(tpl, sizeof(tpl), "{{'offline' if %s else 'online'}}", negcond);
|
snprintf(tpl, sizeof(tpl), "{{'offline' if %s else 'online'}}", negcond);
|
||||||
avty_json["val_tpl"] = tpl;
|
avty_json["val_tpl"] = tpl;
|
||||||
avty.add(avty_json);
|
avty.add(avty_json); // returns 0 if no mem
|
||||||
}
|
}
|
||||||
|
|
||||||
config["avty_mode"] = "all";
|
config["avty_mode"] = "all";
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ using mqtt_sub_function_p = std::function<bool(const char * message)>;
|
|||||||
|
|
||||||
class Mqtt {
|
class Mqtt {
|
||||||
public:
|
public:
|
||||||
enum discoveryType : uint8_t { HOMEASSISTANT, DOMOTICZ };
|
enum discoveryType : uint8_t { HOMEASSISTANT, DOMOTICZ, DOMOTICZ_LATEST };
|
||||||
enum entityFormat : uint8_t { SINGLE_LONG, SINGLE_SHORT, MULTI_SHORT };
|
enum entityFormat : uint8_t { SINGLE_LONG, SINGLE_SHORT, MULTI_SHORT };
|
||||||
|
|
||||||
void loop();
|
void loop();
|
||||||
@@ -219,7 +219,7 @@ class Mqtt {
|
|||||||
|
|
||||||
static void add_ha_sections_to_doc(const std::string & name,
|
static void add_ha_sections_to_doc(const std::string & name,
|
||||||
const char * state_t,
|
const char * state_t,
|
||||||
const JsonObject & doc,
|
const JsonObject & config,
|
||||||
const bool is_first = false,
|
const bool is_first = false,
|
||||||
const char * cond1 = nullptr,
|
const char * cond1 = nullptr,
|
||||||
const char * cond2 = nullptr,
|
const char * cond2 = nullptr,
|
||||||
|
|||||||
@@ -397,7 +397,7 @@ void WebCustomEntityService::publish(const bool force) {
|
|||||||
snprintf(topic, sizeof(topic), "switch/%s/custom_%s/config", Mqtt::basename().c_str(), entityItem.name.c_str());
|
snprintf(topic, sizeof(topic), "switch/%s/custom_%s/config", Mqtt::basename().c_str(), entityItem.name.c_str());
|
||||||
} else if (entityItem.value_type == DeviceValueType::STRING) {
|
} else if (entityItem.value_type == DeviceValueType::STRING) {
|
||||||
snprintf(topic, sizeof(topic), "sensor/%s/custom_%s/config", Mqtt::basename().c_str(), entityItem.name.c_str());
|
snprintf(topic, sizeof(topic), "sensor/%s/custom_%s/config", Mqtt::basename().c_str(), entityItem.name.c_str());
|
||||||
} else if (Mqtt::discovery_type() == Mqtt::discoveryType::HOMEASSISTANT) {
|
} else if (Mqtt::discovery_type() == Mqtt::discoveryType::HOMEASSISTANT || Mqtt::discovery_type() == Mqtt::discoveryType::DOMOTICZ_LATEST) {
|
||||||
snprintf(topic, sizeof(topic), "number/%s/custom_%s/config", Mqtt::basename().c_str(), entityItem.name.c_str());
|
snprintf(topic, sizeof(topic), "number/%s/custom_%s/config", Mqtt::basename().c_str(), entityItem.name.c_str());
|
||||||
} else {
|
} else {
|
||||||
snprintf(topic, sizeof(topic), "sensor/%s/custom_%s/config", Mqtt::basename().c_str(), entityItem.name.c_str());
|
snprintf(topic, sizeof(topic), "sensor/%s/custom_%s/config", Mqtt::basename().c_str(), entityItem.name.c_str());
|
||||||
|
|||||||
Reference in New Issue
Block a user