add support for reacting on switching on/off all EasyHome switches

This commit is contained in:
Nicu Hodos 2024-04-30 16:23:54 +02:00
parent 7403062837
commit 3ceba3139b
2 changed files with 31 additions and 10 deletions

View File

@ -114,6 +114,26 @@ namespace Ha {
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 {
@ -139,11 +159,13 @@ namespace Ha {
};
struct EasyHomeSwitch : Switch {
unsigned long *on;
unsigned long *off;
unsigned long on[8] = {4972714, 4767530, 4537114, 4326554};
unsigned long off[8] = {4483146, 4626810, 4819642, 4661562};
EasyHomeSwitch(const char* name, const char* id, unsigned long on[4], unsigned long off[4], publisherFunc publisher)
: Switch(name, id, publisher), on(on), off(off) {
: Switch(name, id, publisher) {
memccpy(&this->on[4], on, 4, sizeof(unsigned long));
memccpy(&this->off[4], off, 4, sizeof(unsigned long));
sprintf(commandTopic, "homeassistant/%s/rc-gateway/%s/set", type, id);
}

View File

@ -54,15 +54,16 @@ namespace Mqtt {
(new Ha::PollinSwitch{"Train", "11111", 4, publish})->withArea("Playroom")->withStateTopic()
};
Ha::EasyHomeSwitch* fritz = new Ha::EasyHomeSwitch{"FritzBox", "easy_home_a", (unsigned long[4]){4483136, 4626800, 4819632, 4661552}, (unsigned long[4]){4767520, 4537104, 4326544, 4972704}, publish};
Ha::EasyHomeSwitch* otherSwitches[] = {
fritz
(Ha::Switch::Builder<Ha::EasyHomeSwitch>{new Ha::EasyHomeSwitch{"FritzBox", "easy_home_a", (unsigned long[4]) { 4483136, 4626800, 4819632, 4661552 }, (unsigned long[4]) { 4767520, 4537104, 4326544, 4972704 }, publish}})
.withArea("Basement")->withStateTopic()->build(),
(Ha::Switch::Builder<Ha::EasyHomeSwitch>{new Ha::EasyHomeSwitch{"Outside", "easy_home_b", (unsigned long[4]) { 4661556, 4819636, 4626804, 4483140 }, (unsigned long[4]) { 4326548, 4537108, 4767524, 4972708 }, publish}})
.withArea("Basement")->withStateTopic()->build()
};
unordered_map<string, Ha::Switch*> mapSwitches;
unordered_map<unsigned long, Ha::EasyHomeSwitch*> onSwitches;
unordered_map<unsigned long, Ha::EasyHomeSwitch*> offSwitches;
unordered_multimap<unsigned long, Ha::Switch*> onSwitches;
unordered_map<unsigned long, Ha::Switch*> offSwitches;
void publishComponentConfig(Ha::Component& component) {
StaticJsonDocument<JSON_SIZE> jsonDoc;
@ -78,8 +79,6 @@ namespace Mqtt {
// for (Ha::Component* cmp : sensors) {
// publishComponentConfig(*cmp);
// }
fritz->withArea("Basement");
fritz->withStateTopic();
for (Ha::Component* cmp : buttons) {
publishComponentConfig(*cmp);
}