113 lines
3.8 KiB
C++

#include <ArduinoJson.h>
#define JSON_SIZE 512
#define TOPIC_SIZE 255
namespace Ha {
struct Component {
const char* name;
const char* uniqueId;
char configTopic[TOPIC_SIZE];
Component(const char* name, const char* uniqueId, const char* topic, const char* type) {
this->name = name;
this->uniqueId = uniqueId;
sprintf(configTopic, "homeassistant/%s/esp_clock/%s/config", type, topic);
}
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());
}
};
typedef void (*onMessage)(const char* msg);
struct Command : Component {
char commandTopic[TOPIC_SIZE];
onMessage f;
Command(const char* name, const char* uniqueId, const char* topic, const char* type, onMessage f) : Component(name, uniqueId, topic, type) {
this->f = f;
}
void buildConfig(JsonDocument& jsonDoc) override {
Component::buildConfig(jsonDoc);
jsonDoc["command_topic"] = commandTopic;
}
};
struct Button : Command {
static constexpr const char* type = "button";
Button(const char* name, const char* uniqueId, const char* topic, onMessage f) : Command(name, uniqueId, topic, type, f) {
sprintf(commandTopic, "homeassistant/%s/esp_clock/%s", type, topic);
}
};
struct Switch : Command {
static constexpr const char* type = "switch";
Switch(const char* name, const char* uniqueId, const char* topic, onMessage f) : Command(name, uniqueId, topic, type, f) {
sprintf(commandTopic, "homeassistant/%s/esp_clock/%s/set", type, topic);
}
};
struct Sensor : Component {
const char* deviceClass;
const char* unitMeasure;
const char* valueTemplate;
static constexpr const char* stateTopic = "homeassistant/sensor/esp_clock/state";
Sensor(const char* name, const char* uniqueId, const char* topic) : Component(name, uniqueId, topic, "sensor") {
}
void buildConfig(JsonDocument& jsonDoc) override {
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* topic) : Sensor(name, uniqueId, topic) {
deviceClass = "temperature";
unitMeasure = "°C";
valueTemplate = "{{ value_json.temperature }}";
}
};
struct PressureSensor : Sensor {
PressureSensor(const char* name, const char* uniqueId, const char* topic) : Sensor(name, uniqueId, topic) {
deviceClass = "pressure";
unitMeasure = "hPa";
valueTemplate = "{{ value_json.pressure }}";
}
};
struct AltitudeSensor : Sensor {
AltitudeSensor(const char* name, const char* uniqueId, const char* topic) : Sensor(name, uniqueId, topic) {
deviceClass = "distance";
unitMeasure = "m";
valueTemplate = "{{ value_json.altitude }}";
}
};
}