unify switches under same list and get rid of Builder

This commit is contained in:
Nicu Hodos 2024-04-30 23:08:22 +02:00
parent a1378dc122
commit e48c75796b
2 changed files with 17 additions and 47 deletions

View File

@ -76,7 +76,9 @@ namespace Ha {
char stateTopic[TOPIC_SIZE];
const char* area;
publisherFunc publisher;
virtual void onCommand(const char* msg){}
virtual void addToMap(){}
Switch(const char* name, const char* id, publisherFunc publisher = nullptr)
: Command(name, id, type), publisher(publisher) {
@ -113,27 +115,6 @@ namespace Ha {
void publishState(bool state) {
publisher(stateTopic, state ? "ON" : "OFF");
}
template <class T>
struct Builder {
T* t;
Builder(T* t) : t(t) {}
T* build() {
return t;
}
Builder<T>* withArea(const char* area) {
t->withArea(area);
return this;
}
Builder<T>* withStateTopic() {
t->withStateTopic();
return this;
}
};
};
struct PollinSwitch : Switch {
@ -156,6 +137,10 @@ namespace Ha {
device["manufacturer"] = "Pollin";
}
void addToMap() override {
p1Switches.insert({string(id), this});
}
};
struct EasyHomeSwitch : Switch {
@ -181,6 +166,13 @@ namespace Ha {
device["manufacturer"] = "Intertek";
device["model"] = "Easy Home";
}
void addToMap() override {
for (int i = 0; i < 8; i++) {
onSwitches.insert({on[i], this});
offSwitches.insert({off[i], this});
}
}
};
struct Sensor : Component {

View File

@ -51,20 +51,11 @@ namespace Mqtt {
(new Ha::PollinSwitch{"Meeting sensor", "00001", 1, publish})->withArea("Dining room")->withStateTopic(),
(new Ha::PollinSwitch{"Fire Tv", "00001", 2, publish})->withArea("Living room")->withStateTopic(),
(new Ha::PollinSwitch{"Diningroom player", "00001", 3, publish})->withArea("Dining room")->withStateTopic(),
(new Ha::PollinSwitch{"Train", "11111", 4, publish})->withArea("Playroom")->withStateTopic()
(new Ha::PollinSwitch{"Train", "11111", 4, publish})->withArea("Playroom")->withStateTopic(),
(new Ha::EasyHomeSwitch{"FritzBox", "easy_home_a", (unsigned long[4]) { 4483136, 4626800, 4661552, 4819632 }, (unsigned long[4]) { 4326544, 4537104, 4767520, 4972704 }, publish})->withArea("Basement")->withStateTopic(),
(new Ha::EasyHomeSwitch{"Outside", "easy_home_b", (unsigned long[4]) { 4483140, 4626804, 4661556, 4819636 }, (unsigned long[4]) { 4326548, 4537108, 4767524, 4972708 }, publish})->withArea("Basement")->withStateTopic()
};
Ha::EasyHomeSwitch* otherSwitches[] = {
(Ha::Switch::Builder<Ha::EasyHomeSwitch>{new Ha::EasyHomeSwitch{"FritzBox", "easy_home_a", (unsigned long[4]) { 4483136, 4626800, 4661552, 4819632 }, (unsigned long[4]) { 4326544, 4537104, 4767520, 4972704 }, publish}})
.withArea("Basement")->withStateTopic()->build(),
(Ha::Switch::Builder<Ha::EasyHomeSwitch>{new Ha::EasyHomeSwitch{"Outside", "easy_home_b", (unsigned long[4]) { 4483140, 4626804, 4661556, 4819636 }, (unsigned long[4]) { 4326548, 4537108, 4767524, 4972708 }, publish}})
.withArea("Basement")->withStateTopic()->build()
};
unordered_map<string, Ha::Switch*> mapSwitches;
unordered_multimap<unsigned long, Ha::Switch*> onSwitches;
unordered_multimap<unsigned long, Ha::Switch*> offSwitches;
void publishComponentConfig(Ha::Component& component) {
StaticJsonDocument<JSON_SIZE> jsonDoc;
component.buildConfig(jsonDoc);
@ -83,14 +74,7 @@ namespace Mqtt {
publishComponentConfig(*cmp);
}
for (Ha::Switch* cmp : switches) {
mapSwitches.insert({string(cmp->id), cmp});
publishComponentConfig(*cmp);
}
for (Ha::EasyHomeSwitch* cmp : otherSwitches) {
for (int i = 0; i < 8; i++) {
onSwitches.insert({cmp->on[i], cmp});
offSwitches.insert({cmp->off[i], cmp});
}
cmp->addToMap();
publishComponentConfig(*cmp);
}
ts.deleteTask(tPublishInit);
@ -123,12 +107,6 @@ namespace Mqtt {
return;
}
}
for (Ha::Switch* cmd : otherSwitches) {
if (String{ cmd->commandTopic }.equals(topic)) {
cmd->onCommand(msg);
return;
}
}
}
void setup() {