Version 1.23

Added value template to config message.
This commit is contained in:
Alexey Zholtikov 2023-01-15 11:58:40 +03:00
parent 8c3b51304e
commit 74c6279f50

View File

@ -34,7 +34,7 @@ void setupWebServer(void);
void connectToMqtt(void); void connectToMqtt(void);
const String firmware{"1.22"}; const String firmware{"1.23"};
String espnowNetName{"DEFAULT"}; String espnowNetName{"DEFAULT"};
@ -184,7 +184,7 @@ void onEspnowMessage(const char *data, const uint8_t *sender)
jsonConfig["unique_id"] = myNet.macToString(sender) + "-" + unit; jsonConfig["unique_id"] = myNet.macToString(sender) + "-" + unit;
jsonConfig["device_class"] = getValueName(json["class"].as<ha_switch_device_class_t>()); jsonConfig["device_class"] = getValueName(json["class"].as<ha_switch_device_class_t>());
jsonConfig["state_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/state"; jsonConfig["state_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/state";
jsonConfig["value_template"] = "{{ value_json.state }}"; jsonConfig["value_template"] = "{{ value_json." + json["template"].as<String>() + " }}";
jsonConfig["command_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/set"; jsonConfig["command_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/set";
jsonConfig["json_attributes_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/attributes"; jsonConfig["json_attributes_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/attributes";
jsonConfig["availability_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/status"; jsonConfig["availability_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/status";
@ -252,19 +252,16 @@ void onEspnowMessage(const char *data, const uint8_t *sender)
jsonConfig["name"] = json["name"]; jsonConfig["name"] = json["name"];
jsonConfig["unique_id"] = myNet.macToString(sender) + "-" + unit; jsonConfig["unique_id"] = myNet.macToString(sender) + "-" + unit;
jsonConfig["state_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/state"; jsonConfig["state_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/state";
jsonConfig["value_template"] = "{{ value_json.state }}"; jsonConfig["value_template"] = "{{ value_json." + json["template"].as<String>() + " }}";
jsonConfig["json_attributes_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/attributes"; jsonConfig["json_attributes_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/attributes";
jsonConfig["force_update"] = "true"; jsonConfig["force_update"] = "true";
jsonConfig["qos"] = 2; jsonConfig["qos"] = 2;
jsonConfig["retain"] = "true"; jsonConfig["retain"] = "true";
if (type == HACT_BINARY_SENSOR) if (type == HACT_BINARY_SENSOR)
{ {
ha_binary_sensor_device_class_t deviceClass = json["class"].as<ha_binary_sensor_device_class_t>(); jsonConfig["device_class"] = getValueName(json["class"].as<ha_binary_sensor_device_class_t>());
jsonConfig["device_class"] = getValueName(deviceClass);
jsonConfig["payload_on"] = json["payload_on"]; jsonConfig["payload_on"] = json["payload_on"];
jsonConfig["payload_off"] = json["payload_off"]; jsonConfig["payload_off"] = json["payload_off"];
if (deviceClass == HABSDC_BATTERY)
jsonConfig["value_template"] = "{{ value_json.battery }}";
} }
char buffer[2048]{0}; char buffer[2048]{0};
serializeJsonPretty(jsonConfig, buffer); serializeJsonPretty(jsonConfig, buffer);