publish state for hour format change
This commit is contained in:
parent
e8b8c42959
commit
3cc84ae4d2
@ -23,6 +23,10 @@ namespace Display {
|
|||||||
int currentHour = -1;
|
int currentHour = -1;
|
||||||
int currentMin = -1;
|
int currentMin = -1;
|
||||||
bool hourFormat24 = false;
|
bool hourFormat24 = false;
|
||||||
|
void (*hourFormatChangedCallback)(bool format24);
|
||||||
|
void onHourFormatChanged(void (*f)(bool)) {
|
||||||
|
hourFormatChangedCallback = f;
|
||||||
|
}
|
||||||
|
|
||||||
// Create display object
|
// Create display object
|
||||||
Adafruit_7segment clockDisplay = Adafruit_7segment();
|
Adafruit_7segment clockDisplay = Adafruit_7segment();
|
||||||
@ -121,6 +125,12 @@ namespace Display {
|
|||||||
tDisplay.enableDelayed(DISPLAY_TIME);
|
tDisplay.enableDelayed(DISPLAY_TIME);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void changeHourFormat24(bool format24) {
|
||||||
|
hourFormat24 = format24;
|
||||||
|
drawTime();
|
||||||
|
hourFormatChangedCallback(format24);
|
||||||
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
clockDisplay.begin(DISPLAY_ADDRESS);
|
clockDisplay.begin(DISPLAY_ADDRESS);
|
||||||
clockDisplay.setBrightness(Brightness::current);
|
clockDisplay.setBrightness(Brightness::current);
|
||||||
|
|||||||
@ -66,6 +66,12 @@ namespace Mqtt {
|
|||||||
Display::Brightness::set(String{ msg }.toInt());
|
Display::Brightness::set(String{ msg }.toInt());
|
||||||
}
|
}
|
||||||
})->withStateTopic();
|
})->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[] = {
|
Ha::Command* switches[] = {
|
||||||
new Ha::Button{"ESP Clock Restart", "restart",
|
new Ha::Button{"ESP Clock Restart", "restart",
|
||||||
@ -74,17 +80,7 @@ namespace Mqtt {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
ledMqtt,
|
ledMqtt,
|
||||||
new Ha::Switch{"ESP Clock Format 24h", "format_24h",
|
hourFormatMqtt,
|
||||||
[](const char* msg) {
|
|
||||||
if (String{ "ON" }.equals(msg)) {
|
|
||||||
Display::hourFormat24 = true;
|
|
||||||
Display::drawTime();
|
|
||||||
} else {
|
|
||||||
Display::hourFormat24 = false;
|
|
||||||
Display::drawTime();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
brightnessMqtt
|
brightnessMqtt
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -103,7 +99,7 @@ namespace Mqtt {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void publishBrightness() {
|
void publishBrightness() {
|
||||||
if (!client.connected()) return ;
|
if (!client.connected()) return;
|
||||||
char message[32];
|
char message[32];
|
||||||
sprintf(message, "%u", Display::Brightness::current);
|
sprintf(message, "%u", Display::Brightness::current);
|
||||||
publish(brightnessMqtt->stateTopic, message);
|
publish(brightnessMqtt->stateTopic, message);
|
||||||
@ -157,6 +153,9 @@ namespace Mqtt {
|
|||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Display::Brightness::onChanged(publishBrightness);
|
Display::Brightness::onChanged(publishBrightness);
|
||||||
|
Display::onHourFormatChanged([](bool format24) {
|
||||||
|
if (client.connected()) publish(hourFormatMqtt->stateTopic, format24 ? "ON" : "OFF");
|
||||||
|
});
|
||||||
client.onConnect([](bool sessionPresent) {
|
client.onConnect([](bool sessionPresent) {
|
||||||
tPublishConfig.enable();
|
tPublishConfig.enable();
|
||||||
tPublishBmp.enableIfNot();
|
tPublishBmp.enableIfNot();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user