From 1e69ecd9c9f6368c1896c0206532506754c424c5 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Fri, 3 May 2024 12:32:44 +0200 Subject: [PATCH] publish config from list of all components --- gateway/include/devices.h | 22 ++++++++-------------- gateway/include/ha.h | 14 ++++++-------- gateway/include/mqtt.h | 13 +++---------- 3 files changed, 17 insertions(+), 32 deletions(-) diff --git a/gateway/include/devices.h b/gateway/include/devices.h index 27f0983..eb84503 100644 --- a/gateway/include/devices.h +++ b/gateway/include/devices.h @@ -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 }); - } - } }; diff --git a/gateway/include/ha.h b/gateway/include/ha.h index 864119d..e0f1b42 100644 --- a/gateway/include/ha.h +++ b/gateway/include/ha.h @@ -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 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(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