From 8428c702f72459219e81b6a393af85bceafdc33e Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Sun, 19 May 2024 11:50:19 +0200 Subject: [PATCH] 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; }