Merge branch 'doorbell' into huzzah

This commit is contained in:
Nicu Hodos 2024-05-19 18:11:28 +02:00
commit 8d893645fe

View File

@ -16,12 +16,10 @@ namespace Ha {
const char* model = nullptr; const char* model = nullptr;
const char* manufacturer = nullptr; const char* manufacturer = nullptr;
const char* area = nullptr; const char* area = nullptr;
DeviceConfig* parent = nullptr; const DeviceConfig* parent = nullptr;
DeviceConfig(const char* id) : id(id) {} static DeviceConfig& create(const char* id) {
return *(new DeviceConfig{ id });
static DeviceConfig* create(const char* id) {
return new DeviceConfig{ id };
} }
void buildConfig(JsonDocument& jsonDoc) { void buildConfig(JsonDocument& jsonDoc) {
@ -35,30 +33,33 @@ namespace Ha {
identifiers.add(id); identifiers.add(id);
} }
DeviceConfig* withName(const char* value) { DeviceConfig& withName(const char* value) {
name = value; name = value;
return this; return *this;
} }
DeviceConfig* withModel(const char* value) { DeviceConfig& withModel(const char* value) {
model = value; model = value;
return this; return *this;
} }
DeviceConfig* withManufacturer(const char* value) { DeviceConfig& withManufacturer(const char* value) {
manufacturer = value; manufacturer = value;
return this; return *this;
} }
DeviceConfig* withArea(const char* value) { DeviceConfig& withArea(const char* value) {
area = value; area = value;
return this; return *this;
} }
DeviceConfig* withParent(DeviceConfig* deviceConfig) { DeviceConfig& withParent(const DeviceConfig* deviceConfig) {
parent = deviceConfig; parent = deviceConfig;
return this; return *this;
} }
protected:
DeviceConfig(const char* id) : id(id) {}
}; };
struct Component { struct Component {
@ -108,7 +109,6 @@ namespace Ha {
publisher(configTopic, ""); publisher(configTopic, "");
} }
}; };
List<Component> Component::components;
struct AbstractBuilder { struct AbstractBuilder {
static List<AbstractBuilder> builders; static List<AbstractBuilder> builders;
@ -122,7 +122,6 @@ namespace Ha {
builders.empty(); builders.empty();
} }
}; };
List<AbstractBuilder> AbstractBuilder::builders;
template <class T> template <class T>
struct Builder : AbstractBuilder { struct Builder : AbstractBuilder {
@ -161,15 +160,14 @@ namespace Ha {
return *this; return *this;
} }
Builder& withSecondary(Component* c) { Builder& asSecondary(Component* c) {
c->mainDevice = new DeviceConfig{ cmp->id }; c->mainDevice = &DeviceConfig::create(cmp->id);
return *this; return *this;
} }
Builder& withDiagnostic(Component* c) { Builder& asDiagnostic(Component* c) {
c->entityCategory = "diagnostic"; c->entityCategory = "diagnostic";
c->mainDevice = new DeviceConfig{ cmp->id }; return asSecondary(c);
return *this;
} }
Builder& asDevice(DeviceConfig* deviceConfig) { Builder& asDevice(DeviceConfig* deviceConfig) {
@ -198,7 +196,6 @@ namespace Ha {
} }
}; };
unordered_map<string, Command*> Command::mapCommands;
struct Button : Command { struct Button : Command {
@ -227,7 +224,7 @@ namespace Ha {
if (stateTopic[0]) jsonDoc["state_topic"] = stateTopic; if (stateTopic[0]) jsonDoc["state_topic"] = stateTopic;
} }
void publishState(bool state) { void updateState(bool state) {
publisher(stateTopic, state ? "ON" : "OFF"); publisher(stateTopic, state ? "ON" : "OFF");
} }
}; };
@ -235,13 +232,11 @@ namespace Ha {
struct Sensor : Component, StateConfig<Sensor> { struct Sensor : Component, StateConfig<Sensor> {
const char* unitMeasure = nullptr; const char* unitMeasure = nullptr;
const char* valueTemplate = nullptr; const char* valueTemplate = nullptr;
static unordered_map<string, Sensor*> mapSensors;
Sensor() : Component(name, id, "sensor") {
withStateTopic();
}
Sensor(const char* name, const char* id) : Component(name, id, "sensor") { Sensor(const char* name, const char* id) : Component(name, id, "sensor") {
withStateTopic(); withStateTopic();
mapSensors.insert({ id, this });
} }
void buildUniqueId(char* uniqueId) override { void buildUniqueId(char* uniqueId) override {
@ -267,6 +262,10 @@ namespace Ha {
jsonDoc["state_topic"] = stateTopic; jsonDoc["state_topic"] = stateTopic;
jsonDoc["suggested_display_precision"] = 2; jsonDoc["suggested_display_precision"] = 2;
} }
void updateState(const char* message) {
publisher(stateTopic, message);
}
}; };
struct TemperatureSensor : Sensor { struct TemperatureSensor : Sensor {
@ -307,4 +306,9 @@ namespace Ha {
// valueTemplate = "{{ value_json.sensor.pressure }}"; // valueTemplate = "{{ value_json.sensor.pressure }}";
} }
}; };
List<Component> Component::components;
List<AbstractBuilder> AbstractBuilder::builders;
unordered_map<string, Command*> Command::mapCommands;
unordered_map<string, Sensor*> Sensor::mapSensors;
} }