publish config from list of all components

This commit is contained in:
Nicu Hodos 2024-05-03 12:32:44 +02:00
parent 86b2d75ea6
commit 1e69ecd9c9
3 changed files with 17 additions and 32 deletions

View File

@ -6,7 +6,7 @@
using namespace Ha;
DeviceConfig* gatewayConfig = (new DeviceConfig{MAIN_DEVICE_ID, "RC Gateway"})->withManufacturer("Adafruit")->withModel("Huzzah Esp8266");
DeviceConfig* gatewayDevice = (new DeviceConfig{MAIN_DEVICE_ID, "RC Gateway"})->withManufacturer("Adafruit")->withModel("Huzzah Esp8266");
struct PollinSwitch : Switch {
const char* group;
@ -14,7 +14,8 @@ struct PollinSwitch : Switch {
PollinSwitch(const char* name, const char* group, const unsigned char channel)
: Switch(name, Protocol_1::buildId(group, channel)), group(group), channel(channel) {
asDevice((new DeviceConfig{id, name})->withManufacturer("Pollin")->withParent(gatewayConfig));
asDevice((new DeviceConfig{id, name})->withManufacturer("Pollin")->withParent(gatewayDevice));
p1Switches.insert({ string(id), this });
}
void onCommand(const char* msg) override {
@ -22,10 +23,6 @@ struct PollinSwitch : Switch {
publisher(stateTopic, msg);
}
void addToMap() override {
p1Switches.insert({ string(id), this });
}
};
struct EasyHomeSwitch : Switch {
@ -36,7 +33,11 @@ struct EasyHomeSwitch : Switch {
: Switch(name, id) {
memcpy(&this->on[4], on, 4 * sizeof(unsigned long));
memcpy(&this->off[4], off, 4 * sizeof(unsigned long));
asDevice((new DeviceConfig{id, name})->withManufacturer("Intertek")->withModel("Easy Home")->withParent(gatewayConfig));
asDevice((new DeviceConfig{id, name})->withManufacturer("Intertek")->withModel("Easy Home")->withParent(gatewayDevice));
for (int i = 0; i < 8; i++) {
onSwitches.insert({ on[i], this });
offSwitches.insert({ off[i], this });
}
}
void onCommand(const char* msg) override {
@ -44,11 +45,4 @@ struct EasyHomeSwitch : Switch {
String{ "ON" }.equals(msg) ? mySwitch.send(on[4], 24) : mySwitch.send(off[4], 24);
publisher(stateTopic, msg);
}
void addToMap() override {
for (int i = 0; i < 8; i++) {
onSwitches.insert({ on[i], this });
offSwitches.insert({ off[i], this });
}
}
};

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);
}