Refactor Button Switch MQTT data

This commit is contained in:
Theo Arends 2020-11-03 16:24:49 +01:00
parent 7b68935ae3
commit 653adfa1cb
4 changed files with 10 additions and 10 deletions

View File

@ -732,6 +732,8 @@ const char S_JSON_SENSOR_INDEX_SVALUE[] PROGMEM = "{\"" D_CMND_SENSO
const char S_JSON_DRIVER_INDEX_NVALUE[] PROGMEM = "{\"" D_CMND_DRIVER "%d\":%d}"; const char S_JSON_DRIVER_INDEX_NVALUE[] PROGMEM = "{\"" D_CMND_DRIVER "%d\":%d}";
const char S_JSON_DRIVER_INDEX_SVALUE[] PROGMEM = "{\"" D_CMND_DRIVER "%d\":\"%s\"}"; const char S_JSON_DRIVER_INDEX_SVALUE[] PROGMEM = "{\"" D_CMND_DRIVER "%d\":\"%s\"}";
const char S_JSON_SVALUE_ACTION_SVALUE[] PROGMEM = "{\"%s\":{\"Action\":\"%s\"}}";
const char JSON_SNS_TEMP[] PROGMEM = ",\"%s\":{\"" D_JSON_TEMPERATURE "\":%s}"; const char JSON_SNS_TEMP[] PROGMEM = ",\"%s\":{\"" D_JSON_TEMPERATURE "\":%s}";
const char JSON_SNS_ILLUMINANCE[] PROGMEM = ",\"%s\":{\"" D_JSON_ILLUMINANCE "\":%d}"; const char JSON_SNS_ILLUMINANCE[] PROGMEM = ",\"%s\":{\"" D_JSON_ILLUMINANCE "\":%d}";

View File

@ -373,10 +373,10 @@ void MqttButtonTopic(uint32_t button_id, uint32_t action, uint32_t hold) {
SendKey(KEY_BUTTON, button_id, (hold) ? 3 : action +9); SendKey(KEY_BUTTON, button_id, (hold) ? 3 : action +9);
if (!Settings.flag.hass_discovery) { // SetOption19 - Control Home Assistant automatic discovery (See SetOption59) if (!Settings.flag.hass_discovery) { // SetOption19 - Control Home Assistant automatic discovery (See SetOption59)
char mqttstate[7];
Response_P(PSTR("{\"" D_JSON_BUTTON "%d\":{\"Action\":\"%s\"}}"), button_id, (hold) ? SettingsText(SET_STATE_TXT4) : GetTextIndexed(mqttstate, sizeof(mqttstate), action, kMultiPress));
char scommand[10]; char scommand[10];
snprintf_P(scommand, sizeof(scommand), PSTR("BUTTON%d"), button_id); snprintf_P(scommand, sizeof(scommand), PSTR(D_JSON_BUTTON "%d"), button_id);
char mqttstate[7];
Response_P(S_JSON_SVALUE_ACTION_SVALUE, scommand, (hold) ? SettingsText(SET_STATE_TXT4) : GetTextIndexed(mqttstate, sizeof(mqttstate), action, kMultiPress));
MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_STAT, scommand); MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_STAT, scommand);
} }
} }

View File

@ -426,9 +426,9 @@ void SwitchHandler(uint32_t mode) {
} else { } else {
GetTextIndexed(mqtt_state_str, sizeof(mqtt_state_str), mqtt_action, kSwitchPressStates); GetTextIndexed(mqtt_state_str, sizeof(mqtt_state_str), mqtt_action, kSwitchPressStates);
} }
Response_P(PSTR("{\"%s\":{\"Action\":\"%s\"}}"), GetSwitchText(i).c_str(), mqtt_state); Response_P(S_JSON_SVALUE_ACTION_SVALUE, GetSwitchText(i).c_str(), mqtt_state);
char scommand[10]; char scommand[10];
snprintf_P(scommand, sizeof(scommand), PSTR("SWITCH%d"), i +1); snprintf_P(scommand, sizeof(scommand), PSTR(D_JSON_SWITCH "%d"), i +1);
MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_STAT, scommand); MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_STAT, scommand);
} }
mqtt_action = POWER_NONE; mqtt_action = POWER_NONE;

View File

@ -352,13 +352,11 @@ void MqttPublishPrefixTopic_P(uint32_t prefix, const char* subtopic, bool retain
* prefix 6 = tele using subtopic or RESULT * prefix 6 = tele using subtopic or RESULT
*/ */
char romram[64]; char romram[64];
char stopic[TOPSZ];
snprintf_P(romram, sizeof(romram), ((prefix > 3) && !Settings.flag.mqtt_response) ? S_RSLT_RESULT : subtopic); // SetOption4 - Switch between MQTT RESULT or COMMAND snprintf_P(romram, sizeof(romram), ((prefix > 3) && !Settings.flag.mqtt_response) ? S_RSLT_RESULT : subtopic); // SetOption4 - Switch between MQTT RESULT or COMMAND
for (uint32_t i = 0; i < strlen(romram); i++) { UpperCase(romram, romram);
romram[i] = toupper(romram[i]);
}
prefix &= 3; prefix &= 3;
char stopic[TOPSZ];
GetTopic_P(stopic, prefix, TasmotaGlobal.mqtt_topic, romram); GetTopic_P(stopic, prefix, TasmotaGlobal.mqtt_topic, romram);
MqttPublish(stopic, retained); MqttPublish(stopic, retained);