allow creation of DeviceConfig only with factory method

This commit is contained in:
Nicu Hodos 2024-05-18 22:37:58 +02:00
parent e9c404a4d5
commit 2706b6f1a5
2 changed files with 8 additions and 7 deletions

View File

@ -6,7 +6,7 @@
using namespace Ha; using namespace Ha;
DeviceConfig* gatewayDevice = (new DeviceConfig{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) {
@ -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 = (new DeviceConfig{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 = (new DeviceConfig{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 });

View File

@ -18,8 +18,6 @@ namespace Ha {
const char* area = nullptr; const char* area = nullptr;
DeviceConfig* parent = nullptr; DeviceConfig* parent = nullptr;
DeviceConfig(const char* id) : id(id) {}
static DeviceConfig* create(const char* id) { static DeviceConfig* create(const char* id) {
return new DeviceConfig{ id }; return new DeviceConfig{ id };
} }
@ -59,6 +57,9 @@ namespace Ha {
parent = deviceConfig; parent = deviceConfig;
return this; return this;
} }
protected:
DeviceConfig(const char* id) : id(id) {}
}; };
struct Component { struct Component {
@ -162,13 +163,13 @@ namespace Ha {
} }
Builder& withSecondary(Component* c) { Builder& withSecondary(Component* c) {
c->mainDevice = new DeviceConfig{ 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 = new DeviceConfig{ cmp->id }; c->mainDevice = DeviceConfig::create(cmp->id);
return *this; return *this;
} }