encapsulate pushlish state into HA Comand

This commit is contained in:
Nicu Hodos 2023-07-12 19:39:38 +02:00
parent f1d41ade51
commit 5ea1d96ba4
4 changed files with 34 additions and 35 deletions

View File

@ -23,8 +23,8 @@ 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 (*hourFormatChangedCallback)();
void onHourFormatChanged(void (*f)(bool)) { void onHourFormatChanged(void (*f)()) {
hourFormatChangedCallback = f; hourFormatChangedCallback = f;
} }
@ -128,7 +128,7 @@ namespace Display {
void changeHourFormat24(bool format24) { void changeHourFormat24(bool format24) {
hourFormat24 = format24; hourFormat24 = format24;
drawTime(); drawTime();
hourFormatChangedCallback(format24); hourFormatChangedCallback();
} }
void setup() { void setup() {

View File

@ -47,9 +47,11 @@ namespace Ha {
char commandTopic[TOPIC_SIZE]; char commandTopic[TOPIC_SIZE];
char stateTopic[TOPIC_SIZE]; char stateTopic[TOPIC_SIZE];
onMessage f; onMessage f;
void (*publishState)();
Command(const char* name, const char* id, const char* type, onMessage f) : Component(name, id, type) { Command(const char* name, const char* id, const char* type, onMessage f, void (*publishState)() = nullptr) : Component(name, id, type) {
this->f = f; this->f = f;
this->publishState = publishState;
} }
void buildUniqueId(char* uniqueId) override { void buildUniqueId(char* uniqueId) override {
@ -81,7 +83,7 @@ namespace Ha {
struct Switch : Command { struct Switch : Command {
static constexpr const char* type = "switch"; static constexpr const char* type = "switch";
Switch(const char* name, const char* id, onMessage f) : Command(name, id, type, f) { Switch(const char* name, const char* id, onMessage f, void (*publishState)()) : Command(name, id, type, f, publishState) {
sprintf(commandTopic, "homeassistant/%s/esp_clock/%s/set", type, id); sprintf(commandTopic, "homeassistant/%s/esp_clock/%s/set", type, id);
} }
@ -90,7 +92,7 @@ namespace Ha {
struct Brightness : Command { struct Brightness : Command {
static constexpr const char* type = "number"; static constexpr const char* type = "number";
Brightness(const char* name, const char* id, onMessage f) : Command(name, id, type, f) { Brightness(const char* name, const char* id, onMessage f, void (*publishState)()) : Command(name, id, type, f, publishState) {
sprintf(commandTopic, "homeassistant/%s/esp_clock/%s", type, id); sprintf(commandTopic, "homeassistant/%s/esp_clock/%s", type, id);
} }

View File

@ -12,13 +12,13 @@
namespace Mqtt { namespace Mqtt {
void publishConfig(); void publishInit();
void publishCommand(); void publishCommand();
void publishBmp280(); void publishBmp280();
void connect(); void connect();
void disconnect(); void disconnect();
Task tReConnect(5 * TASK_MINUTE, TASK_FOREVER, connect, &ts); Task tReConnect(5 * TASK_MINUTE, TASK_FOREVER, connect, &ts);
Task tPublishConfig(TASK_IMMEDIATE, TASK_ONCE, publishConfig, &ts); Task tPublishInit(TASK_IMMEDIATE, TASK_ONCE, publishInit, &ts);
Task tPublishBmp(5 * TASK_MINUTE, TASK_FOREVER, publishBmp280, &ts); Task tPublishBmp(5 * TASK_MINUTE, TASK_FOREVER, publishBmp280, &ts);
Task tPublishCommand(TASK_SECOND, TASK_FOREVER, publishCommand, &ts); Task tPublishCommand(TASK_SECOND, TASK_FOREVER, publishCommand, &ts);
@ -49,6 +49,10 @@ namespace Mqtt {
client.disconnect(); client.disconnect();
} }
uint16_t publish(const char* topic, const char* message) {
return client.publish(topic, 0, true, message);
}
Ha::Sensor* sensors[] = { Ha::Sensor* sensors[] = {
new Ha::TemperatureSensor{"Living room Temperature", "temperature"}, new Ha::TemperatureSensor{"Living room Temperature", "temperature"},
new Ha::PressureSensor{"Living room Pressure", "pressure"}, new Ha::PressureSensor{"Living room Pressure", "pressure"},
@ -58,18 +62,29 @@ namespace Mqtt {
Ha::Command* ledMqtt = (new Ha::Switch{"ESP Clock Led", "led", Ha::Command* ledMqtt = (new Ha::Switch{"ESP Clock Led", "led",
[](const char* msg) { [](const char* msg) {
String{ "ON" }.equals(msg) ? digitalWrite(LED_BUILTIN, LOW) : digitalWrite(LED_BUILTIN, HIGH); String{ "ON" }.equals(msg) ? digitalWrite(LED_BUILTIN, LOW) : digitalWrite(LED_BUILTIN, HIGH);
},
[]() {
publish(ledMqtt->stateTopic, digitalRead(LED_BUILTIN) ? "OFF" : "ON");
} }
})->withStateTopic(); })->withStateTopic();
Ha::Command* brightnessMqtt = (new Ha::Brightness{ "ESP Clock Brightness", "brightness", Ha::Command* brightnessMqtt = (new Ha::Brightness{ "ESP Clock Brightness", "brightness",
[](const char* msg) { [](const char* msg) {
Display::Brightness::set(String{ msg }.toInt()); Display::Brightness::set(String{ msg }.toInt());
},
[]() {
char message[32];
sprintf(message, "%u", Display::Brightness::current);
publish(brightnessMqtt->stateTopic, message);
} }
})->withStateTopic(); })->withStateTopic();
Ha::Command* hourFormatMqtt = (new Ha::Switch{"ESP Clock Format 24h", "format_24h", Ha::Command* hourFormatMqtt = (new Ha::Switch{"ESP Clock Format 24h", "format_24h",
[](const char* msg) { [](const char* msg) {
String{ "ON" }.equals(msg) ? Display::changeHourFormat24(true) : Display::changeHourFormat24(false); String{ "ON" }.equals(msg) ? Display::changeHourFormat24(true) : Display::changeHourFormat24(false);
},
[]() {
publish(hourFormatMqtt->stateTopic, Display::hourFormat24 ? "ON" : "OFF");
} }
})->withStateTopic(); })->withStateTopic();
@ -84,10 +99,6 @@ namespace Mqtt {
brightnessMqtt brightnessMqtt
}; };
uint16_t publish(const char* topic, const char* message) {
return client.publish(topic, 0, true, message);
}
void publishComponentConfig(Ha::Component& component) { void publishComponentConfig(Ha::Component& component) {
StaticJsonDocument<JSON_SIZE> jsonDoc; StaticJsonDocument<JSON_SIZE> jsonDoc;
component.buildConfig(jsonDoc); component.buildConfig(jsonDoc);
@ -98,31 +109,17 @@ namespace Mqtt {
publish(component.configTopic, message); publish(component.configTopic, message);
} }
void publishBrightness() { void publishInit() {
if (!client.connected()) return;
char message[32];
sprintf(message, "%u", Display::Brightness::current);
publish(brightnessMqtt->stateTopic, message);
}
void publishHourFormat(bool format24) {
if (client.connected()) publish(hourFormatMqtt->stateTopic, format24 ? "ON" : "OFF");
}
void publishLedState() {
Mqtt::publish(Mqtt::ledMqtt->stateTopic, digitalRead(LED_BUILTIN) ? "OFF" : "ON");
}
void publishConfig() {
for (Ha::Component* cmp : sensors) { for (Ha::Component* cmp : sensors) {
publishComponentConfig(*cmp); publishComponentConfig(*cmp);
} }
for (Ha::Component* cmp : switches) { for (Ha::Component* cmp : switches) {
publishComponentConfig(*cmp); publishComponentConfig(*cmp);
} }
publishBrightness(); brightnessMqtt->publishState();
publishHourFormat(Display::hourFormat24); hourFormatMqtt->publishState();
ts.deleteTask(tPublishConfig); ledMqtt->publishState();
ts.deleteTask(tPublishInit);
} }
void publishBmp280() { void publishBmp280() {
@ -161,10 +158,10 @@ namespace Mqtt {
} }
void setup() { void setup() {
Display::Brightness::onChanged(publishBrightness); Display::Brightness::onChanged(brightnessMqtt->publishState);
Display::onHourFormatChanged(publishHourFormat); Display::onHourFormatChanged(hourFormatMqtt->publishState);
client.onConnect([](bool sessionPresent) { client.onConnect([](bool sessionPresent) {
tPublishConfig.enable(); tPublishInit.enable();
tPublishBmp.enableIfNot(); tPublishBmp.enableIfNot();
tPublishCommand.enableDelayed(); tPublishCommand.enableDelayed();
client.subscribe(espClockTopic, 0); client.subscribe(espClockTopic, 0);

View File

@ -25,7 +25,7 @@ Task tButton(TASK_IMMEDIATE, TASK_ONCE, []() {
Display::displayTemp(Bmp::data.readTemp()); Display::displayTemp(Bmp::data.readTemp());
}, &ts); }, &ts);
Task tLed(TASK_IMMEDIATE, TASK_ONCE, []() { Task tLed(TASK_IMMEDIATE, TASK_ONCE, []() {
Mqtt::publishLedState(); Mqtt::ledMqtt->publishState();
}, &ts); }, &ts);
void setup() { void setup() {