separate generic Ha components - could be used in a library
This commit is contained in:
parent
e67ce3b33c
commit
86b2d75ea6
@ -44,7 +44,7 @@ enum SensorId : unsigned short {
|
||||
};
|
||||
```
|
||||
#### Stairs temperature
|
||||
##### Value and volatage
|
||||
##### Value and voltage
|
||||
```json
|
||||
{
|
||||
"sensor": {
|
||||
@ -66,7 +66,7 @@ enum SensorId : unsigned short {
|
||||
}
|
||||
```
|
||||
#### Oil sensor
|
||||
##### Value and volatage
|
||||
##### Value and voltage
|
||||
```json
|
||||
{
|
||||
"sensor": {
|
||||
|
||||
@ -1,16 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#define MAIN_DEVICE_ID "rc-gateway"
|
||||
|
||||
#include "ha.h"
|
||||
|
||||
using namespace Ha;
|
||||
|
||||
DeviceConfig* gatewayConfig = (new DeviceConfig{MAIN_DEVICE_ID, "RC Gateway"})->withManufacturer("Adafruit")->withModel("Huzzah Esp8266");
|
||||
|
||||
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);
|
||||
asDevice((new DeviceConfig{id, name})->withManufacturer("Pollin")->withParent(gatewayConfig));
|
||||
}
|
||||
|
||||
void onCommand(const char* msg) override {
|
||||
@ -18,12 +22,6 @@ struct PollinSwitch : Switch {
|
||||
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 });
|
||||
}
|
||||
@ -38,7 +36,7 @@ struct EasyHomeSwitch : Switch {
|
||||
: 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);
|
||||
asDevice((new DeviceConfig{id, name})->withManufacturer("Intertek")->withModel("Easy Home")->withParent(gatewayConfig));
|
||||
}
|
||||
|
||||
void onCommand(const char* msg) override {
|
||||
@ -47,13 +45,6 @@ struct EasyHomeSwitch : Switch {
|
||||
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 });
|
||||
|
||||
@ -5,73 +5,123 @@
|
||||
|
||||
#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 DeviceConfig {
|
||||
const char* id;
|
||||
const char* name;
|
||||
const char* model;
|
||||
const char* manufacturer;
|
||||
const char* area;
|
||||
DeviceConfig* parent = nullptr;
|
||||
|
||||
DeviceConfig(const char* id, const char* name) : id(id), name(name) {}
|
||||
|
||||
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;
|
||||
char* id;
|
||||
const char* type;
|
||||
char configTopic[TOPIC_SIZE];
|
||||
static List<Component> configs;
|
||||
DeviceConfig* mainDevice;
|
||||
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/rc-gateway/%s/config", type, id);
|
||||
configs.add(this);
|
||||
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) {
|
||||
buildDeviceConfig(jsonDoc);
|
||||
if (mainDevice) mainDevice->buildConfig(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);
|
||||
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);
|
||||
}
|
||||
};
|
||||
List<Component> Component::configs;
|
||||
|
||||
List<Component> Component::components;
|
||||
|
||||
struct Command : Component {
|
||||
template <class T>
|
||||
struct Command : EntityConfig<T> {
|
||||
char commandTopic[TOPIC_SIZE];
|
||||
|
||||
Command(const char* name, const char* id, const char* type) : Component(name, id, type) {
|
||||
Command(const char* name, const char* id, const char* type) : EntityConfig<T>(name, id, type) {
|
||||
}
|
||||
|
||||
void buildUniqueId(char* uniqueId) override {
|
||||
sprintf(uniqueId, "rc_gateway_%s", id);
|
||||
sprintf(uniqueId, "%s_%s", MAIN_DEVICE_ID, this->id);
|
||||
}
|
||||
|
||||
void buildConfig(JsonDocument& jsonDoc) override {
|
||||
Component::buildConfig(jsonDoc);
|
||||
EntityConfig<T>::buildConfig(jsonDoc);
|
||||
jsonDoc["command_topic"] = commandTopic;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
typedef void (*onMessage)(const char* msg);
|
||||
struct Button : Command {
|
||||
struct Button : Command<Button> {
|
||||
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);
|
||||
}
|
||||
Button(const char* name, const char* id, onMessage f) : Command(name, id, type), f(f) {}
|
||||
|
||||
};
|
||||
|
||||
@ -80,30 +130,20 @@ namespace Ha {
|
||||
char stateTopic[TOPIC_SIZE];
|
||||
|
||||
T* withStateTopic() {
|
||||
sprintf(stateTopic, "homeassistant/%s/rc-gateway/%s/state", ((T*)this)->type, ((T*)this)->id);
|
||||
return (T*)this;
|
||||
sprintf(stateTopic, "homeassistant/%s/%s/%s/state", ((T*)this)->type, MAIN_DEVICE_ID, ((T*)this)->id);
|
||||
return static_cast<T*>(this);
|
||||
}
|
||||
};
|
||||
|
||||
struct Switch : Command, StateConfig<Switch> {
|
||||
struct Switch : Command<Switch>, StateConfig<Switch> {
|
||||
static constexpr const char* type = "switch";
|
||||
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);
|
||||
Switch(const char* name, const char* id) : Command(name, id, type) {
|
||||
sprintf(commandTopic, "homeassistant/%s/%s/%s/set", type, MAIN_DEVICE_ID, id);
|
||||
}
|
||||
|
||||
void buildConfig(JsonDocument& jsonDoc) override {
|
||||
@ -114,54 +154,39 @@ namespace Ha {
|
||||
if (stateTopic[0]) jsonDoc["state_topic"] = stateTopic;
|
||||
}
|
||||
|
||||
Switch* withArea(const char* area) {
|
||||
this->area = area;
|
||||
return this;
|
||||
}
|
||||
|
||||
void publishState(bool state) {
|
||||
publisher(stateTopic, state ? "ON" : "OFF");
|
||||
}
|
||||
};
|
||||
|
||||
struct Sensor : Component {
|
||||
struct Sensor : EntityConfig<Sensor> {
|
||||
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") {
|
||||
Sensor(const char* name, const char* id) : EntityConfig(name, id, "sensor") {
|
||||
}
|
||||
|
||||
void buildUniqueId(char* uniqueId) override {
|
||||
sprintf(uniqueId, "living_room_%s", id);
|
||||
sprintf(uniqueId, "id%s", id);
|
||||
}
|
||||
|
||||
void buildConfig(JsonDocument& jsonDoc) override {
|
||||
Component::buildConfig(jsonDoc);
|
||||
EntityConfig::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 }}";
|
||||
valueTemplate = "{{ value_json.sensor.temperature }}";
|
||||
}
|
||||
};
|
||||
|
||||
@ -169,7 +194,7 @@ namespace Ha {
|
||||
HumiditySensor(const char* name, const char* id) : Sensor(name, id) {
|
||||
deviceClass = "humidity";
|
||||
unitMeasure = "%";
|
||||
valueTemplate = "{{ value_json.humidity }}";
|
||||
valueTemplate = "{{ value_json.sensor.humidity }}";
|
||||
}
|
||||
};
|
||||
|
||||
@ -177,7 +202,7 @@ namespace Ha {
|
||||
PressureSensor(const char* name, const char* id) : Sensor(name, id) {
|
||||
deviceClass = "pressure";
|
||||
unitMeasure = "hPa";
|
||||
valueTemplate = "{{ value_json.pressure }}";
|
||||
valueTemplate = "{{ value_json.sensor.pressure }}";
|
||||
}
|
||||
};
|
||||
|
||||
@ -185,7 +210,7 @@ namespace Ha {
|
||||
AltitudeSensor(const char* name, const char* id) : Sensor(name, id) {
|
||||
deviceClass = "distance";
|
||||
unitMeasure = "m";
|
||||
valueTemplate = "{{ value_json.altitude }}";
|
||||
valueTemplate = "{{ value_json.sensor.altitude }}";
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@ -40,11 +40,11 @@ namespace Mqtt {
|
||||
};
|
||||
|
||||
Ha::Button* buttons[] = {
|
||||
new Ha::Button{"Restart", "restart",
|
||||
(new Ha::Button{"Restart", "restart",
|
||||
[](const char* msg) {
|
||||
if (String { "PRESS" }.equals(msg)) ESP.restart();
|
||||
}
|
||||
}
|
||||
})->asDevice(gatewayConfig)
|
||||
};
|
||||
|
||||
Ha::Switch* switches[] = {
|
||||
@ -82,7 +82,7 @@ namespace Mqtt {
|
||||
}
|
||||
|
||||
void publishCleanupConfig() {
|
||||
for (List<Ha::Component>::Container* c = Ha::Component::configs.first; c; c = c->next) {
|
||||
for (List<Ha::Component>::Container* c = Ha::Component::components.first; c; c = c->next) {
|
||||
publish(c->t->configTopic, "");
|
||||
}
|
||||
}
|
||||
|
||||
@ -12,6 +12,7 @@ namespace Ota {
|
||||
ArduinoOTA.onStart([]() {
|
||||
Serial.println("Starting OTA");
|
||||
Mqtt::publishCleanupConfig();
|
||||
delay(2000);
|
||||
Mqtt::disconnect();
|
||||
});
|
||||
ArduinoOTA.onEnd([]() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user