246 lines
8.1 KiB
C++
246 lines
8.1 KiB
C++
#pragma once
|
|
|
|
#include <ArduinoJson.h>
|
|
#include "utils.h"
|
|
|
|
#define JSON_SIZE 512
|
|
#define TOPIC_SIZE 255
|
|
#define DEVICE_ID "rc-gateway"
|
|
|
|
namespace Ha {
|
|
uint16_t (*publisher)(const char* topic, const char* message);
|
|
|
|
struct Component {
|
|
const char* name;
|
|
char* id;
|
|
const char* type;
|
|
char configTopic[TOPIC_SIZE];
|
|
static List<Component> configs;
|
|
|
|
Component(const char* name, const char* id, const char* type) : name(name), id((char*)id), type(type) {
|
|
sprintf(configTopic, "homeassistant/%s/rc-gateway/%s/config", type, id);
|
|
configs.add(this);
|
|
}
|
|
|
|
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"] = "RC Gateway";
|
|
device["model"] = "Huzzah Esp8266";
|
|
device["manufacturer"] = "Adafruit";
|
|
JsonArray connections = device.createNestedArray("connections");
|
|
JsonArray mac = connections.createNestedArray();
|
|
mac.add("mac");
|
|
mac.add(WiFi.macAddress());
|
|
JsonArray identifiers = device.createNestedArray("identifiers");
|
|
identifiers.add(DEVICE_ID);
|
|
}
|
|
};
|
|
List<Component> Component::configs;
|
|
|
|
struct Command : Component {
|
|
char commandTopic[TOPIC_SIZE];
|
|
|
|
Command(const char* name, const char* id, const char* type) : Component(name, id, type) {
|
|
}
|
|
|
|
void buildUniqueId(char* uniqueId) override {
|
|
sprintf(uniqueId, "rc_gateway_%s", id);
|
|
}
|
|
|
|
void buildConfig(JsonDocument& jsonDoc) override {
|
|
Component::buildConfig(jsonDoc);
|
|
jsonDoc["command_topic"] = commandTopic;
|
|
}
|
|
|
|
};
|
|
|
|
typedef void (*onMessage)(const char* msg);
|
|
struct Button : Command {
|
|
static constexpr const char* type = "button";
|
|
onMessage f;
|
|
|
|
Button(const char* name, const char* id, onMessage f) : Command(name, id, type), f(f) {
|
|
sprintf(commandTopic, "homeassistant/%s/rc-gateway/%s", type, id);
|
|
}
|
|
|
|
};
|
|
|
|
struct Switch : Command {
|
|
static constexpr const char* type = "switch";
|
|
char stateTopic[TOPIC_SIZE];
|
|
const char* area;
|
|
|
|
virtual void onCommand(const char* msg){}
|
|
virtual void addToMap(){}
|
|
|
|
Switch(const char* name, const char* id)
|
|
: Command(name, id, type) {
|
|
sprintf(commandTopic, "homeassistant/%s/rc-gateway/%s/set", type, id);
|
|
}
|
|
|
|
void buildDeviceConfig(JsonDocument& jsonDoc) override {
|
|
JsonObject device = jsonDoc.createNestedObject("device");
|
|
device["name"] = name;
|
|
device["via_device"] = DEVICE_ID;
|
|
device["suggested_area"] = area;
|
|
JsonArray identifiers = device.createNestedArray("identifiers");
|
|
identifiers.add(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;
|
|
}
|
|
|
|
Switch* withStateTopic() {
|
|
sprintf(stateTopic, "homeassistant/%s/rc-gateway/%s/state", type, id);
|
|
return this;
|
|
}
|
|
|
|
Switch* withArea(const char* area) {
|
|
this->area = area;
|
|
return this;
|
|
}
|
|
|
|
void publishState(bool state) {
|
|
publisher(stateTopic, state ? "ON" : "OFF");
|
|
}
|
|
};
|
|
|
|
struct PollinSwitch : Switch {
|
|
const char* group;
|
|
unsigned char channel;
|
|
|
|
PollinSwitch(const char* name, const char* group, const unsigned char channel)
|
|
: Switch(name, Protocol_1::buildId(group, channel)), group(group), channel(channel) {
|
|
sprintf(commandTopic, "homeassistant/%s/rc-gateway/%s/set", type, id);
|
|
}
|
|
|
|
void onCommand(const char* msg) override {
|
|
(String{ "ON" }.equals(msg)) ? mySwitch.switchOn(group, channel) : mySwitch.switchOff(group, channel);
|
|
publisher(stateTopic, msg);
|
|
}
|
|
|
|
void buildConfig(JsonDocument& jsonDoc) override {
|
|
Switch::buildConfig(jsonDoc);
|
|
JsonObject device = jsonDoc["device"];
|
|
device["manufacturer"] = "Pollin";
|
|
}
|
|
|
|
void addToMap() override {
|
|
p1Switches.insert({string(id), this});
|
|
}
|
|
|
|
};
|
|
|
|
struct EasyHomeSwitch : Switch {
|
|
unsigned long on[8] = {4326554, 4537114, 4767530, 4972714};
|
|
unsigned long off[8] = {4483146, 4626810, 4661562, 4819642};
|
|
|
|
EasyHomeSwitch(const char* name, const char* id, unsigned long on[4], unsigned long off[4])
|
|
: Switch(name, id) {
|
|
memcpy(&this->on[4], on, 4*sizeof(unsigned long));
|
|
memcpy(&this->off[4], off, 4*sizeof(unsigned long));
|
|
sprintf(commandTopic, "homeassistant/%s/rc-gateway/%s/set", type, id);
|
|
}
|
|
|
|
void onCommand(const char* msg) override {
|
|
mySwitch.setProtocol(4);
|
|
String{ "ON" }.equals(msg) ? mySwitch.send(on[4], 24) : mySwitch.send(off[4], 24);
|
|
publisher(stateTopic, msg);
|
|
}
|
|
|
|
void buildConfig(JsonDocument& jsonDoc) override {
|
|
Switch::buildConfig(jsonDoc);
|
|
JsonObject device = jsonDoc["device"];
|
|
device["manufacturer"] = "Intertek";
|
|
device["model"] = "Easy Home";
|
|
}
|
|
|
|
void addToMap() override {
|
|
for (int i = 0; i < 8; i++) {
|
|
onSwitches.insert({on[i], this});
|
|
offSwitches.insert({off[i], this});
|
|
}
|
|
}
|
|
};
|
|
|
|
struct Sensor : Component {
|
|
const char* deviceClass;
|
|
const char* unitMeasure;
|
|
const char* valueTemplate;
|
|
static constexpr const char* stateTopic = "homeassistant/sensor/rc-gateway/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"] = "rc-gateway";
|
|
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 }}";
|
|
}
|
|
};
|
|
}
|