225 lines
7.1 KiB
C++
225 lines
7.1 KiB
C++
#pragma once
|
|
|
|
#include <ArduinoJson.h>
|
|
#include "utils.h"
|
|
|
|
#define JSON_SIZE 512
|
|
#define TOPIC_SIZE 255
|
|
|
|
namespace Ha {
|
|
uint16_t (*publisher)(const char* topic, const char* message);
|
|
|
|
struct DeviceConfig {
|
|
const char* id = nullptr;
|
|
const char* name = nullptr;
|
|
const char* model = nullptr;
|
|
const char* manufacturer = nullptr;
|
|
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");
|
|
device["name"] = name;
|
|
// JsonArray connections = device.createNestedArray("connections");
|
|
// JsonArray mac = connections.createNestedArray();
|
|
// mac.add("mac");
|
|
// mac.add(WiFi.macAddress());
|
|
JsonArray identifiers = device.createNestedArray("identifiers");
|
|
identifiers.add(id);
|
|
if (model) device["model"] = model;
|
|
if (manufacturer) device["manufacturer"] = manufacturer;
|
|
if (area) device["suggested_area"] = area;
|
|
if (parent) device["via_device"] = parent->id;
|
|
}
|
|
|
|
DeviceConfig* withModel(const char* value) {
|
|
model = value;
|
|
return this;
|
|
}
|
|
|
|
DeviceConfig* withManufacturer(const char* value) {
|
|
manufacturer = value;
|
|
return this;
|
|
}
|
|
|
|
DeviceConfig* withArea(const char* value) {
|
|
area = value;
|
|
return this;
|
|
}
|
|
|
|
DeviceConfig* withParent(DeviceConfig* deviceConfig) {
|
|
parent = deviceConfig;
|
|
return this;
|
|
}
|
|
};
|
|
|
|
struct Component {
|
|
const char* name = nullptr;
|
|
char* id = nullptr;
|
|
const char* type = nullptr;
|
|
char configTopic[TOPIC_SIZE];
|
|
DeviceConfig* mainDevice = nullptr;
|
|
static List<Component> components;
|
|
|
|
Component(const char* name, const char* id, const char* type) : name(name), id((char*)id), type(type) {
|
|
sprintf(configTopic, "homeassistant/%s/%s/%s/config", type, MAIN_DEVICE_ID, id);
|
|
components.add(this);
|
|
}
|
|
|
|
virtual void buildUniqueId(char* uniqueId) = 0;
|
|
|
|
virtual void buildConfig(JsonDocument& jsonDoc) {
|
|
if (mainDevice) mainDevice->buildConfig(jsonDoc);
|
|
jsonDoc["name"] = name;
|
|
char uniqueId[50];
|
|
buildUniqueId(uniqueId);
|
|
jsonDoc["unique_id"] = uniqueId;
|
|
}
|
|
};
|
|
|
|
template <class T>
|
|
struct EntityConfig : Component {
|
|
|
|
EntityConfig(const char* name, const char* id, const char* type) : Component(name, id, type) {
|
|
}
|
|
|
|
T* withArea(const char* value) {
|
|
if (mainDevice) mainDevice->withArea(value);
|
|
return static_cast<T*>(this);
|
|
}
|
|
|
|
T* asDevice(DeviceConfig* deviceConfig) {
|
|
mainDevice = deviceConfig;
|
|
return static_cast<T*>(this);
|
|
}
|
|
|
|
T* copyFromDevice(DeviceConfig* deviceConfig) {
|
|
mainDevice = new DeviceConfig{*deviceConfig};
|
|
mainDevice->id = id;
|
|
mainDevice->name = name;
|
|
return static_cast<T*>(this);
|
|
}
|
|
};
|
|
|
|
List<Component> Component::components;
|
|
|
|
template <class T>
|
|
struct Command : EntityConfig<T> {
|
|
char commandTopic[TOPIC_SIZE];
|
|
|
|
Command(const char* name, const char* id, const char* type) : EntityConfig<T>(name, id, type) {
|
|
sprintf(commandTopic, "homeassistant/%s/%s/%s", type, MAIN_DEVICE_ID, id);
|
|
}
|
|
|
|
void buildUniqueId(char* uniqueId) override {
|
|
sprintf(uniqueId, "%s_%s", MAIN_DEVICE_ID, this->id);
|
|
}
|
|
|
|
void buildConfig(JsonDocument& jsonDoc) override {
|
|
EntityConfig<T>::buildConfig(jsonDoc);
|
|
jsonDoc["command_topic"] = commandTopic;
|
|
}
|
|
|
|
};
|
|
|
|
typedef void (*onMessage)(const char* msg);
|
|
struct Button : Command<Button> {
|
|
onMessage f;
|
|
|
|
Button(const char* name, const char* id, onMessage f) : Command(name, id, "button"), f(f) {}
|
|
|
|
};
|
|
|
|
template <class T>
|
|
struct StateConfig {
|
|
char stateTopic[TOPIC_SIZE];
|
|
|
|
T* withStateTopic() {
|
|
sprintf(stateTopic, "homeassistant/%s/%s/%s/state", ((T*)this)->type, MAIN_DEVICE_ID, ((T*)this)->id);
|
|
return static_cast<T*>(this);
|
|
}
|
|
};
|
|
|
|
struct Switch : Command<Switch>, StateConfig<Switch> {
|
|
|
|
virtual void onCommand(const char* msg){}
|
|
|
|
Switch(const char* name, const char* id) : Command(name, id, "switch") {
|
|
sprintf(commandTopic, "homeassistant/%s/%s/%s/set", type, MAIN_DEVICE_ID, id);
|
|
}
|
|
|
|
void buildConfig(JsonDocument& jsonDoc) override {
|
|
Command::buildConfig(jsonDoc);
|
|
jsonDoc["name"] = nullptr;
|
|
jsonDoc["device_class"] = "outlet";
|
|
jsonDoc["retain"] = true;
|
|
if (stateTopic[0]) jsonDoc["state_topic"] = stateTopic;
|
|
}
|
|
|
|
void publishState(bool state) {
|
|
publisher(stateTopic, state ? "ON" : "OFF");
|
|
}
|
|
};
|
|
|
|
struct Sensor : EntityConfig<Sensor>, StateConfig<Sensor> {
|
|
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, "%s", id);
|
|
}
|
|
|
|
void buildConfig(JsonDocument& jsonDoc) override {
|
|
EntityConfig::buildConfig(jsonDoc);
|
|
jsonDoc["device_class"] = deviceClass;
|
|
jsonDoc["unit_of_measurement"] = unitMeasure;
|
|
jsonDoc["value_template"] = valueTemplate;
|
|
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 }}";
|
|
}
|
|
};
|
|
|
|
struct HumiditySensor : Sensor {
|
|
HumiditySensor(const char* name, const char* id) : Sensor(name, id) {
|
|
name = "Humidity";
|
|
deviceClass = "humidity";
|
|
unitMeasure = "%";
|
|
valueTemplate = "{{ value_json.sensor.humidity }}";
|
|
}
|
|
};
|
|
|
|
struct PressureSensor : Sensor {
|
|
PressureSensor(const char* name, const char* id) : Sensor(name, id) {
|
|
name = "Pressure";
|
|
deviceClass = "pressure";
|
|
unitMeasure = "hPa";
|
|
valueTemplate = "{{ value_json.sensor.pressure }}";
|
|
}
|
|
};
|
|
|
|
struct AltitudeSensor : Sensor {
|
|
AltitudeSensor(const char* name, const char* id) : Sensor(name, id) {
|
|
deviceClass = "distance";
|
|
unitMeasure = "m";
|
|
valueTemplate = "{{ value_json.sensor.altitude }}";
|
|
}
|
|
};
|
|
}
|