diff --git a/gateway/include/ha.h b/gateway/include/ha.h index 61515cc..904f254 100644 --- a/gateway/include/ha.h +++ b/gateway/include/ha.h @@ -93,31 +93,56 @@ namespace Ha { jsonDoc["unique_id"] = uniqueId; } }; - - template - struct EntityConfig : Component { - - EntityConfig(const char* name, const char* id, const char* type) : Component(name, id, type) { - } - - T* asDevice(DeviceConfig* deviceConfig) { - mainDevice = deviceConfig; - return static_cast(this); - } - - T* ofDevice(const char* id) { - mainDevice = new DeviceConfig{id}; - return static_cast(this); - } - }; List Component::components; template - struct Command : EntityConfig { + struct Builder { + T* cmp; + + Builder() {} + Builder(T* cmp) : cmp(cmp) {} + Builder(const char* id) { + cmp = new T{id}; + } + + static Builder& instance() { + return *(new Builder()); + } + + static Builder& instance(T* c) { + return *(new Builder(c)); + } + + static Builder& instance(const char* id) { + return *(new Builder(id)); + } + + T* build() { + return static_cast(cmp); + } + + Builder& withSecondary(Component* c) { + c->mainDevice = new DeviceConfig{ cmp->id }; + return *this; + } + + Builder& withDiagnostic(Component* c) { + c->entityCategory = "diagnostic"; + c->mainDevice = new DeviceConfig{ cmp->id }; + return *this; + } + + Builder& asDevice(DeviceConfig* deviceConfig) { + cmp->mainDevice = deviceConfig; + return *this; + } + }; + + struct Command : Component { char commandTopic[TOPIC_SIZE]; - Command(const char* name, const char* id, const char* type) : EntityConfig(name, id, type) { + Command(const char* name, const char* id, const char* type) : Component(name, id, type) { sprintf(commandTopic, "homeassistant/%s/%s/%s", type, MAIN_DEVICE_ID, id); } @@ -126,17 +151,17 @@ namespace Ha { } void buildConfig(JsonDocument& jsonDoc) override { - EntityConfig::buildConfig(jsonDoc); + Component::buildConfig(jsonDoc); jsonDoc["command_topic"] = commandTopic; } }; typedef void (*onMessage)(const char* msg); - struct Button : Command