183 lines
6.0 KiB
C++
183 lines
6.0 KiB
C++
#pragma once
|
|
|
|
#include <ArduinoJson.h>
|
|
#include "display.h"
|
|
|
|
#define JSON_SIZE 512
|
|
#define TOPIC_SIZE 255
|
|
|
|
namespace Ha {
|
|
|
|
struct Component {
|
|
const char* name;
|
|
const char* id;
|
|
const char* type;
|
|
char configTopic[TOPIC_SIZE];
|
|
|
|
Component(const char* name, const char* id, const char* type) {
|
|
this->name = name;
|
|
this->id = id;
|
|
this->type = type;
|
|
sprintf(configTopic, "homeassistant/%s/esp_clock/%s/config", type, id);
|
|
}
|
|
|
|
virtual void buildUniqueId(char* uniqueId) = 0;
|
|
|
|
virtual void buildConfig(JsonDocument& jsonDoc) {
|
|
buildDeviceConfig(jsonDoc);
|
|
jsonDoc["name"] = name;
|
|
char uniqueId[50];
|
|
buildUniqueId(uniqueId);
|
|
jsonDoc["unique_id"] = uniqueId;
|
|
}
|
|
|
|
virtual void buildDeviceConfig(JsonDocument& jsonDoc) {
|
|
JsonObject device = jsonDoc.createNestedObject("device");
|
|
device["name"] = "ESP Clock";
|
|
device["model"] = "Esp8266 D1 Mini";
|
|
device["manufacturer"] = "Espressif";
|
|
device["suggested_area"] = "Living room";
|
|
JsonArray connections = device.createNestedArray("connections");
|
|
JsonArray mac = connections.createNestedArray();
|
|
mac.add("mac");
|
|
mac.add(WiFi.macAddress());
|
|
JsonArray identifiers = device.createNestedArray("identifiers");
|
|
identifiers.add("esp-clock");
|
|
}
|
|
};
|
|
|
|
typedef void (*onMessage)(const char* msg);
|
|
struct Command : Component {
|
|
char commandTopic[TOPIC_SIZE];
|
|
char stateTopic[TOPIC_SIZE];
|
|
onMessage f;
|
|
void (*publishState)();
|
|
|
|
Command(const char* name, const char* id, const char* type, onMessage f, void (*publishState)() = nullptr) : Component(name, id, type) {
|
|
this->f = f;
|
|
this->publishState = publishState;
|
|
}
|
|
|
|
void buildUniqueId(char* uniqueId) override {
|
|
sprintf(uniqueId, "esp_clock_%s", id);
|
|
}
|
|
|
|
void buildConfig(JsonDocument& jsonDoc) override {
|
|
Component::buildConfig(jsonDoc);
|
|
jsonDoc["command_topic"] = commandTopic;
|
|
if (stateTopic[0]) jsonDoc["state_topic"] = stateTopic;
|
|
}
|
|
|
|
Command* withStateTopic() {
|
|
sprintf(stateTopic, "homeassistant/%s/esp_clock/%s/state", type, id);
|
|
return this;
|
|
}
|
|
|
|
};
|
|
|
|
struct Button : Command {
|
|
static constexpr const char* type = "button";
|
|
|
|
Button(const char* name, const char* id, onMessage f) : Command(name, id, type, f) {
|
|
sprintf(commandTopic, "homeassistant/%s/esp_clock/%s", type, id);
|
|
}
|
|
|
|
};
|
|
|
|
struct Switch : Command {
|
|
static constexpr const char* type = "switch";
|
|
|
|
Switch(const char* name, const char* id, onMessage f, void (*publishState)()) : Command(name, id, type, f, publishState) {
|
|
sprintf(commandTopic, "homeassistant/%s/esp_clock/%s/set", type, id);
|
|
}
|
|
|
|
void buildConfig(JsonDocument& jsonDoc) override {
|
|
Command::buildConfig(jsonDoc);
|
|
jsonDoc["retain"] = true;
|
|
}
|
|
|
|
};
|
|
|
|
struct Brightness : Command {
|
|
static constexpr const char* type = "number";
|
|
|
|
Brightness(const char* name, const char* id, onMessage f, void (*publishState)()) : Command(name, id, type, f, publishState) {
|
|
sprintf(commandTopic, "homeassistant/%s/esp_clock/%s", type, id);
|
|
}
|
|
|
|
void buildConfig(JsonDocument& jsonDoc) override {
|
|
Command::buildConfig(jsonDoc);
|
|
jsonDoc["retain"] = true;
|
|
jsonDoc["min"] = BRIGHTNESS_MIN;
|
|
jsonDoc["max"] = BRIGHTNESS_MAX;
|
|
jsonDoc["step"] = BRIGHTNESS_STEP;
|
|
}
|
|
|
|
};
|
|
|
|
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* id) : Component(name, id, "sensor") {
|
|
}
|
|
|
|
void buildUniqueId(char* uniqueId) override {
|
|
sprintf(uniqueId, "living_room_%s", id);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
void buildDeviceConfig(JsonDocument& jsonDoc) override {
|
|
JsonObject device = jsonDoc.createNestedObject("device");
|
|
device["name"] = "Living room";
|
|
device["model"] = "BPM280";
|
|
device["suggested_area"] = "Living room";
|
|
device["via_device"] = "esp-clock";
|
|
JsonArray identifiers = device.createNestedArray("identifiers");
|
|
identifiers.add("esp-clock-living-room");
|
|
}
|
|
|
|
};
|
|
|
|
struct TemperatureSensor : Sensor {
|
|
TemperatureSensor(const char* name, const char* id) : Sensor(name, id) {
|
|
deviceClass = "temperature";
|
|
unitMeasure = "°C";
|
|
valueTemplate = "{{ value_json.temperature }}";
|
|
}
|
|
};
|
|
|
|
struct HumiditySensor : Sensor {
|
|
HumiditySensor(const char* name, const char* id) : Sensor(name, id) {
|
|
deviceClass = "humidity";
|
|
unitMeasure = "%";
|
|
valueTemplate = "{{ value_json.humidity }}";
|
|
}
|
|
};
|
|
|
|
struct PressureSensor : Sensor {
|
|
PressureSensor(const char* name, const char* id) : Sensor(name, id) {
|
|
deviceClass = "pressure";
|
|
unitMeasure = "hPa";
|
|
valueTemplate = "{{ value_json.pressure }}";
|
|
}
|
|
};
|
|
|
|
struct AltitudeSensor : Sensor {
|
|
AltitudeSensor(const char* name, const char* id) : Sensor(name, id) {
|
|
deviceClass = "distance";
|
|
unitMeasure = "m";
|
|
valueTemplate = "{{ value_json.altitude }}";
|
|
}
|
|
};
|
|
}
|