93 lines
3.1 KiB
C++
93 lines
3.1 KiB
C++
#include <ArduinoJson.h>
|
|
|
|
#define JSON_SIZE 512
|
|
|
|
namespace Ha {
|
|
|
|
struct Component {
|
|
const char* name;
|
|
const char* uniqueId;
|
|
const char* configTopic;
|
|
|
|
Component(const char* name, const char* uniqueId, const char* configTopic) {
|
|
this->name = name;
|
|
this->uniqueId = uniqueId;
|
|
this->configTopic = configTopic;
|
|
}
|
|
|
|
virtual void buildConfig(JsonDocument& jsonDoc) {
|
|
buildDeviceConfig(jsonDoc);
|
|
jsonDoc["name"] = name;
|
|
jsonDoc["unique_id"] = uniqueId;
|
|
}
|
|
|
|
void buildDeviceConfig(JsonDocument& jsonDoc) {
|
|
JsonObject device = jsonDoc.createNestedObject("device");
|
|
device["name"] = "ESP Clock";
|
|
device["suggested_area"] = "Living room";
|
|
JsonArray connections = device.createNestedArray("connections");
|
|
JsonArray mac = connections.createNestedArray();
|
|
mac.add("mac");
|
|
mac.add(WiFi.macAddress());
|
|
}
|
|
};
|
|
|
|
struct Command : Component {
|
|
const char* commandTopic;
|
|
|
|
Command(const char* name, const char* uniqueId, const char* configTopic, const char* commandTopic) : Component(name, uniqueId, configTopic) {
|
|
this->commandTopic = commandTopic;
|
|
}
|
|
|
|
void buildConfig(JsonDocument& jsonDoc) {
|
|
Component::buildConfig(jsonDoc);
|
|
jsonDoc["command_topic"] = commandTopic;
|
|
}
|
|
|
|
};
|
|
|
|
struct Sensor : Component {
|
|
const char* deviceClass;
|
|
const char* unitMeasure;
|
|
const char* valueTemplate;
|
|
const char* stateTopic;
|
|
|
|
Sensor(const char* name, const char* uniqueId, const char* configTopic, const char* stateTopic) : Component(name, uniqueId, configTopic) {
|
|
this->stateTopic = stateTopic;
|
|
}
|
|
|
|
void buildConfig(JsonDocument& jsonDoc) {
|
|
Component::buildConfig(jsonDoc);
|
|
jsonDoc["device_class"] = deviceClass;
|
|
jsonDoc["unit_of_measurement"] = unitMeasure;
|
|
jsonDoc["value_template"] = valueTemplate;
|
|
jsonDoc["state_topic"] = stateTopic;
|
|
}
|
|
|
|
};
|
|
|
|
struct TemperatureSensor : Sensor {
|
|
TemperatureSensor(const char* name, const char* uniqueId, const char* configTopic, const char* stateTopic) : Sensor(name, uniqueId, configTopic, stateTopic) {
|
|
deviceClass = "temperature";
|
|
unitMeasure = "°C";
|
|
valueTemplate = "{{ value_json.temperature }}";
|
|
}
|
|
};
|
|
|
|
struct PressureSensor : Sensor {
|
|
PressureSensor(const char* name, const char* uniqueId, const char* configTopic, const char* stateTopic) : Sensor(name, uniqueId, configTopic, stateTopic) {
|
|
deviceClass = "pressure";
|
|
unitMeasure = "hPa";
|
|
valueTemplate = "{{ value_json.pressure }}";
|
|
}
|
|
};
|
|
|
|
struct AltitudeSensor : Sensor {
|
|
AltitudeSensor(const char* name, const char* uniqueId, const char* configTopic, const char* stateTopic) : Sensor(name, uniqueId, configTopic, stateTopic) {
|
|
deviceClass = "distance";
|
|
unitMeasure = "m";
|
|
valueTemplate = "{{ value_json.altitude }}";
|
|
}
|
|
};
|
|
}
|