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
|
#### Stairs temperature
|
||||||
##### Value and volatage
|
##### Value and voltage
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"sensor": {
|
"sensor": {
|
||||||
@ -66,7 +66,7 @@ enum SensorId : unsigned short {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
#### Oil sensor
|
#### Oil sensor
|
||||||
##### Value and volatage
|
##### Value and voltage
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"sensor": {
|
"sensor": {
|
||||||
|
|||||||
@ -1,16 +1,20 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#define MAIN_DEVICE_ID "rc-gateway"
|
||||||
|
|
||||||
#include "ha.h"
|
#include "ha.h"
|
||||||
|
|
||||||
using namespace Ha;
|
using namespace Ha;
|
||||||
|
|
||||||
|
DeviceConfig* gatewayConfig = (new DeviceConfig{MAIN_DEVICE_ID, "RC Gateway"})->withManufacturer("Adafruit")->withModel("Huzzah Esp8266");
|
||||||
|
|
||||||
struct PollinSwitch : Switch {
|
struct PollinSwitch : Switch {
|
||||||
const char* group;
|
const char* group;
|
||||||
unsigned char channel;
|
unsigned char channel;
|
||||||
|
|
||||||
PollinSwitch(const char* name, const char* group, const 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) {
|
: 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 {
|
void onCommand(const char* msg) override {
|
||||||
@ -18,12 +22,6 @@ struct PollinSwitch : Switch {
|
|||||||
publisher(stateTopic, msg);
|
publisher(stateTopic, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
void buildConfig(JsonDocument& jsonDoc) override {
|
|
||||||
Switch::buildConfig(jsonDoc);
|
|
||||||
JsonObject device = jsonDoc["device"];
|
|
||||||
device["manufacturer"] = "Pollin";
|
|
||||||
}
|
|
||||||
|
|
||||||
void addToMap() override {
|
void addToMap() override {
|
||||||
p1Switches.insert({ string(id), this });
|
p1Switches.insert({ string(id), this });
|
||||||
}
|
}
|
||||||
@ -38,7 +36,7 @@ struct EasyHomeSwitch : Switch {
|
|||||||
: Switch(name, id) {
|
: Switch(name, id) {
|
||||||
memcpy(&this->on[4], on, 4 * sizeof(unsigned long));
|
memcpy(&this->on[4], on, 4 * sizeof(unsigned long));
|
||||||
memcpy(&this->off[4], off, 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 {
|
void onCommand(const char* msg) override {
|
||||||
@ -47,13 +45,6 @@ struct EasyHomeSwitch : Switch {
|
|||||||
publisher(stateTopic, msg);
|
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 {
|
void addToMap() override {
|
||||||
for (int i = 0; i < 8; i++) {
|
for (int i = 0; i < 8; i++) {
|
||||||
onSwitches.insert({ on[i], this });
|
onSwitches.insert({ on[i], this });
|
||||||
|
|||||||
@ -5,73 +5,123 @@
|
|||||||
|
|
||||||
#define JSON_SIZE 512
|
#define JSON_SIZE 512
|
||||||
#define TOPIC_SIZE 255
|
#define TOPIC_SIZE 255
|
||||||
#define DEVICE_ID "rc-gateway"
|
|
||||||
|
|
||||||
namespace Ha {
|
namespace Ha {
|
||||||
uint16_t (*publisher)(const char* topic, const char* message);
|
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 {
|
struct Component {
|
||||||
const char* name;
|
const char* name;
|
||||||
char* id;
|
char* id;
|
||||||
const char* type;
|
const char* type;
|
||||||
char configTopic[TOPIC_SIZE];
|
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) {
|
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);
|
sprintf(configTopic, "homeassistant/%s/%s/%s/config", type, MAIN_DEVICE_ID, id);
|
||||||
configs.add(this);
|
components.add(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void buildUniqueId(char* uniqueId) = 0;
|
virtual void buildUniqueId(char* uniqueId) = 0;
|
||||||
|
|
||||||
virtual void buildConfig(JsonDocument& jsonDoc) {
|
virtual void buildConfig(JsonDocument& jsonDoc) {
|
||||||
buildDeviceConfig(jsonDoc);
|
if (mainDevice) mainDevice->buildConfig(jsonDoc);
|
||||||
jsonDoc["name"] = name;
|
jsonDoc["name"] = name;
|
||||||
char uniqueId[50];
|
char uniqueId[50];
|
||||||
buildUniqueId(uniqueId);
|
buildUniqueId(uniqueId);
|
||||||
jsonDoc["unique_id"] = uniqueId;
|
jsonDoc["unique_id"] = uniqueId;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
virtual void buildDeviceConfig(JsonDocument& jsonDoc) {
|
template <class T>
|
||||||
JsonObject device = jsonDoc.createNestedObject("device");
|
struct EntityConfig : Component {
|
||||||
device["name"] = "RC Gateway";
|
|
||||||
device["model"] = "Huzzah Esp8266";
|
EntityConfig(const char* name, const char* id, const char* type) : Component(name, id, type) {
|
||||||
device["manufacturer"] = "Adafruit";
|
}
|
||||||
JsonArray connections = device.createNestedArray("connections");
|
|
||||||
JsonArray mac = connections.createNestedArray();
|
T* withArea(const char* value) {
|
||||||
mac.add("mac");
|
if (mainDevice) mainDevice->withArea(value);
|
||||||
mac.add(WiFi.macAddress());
|
return static_cast<T*>(this);
|
||||||
JsonArray identifiers = device.createNestedArray("identifiers");
|
}
|
||||||
identifiers.add(DEVICE_ID);
|
|
||||||
|
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];
|
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 {
|
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 {
|
void buildConfig(JsonDocument& jsonDoc) override {
|
||||||
Component::buildConfig(jsonDoc);
|
EntityConfig<T>::buildConfig(jsonDoc);
|
||||||
jsonDoc["command_topic"] = commandTopic;
|
jsonDoc["command_topic"] = commandTopic;
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef void (*onMessage)(const char* msg);
|
typedef void (*onMessage)(const char* msg);
|
||||||
struct Button : Command {
|
struct Button : Command<Button> {
|
||||||
static constexpr const char* type = "button";
|
static constexpr const char* type = "button";
|
||||||
onMessage f;
|
onMessage f;
|
||||||
|
|
||||||
Button(const char* name, const char* id, onMessage f) : Command(name, id, type), f(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);
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -80,30 +130,20 @@ namespace Ha {
|
|||||||
char stateTopic[TOPIC_SIZE];
|
char stateTopic[TOPIC_SIZE];
|
||||||
|
|
||||||
T* withStateTopic() {
|
T* withStateTopic() {
|
||||||
sprintf(stateTopic, "homeassistant/%s/rc-gateway/%s/state", ((T*)this)->type, ((T*)this)->id);
|
sprintf(stateTopic, "homeassistant/%s/%s/%s/state", ((T*)this)->type, MAIN_DEVICE_ID, ((T*)this)->id);
|
||||||
return (T*)this;
|
return static_cast<T*>(this);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Switch : Command, StateConfig<Switch> {
|
struct Switch : Command<Switch>, StateConfig<Switch> {
|
||||||
static constexpr const char* type = "switch";
|
static constexpr const char* type = "switch";
|
||||||
const char* area;
|
const char* area;
|
||||||
|
|
||||||
virtual void onCommand(const char* msg){}
|
virtual void onCommand(const char* msg){}
|
||||||
virtual void addToMap(){}
|
virtual void addToMap(){}
|
||||||
|
|
||||||
Switch(const char* name, const char* id)
|
Switch(const char* name, const char* id) : Command(name, id, type) {
|
||||||
: Command(name, id, type) {
|
sprintf(commandTopic, "homeassistant/%s/%s/%s/set", type, MAIN_DEVICE_ID, id);
|
||||||
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 {
|
void buildConfig(JsonDocument& jsonDoc) override {
|
||||||
@ -114,54 +154,39 @@ namespace Ha {
|
|||||||
if (stateTopic[0]) jsonDoc["state_topic"] = stateTopic;
|
if (stateTopic[0]) jsonDoc["state_topic"] = stateTopic;
|
||||||
}
|
}
|
||||||
|
|
||||||
Switch* withArea(const char* area) {
|
|
||||||
this->area = area;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
void publishState(bool state) {
|
void publishState(bool state) {
|
||||||
publisher(stateTopic, state ? "ON" : "OFF");
|
publisher(stateTopic, state ? "ON" : "OFF");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Sensor : Component {
|
struct Sensor : EntityConfig<Sensor> {
|
||||||
const char* deviceClass;
|
const char* deviceClass;
|
||||||
const char* unitMeasure;
|
const char* unitMeasure;
|
||||||
const char* valueTemplate;
|
const char* valueTemplate;
|
||||||
static constexpr const char* stateTopic = "homeassistant/sensor/rc-gateway/state";
|
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 {
|
void buildUniqueId(char* uniqueId) override {
|
||||||
sprintf(uniqueId, "living_room_%s", id);
|
sprintf(uniqueId, "id%s", id);
|
||||||
}
|
}
|
||||||
|
|
||||||
void buildConfig(JsonDocument& jsonDoc) override {
|
void buildConfig(JsonDocument& jsonDoc) override {
|
||||||
Component::buildConfig(jsonDoc);
|
EntityConfig::buildConfig(jsonDoc);
|
||||||
jsonDoc["device_class"] = deviceClass;
|
jsonDoc["device_class"] = deviceClass;
|
||||||
jsonDoc["unit_of_measurement"] = unitMeasure;
|
jsonDoc["unit_of_measurement"] = unitMeasure;
|
||||||
jsonDoc["value_template"] = valueTemplate;
|
jsonDoc["value_template"] = valueTemplate;
|
||||||
jsonDoc["state_topic"] = stateTopic;
|
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 {
|
struct TemperatureSensor : Sensor {
|
||||||
TemperatureSensor(const char* name, const char* id) : Sensor(name, id) {
|
TemperatureSensor(const char* name, const char* id) : Sensor(name, id) {
|
||||||
deviceClass = "temperature";
|
deviceClass = "temperature";
|
||||||
unitMeasure = "°C";
|
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) {
|
HumiditySensor(const char* name, const char* id) : Sensor(name, id) {
|
||||||
deviceClass = "humidity";
|
deviceClass = "humidity";
|
||||||
unitMeasure = "%";
|
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) {
|
PressureSensor(const char* name, const char* id) : Sensor(name, id) {
|
||||||
deviceClass = "pressure";
|
deviceClass = "pressure";
|
||||||
unitMeasure = "hPa";
|
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) {
|
AltitudeSensor(const char* name, const char* id) : Sensor(name, id) {
|
||||||
deviceClass = "distance";
|
deviceClass = "distance";
|
||||||
unitMeasure = "m";
|
unitMeasure = "m";
|
||||||
valueTemplate = "{{ value_json.altitude }}";
|
valueTemplate = "{{ value_json.sensor.altitude }}";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -40,11 +40,11 @@ namespace Mqtt {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Ha::Button* buttons[] = {
|
Ha::Button* buttons[] = {
|
||||||
new Ha::Button{"Restart", "restart",
|
(new Ha::Button{"Restart", "restart",
|
||||||
[](const char* msg) {
|
[](const char* msg) {
|
||||||
if (String { "PRESS" }.equals(msg)) ESP.restart();
|
if (String { "PRESS" }.equals(msg)) ESP.restart();
|
||||||
}
|
}
|
||||||
}
|
})->asDevice(gatewayConfig)
|
||||||
};
|
};
|
||||||
|
|
||||||
Ha::Switch* switches[] = {
|
Ha::Switch* switches[] = {
|
||||||
@ -82,7 +82,7 @@ namespace Mqtt {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void publishCleanupConfig() {
|
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, "");
|
publish(c->t->configTopic, "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,6 +12,7 @@ namespace Ota {
|
|||||||
ArduinoOTA.onStart([]() {
|
ArduinoOTA.onStart([]() {
|
||||||
Serial.println("Starting OTA");
|
Serial.println("Starting OTA");
|
||||||
Mqtt::publishCleanupConfig();
|
Mqtt::publishCleanupConfig();
|
||||||
|
delay(2000);
|
||||||
Mqtt::disconnect();
|
Mqtt::disconnect();
|
||||||
});
|
});
|
||||||
ArduinoOTA.onEnd([]() {
|
ArduinoOTA.onEnd([]() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user