standardize components configuration
This commit is contained in:
parent
7e99bafff9
commit
65f7919ed9
41
include/ha.h
41
include/ha.h
@ -1,18 +1,19 @@
|
|||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
#define JSON_SIZE 512
|
#define JSON_SIZE 512
|
||||||
|
#define TOPIC_SIZE 255
|
||||||
|
|
||||||
namespace Ha {
|
namespace Ha {
|
||||||
|
|
||||||
struct Component {
|
struct Component {
|
||||||
const char* name;
|
const char* name;
|
||||||
const char* uniqueId;
|
const char* uniqueId;
|
||||||
const char* configTopic;
|
char configTopic[TOPIC_SIZE];
|
||||||
|
|
||||||
Component(const char* name, const char* uniqueId, const char* configTopic) {
|
Component(const char* name, const char* uniqueId, const char* topic, const char* type) {
|
||||||
this->name = name;
|
this->name = name;
|
||||||
this->uniqueId = uniqueId;
|
this->uniqueId = uniqueId;
|
||||||
this->configTopic = configTopic;
|
sprintf(configTopic, "homeassistant/%s/esp_clock/%s/config", type, topic);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void buildConfig(JsonDocument& jsonDoc) {
|
virtual void buildConfig(JsonDocument& jsonDoc) {
|
||||||
@ -34,11 +35,10 @@ namespace Ha {
|
|||||||
|
|
||||||
typedef void (*onMessage)(const char* msg);
|
typedef void (*onMessage)(const char* msg);
|
||||||
struct Command : Component {
|
struct Command : Component {
|
||||||
const char* commandTopic;
|
char commandTopic[TOPIC_SIZE];
|
||||||
onMessage f;
|
onMessage f;
|
||||||
|
|
||||||
Command(const char* name, const char* uniqueId, const char* configTopic, const char* commandTopic, onMessage f) : Component(name, uniqueId, configTopic) {
|
Command(const char* name, const char* uniqueId, const char* topic, const char* type, onMessage f) : Component(name, uniqueId, topic, type) {
|
||||||
this->commandTopic = commandTopic;
|
|
||||||
this->f = f;
|
this->f = f;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,14 +49,31 @@ namespace Ha {
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct Button : Command {
|
||||||
|
static constexpr const char* type = "button";
|
||||||
|
|
||||||
|
Button(const char* name, const char* uniqueId, const char* topic, onMessage f) : Command(name, uniqueId, topic, type, f) {
|
||||||
|
sprintf(commandTopic, "homeassistant/%s/esp_clock/%s", type, topic);
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Switch : Command {
|
||||||
|
static constexpr const char* type = "switch";
|
||||||
|
|
||||||
|
Switch(const char* name, const char* uniqueId, const char* topic, onMessage f) : Command(name, uniqueId, topic, type, f) {
|
||||||
|
sprintf(commandTopic, "homeassistant/%s/esp_clock/%s/set", type, topic);
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
struct Sensor : Component {
|
struct Sensor : Component {
|
||||||
const char* deviceClass;
|
const char* deviceClass;
|
||||||
const char* unitMeasure;
|
const char* unitMeasure;
|
||||||
const char* valueTemplate;
|
const char* valueTemplate;
|
||||||
const char* stateTopic;
|
static constexpr const char* stateTopic = "homeassistant/sensor/esp_clock/state";
|
||||||
|
|
||||||
Sensor(const char* name, const char* uniqueId, const char* configTopic, const char* stateTopic) : Component(name, uniqueId, configTopic) {
|
Sensor(const char* name, const char* uniqueId, const char* topic) : Component(name, uniqueId, topic, "sensor") {
|
||||||
this->stateTopic = stateTopic;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void buildConfig(JsonDocument& jsonDoc) {
|
void buildConfig(JsonDocument& jsonDoc) {
|
||||||
@ -70,7 +87,7 @@ namespace Ha {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct TemperatureSensor : Sensor {
|
struct TemperatureSensor : Sensor {
|
||||||
TemperatureSensor(const char* name, const char* uniqueId, const char* configTopic, const char* stateTopic) : Sensor(name, uniqueId, configTopic, stateTopic) {
|
TemperatureSensor(const char* name, const char* uniqueId, const char* topic) : Sensor(name, uniqueId, topic) {
|
||||||
deviceClass = "temperature";
|
deviceClass = "temperature";
|
||||||
unitMeasure = "°C";
|
unitMeasure = "°C";
|
||||||
valueTemplate = "{{ value_json.temperature }}";
|
valueTemplate = "{{ value_json.temperature }}";
|
||||||
@ -78,7 +95,7 @@ namespace Ha {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct PressureSensor : Sensor {
|
struct PressureSensor : Sensor {
|
||||||
PressureSensor(const char* name, const char* uniqueId, const char* configTopic, const char* stateTopic) : Sensor(name, uniqueId, configTopic, stateTopic) {
|
PressureSensor(const char* name, const char* uniqueId, const char* topic) : Sensor(name, uniqueId, topic) {
|
||||||
deviceClass = "pressure";
|
deviceClass = "pressure";
|
||||||
unitMeasure = "hPa";
|
unitMeasure = "hPa";
|
||||||
valueTemplate = "{{ value_json.pressure }}";
|
valueTemplate = "{{ value_json.pressure }}";
|
||||||
@ -86,7 +103,7 @@ namespace Ha {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct AltitudeSensor : Sensor {
|
struct AltitudeSensor : Sensor {
|
||||||
AltitudeSensor(const char* name, const char* uniqueId, const char* configTopic, const char* stateTopic) : Sensor(name, uniqueId, configTopic, stateTopic) {
|
AltitudeSensor(const char* name, const char* uniqueId, const char* topic) : Sensor(name, uniqueId, topic) {
|
||||||
deviceClass = "distance";
|
deviceClass = "distance";
|
||||||
unitMeasure = "m";
|
unitMeasure = "m";
|
||||||
valueTemplate = "{{ value_json.altitude }}";
|
valueTemplate = "{{ value_json.altitude }}";
|
||||||
|
|||||||
@ -34,7 +34,6 @@ namespace Mqtt {
|
|||||||
}
|
}
|
||||||
} commands;
|
} commands;
|
||||||
|
|
||||||
const char* bmpTopic = "homeassistant/sensor/esp_clock/state";
|
|
||||||
const char* espClockTopic = "homeassistant/+/esp_clock/#";
|
const char* espClockTopic = "homeassistant/+/esp_clock/#";
|
||||||
|
|
||||||
void connect() {
|
void connect() {
|
||||||
@ -47,23 +46,23 @@ namespace Mqtt {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Ha::Sensor sensors[] = {
|
Ha::Sensor sensors[] = {
|
||||||
Ha::TemperatureSensor{"Living room Temperature", "living_room_temperature", "homeassistant/sensor/esp_clock/temperature/config", bmpTopic},
|
Ha::TemperatureSensor{"Living room Temperature", "living_room_temperature", "temperature"},
|
||||||
Ha::PressureSensor{"Living room Pressure", "living_room_pressure", "homeassistant/sensor/esp_clock/pressure/config", bmpTopic},
|
Ha::PressureSensor{"Living room Pressure", "living_room_pressure", "pressure"},
|
||||||
Ha::AltitudeSensor{"Living room Altitude", "living_room_altitude", "homeassistant/sensor/esp_clock/altitude/config", bmpTopic}
|
Ha::AltitudeSensor{"Living room Altitude", "living_room_altitude", "altitude"}
|
||||||
};
|
};
|
||||||
|
|
||||||
Ha::Command switches[] = {
|
Ha::Command switches[] = {
|
||||||
Ha::Command{"Restart", "esp_clock_restart", "homeassistant/button/esp_clock/restart/config", "homeassistant/button/esp_clock/restart",
|
Ha::Button{"ESP Clock Restart", "esp_clock_restart", "restart",
|
||||||
[](const char* msg) {
|
[](const char* msg) {
|
||||||
if (String { "PRESS" }.equals(msg)) ESP.restart();
|
if (String { "PRESS" }.equals(msg)) ESP.restart();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Ha::Command{"ESP Clock Led", "esp_clock_led", "homeassistant/switch/esp_clock/led/config", "homeassistant/switch/esp_clock/led/set",
|
Ha::Switch{"ESP Clock Led", "esp_clock_led", "led",
|
||||||
[](const char* msg) {
|
[](const char* msg) {
|
||||||
String{ "ON" }.equals(msg) ? digitalWrite(LED_BUILTIN, LOW) : digitalWrite(LED_BUILTIN, HIGH);
|
String{ "ON" }.equals(msg) ? digitalWrite(LED_BUILTIN, LOW) : digitalWrite(LED_BUILTIN, HIGH);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Ha::Command{"ESP Clock Format 24h", "esp_clock_format_24h", "homeassistant/switch/esp_clock/hour_format/config", "homeassistant/switch/esp_clock/hour_format/set",
|
Ha::Switch{"ESP Clock Format 24h", "esp_clock_format_24h", "hour_format",
|
||||||
[](const char* msg) {
|
[](const char* msg) {
|
||||||
if (String{ "ON" }.equals(msg)) {
|
if (String{ "ON" }.equals(msg)) {
|
||||||
Display::hourFormat24 = true;
|
Display::hourFormat24 = true;
|
||||||
@ -104,7 +103,7 @@ namespace Mqtt {
|
|||||||
jsonDoc["altitude"] = Bmp::data.altitude;
|
jsonDoc["altitude"] = Bmp::data.altitude;
|
||||||
char message[255];
|
char message[255];
|
||||||
serializeJson(jsonDoc, message);
|
serializeJson(jsonDoc, message);
|
||||||
client.publish(bmpTopic, 0, true, message);
|
client.publish(Ha::Sensor::stateTopic, 0, true, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
void publishCommand() {
|
void publishCommand() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user