encapsulate pushlish state into HA Comand
This commit is contained in:
parent
f1d41ade51
commit
5ea1d96ba4
@ -23,8 +23,8 @@ namespace Display {
|
||||
int currentHour = -1;
|
||||
int currentMin = -1;
|
||||
bool hourFormat24 = false;
|
||||
void (*hourFormatChangedCallback)(bool format24);
|
||||
void onHourFormatChanged(void (*f)(bool)) {
|
||||
void (*hourFormatChangedCallback)();
|
||||
void onHourFormatChanged(void (*f)()) {
|
||||
hourFormatChangedCallback = f;
|
||||
}
|
||||
|
||||
@ -128,7 +128,7 @@ namespace Display {
|
||||
void changeHourFormat24(bool format24) {
|
||||
hourFormat24 = format24;
|
||||
drawTime();
|
||||
hourFormatChangedCallback(format24);
|
||||
hourFormatChangedCallback();
|
||||
}
|
||||
|
||||
void setup() {
|
||||
|
||||
@ -47,9 +47,11 @@ namespace Ha {
|
||||
char commandTopic[TOPIC_SIZE];
|
||||
char stateTopic[TOPIC_SIZE];
|
||||
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->publishState = publishState;
|
||||
}
|
||||
|
||||
void buildUniqueId(char* uniqueId) override {
|
||||
@ -81,7 +83,7 @@ namespace Ha {
|
||||
struct Switch : Command {
|
||||
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);
|
||||
}
|
||||
|
||||
@ -90,7 +92,7 @@ namespace Ha {
|
||||
struct Brightness : Command {
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@ -12,13 +12,13 @@
|
||||
|
||||
namespace Mqtt {
|
||||
|
||||
void publishConfig();
|
||||
void publishInit();
|
||||
void publishCommand();
|
||||
void publishBmp280();
|
||||
void connect();
|
||||
void disconnect();
|
||||
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 tPublishCommand(TASK_SECOND, TASK_FOREVER, publishCommand, &ts);
|
||||
|
||||
@ -49,6 +49,10 @@ namespace Mqtt {
|
||||
client.disconnect();
|
||||
}
|
||||
|
||||
uint16_t publish(const char* topic, const char* message) {
|
||||
return client.publish(topic, 0, true, message);
|
||||
}
|
||||
|
||||
Ha::Sensor* sensors[] = {
|
||||
new Ha::TemperatureSensor{"Living room Temperature", "temperature"},
|
||||
new Ha::PressureSensor{"Living room Pressure", "pressure"},
|
||||
@ -58,18 +62,29 @@ namespace Mqtt {
|
||||
Ha::Command* ledMqtt = (new Ha::Switch{"ESP Clock Led", "led",
|
||||
[](const char* msg) {
|
||||
String{ "ON" }.equals(msg) ? digitalWrite(LED_BUILTIN, LOW) : digitalWrite(LED_BUILTIN, HIGH);
|
||||
},
|
||||
[]() {
|
||||
publish(ledMqtt->stateTopic, digitalRead(LED_BUILTIN) ? "OFF" : "ON");
|
||||
}
|
||||
})->withStateTopic();
|
||||
|
||||
Ha::Command* brightnessMqtt = (new Ha::Brightness{ "ESP Clock Brightness", "brightness",
|
||||
[](const char* msg) {
|
||||
Display::Brightness::set(String{ msg }.toInt());
|
||||
},
|
||||
[]() {
|
||||
char message[32];
|
||||
sprintf(message, "%u", Display::Brightness::current);
|
||||
publish(brightnessMqtt->stateTopic, message);
|
||||
}
|
||||
})->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);
|
||||
},
|
||||
[]() {
|
||||
publish(hourFormatMqtt->stateTopic, Display::hourFormat24 ? "ON" : "OFF");
|
||||
}
|
||||
})->withStateTopic();
|
||||
|
||||
@ -84,10 +99,6 @@ namespace Mqtt {
|
||||
brightnessMqtt
|
||||
};
|
||||
|
||||
uint16_t publish(const char* topic, const char* message) {
|
||||
return client.publish(topic, 0, true, message);
|
||||
}
|
||||
|
||||
void publishComponentConfig(Ha::Component& component) {
|
||||
StaticJsonDocument<JSON_SIZE> jsonDoc;
|
||||
component.buildConfig(jsonDoc);
|
||||
@ -98,31 +109,17 @@ namespace Mqtt {
|
||||
publish(component.configTopic, message);
|
||||
}
|
||||
|
||||
void publishBrightness() {
|
||||
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() {
|
||||
void publishInit() {
|
||||
for (Ha::Component* cmp : sensors) {
|
||||
publishComponentConfig(*cmp);
|
||||
}
|
||||
for (Ha::Component* cmp : switches) {
|
||||
publishComponentConfig(*cmp);
|
||||
}
|
||||
publishBrightness();
|
||||
publishHourFormat(Display::hourFormat24);
|
||||
ts.deleteTask(tPublishConfig);
|
||||
brightnessMqtt->publishState();
|
||||
hourFormatMqtt->publishState();
|
||||
ledMqtt->publishState();
|
||||
ts.deleteTask(tPublishInit);
|
||||
}
|
||||
|
||||
void publishBmp280() {
|
||||
@ -161,10 +158,10 @@ namespace Mqtt {
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Display::Brightness::onChanged(publishBrightness);
|
||||
Display::onHourFormatChanged(publishHourFormat);
|
||||
Display::Brightness::onChanged(brightnessMqtt->publishState);
|
||||
Display::onHourFormatChanged(hourFormatMqtt->publishState);
|
||||
client.onConnect([](bool sessionPresent) {
|
||||
tPublishConfig.enable();
|
||||
tPublishInit.enable();
|
||||
tPublishBmp.enableIfNot();
|
||||
tPublishCommand.enableDelayed();
|
||||
client.subscribe(espClockTopic, 0);
|
||||
|
||||
@ -25,7 +25,7 @@ Task tButton(TASK_IMMEDIATE, TASK_ONCE, []() {
|
||||
Display::displayTemp(Bmp::data.readTemp());
|
||||
}, &ts);
|
||||
Task tLed(TASK_IMMEDIATE, TASK_ONCE, []() {
|
||||
Mqtt::publishLedState();
|
||||
Mqtt::ledMqtt->publishState();
|
||||
}, &ts);
|
||||
|
||||
void setup() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user