diff --git a/gateway/include/ha.h b/gateway/include/ha.h index 26385c3..6a56af9 100644 --- a/gateway/include/ha.h +++ b/gateway/include/ha.h @@ -16,12 +16,10 @@ namespace Ha { const char* model = nullptr; const char* manufacturer = nullptr; const char* area = nullptr; - DeviceConfig* parent = nullptr; + const DeviceConfig* parent = nullptr; - DeviceConfig(const char* id) : id(id) {} - - static DeviceConfig* create(const char* id) { - return new DeviceConfig{ id }; + static DeviceConfig& create(const char* id) { + return *(new DeviceConfig{ id }); } void buildConfig(JsonDocument& jsonDoc) { @@ -35,30 +33,33 @@ namespace Ha { identifiers.add(id); } - DeviceConfig* withName(const char* value) { + DeviceConfig& withName(const char* value) { name = value; - return this; + return *this; } - DeviceConfig* withModel(const char* value) { + DeviceConfig& withModel(const char* value) { model = value; - return this; + return *this; } - DeviceConfig* withManufacturer(const char* value) { + DeviceConfig& withManufacturer(const char* value) { manufacturer = value; - return this; + return *this; } - DeviceConfig* withArea(const char* value) { + DeviceConfig& withArea(const char* value) { area = value; - return this; + return *this; } - DeviceConfig* withParent(DeviceConfig* deviceConfig) { + DeviceConfig& withParent(const DeviceConfig* deviceConfig) { parent = deviceConfig; - return this; + return *this; } + + protected: + DeviceConfig(const char* id) : id(id) {} }; struct Component { @@ -108,7 +109,6 @@ namespace Ha { publisher(configTopic, ""); } }; - List Component::components; struct AbstractBuilder { static List builders; @@ -122,7 +122,6 @@ namespace Ha { builders.empty(); } }; - List AbstractBuilder::builders; template struct Builder : AbstractBuilder { @@ -161,15 +160,14 @@ namespace Ha { return *this; } - Builder& withSecondary(Component* c) { - c->mainDevice = new DeviceConfig{ cmp->id }; + Builder& asSecondary(Component* c) { + c->mainDevice = &DeviceConfig::create(cmp->id); return *this; } - Builder& withDiagnostic(Component* c) { + Builder& asDiagnostic(Component* c) { c->entityCategory = "diagnostic"; - c->mainDevice = new DeviceConfig{ cmp->id }; - return *this; + return asSecondary(c); } Builder& asDevice(DeviceConfig* deviceConfig) { @@ -198,7 +196,6 @@ namespace Ha { } }; - unordered_map Command::mapCommands; struct Button : Command { @@ -227,7 +224,7 @@ namespace Ha { if (stateTopic[0]) jsonDoc["state_topic"] = stateTopic; } - void publishState(bool state) { + void updateState(bool state) { publisher(stateTopic, state ? "ON" : "OFF"); } }; @@ -235,13 +232,11 @@ namespace Ha { struct Sensor : Component, StateConfig { const char* unitMeasure = nullptr; const char* valueTemplate = nullptr; - - Sensor() : Component(name, id, "sensor") { - withStateTopic(); - } + static unordered_map mapSensors; Sensor(const char* name, const char* id) : Component(name, id, "sensor") { withStateTopic(); + mapSensors.insert({ id, this }); } void buildUniqueId(char* uniqueId) override { @@ -267,6 +262,10 @@ namespace Ha { jsonDoc["state_topic"] = stateTopic; jsonDoc["suggested_display_precision"] = 2; } + + void updateState(const char* message) { + publisher(stateTopic, message); + } }; struct TemperatureSensor : Sensor { @@ -307,4 +306,9 @@ namespace Ha { // valueTemplate = "{{ value_json.sensor.pressure }}"; } }; + + List Component::components; + List AbstractBuilder::builders; + unordered_map Command::mapCommands; + unordered_map Sensor::mapSensors; }