ha-mqtt/src/ha.h

524 lines
16 KiB
C++

#pragma once
#include <ArduinoJson.h>
#include "list.h"
#include "enums.h"
using namespace std;
#define JSON_SIZE 512
#define TOPIC_SIZE 255
#define CONFIG_TOPIC "homeassistant/%s/" MAIN_DEVICE_ID "/%s"
#define BASE_TOPIC MAIN_DEVICE_ID "/%s"
namespace Ha {
uint16_t (*publisher)(const char*, const char*);
typedef void (*onMessage)(const char*);
struct Config {
virtual void buildConfig(JsonDocument& jsonDoc) = 0;
};
struct DeviceConfig : Config {
const char* id = nullptr;
const char* name = nullptr;
const char* model = nullptr;
const char* manufacturer = nullptr;
const char* area = nullptr;
const DeviceConfig* parent = nullptr;
static DeviceConfig& create(const char* id) {
return *(new DeviceConfig{ id });
}
DeviceConfig& withName(const char* value) {
name = value;
return *this;
}
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(const DeviceConfig* deviceConfig) {
parent = deviceConfig;
return *this;
}
void buildConfig(JsonDocument& jsonDoc) override {
JsonObject device = jsonDoc.createNestedObject("device");
if (name) device["name"] = name;
if (model) device["model"] = model;
if (manufacturer) device["manufacturer"] = manufacturer;
if (area) device["suggested_area"] = area;
if (parent) device["via_device"] = parent->id;
JsonArray identifiers = device.createNestedArray("identifiers");
identifiers.add(id);
}
private:
DeviceConfig(const char* id) : id(id) {}
};
struct Component : Config {
const char* id = nullptr;
const char* type = nullptr;
const char* entityId = nullptr;
const char* name = nullptr;
const char* entityCategory = nullptr;
const char* deviceClass = nullptr;
const char* icon = nullptr;
DeviceConfig* mainDevice = nullptr;
inline static List<Component> components;
bool multiValueComponent = false;
Component(const char* id, const char* name, const char* type) : id(id), type(type), name(name) {
components.add(this);
}
void publishConfig() {
StaticJsonDocument<JSON_SIZE> jsonDoc;
buildConfig(jsonDoc);
char message[JSON_SIZE] = {};
serializeJson(jsonDoc, message);
auto configTopic = buildConfigTopic();
publisher(configTopic, message);
delete configTopic;
}
void publishCleanupConfig() {
auto configTopic = buildConfigTopic();
publisher(configTopic, "");
delete configTopic;
}
void toJson(JsonDocument& jsonDoc) {
jsonDoc["id"] = id;
jsonDoc["name"] = name;
jsonDoc["type"] = type;
}
protected:
void buildConfig(JsonDocument& jsonDoc) override {
if (mainDevice) mainDevice->buildConfig(jsonDoc);
if (entityId) jsonDoc["object_id"] = entityId;
if (entityCategory) jsonDoc["entity_category"] = entityCategory;
if (deviceClass) jsonDoc["device_class"] = deviceClass;
if (icon) jsonDoc["icon"] = icon;
jsonDoc["name"] = name;
buildUniqueId(jsonDoc);
}
private:
void buildUniqueId(JsonDocument& jsonDoc) {
char uniqueId[50];
if (multiValueComponent && deviceClass) {
snprintf(uniqueId, sizeof(uniqueId), "%s_%s_%s", MAIN_DEVICE_ID, deviceClass, id);
} else {
snprintf(uniqueId, sizeof(uniqueId), "%s_%s", MAIN_DEVICE_ID, id);
}
jsonDoc["unique_id"] = uniqueId;
}
const char* buildConfigTopic() {
char* configTopic = new char[TOPIC_SIZE];
if (multiValueComponent && deviceClass) {
snprintf(configTopic, TOPIC_SIZE, CONFIG_TOPIC"_%s""/config", type, deviceClass, id);
} else {
snprintf(configTopic, TOPIC_SIZE, CONFIG_TOPIC"/config", type, id);
}
return configTopic;
}
};
struct Command : Config {
bool retain = false;
inline static unordered_map<string, Command*> mapCommandTopics;
inline static unordered_map<string, Command*> mapCommandIds;
Command(Component* cmp, onMessage f) : f(f), cmp(cmp) {
snprintf(commandTopic, sizeof(commandTopic), BASE_TOPIC"/set", cmp->id);
mapCommandTopics.insert({ string(commandTopic), this });
mapCommandIds.insert({ string(cmp->id), this });
}
virtual void onCommand(const char* msg) {
if (f) f(msg);
}
void toJson(JsonDocument& jsonDoc) {
cmp->toJson(jsonDoc);
}
protected:
char commandTopic[TOPIC_SIZE] = {};
onMessage f;
void buildConfig(JsonDocument& jsonDoc) override {
jsonDoc["command_topic"] = (const char*)commandTopic;
if (retain) jsonDoc["retain"] = true;
}
private:
Component* cmp;
};
struct State : Config {
char stateTopic[TOPIC_SIZE] = {};
const char* jsonAttributesTemplate = nullptr;
const char* valueTemplate = nullptr;
State(Component* cmp) : cmp(cmp) {}
void withStateTopic() {
snprintf(stateTopic, sizeof(stateTopic), BASE_TOPIC"/state", cmp->id);
}
void updateState(const char* message) {
if (stateTopic[0]) publisher(stateTopic, message);
}
protected:
void buildConfig(JsonDocument& jsonDoc) override {
if (stateTopic[0]) {
jsonDoc["state_topic"] = (const char*)stateTopic;
if (jsonAttributesTemplate) {
jsonDoc["json_attributes_template"] = jsonAttributesTemplate;
jsonDoc["json_attributes_topic"] = (const char*)stateTopic;
}
if (valueTemplate) jsonDoc["value_template"] = valueTemplate;
}
}
private:
Component* cmp;
};
struct StatefulCommand : Command, State {
inline static unordered_map<string, Command*> mapRestoreStateTopics;
StatefulCommand(Component* cmp, onMessage f) : Command(cmp, f), State(cmp) {}
void restoreStateFromCommand() {
withStateTopic();
mapRestoreStateTopics.insert({stateTopic, this});
}
protected:
void buildConfig(JsonDocument& jsonDoc) override {
Command::buildConfig(jsonDoc);
State::buildConfig(jsonDoc);
}
};
struct Button : Component, Command {
Button(const char* name, const char* id, onMessage f = nullptr) : Component(id, name, "button"), Command(this, f) {}
void buildConfig(JsonDocument& jsonDoc) override {
Component::buildConfig(jsonDoc);
Command::buildConfig(jsonDoc);
}
};
struct Switch : Component, StatefulCommand {
Switch(const char* name, const char* id, onMessage f = nullptr) : Component(id, name, "switch"), StatefulCommand(this, f) {}
void updateState(bool state) {
State::updateState(state ? "ON" : "OFF");
}
void buildConfig(JsonDocument& jsonDoc) override {
Component::buildConfig(jsonDoc);
StatefulCommand::buildConfig(jsonDoc);
}
};
struct Light : Component, StatefulCommand {
Light(const char* name, const char* id, onMessage f = nullptr) : Component(id, name, "light"), StatefulCommand(this, f) {}
void updateState(bool state) {
State::updateState(state ? "ON" : "OFF");
}
void buildConfig(JsonDocument& jsonDoc) override {
Component::buildConfig(jsonDoc);
StatefulCommand::buildConfig(jsonDoc);
}
};
struct Number : Component, StatefulCommand {
uint16_t min = 1;
uint16_t max = 100;
uint16_t step = 1;
const char* unitMeasure = nullptr;
Number(const char* name, const char* id, onMessage f) : Component(id, name, "number"), StatefulCommand(this, f) {}
void updateState(uint16_t value) {
State::updateState(to_string(value).c_str());
}
void buildConfig(JsonDocument& jsonDoc) override {
Component::buildConfig(jsonDoc);
StatefulCommand::buildConfig(jsonDoc);
jsonDoc["min"] = min;
jsonDoc["max"] = max;
jsonDoc["step"] = step;
if (unitMeasure) jsonDoc["unit_of_measurement"] = unitMeasure;
}
};
struct Text : Component, StatefulCommand {
uint16_t min = 1;
uint16_t max = 100;
const char* pattern = nullptr;
Text(const char* name, const char* id, onMessage f) : Component(id, name, "text"), StatefulCommand(this, f) {}
void updateState(uint16_t value) {
State::updateState(to_string(value).c_str());
}
void buildConfig(JsonDocument& jsonDoc) override {
Component::buildConfig(jsonDoc);
StatefulCommand::buildConfig(jsonDoc);
jsonDoc["min"] = min;
jsonDoc["max"] = max;
jsonDoc["platform"] = "text";
if (pattern) jsonDoc["pattern"] = pattern;
}
};
struct GenericSensor : Component, State {
inline static unordered_map<string, GenericSensor*> mapSensors;
GenericSensor(const char* id, const char* name, const char* type) : Component(id, name, type), State(this) {
withStateTopic();
mapSensors.insert({ string(id), this });
}
void buildConfig(JsonDocument& jsonDoc) override {
Component::buildConfig(jsonDoc);
State::buildConfig(jsonDoc);
}
};
struct BinarySensor : GenericSensor {
uint16_t off_delay_seconds = 0;
BinarySensor(const char* id, const char* name = nullptr) : GenericSensor(id, name, "binary_sensor") {}
void buildConfig(JsonDocument& jsonDoc) override {
GenericSensor::buildConfig(jsonDoc);
if (off_delay_seconds) jsonDoc["off_delay"] = off_delay_seconds;
}
};
struct Sensor : GenericSensor {
const char* unitMeasure = nullptr;
uint16_t precision = 2;
SensorStateClass sensorStateClass;
Sensor(const char* name, const char* id) : GenericSensor(id, name, "sensor") {}
void buildConfig(JsonDocument& jsonDoc) override {
GenericSensor::buildConfig(jsonDoc);
if (unitMeasure) jsonDoc["unit_of_measurement"] = unitMeasure;
if (isNumericSensor()) jsonDoc["suggested_display_precision"] = precision;
if (sensorStateClass) jsonDoc["state_class"] = static_cast<const char*>(sensorStateClass);
}
private:
bool isNumericSensor() {
return deviceClass || unitMeasure;
}
};
struct TemperatureSensor : Sensor {
TemperatureSensor(const char* id, const char* name = "Temperature") : Sensor(name, id) {
deviceClass = "temperature";
unitMeasure = "°C";
sensorStateClass = MEASUREMENT;
}
};
struct HumiditySensor : Sensor {
HumiditySensor(const char* id, const char* name = "Humidity") : Sensor(name, id) {
deviceClass = "humidity";
unitMeasure = "%";
sensorStateClass = MEASUREMENT;
}
};
struct PressureSensor : Sensor {
PressureSensor(const char* id, const char* name = "Pressure") : Sensor(name, id) {
deviceClass = "pressure";
unitMeasure = "hPa";
}
};
struct VoltageSensor : Sensor {
VoltageSensor(const char* id, const char* name, const char* valueTemplate) : Sensor(name, id) {
this->valueTemplate = valueTemplate;
deviceClass = "voltage";
unitMeasure = "V";
}
};
struct BatterySensor : Sensor {
BatterySensor(const char* id, const char* name, const char* valueTemplate) : Sensor(name, id) {
this->valueTemplate = valueTemplate;
deviceClass = "battery";
unitMeasure = "%";
sensorStateClass = MEASUREMENT;
}
};
template <class T>
struct Builder {
T* cmp;
Builder(T* cmp) : cmp(cmp) {}
Builder(const char* id) {
cmp = new T{ id };
}
T* build() {
return static_cast<T*>(cmp);
}
Builder& withDeviceClass(const char* value) {
cmp->deviceClass = value;
return *this;
}
Builder& withEntityId(const char* value) {
cmp->entityId = value;
return *this;
}
Builder& withUnitMeasure(const char* value) {
cmp->unitMeasure = value;
return *this;
}
Builder& withIcon(const char* value) {
cmp->icon = value;
return *this;
}
Builder& withValueTemplate(const char* value) {
cmp->valueTemplate = value;
return *this;
}
Builder& withPrecision(uint16_t value) {
cmp->precision = value;
return *this;
}
Builder& withSensorStateClass(SensorStateClass value) {
cmp->sensorStateClass = value;
return *this;
}
Builder& withMin(uint16_t value) {
cmp->min = value;
return *this;
}
Builder& withMax(uint16_t value) {
cmp->max = value;
return *this;
}
Builder& withStep(uint16_t value) {
cmp->step = value;
return *this;
}
Builder& withOffDelaySeconds(uint16_t value) {
cmp->off_delay_seconds = value;
return *this;
}
Builder& withPattern(const char* value) {
cmp->pattern = value;
return *this;
}
Builder& withJsonAttributes(const char* value) {
cmp->jsonAttributesTemplate = value;
return *this;
}
Builder& addSecondary(Component* c) {
if (cmp->mainDevice) c->mainDevice = &DeviceConfig::create(cmp->mainDevice->id).withName(cmp->mainDevice->name);
if (!strcmp(cmp->id, c->id)) c->multiValueComponent = true;
return *this;
}
Builder& addDiagnostic(Component* c) {
c->entityCategory = "diagnostic";
return addSecondary(c);
}
Builder& addPreconfigured(Builder& (*factoryBuilder)(Builder& builder)) {
return factoryBuilder(*this);
}
template <typename Callable>
Builder& addPreconfigured(Callable&& factoryBuilder) {
return factoryBuilder(*this);
}
Builder& addConfiguration(Component* c) {
c->entityCategory = "config";
return addSecondary(c);
}
Builder& asDevice(DeviceConfig* deviceConfig) {
cmp->mainDevice = deviceConfig;
return *this;
}
Builder& withStateTopic() {
cmp->withStateTopic();
return *this;
}
Builder& withRetain(bool retain = true) {
cmp->retain = retain;
return *this;
}
[[deprecated("Use restoreStateFromCommand() instead")]]
Builder& restoreFromState() {
cmp->restoreStateFromCommand();
return *this;
}
Builder& restoreStateFromCommand() {
cmp->restoreStateFromCommand();
return *this;
}
};
}