unify command components

This commit is contained in:
Nicu Hodos 2023-07-08 00:46:40 +02:00
parent 08ff0ba367
commit 7412029272
2 changed files with 7 additions and 25 deletions

View File

@ -64,30 +64,12 @@ namespace Ha {
} }
}; };
void buildRestartConfig(char(&output)[JSON_SIZE], const char* topic) { void buildCommandConfig(char(&output)[JSON_SIZE], const char* name, const char* uniqueId, const char* commandTopic) {
StaticJsonDocument<JSON_SIZE> jsonDoc; StaticJsonDocument<JSON_SIZE> jsonDoc;
buildDeviceConfig(jsonDoc); buildDeviceConfig(jsonDoc);
jsonDoc["name"] = "Restart"; jsonDoc["name"] = name;
jsonDoc["unique_id"] = "esp_clock_restart"; jsonDoc["unique_id"] = uniqueId;
jsonDoc["command_topic"] = topic; jsonDoc["command_topic"] = commandTopic;
serializeJson(jsonDoc, output);
}
void buildLedConfig(char(&output)[JSON_SIZE], const char* topic) {
StaticJsonDocument<JSON_SIZE> jsonDoc;
buildDeviceConfig(jsonDoc);
jsonDoc["name"] = "ESP Clock Led";
jsonDoc["unique_id"] = "esp_clock_led";
jsonDoc["command_topic"] = topic;
serializeJson(jsonDoc, output);
}
void buildHourFormatConfig(char(&output)[JSON_SIZE], const char* topic) {
StaticJsonDocument<JSON_SIZE> jsonDoc;
buildDeviceConfig(jsonDoc);
jsonDoc["name"] = "ESP Clock Format 24h";
jsonDoc["unique_id"] = "esp_clock_format_24h";
jsonDoc["command_topic"] = topic;
serializeJson(jsonDoc, output); serializeJson(jsonDoc, output);
} }
} }

View File

@ -69,19 +69,19 @@ namespace Mqtt {
void publishRestartConfig() { void publishRestartConfig() {
char message[JSON_SIZE]; char message[JSON_SIZE];
Ha::buildRestartConfig(message, restartTopic); Ha::buildCommandConfig(message, "Restart", "esp_clock_restart", restartTopic);
client.publish("homeassistant/button/esp_clock/restart/config", 0, true, message); client.publish("homeassistant/button/esp_clock/restart/config", 0, true, message);
} }
void publishLedConfig() { void publishLedConfig() {
char message[JSON_SIZE]; char message[JSON_SIZE];
Ha::buildLedConfig(message, ledTopic); Ha::buildCommandConfig(message, "ESP Clock Led", "esp_clock_led", ledTopic);
client.publish("homeassistant/switch/esp_clock/led/config", 0, true, message); client.publish("homeassistant/switch/esp_clock/led/config", 0, true, message);
} }
void publishHourFormatConfig() { void publishHourFormatConfig() {
char message[JSON_SIZE]; char message[JSON_SIZE];
Ha::buildHourFormatConfig(message, hourFormatTopic); Ha::buildCommandConfig(message, "ESP Clock Format 24h", "esp_clock_format_24h", hourFormatTopic);
client.publish("homeassistant/switch/esp_clock/hour_format/config", 0, true, message); client.publish("homeassistant/switch/esp_clock/hour_format/config", 0, true, message);
} }