diff --git a/include/display.h b/include/display.h index a560284..8b660b8 100644 --- a/include/display.h +++ b/include/display.h @@ -23,6 +23,10 @@ namespace Display { int currentHour = -1; int currentMin = -1; bool hourFormat24 = false; + void (*hourFormatChangedCallback)(bool format24); + void onHourFormatChanged(void (*f)(bool)) { + hourFormatChangedCallback = f; + } // Create display object Adafruit_7segment clockDisplay = Adafruit_7segment(); @@ -121,6 +125,12 @@ namespace Display { tDisplay.enableDelayed(DISPLAY_TIME); } + void changeHourFormat24(bool format24) { + hourFormat24 = format24; + drawTime(); + hourFormatChangedCallback(format24); + } + void setup() { clockDisplay.begin(DISPLAY_ADDRESS); clockDisplay.setBrightness(Brightness::current); diff --git a/include/mqtt.h b/include/mqtt.h index 085d196..5d7d229 100644 --- a/include/mqtt.h +++ b/include/mqtt.h @@ -66,6 +66,12 @@ namespace Mqtt { Display::Brightness::set(String{ msg }.toInt()); } })->withStateTopic(); + + Ha::Command* hourFormatMqtt = (new Ha::Switch{"ESP Clock Format 24h", "format_24h", + [](const char* msg) { + String{ "ON" }.equals(msg) ? Display::changeHourFormat24(true) : Display::changeHourFormat24(false); + } + })->withStateTopic(); Ha::Command* switches[] = { new Ha::Button{"ESP Clock Restart", "restart", @@ -74,17 +80,7 @@ namespace Mqtt { } }, ledMqtt, - new Ha::Switch{"ESP Clock Format 24h", "format_24h", - [](const char* msg) { - if (String{ "ON" }.equals(msg)) { - Display::hourFormat24 = true; - Display::drawTime(); - } else { - Display::hourFormat24 = false; - Display::drawTime(); - } - } - }, + hourFormatMqtt, brightnessMqtt }; @@ -103,7 +99,7 @@ namespace Mqtt { } void publishBrightness() { - if (!client.connected()) return ; + if (!client.connected()) return; char message[32]; sprintf(message, "%u", Display::Brightness::current); publish(brightnessMqtt->stateTopic, message); @@ -157,6 +153,9 @@ namespace Mqtt { void setup() { Display::Brightness::onChanged(publishBrightness); + Display::onHourFormatChanged([](bool format24) { + if (client.connected()) publish(hourFormatMqtt->stateTopic, format24 ? "ON" : "OFF"); + }); client.onConnect([](bool sessionPresent) { tPublishConfig.enable(); tPublishBmp.enableIfNot();