From 6793b2da35dcebb94412525a969c12b1a5ff399b Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Fri, 3 May 2024 09:17:34 +0200 Subject: [PATCH] first draft - needs copy constructor --- gateway/include/devices.h | 3 ++- gateway/include/ha.h | 14 +++++++++++--- gateway/include/mqtt.h | 6 +++++- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/gateway/include/devices.h b/gateway/include/devices.h index be73e32..86b9afb 100644 --- a/gateway/include/devices.h +++ b/gateway/include/devices.h @@ -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; diff --git a/gateway/include/ha.h b/gateway/include/ha.h index ed42855..562b638 100644 --- a/gateway/include/ha.h +++ b/gateway/include/ha.h @@ -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(this); } + + T* asSelfDevice(DeviceConfig* deviceConfig) { + mainDevice = deviceConfig; + mainDevice->id = id; + mainDevice->name = name; + return static_cast(this); + } }; List Component::components; @@ -158,11 +166,11 @@ namespace Ha { } }; - struct Sensor : EntityConfig { + struct Sensor : EntityConfig, StateConfig { + 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; } }; diff --git a/gateway/include/mqtt.h b/gateway/include/mqtt.h index 9c80fc7..dc193a2 100644 --- a/gateway/include/mqtt.h +++ b/gateway/include/mqtt.h @@ -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::Container* c = Ha::Component::components.first; c; c = c->next) { publishComponentConfig(*c->t); } + for (Ha::Component* cmp : sensors) { + publishComponentConfig(*cmp); + } ts.deleteTask(tPublishInit); }