diff --git a/gateway/include/devices.h b/gateway/include/devices.h index eb84503..f7fd16d 100644 --- a/gateway/include/devices.h +++ b/gateway/include/devices.h @@ -7,6 +7,7 @@ using namespace Ha; 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 108ef84..a352558 100644 --- a/gateway/include/ha.h +++ b/gateway/include/ha.h @@ -17,7 +17,9 @@ namespace Ha { const char* area = nullptr; DeviceConfig* parent = nullptr; + DeviceConfig() {} DeviceConfig(const char* id, const char* name) : id(id), name(name) {} + DeviceConfig(DeviceConfig& d) : id(d.id), name(d.name), model(d.model), manufacturer(d.manufacturer), area(d.area), parent(d.parent) {} void buildConfig(JsonDocument& jsonDoc) { JsonObject device = jsonDoc.createNestedObject("device"); @@ -94,6 +96,13 @@ namespace Ha { mainDevice = deviceConfig; return static_cast(this); } + + T* copyFromDevice(DeviceConfig* deviceConfig) { + mainDevice = new DeviceConfig{*deviceConfig}; + mainDevice->id = id; + mainDevice->name = name; + return static_cast(this); + } }; List Component::components; @@ -156,17 +165,16 @@ namespace Ha { } }; - struct Sensor : EntityConfig { - const char* deviceClass; - const char* unitMeasure; - const char* valueTemplate; - static constexpr const char* stateTopic = "homeassistant/sensor/rc-gateway/state"; + struct Sensor : EntityConfig, StateConfig { + const char* deviceClass = nullptr; + const char* unitMeasure = nullptr; + const char* valueTemplate = nullptr; Sensor(const char* name, const char* id) : EntityConfig(name, id, "sensor") { } void buildUniqueId(char* uniqueId) override { - sprintf(uniqueId, "id%s", id); + sprintf(uniqueId, "%s", id); } void buildConfig(JsonDocument& jsonDoc) override { @@ -174,13 +182,14 @@ namespace Ha { jsonDoc["device_class"] = deviceClass; jsonDoc["unit_of_measurement"] = unitMeasure; jsonDoc["value_template"] = valueTemplate; - jsonDoc["state_topic"] = stateTopic; + if (stateTopic[0]) jsonDoc["state_topic"] = stateTopic; } }; struct TemperatureSensor : Sensor { TemperatureSensor(const char* name, const char* id) : Sensor(name, id) { + name = "Temperature"; deviceClass = "temperature"; unitMeasure = "°C"; valueTemplate = "{{ value_json.sensor.temperature }}"; @@ -189,6 +198,7 @@ namespace Ha { struct HumiditySensor : Sensor { HumiditySensor(const char* name, const char* id) : Sensor(name, id) { + name = "Humidity"; deviceClass = "humidity"; unitMeasure = "%"; valueTemplate = "{{ value_json.sensor.humidity }}"; @@ -197,6 +207,7 @@ namespace Ha { struct PressureSensor : Sensor { PressureSensor(const char* name, const char* id) : Sensor(name, id) { + name = "Pressure"; deviceClass = "pressure"; unitMeasure = "hPa"; valueTemplate = "{{ value_json.sensor.pressure }}"; diff --git a/gateway/include/mqtt.h b/gateway/include/mqtt.h index 71b7294..4193864 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()->copyFromDevice(atTinyDevice)->withArea("Basement") // new Ha::TemperatureSensor{"Temperature", "temperature"}, // new Ha::HumiditySensor{"Humidity", "humidity"}, // new Ha::PressureSensor{"Pressure", "pressure"},