first draft - needs copy constructor

This commit is contained in:
Nicu Hodos 2024-05-03 09:17:34 +02:00
parent 6357ad1436
commit 6793b2da35
3 changed files with 18 additions and 5 deletions

View File

@ -6,7 +6,8 @@
using namespace Ha;
DeviceConfig* gatewayConfig = (new DeviceConfig{MAIN_DEVICE_ID, "RC Gateway"})->withManufacturer("Adafruit")->withModel("Huzzah Esp8266");
DeviceConfig* gatewayDevice = (new DeviceConfig{MAIN_DEVICE_ID, "RC Gateway"})->withManufacturer("Adafruit")->withModel("Huzzah Esp8266");
DeviceConfig* atTinyDevice = (new DeviceConfig{})->withManufacturer("Atmel")->withModel("AtTiny85")->withParent(gatewayDevice);
struct PollinSwitch : Switch {
const char* group;

View File

@ -17,6 +17,7 @@ namespace Ha {
const char* area;
DeviceConfig* parent = nullptr;
DeviceConfig() {}
DeviceConfig(const char* id, const char* name) : id(id), name(name) {}
void buildConfig(JsonDocument& jsonDoc) {
@ -94,6 +95,13 @@ namespace Ha {
mainDevice = deviceConfig;
return static_cast<T*>(this);
}
T* asSelfDevice(DeviceConfig* deviceConfig) {
mainDevice = deviceConfig;
mainDevice->id = id;
mainDevice->name = name;
return static_cast<T*>(this);
}
};
List<Component> Component::components;
@ -158,11 +166,11 @@ namespace Ha {
}
};
struct Sensor : EntityConfig<Sensor> {
struct Sensor : EntityConfig<Sensor>, StateConfig<Sensor> {
static constexpr const char* type = "sensor";
const char* deviceClass;
const char* unitMeasure;
const char* valueTemplate;
static constexpr const char* stateTopic = "homeassistant/sensor/rc-gateway/state";
Sensor(const char* name, const char* id) : EntityConfig(name, id, "sensor") {
}
@ -173,10 +181,10 @@ namespace Ha {
void buildConfig(JsonDocument& jsonDoc) override {
EntityConfig::buildConfig(jsonDoc);
jsonDoc["name"] = "Temperature";
jsonDoc["device_class"] = deviceClass;
jsonDoc["unit_of_measurement"] = unitMeasure;
jsonDoc["value_template"] = valueTemplate;
jsonDoc["state_topic"] = stateTopic;
}
};

View File

@ -33,6 +33,7 @@ namespace Mqtt {
}
Ha::Sensor* sensors[] = {
(new Ha::TemperatureSensor{"Oil tank room1", "id4"})->withStateTopic()->asSelfDevice(atTinyDevice)
// new Ha::TemperatureSensor{"Temperature", "temperature"},
// new Ha::HumiditySensor{"Humidity", "humidity"},
// new Ha::PressureSensor{"Pressure", "pressure"},
@ -44,7 +45,7 @@ namespace Mqtt {
[](const char* msg) {
if (String { "PRESS" }.equals(msg)) ESP.restart();
}
})->asDevice(gatewayConfig)
})->asDevice(gatewayDevice)
};
Ha::Switch* switches[] = {
@ -71,6 +72,9 @@ namespace Mqtt {
for (List<Ha::Component>::Container* c = Ha::Component::components.first; c; c = c->next) {
publishComponentConfig(*c->t);
}
for (Ha::Component* cmp : sensors) {
publishComponentConfig(*cmp);
}
ts.deleteTask(tPublishInit);
}