From e67ce3b33cffdcfacd325e9e6dfbe54a5551601d Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Thu, 2 May 2024 21:24:41 +0200 Subject: [PATCH] extract StateConfig into a dedicated class --- gateway/include/ha.h | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/gateway/include/ha.h b/gateway/include/ha.h index 959d790..54e41e0 100644 --- a/gateway/include/ha.h +++ b/gateway/include/ha.h @@ -75,9 +75,18 @@ namespace Ha { }; - struct Switch : Command { - static constexpr const char* type = "switch"; + template + struct StateConfig { char stateTopic[TOPIC_SIZE]; + + T* withStateTopic() { + sprintf(stateTopic, "homeassistant/%s/rc-gateway/%s/state", ((T*)this)->type, ((T*)this)->id); + return (T*)this; + } + }; + + struct Switch : Command, StateConfig { + static constexpr const char* type = "switch"; const char* area; virtual void onCommand(const char* msg){} @@ -105,11 +114,6 @@ namespace Ha { if (stateTopic[0]) jsonDoc["state_topic"] = stateTopic; } - Switch* withStateTopic() { - sprintf(stateTopic, "homeassistant/%s/rc-gateway/%s/state", type, id); - return this; - } - Switch* withArea(const char* area) { this->area = area; return this;