generalize components configuration
This commit is contained in:
parent
04e4c6531b
commit
294eef45c6
@ -34,9 +34,11 @@ namespace Ha {
|
||||
|
||||
struct Command : Component {
|
||||
const char* commandTopic;
|
||||
void (*f)(const char* msg);
|
||||
|
||||
Command(const char* name, const char* uniqueId, const char* configTopic, const char* commandTopic) : Component(name, uniqueId, configTopic) {
|
||||
Command(const char* name, const char* uniqueId, const char* configTopic, const char* commandTopic, void (*f)(const char* msg)) : Component(name, uniqueId, configTopic) {
|
||||
this->commandTopic = commandTopic;
|
||||
this->f = f;
|
||||
}
|
||||
|
||||
void buildConfig(JsonDocument& jsonDoc) {
|
||||
|
||||
@ -35,9 +35,6 @@ namespace Mqtt {
|
||||
} commands;
|
||||
|
||||
const char* bmpTopic = "homeassistant/sensor/esp_clock/state";
|
||||
const char* restartTopic = "homeassistant/button/esp_clock/restart";
|
||||
const char* ledTopic = "homeassistant/switch/esp_clock/led/set";
|
||||
const char* hourFormatTopic = "homeassistant/switch/esp_clock/hour_format/set";
|
||||
const char* espClockTopic = "homeassistant/+/esp_clock/#";
|
||||
|
||||
void connect() {
|
||||
@ -49,13 +46,34 @@ namespace Mqtt {
|
||||
client.disconnect();
|
||||
}
|
||||
|
||||
Ha::Component components[] = {
|
||||
Ha::Sensor sensors[] = {
|
||||
Ha::TemperatureSensor{"Living room Temperature", "living_room_temperature", "homeassistant/sensor/esp_clock/temperature/config", bmpTopic},
|
||||
Ha::PressureSensor{"Living room Pressure", "living_room_pressure", "homeassistant/sensor/esp_clock/pressure/config", bmpTopic},
|
||||
Ha::AltitudeSensor{"Living room Altitude", "living_room_altitude", "homeassistant/sensor/esp_clock/altitude/config", bmpTopic},
|
||||
Ha::Command{"Restart", "esp_clock_restart", "homeassistant/button/esp_clock/restart/config", restartTopic},
|
||||
Ha::Command{"ESP Clock Led", "esp_clock_led", "homeassistant/switch/esp_clock/led/config", ledTopic},
|
||||
Ha::Command{"ESP Clock Format 24h", "esp_clock_format_24h", "homeassistant/switch/esp_clock/hour_format/config", hourFormatTopic}
|
||||
Ha::AltitudeSensor{"Living room Altitude", "living_room_altitude", "homeassistant/sensor/esp_clock/altitude/config", bmpTopic}
|
||||
};
|
||||
|
||||
Ha::Command switches[] = {
|
||||
Ha::Command{"Restart", "esp_clock_restart", "homeassistant/button/esp_clock/restart/config", "homeassistant/button/esp_clock/restart",
|
||||
[](const char* msg) {
|
||||
if (String { "PRESS" }.equals(msg)) ESP.restart();
|
||||
}
|
||||
},
|
||||
Ha::Command{"ESP Clock Led", "esp_clock_led", "homeassistant/switch/esp_clock/led/config", "homeassistant/switch/esp_clock/led/set",
|
||||
[](const char* msg) {
|
||||
String{ "ON" }.equals(msg) ? digitalWrite(LED_BUILTIN, LOW) : digitalWrite(LED_BUILTIN, HIGH);
|
||||
}
|
||||
},
|
||||
Ha::Command{"ESP Clock Format 24h", "esp_clock_format_24h", "homeassistant/switch/esp_clock/hour_format/config", "homeassistant/switch/esp_clock/hour_format/set",
|
||||
[](const char* msg) {
|
||||
if (String{ "ON" }.equals(msg)) {
|
||||
Display::hourFormat24 = true;
|
||||
Display::drawTime();
|
||||
} else {
|
||||
Display::hourFormat24 = false;
|
||||
Display::drawTime();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void publishComponentConfig(Ha::Component& component) {
|
||||
@ -69,7 +87,10 @@ namespace Mqtt {
|
||||
}
|
||||
|
||||
void publishConfig() {
|
||||
for (Ha::Component cmp : components) {
|
||||
for (Ha::Component cmp : sensors) {
|
||||
publishComponentConfig(cmp);
|
||||
}
|
||||
for (Ha::Component cmp : switches) {
|
||||
publishComponentConfig(cmp);
|
||||
}
|
||||
ts.deleteTask(tPublishConfig);
|
||||
@ -102,23 +123,10 @@ namespace Mqtt {
|
||||
char msg[len + 1];
|
||||
memcpy(msg, payload, len);
|
||||
msg[len] = 0;
|
||||
if (String{ restartTopic }.equals(topic) && String { "PRESS" }.equals(msg)) {
|
||||
ESP.restart();
|
||||
}
|
||||
if (String{ ledTopic }.equals(topic)) {
|
||||
if (String{ "ON" }.equals(msg)) {
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
} else {
|
||||
digitalWrite(LED_BUILTIN, HIGH);
|
||||
}
|
||||
}
|
||||
if (String{ hourFormatTopic }.equals(topic)) {
|
||||
if (String{ "ON" }.equals(msg)) {
|
||||
Display::hourFormat24 = true;
|
||||
Display::drawTime();
|
||||
} else {
|
||||
Display::hourFormat24 = false;
|
||||
Display::drawTime();
|
||||
for (Ha::Command cmd : switches) {
|
||||
if (String{ cmd.commandTopic }.equals(topic)) {
|
||||
cmd.f(msg);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user