use Builder instead of EntityConfig

This commit is contained in:
Nicu Hodos 2024-05-15 00:53:38 +02:00
parent af5be1190a
commit 6ccdd24e62
2 changed files with 58 additions and 31 deletions

View File

@ -93,31 +93,56 @@ namespace Ha {
jsonDoc["unique_id"] = uniqueId;
}
};
template <class T>
struct EntityConfig : Component {
EntityConfig(const char* name, const char* id, const char* type) : Component(name, id, type) {
}
T* asDevice(DeviceConfig* deviceConfig) {
mainDevice = deviceConfig;
return static_cast<T*>(this);
}
T* ofDevice(const char* id) {
mainDevice = new DeviceConfig{id};
return static_cast<T*>(this);
}
};
List<Component> Component::components;
template <class T>
struct Command : EntityConfig<T> {
struct Builder {
T* cmp;
Builder() {}
Builder(T* cmp) : cmp(cmp) {}
Builder(const char* id) {
cmp = new T{id};
}
static Builder& instance() {
return *(new Builder<T>());
}
static Builder& instance(T* c) {
return *(new Builder<T>(c));
}
static Builder& instance(const char* id) {
return *(new Builder<T>(id));
}
T* build() {
return static_cast<T*>(cmp);
}
Builder& withSecondary(Component* c) {
c->mainDevice = new DeviceConfig{ cmp->id };
return *this;
}
Builder& withDiagnostic(Component* c) {
c->entityCategory = "diagnostic";
c->mainDevice = new DeviceConfig{ cmp->id };
return *this;
}
Builder& asDevice(DeviceConfig* deviceConfig) {
cmp->mainDevice = deviceConfig;
return *this;
}
};
struct Command : Component {
char commandTopic[TOPIC_SIZE];
Command(const char* name, const char* id, const char* type) : EntityConfig<T>(name, id, type) {
Command(const char* name, const char* id, const char* type) : Component(name, id, type) {
sprintf(commandTopic, "homeassistant/%s/%s/%s", type, MAIN_DEVICE_ID, id);
}
@ -126,17 +151,17 @@ namespace Ha {
}
void buildConfig(JsonDocument& jsonDoc) override {
EntityConfig<T>::buildConfig(jsonDoc);
Component::buildConfig(jsonDoc);
jsonDoc["command_topic"] = commandTopic;
}
};
typedef void (*onMessage)(const char* msg);
struct Button : Command<Button> {
struct Button : Command {
onMessage f;
Button(const char* name, const char* id, onMessage f) : Command(name, id, "button"), f(f) {}
Button(const char* name, const char* id, onMessage f = nullptr) : Command(name, id, "button"), f(f) {}
};
@ -150,7 +175,7 @@ namespace Ha {
}
};
struct Switch : Command<Switch>, StateConfig<Switch> {
struct Switch : Command, StateConfig<Switch> {
virtual void onCommand(const char* msg){}
@ -171,11 +196,15 @@ namespace Ha {
}
};
struct Sensor : EntityConfig<Sensor>, StateConfig<Sensor> {
struct Sensor : Component, StateConfig<Sensor> {
const char* unitMeasure = nullptr;
const char* valueTemplate = nullptr;
Sensor(const char* name, const char* id) : EntityConfig(name, Protocol_2::buildId(id), "sensor") {
Sensor() : Component(name, Protocol_2::buildId(id), "sensor") {
withStateTopic();
}
Sensor(const char* name, const char* id) : Component(name, Protocol_2::buildId(id), "sensor") {
withStateTopic();
}
@ -184,7 +213,7 @@ namespace Ha {
}
void buildConfig(JsonDocument& jsonDoc) override {
EntityConfig::buildConfig(jsonDoc);
Component::buildConfig(jsonDoc);
if (unitMeasure) jsonDoc["unit_of_measurement"] = unitMeasure;
if (valueTemplate) jsonDoc["value_template"] = valueTemplate;
jsonDoc["state_topic"] = stateTopic;
@ -203,7 +232,6 @@ namespace Ha {
struct VoltageSensor : Sensor {
VoltageSensor(const char* id, const char* name, const char* valueTemplate) : Sensor(name, id) {
this->valueTemplate = valueTemplate;
entityCategory = "diagnostic";
deviceClass = "voltage";
unitMeasure = "V";
}
@ -212,7 +240,6 @@ namespace Ha {
struct BatterySensor : Sensor {
BatterySensor(const char* id, const char* name, const char* valueTemplate) : Sensor(name, id) {
this->valueTemplate = valueTemplate;
entityCategory = "diagnostic";
deviceClass = "battery";
unitMeasure = "%";
}

View File

@ -31,12 +31,12 @@ namespace Mqtt {
return client.publish(topic, 0, true, message);
}
Ha::Button* buttons[] = {
(new Ha::Button{"Restart", "restart",
Button* buttons[] = {
Builder<Button>::instance(new Button{"Restart", "restart",
[](const char* msg) {
if (String { "PRESS" }.equals(msg)) ESP.restart();
}
})->asDevice(gatewayDevice)
}).asDevice(gatewayDevice).build()
};
Ha::Switch* switches[] = {