publish state for hour format change

This commit is contained in:
Nicu Hodos 2023-07-11 17:38:22 +02:00
parent e8b8c42959
commit 3cc84ae4d2
2 changed files with 21 additions and 12 deletions

View File

@ -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);

View File

@ -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();