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; 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 { namespace OilTank {
Sensor* buildRoomSensor(const char* id) { Sensor* buildRoomSensor(const char* id) {
auto device = DeviceConfig::create(id) DeviceConfig* device = &DeviceConfig::create(id)
->withName("Oil tank room") .withName("Oil tank room")
->withManufacturer("Atmel") .withManufacturer("Atmel")
->withModel("AtTiny85") .withModel("AtTiny85")
->withArea("Basement") .withArea("Basement")
->withParent(gatewayDevice); .withParent(gatewayDevice);
return Builder<TemperatureSensor>::instance(id) return Builder<TemperatureSensor>::instance(id)
.asDevice(device) .asDevice(device)
.withValueTemplate("{{ value_json.sensor.temperature }}") .withValueTemplate("{{ value_json.sensor.temperature }}")
@ -25,12 +25,12 @@ namespace OilTank {
} }
Sensor* buildTankSensor(const char* id) { Sensor* buildTankSensor(const char* id) {
auto device = DeviceConfig::create(id) DeviceConfig* device = &DeviceConfig::create(id)
->withName("Oil tank") .withName("Oil tank")
->withManufacturer("Arduino") .withManufacturer("Arduino")
->withModel("Pro Mini") .withModel("Pro Mini")
->withArea("Basement") .withArea("Basement")
->withParent(gatewayDevice); .withParent(gatewayDevice);
return Builder<Sensor>::instance(new Sensor{ "Depth", id }) return Builder<Sensor>::instance(new Sensor{ "Depth", id })
.asDevice(device) .asDevice(device)
.withDeviceClass("distance") .withDeviceClass("distance")
@ -60,7 +60,7 @@ struct PollinSwitch : Switch {
strcpy(uId, s.c_str()); strcpy(uId, s.c_str());
return uId; return uId;
}()), group(group), channel(channel) { }()), 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"; deviceClass = "outlet";
p1Switches.insert({ string(id), this }); p1Switches.insert({ string(id), this });
} }
@ -80,7 +80,7 @@ struct EasyHomeSwitch : Switch {
: Switch(name, id) { : Switch(name, id) {
memcpy(&this->on[4], on, 4 * sizeof(unsigned long)); memcpy(&this->on[4], on, 4 * sizeof(unsigned long));
memcpy(&this->off[4], off, 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"; deviceClass = "outlet";
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
onSwitches.insert({ this->on[i], this }); onSwitches.insert({ this->on[i], this });
@ -107,11 +107,11 @@ Command* commands[] = {
} }
}) })
.asDevice( .asDevice(
DeviceConfig::create("doorbell") &DeviceConfig::create("doorbell")
->withName("Doorbell") .withName("Doorbell")
->withManufacturer("Thomson") .withManufacturer("Thomson")
->withModel("Kinetic Halo") .withModel("Kinetic Halo")
->withParent(gatewayDevice) .withParent(gatewayDevice)
) )
.build(), .build(),
(new EasyHomeSwitch{"FritzBox", "easy_home_a", (unsigned long[4]) { 4483136, 4626800, 4661552, 4819632 }, (unsigned long[4]) { 4326544, 4537104, 4767520, 4972704 }, "Basement"})->withStateTopic(), (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* 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;
static DeviceConfig* create(const char* id) { static DeviceConfig& create(const char* id) {
return new DeviceConfig{ id }; return *(new DeviceConfig{ id });
} }
void buildConfig(JsonDocument& jsonDoc) { void buildConfig(JsonDocument& jsonDoc) {
@ -33,29 +33,29 @@ 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: protected:
@ -163,13 +163,13 @@ namespace Ha {
} }
Builder& withSecondary(Component* c) { Builder& withSecondary(Component* c) {
c->mainDevice = DeviceConfig::create(cmp->id); c->mainDevice = &DeviceConfig::create(cmp->id);
return *this; return *this;
} }
Builder& withDiagnostic(Component* c) { Builder& withDiagnostic(Component* c) {
c->entityCategory = "diagnostic"; c->entityCategory = "diagnostic";
c->mainDevice = DeviceConfig::create(cmp->id); c->mainDevice = &DeviceConfig::create(cmp->id);
return *this; return *this;
} }