From 07c28594002d4745435ab9542178a79bdfb8c78a Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Sat, 18 May 2024 22:37:58 +0200 Subject: [PATCH 1/4] allow creation of DeviceConfig only with factory method --- gateway/include/ha.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/gateway/include/ha.h b/gateway/include/ha.h index 26385c3..6c856ff 100644 --- a/gateway/include/ha.h +++ b/gateway/include/ha.h @@ -18,8 +18,6 @@ namespace Ha { const char* area = nullptr; DeviceConfig* parent = nullptr; - DeviceConfig(const char* id) : id(id) {} - static DeviceConfig* create(const char* id) { return new DeviceConfig{ id }; } @@ -59,6 +57,9 @@ namespace Ha { parent = deviceConfig; return this; } + + protected: + DeviceConfig(const char* id) : id(id) {} }; struct Component { @@ -162,13 +163,13 @@ namespace Ha { } Builder& withSecondary(Component* c) { - c->mainDevice = new DeviceConfig{ cmp->id }; + c->mainDevice = DeviceConfig::create(cmp->id); return *this; } Builder& withDiagnostic(Component* c) { c->entityCategory = "diagnostic"; - c->mainDevice = new DeviceConfig{ cmp->id }; + c->mainDevice = DeviceConfig::create(cmp->id); return *this; } From 3e87d6043435f3b4cf4579618a8c46f996dccb8e Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Sun, 19 May 2024 01:31:41 +0200 Subject: [PATCH 2/4] use reference when building DeviceConfig --- gateway/include/ha.h | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/gateway/include/ha.h b/gateway/include/ha.h index 6c856ff..ef64de7 100644 --- a/gateway/include/ha.h +++ b/gateway/include/ha.h @@ -16,10 +16,10 @@ namespace Ha { const char* model = nullptr; const char* manufacturer = nullptr; const char* area = nullptr; - DeviceConfig* parent = nullptr; + const DeviceConfig* parent = nullptr; - static DeviceConfig* create(const char* id) { - return new DeviceConfig{ id }; + static DeviceConfig& create(const char* id) { + return *(new DeviceConfig{ id }); } void buildConfig(JsonDocument& jsonDoc) { @@ -33,29 +33,29 @@ 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: @@ -163,13 +163,13 @@ namespace Ha { } Builder& withSecondary(Component* c) { - c->mainDevice = DeviceConfig::create(cmp->id); + c->mainDevice = &DeviceConfig::create(cmp->id); return *this; } Builder& withDiagnostic(Component* c) { c->entityCategory = "diagnostic"; - c->mainDevice = DeviceConfig::create(cmp->id); + c->mainDevice = &DeviceConfig::create(cmp->id); return *this; } From 13bbb5ffc430e7f90ff84380a7050d6eb313d615 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Sun, 19 May 2024 01:33:11 +0200 Subject: [PATCH 3/4] use better names for constructing secondary entities --- gateway/include/ha.h | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/gateway/include/ha.h b/gateway/include/ha.h index ef64de7..abec285 100644 --- a/gateway/include/ha.h +++ b/gateway/include/ha.h @@ -162,15 +162,14 @@ namespace Ha { return *this; } - Builder& withSecondary(Component* c) { + 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 = &DeviceConfig::create(cmp->id); - return *this; + return asSecondary(c); } Builder& asDevice(DeviceConfig* deviceConfig) { From 8428c702f72459219e81b6a393af85bceafdc33e Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Sun, 19 May 2024 11:50:19 +0200 Subject: [PATCH 4/4] use map to find sensors --- gateway/include/ha.h | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/gateway/include/ha.h b/gateway/include/ha.h index abec285..6a56af9 100644 --- a/gateway/include/ha.h +++ b/gateway/include/ha.h @@ -109,7 +109,6 @@ namespace Ha { publisher(configTopic, ""); } }; - List Component::components; struct AbstractBuilder { static List builders; @@ -123,7 +122,6 @@ namespace Ha { builders.empty(); } }; - List AbstractBuilder::builders; template struct Builder : AbstractBuilder { @@ -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; }