diff --git a/gateway/include/ha.h b/gateway/include/ha.h index 381f520..2a7fbda 100644 --- a/gateway/include/ha.h +++ b/gateway/include/ha.h @@ -70,14 +70,15 @@ namespace Ha { }; + typedef uint16_t (*publisherFunc)(const char* topic, const char* message); struct Switch : Command { static constexpr const char* type = "switch"; char stateTopic[TOPIC_SIZE]; const char* area; - uint16_t (*publisher)(const char* topic, const char* message); + publisherFunc publisher; virtual void onCommand(const char* msg){} - Switch(const char* name, const char* id, uint16_t (*publisher)(const char* topic, const char* message) = nullptr) + Switch(const char* name, const char* id, publisherFunc publisher = nullptr) : Command(name, id, type), publisher(publisher) { sprintf(commandTopic, "homeassistant/%s/rc-gateway/%s/set", type, id); } @@ -119,7 +120,7 @@ namespace Ha { const char* group; unsigned char channel; - PollinSwitch(const char* name, const char* group, const unsigned char channel, uint16_t (*publisher)(const char* topic, const char* message)) + PollinSwitch(const char* name, const char* group, const unsigned char channel, publisherFunc publisher) : Switch(name, Protocol_1::buildId(group, channel), publisher), group(group), channel(channel) { sprintf(commandTopic, "homeassistant/%s/rc-gateway/%s/set", type, id); } @@ -138,16 +139,18 @@ namespace Ha { }; struct EasyHomeSwitch : Switch { - const char* on; - const char* off; + unsigned long *on; + unsigned long *off; - EasyHomeSwitch(const char* name, const char* id, const char* on, const char* off) : Switch(name, id), on(on), off(off) { - sprintf(commandTopic, "homeassistant/%s/rc-gateway/easy_home_%s/set", type, id); + 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) { + sprintf(commandTopic, "homeassistant/%s/rc-gateway/%s/set", type, id); } void onCommand(const char* msg) override { mySwitch.setProtocol(4); - String{ "ON" }.equals(msg) ? mySwitch.send(on) : mySwitch.send(off); + String{ "ON" }.equals(msg) ? mySwitch.send(on[0], 24) : mySwitch.send(off[0], 24); + publisher(stateTopic, msg); } void buildConfig(JsonDocument& jsonDoc) override { @@ -156,7 +159,6 @@ namespace Ha { device["manufacturer"] = "Intertek"; device["model"] = "Easy Home"; } - }; struct Sensor : Component { diff --git a/gateway/include/mqtt.h b/gateway/include/mqtt.h index 45a4423..4ce061d 100644 --- a/gateway/include/mqtt.h +++ b/gateway/include/mqtt.h @@ -51,11 +51,18 @@ 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::EasyHomeSwitch{"FritzBox", "easy_home_a", "010001101001100101110000", "010010111110000010100000"})->withArea("Basement") + (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 }; unordered_map mapSwitches; + unordered_map onSwitches; + unordered_map offSwitches; void publishComponentConfig(Ha::Component& component) { StaticJsonDocument jsonDoc; @@ -71,6 +78,8 @@ namespace Mqtt { // for (Ha::Component* cmp : sensors) { // publishComponentConfig(*cmp); // } + fritz->withArea("Basement"); + fritz->withStateTopic(); for (Ha::Component* cmp : buttons) { publishComponentConfig(*cmp); } @@ -78,6 +87,11 @@ namespace Mqtt { mapSwitches.insert({string(cmp->id), cmp}); publishComponentConfig(*cmp); } + for (Ha::EasyHomeSwitch* cmp : otherSwitches) { + for (int i = 0; i < 4; i++) onSwitches.insert({cmp->on[i], cmp}); + for (int i = 0; i < 4; i++) offSwitches.insert({cmp->off[i], cmp}); + publishComponentConfig(*cmp); + } ts.deleteTask(tPublishInit); } @@ -108,6 +122,12 @@ namespace Mqtt { return; } } + for (Ha::Switch* cmd : otherSwitches) { + if (String{ cmd->commandTopic }.equals(topic)) { + cmd->onCommand(msg); + return; + } + } } void setup() {