From 7412029272f795b00b52df9e5841d5e33863bee9 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Sat, 8 Jul 2023 00:46:40 +0200 Subject: [PATCH] unify command components --- include/ha.h | 26 ++++---------------------- include/mqtt.h | 6 +++--- 2 files changed, 7 insertions(+), 25 deletions(-) diff --git a/include/ha.h b/include/ha.h index 6961230..d7f9ed6 100644 --- a/include/ha.h +++ b/include/ha.h @@ -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 jsonDoc; buildDeviceConfig(jsonDoc); - jsonDoc["name"] = "Restart"; - jsonDoc["unique_id"] = "esp_clock_restart"; - jsonDoc["command_topic"] = topic; - serializeJson(jsonDoc, output); - } - - void buildLedConfig(char(&output)[JSON_SIZE], const char* topic) { - StaticJsonDocument 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 jsonDoc; - buildDeviceConfig(jsonDoc); - jsonDoc["name"] = "ESP Clock Format 24h"; - jsonDoc["unique_id"] = "esp_clock_format_24h"; - jsonDoc["command_topic"] = topic; + jsonDoc["name"] = name; + jsonDoc["unique_id"] = uniqueId; + jsonDoc["command_topic"] = commandTopic; serializeJson(jsonDoc, output); } } diff --git a/include/mqtt.h b/include/mqtt.h index 0359bb7..c033754 100644 --- a/include/mqtt.h +++ b/include/mqtt.h @@ -69,19 +69,19 @@ namespace Mqtt { void publishRestartConfig() { 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); } void publishLedConfig() { 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); } void publishHourFormatConfig() { 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); }