diff --git a/src/ha.h b/src/ha.h index f1840c1..31870bd 100644 --- a/src/ha.h +++ b/src/ha.h @@ -259,11 +259,11 @@ namespace Ha { struct Command : Component { bool retain = false; - static unordered_map mapCommands; + static unordered_map mapCommandTopics; Command(const char* name, const char* id, const char* type, onMessage f) : Component(name, id, type), f(f) { snprintf(commandTopic, sizeof(commandTopic), BASE_TOPIC"/set", type, MAIN_DEVICE_ID, id); - mapCommands.insert({ string(commandTopic), this }); + mapCommandTopics.insert({ string(commandTopic), this }); } virtual void onCommand(const char* msg) { @@ -292,7 +292,6 @@ namespace Ha { }; struct StateConfig { - static unordered_map mapStateTopics; char stateTopic[TOPIC_SIZE]; StateConfig(Component* cmp) : cmp(cmp) {} @@ -312,38 +311,41 @@ namespace Ha { } }; - struct Switch : Command, StateConfig { + struct StatefulCommand : Command, StateConfig { + static unordered_map mapRestoreStateTopics; - Switch(const char* name, const char* id, onMessage f = nullptr) : Command(name, id, "switch", f), StateConfig(this) {} + StatefulCommand(const char* name, const char* id, const char* type, onMessage f) + : Command(name, id, "number", f), StateConfig(this) {} + + void restoreFromState() { + mapRestoreStateTopics.insert({stateTopic, this}); + } + }; + + struct Switch : StatefulCommand { + + Switch(const char* name, const char* id, onMessage f = nullptr) : StatefulCommand(name, id, "switch", f) {} void updateState(bool state) { StateConfig::updateState(state ? "ON" : "OFF"); } - void restoreFromState() { - mapStateTopics.insert({stateTopic, this}); - } - protected: void buildCommandConfig(JsonDocument& jsonDoc) override { StateConfig::buildConfig(jsonDoc); } }; - struct Number : Command, StateConfig { + struct Number : StatefulCommand { unsigned int min, max, step; Number(const char* name, const char* id, unsigned int min, unsigned int max, unsigned int step, onMessage f) - : Command(name, id, "number", f), StateConfig(this), min(min), max(max), step(step) {} + : StatefulCommand(name, id, "number", f), min(min), max(max), step(step) {} void updateState(unsigned int value) { StateConfig::updateState(to_string(value).c_str()); } - void restoreFromState() { - mapStateTopics.insert({stateTopic, this}); - } - protected: void buildCommandConfig(JsonDocument& jsonDoc) override { StateConfig::buildConfig(jsonDoc); @@ -433,7 +435,7 @@ namespace Ha { List Component::components; List AbstractBuilder::builders; - unordered_map Command::mapCommands; + unordered_map Command::mapCommandTopics; unordered_map Sensor::mapSensors; - unordered_map StateConfig::mapStateTopics; + unordered_map StatefulCommand::mapRestoreStateTopics; } diff --git a/src/mqtt.h b/src/mqtt.h index 9f2be03..796aa78 100644 --- a/src/mqtt.h +++ b/src/mqtt.h @@ -47,13 +47,13 @@ namespace Mqtt { memcpy(msg, payload, len); msg[len] = 0; auto strTopic = string{ topic }; - auto cmd = Command::mapCommands[strTopic]; + auto cmd = Command::mapCommandTopics[strTopic]; if (cmd) cmd->onCommand(msg); - cmd = StateConfig::mapStateTopics[strTopic]; + cmd = StatefulCommand::mapRestoreStateTopics[strTopic]; if (cmd) { cmd->onCommand(msg); - StateConfig::mapStateTopics.erase(strTopic); + StatefulCommand::mapRestoreStateTopics.erase(strTopic); } }