publish config from list of all components

This commit is contained in:
Nicu Hodos 2024-05-03 12:32:44 +02:00
parent 3cbedd3d99
commit 88b8ef9c01
2 changed files with 9 additions and 18 deletions

View File

@ -56,14 +56,14 @@ namespace Ha {
};
struct Component {
char* id = nullptr;
const char* name;
char* id;
const char* type;
char configTopic[TOPIC_SIZE];
DeviceConfig* mainDevice;
DeviceConfig* mainDevice = nullptr;
static List<Component> components;
Component(const char* name, const char* id, const char* type) : name(name), id((char*)id), type(type) {
Component(const char* name, const char* id, const char* type) : id((char*)id), name(name), type(type) {
sprintf(configTopic, "homeassistant/%s/%s/%s/config", type, MAIN_DEVICE_ID, id);
components.add(this);
}
@ -103,6 +103,7 @@ namespace Ha {
char commandTopic[TOPIC_SIZE];
Command(const char* name, const char* id, const char* type) : EntityConfig<T>(name, id, type) {
sprintf(commandTopic, "homeassistant/%s/%s/%s", type, MAIN_DEVICE_ID, id);
}
void buildUniqueId(char* uniqueId) override {
@ -118,10 +119,9 @@ namespace Ha {
typedef void (*onMessage)(const char* msg);
struct Button : Command<Button> {
static constexpr const char* type = "button";
onMessage f;
Button(const char* name, const char* id, onMessage f) : Command(name, id, type), f(f) {}
Button(const char* name, const char* id, onMessage f) : Command(name, id, "button"), f(f) {}
};
@ -136,13 +136,11 @@ namespace Ha {
};
struct Switch : Command<Switch>, StateConfig<Switch> {
static constexpr const char* type = "switch";
const char* area;
virtual void onCommand(const char* msg){}
virtual void addToMap(){}
Switch(const char* name, const char* id) : Command(name, id, type) {
Switch(const char* name, const char* id) : Command(name, id, "switch") {
sprintf(commandTopic, "homeassistant/%s/%s/%s/set", type, MAIN_DEVICE_ID, id);
}

View File

@ -44,7 +44,7 @@ namespace Mqtt {
[](const char* msg) {
if (String { "PRESS" }.equals(msg)) ESP.restart();
}
})->asDevice(gatewayConfig)
})->asDevice(gatewayDevice)
};
Ha::Switch* switches[] = {
@ -67,16 +67,9 @@ namespace Mqtt {
}
void publishInit() {
// for (Ha::Component* cmp : sensors) {
// publishComponentConfig(*cmp);
// }
Ha::publisher = publish;
for (Ha::Component* cmp : buttons) {
publishComponentConfig(*cmp);
}
for (Ha::Switch* cmp : switches) {
cmp->addToMap();
publishComponentConfig(*cmp);
for (List<Ha::Component>::Container* c = Ha::Component::components.first; c; c = c->next) {
publishComponentConfig(*c->t);
}
ts.deleteTask(tPublishInit);
}