From 294eef45c6eb5242f8737b93c054f5ed4e70e149 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Sat, 8 Jul 2023 11:20:39 +0200 Subject: [PATCH] generalize components configuration --- include/ha.h | 4 +++- include/mqtt.h | 60 ++++++++++++++++++++++++++++---------------------- 2 files changed, 37 insertions(+), 27 deletions(-) diff --git a/include/ha.h b/include/ha.h index 3b46d0d..4490d96 100644 --- a/include/ha.h +++ b/include/ha.h @@ -34,9 +34,11 @@ namespace Ha { struct Command : Component { const char* commandTopic; + void (*f)(const char* msg); - Command(const char* name, const char* uniqueId, const char* configTopic, const char* commandTopic) : Component(name, uniqueId, configTopic) { + Command(const char* name, const char* uniqueId, const char* configTopic, const char* commandTopic, void (*f)(const char* msg)) : Component(name, uniqueId, configTopic) { this->commandTopic = commandTopic; + this->f = f; } void buildConfig(JsonDocument& jsonDoc) { diff --git a/include/mqtt.h b/include/mqtt.h index 6ef9cd6..d151276 100644 --- a/include/mqtt.h +++ b/include/mqtt.h @@ -35,9 +35,6 @@ namespace Mqtt { } commands; const char* bmpTopic = "homeassistant/sensor/esp_clock/state"; - const char* restartTopic = "homeassistant/button/esp_clock/restart"; - const char* ledTopic = "homeassistant/switch/esp_clock/led/set"; - const char* hourFormatTopic = "homeassistant/switch/esp_clock/hour_format/set"; const char* espClockTopic = "homeassistant/+/esp_clock/#"; void connect() { @@ -49,13 +46,34 @@ namespace Mqtt { client.disconnect(); } - Ha::Component components[] = { + Ha::Sensor sensors[] = { Ha::TemperatureSensor{"Living room Temperature", "living_room_temperature", "homeassistant/sensor/esp_clock/temperature/config", bmpTopic}, Ha::PressureSensor{"Living room Pressure", "living_room_pressure", "homeassistant/sensor/esp_clock/pressure/config", bmpTopic}, - Ha::AltitudeSensor{"Living room Altitude", "living_room_altitude", "homeassistant/sensor/esp_clock/altitude/config", bmpTopic}, - Ha::Command{"Restart", "esp_clock_restart", "homeassistant/button/esp_clock/restart/config", restartTopic}, - Ha::Command{"ESP Clock Led", "esp_clock_led", "homeassistant/switch/esp_clock/led/config", ledTopic}, - Ha::Command{"ESP Clock Format 24h", "esp_clock_format_24h", "homeassistant/switch/esp_clock/hour_format/config", hourFormatTopic} + Ha::AltitudeSensor{"Living room Altitude", "living_room_altitude", "homeassistant/sensor/esp_clock/altitude/config", bmpTopic} + }; + + Ha::Command switches[] = { + Ha::Command{"Restart", "esp_clock_restart", "homeassistant/button/esp_clock/restart/config", "homeassistant/button/esp_clock/restart", + [](const char* msg) { + if (String { "PRESS" }.equals(msg)) ESP.restart(); + } + }, + Ha::Command{"ESP Clock Led", "esp_clock_led", "homeassistant/switch/esp_clock/led/config", "homeassistant/switch/esp_clock/led/set", + [](const char* msg) { + String{ "ON" }.equals(msg) ? digitalWrite(LED_BUILTIN, LOW) : digitalWrite(LED_BUILTIN, HIGH); + } + }, + Ha::Command{"ESP Clock Format 24h", "esp_clock_format_24h", "homeassistant/switch/esp_clock/hour_format/config", "homeassistant/switch/esp_clock/hour_format/set", + [](const char* msg) { + if (String{ "ON" }.equals(msg)) { + Display::hourFormat24 = true; + Display::drawTime(); + } else { + Display::hourFormat24 = false; + Display::drawTime(); + } + } + } }; void publishComponentConfig(Ha::Component& component) { @@ -69,7 +87,10 @@ namespace Mqtt { } void publishConfig() { - for (Ha::Component cmp : components) { + for (Ha::Component cmp : sensors) { + publishComponentConfig(cmp); + } + for (Ha::Component cmp : switches) { publishComponentConfig(cmp); } ts.deleteTask(tPublishConfig); @@ -102,23 +123,10 @@ namespace Mqtt { char msg[len + 1]; memcpy(msg, payload, len); msg[len] = 0; - if (String{ restartTopic }.equals(topic) && String { "PRESS" }.equals(msg)) { - ESP.restart(); - } - if (String{ ledTopic }.equals(topic)) { - if (String{ "ON" }.equals(msg)) { - digitalWrite(LED_BUILTIN, LOW); - } else { - digitalWrite(LED_BUILTIN, HIGH); - } - } - if (String{ hourFormatTopic }.equals(topic)) { - if (String{ "ON" }.equals(msg)) { - Display::hourFormat24 = true; - Display::drawTime(); - } else { - Display::hourFormat24 = false; - Display::drawTime(); + for (Ha::Command cmd : switches) { + if (String{ cmd.commandTopic }.equals(topic)) { + cmd.f(msg); + return; } } }