use reference when building DeviceConfig

This commit is contained in:
Nicu Hodos 2024-05-19 01:31:41 +02:00
parent 2706b6f1a5
commit 8282af3992
2 changed files with 35 additions and 35 deletions

View File

@ -6,16 +6,16 @@
using namespace Ha;
auto gatewayDevice = DeviceConfig::create(MAIN_DEVICE_ID)->withName("RC Gateway")->withManufacturer("Adafruit")->withModel("Huzzah Esp8266");
auto gatewayDevice = &DeviceConfig::create(MAIN_DEVICE_ID).withName("RC Gateway").withManufacturer("Adafruit").withModel("Huzzah Esp8266");
namespace OilTank {
Sensor* buildRoomSensor(const char* id) {
auto device = DeviceConfig::create(id)
->withName("Oil tank room")
->withManufacturer("Atmel")
->withModel("AtTiny85")
->withArea("Basement")
->withParent(gatewayDevice);
DeviceConfig* device = &DeviceConfig::create(id)
.withName("Oil tank room")
.withManufacturer("Atmel")
.withModel("AtTiny85")
.withArea("Basement")
.withParent(gatewayDevice);
return Builder<TemperatureSensor>::instance(id)
.asDevice(device)
.withValueTemplate("{{ value_json.sensor.temperature }}")
@ -25,12 +25,12 @@ namespace OilTank {
}
Sensor* buildTankSensor(const char* id) {
auto device = DeviceConfig::create(id)
->withName("Oil tank")
->withManufacturer("Arduino")
->withModel("Pro Mini")
->withArea("Basement")
->withParent(gatewayDevice);
DeviceConfig* device = &DeviceConfig::create(id)
.withName("Oil tank")
.withManufacturer("Arduino")
.withModel("Pro Mini")
.withArea("Basement")
.withParent(gatewayDevice);
return Builder<Sensor>::instance(new Sensor{ "Depth", id })
.asDevice(device)
.withDeviceClass("distance")
@ -60,7 +60,7 @@ struct PollinSwitch : Switch {
strcpy(uId, s.c_str());
return uId;
}()), group(group), channel(channel) {
mainDevice = DeviceConfig::create(id)->withName(name)->withManufacturer("Pollin")->withArea(area)->withParent(gatewayDevice);
mainDevice = &DeviceConfig::create(id).withName(name).withManufacturer("Pollin").withArea(area).withParent(gatewayDevice);
deviceClass = "outlet";
p1Switches.insert({ string(id), this });
}
@ -80,7 +80,7 @@ struct EasyHomeSwitch : Switch {
: Switch(name, id) {
memcpy(&this->on[4], on, 4 * sizeof(unsigned long));
memcpy(&this->off[4], off, 4 * sizeof(unsigned long));
mainDevice = DeviceConfig::create(id)->withName(name)->withManufacturer("Intertek")->withModel("Easy Home")->withArea(area)->withParent(gatewayDevice);
mainDevice = &DeviceConfig::create(id).withName(name).withManufacturer("Intertek").withModel("Easy Home").withArea(area).withParent(gatewayDevice);
deviceClass = "outlet";
for (int i = 0; i < 8; i++) {
onSwitches.insert({ this->on[i], this });
@ -107,11 +107,11 @@ Command* commands[] = {
}
})
.asDevice(
DeviceConfig::create("doorbell")
->withName("Doorbell")
->withManufacturer("Thomson")
->withModel("Kinetic Halo")
->withParent(gatewayDevice)
&DeviceConfig::create("doorbell")
.withName("Doorbell")
.withManufacturer("Thomson")
.withModel("Kinetic Halo")
.withParent(gatewayDevice)
)
.build(),
(new EasyHomeSwitch{"FritzBox", "easy_home_a", (unsigned long[4]) { 4483136, 4626800, 4661552, 4819632 }, (unsigned long[4]) { 4326544, 4537104, 4767520, 4972704 }, "Basement"})->withStateTopic(),

View File

@ -16,10 +16,10 @@ namespace Ha {
const char* model = nullptr;
const char* manufacturer = nullptr;
const char* area = nullptr;
DeviceConfig* parent = nullptr;
const DeviceConfig* parent = nullptr;
static DeviceConfig* create(const char* id) {
return new DeviceConfig{ id };
static DeviceConfig& create(const char* id) {
return *(new DeviceConfig{ id });
}
void buildConfig(JsonDocument& jsonDoc) {
@ -33,29 +33,29 @@ namespace Ha {
identifiers.add(id);
}
DeviceConfig* withName(const char* value) {
DeviceConfig& withName(const char* value) {
name = value;
return this;
return *this;
}
DeviceConfig* withModel(const char* value) {
DeviceConfig& withModel(const char* value) {
model = value;
return this;
return *this;
}
DeviceConfig* withManufacturer(const char* value) {
DeviceConfig& withManufacturer(const char* value) {
manufacturer = value;
return this;
return *this;
}
DeviceConfig* withArea(const char* value) {
DeviceConfig& withArea(const char* value) {
area = value;
return this;
return *this;
}
DeviceConfig* withParent(DeviceConfig* deviceConfig) {
DeviceConfig& withParent(const DeviceConfig* deviceConfig) {
parent = deviceConfig;
return this;
return *this;
}
protected:
@ -163,13 +163,13 @@ namespace Ha {
}
Builder& withSecondary(Component* c) {
c->mainDevice = DeviceConfig::create(cmp->id);
c->mainDevice = &DeviceConfig::create(cmp->id);
return *this;
}
Builder& withDiagnostic(Component* c) {
c->entityCategory = "diagnostic";
c->mainDevice = DeviceConfig::create(cmp->id);
c->mainDevice = &DeviceConfig::create(cmp->id);
return *this;
}