generate uniqueId from id
This commit is contained in:
parent
acf009dbe8
commit
d3d04721e9
38
include/ha.h
38
include/ha.h
@ -7,18 +7,22 @@ namespace Ha {
|
||||
|
||||
struct Component {
|
||||
const char* name;
|
||||
const char* uniqueId;
|
||||
const char* id;
|
||||
char configTopic[TOPIC_SIZE];
|
||||
|
||||
Component(const char* name, const char* uniqueId, const char* topic, const char* type) {
|
||||
Component(const char* name, const char* id, const char* type) {
|
||||
this->name = name;
|
||||
this->uniqueId = uniqueId;
|
||||
sprintf(configTopic, "homeassistant/%s/esp_clock/%s/config", type, topic);
|
||||
this->id = id;
|
||||
sprintf(configTopic, "homeassistant/%s/esp_clock/%s/config", type, id);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@ -38,10 +42,14 @@ namespace Ha {
|
||||
char commandTopic[TOPIC_SIZE];
|
||||
onMessage f;
|
||||
|
||||
Command(const char* name, const char* uniqueId, const char* topic, const char* type, onMessage f) : Component(name, uniqueId, topic, type) {
|
||||
Command(const char* name, const char* id, const char* type, onMessage f) : Component(name, id, type) {
|
||||
this->f = f;
|
||||
}
|
||||
|
||||
void buildUniqueId(char* uniqueId) override {
|
||||
sprintf(uniqueId, "esp_clock_%s", id);
|
||||
}
|
||||
|
||||
void buildConfig(JsonDocument& jsonDoc) override {
|
||||
Component::buildConfig(jsonDoc);
|
||||
jsonDoc["command_topic"] = commandTopic;
|
||||
@ -52,8 +60,8 @@ 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);
|
||||
Button(const char* name, const char* id, onMessage f) : Command(name, id, type, f) {
|
||||
sprintf(commandTopic, "homeassistant/%s/esp_clock/%s", type, id);
|
||||
}
|
||||
|
||||
};
|
||||
@ -61,8 +69,8 @@ namespace Ha {
|
||||
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);
|
||||
Switch(const char* name, const char* id, onMessage f) : Command(name, id, type, f) {
|
||||
sprintf(commandTopic, "homeassistant/%s/esp_clock/%s/set", type, id);
|
||||
}
|
||||
|
||||
};
|
||||
@ -73,7 +81,11 @@ namespace Ha {
|
||||
const char* valueTemplate;
|
||||
static constexpr const char* stateTopic = "homeassistant/sensor/esp_clock/state";
|
||||
|
||||
Sensor(const char* name, const char* uniqueId, const char* topic) : Component(name, uniqueId, topic, "sensor") {
|
||||
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 {
|
||||
@ -87,7 +99,7 @@ namespace Ha {
|
||||
};
|
||||
|
||||
struct TemperatureSensor : Sensor {
|
||||
TemperatureSensor(const char* name, const char* uniqueId, const char* topic) : Sensor(name, uniqueId, topic) {
|
||||
TemperatureSensor(const char* name, const char* id) : Sensor(name, id) {
|
||||
deviceClass = "temperature";
|
||||
unitMeasure = "°C";
|
||||
valueTemplate = "{{ value_json.temperature }}";
|
||||
@ -95,7 +107,7 @@ namespace Ha {
|
||||
};
|
||||
|
||||
struct PressureSensor : Sensor {
|
||||
PressureSensor(const char* name, const char* uniqueId, const char* topic) : Sensor(name, uniqueId, topic) {
|
||||
PressureSensor(const char* name, const char* id) : Sensor(name, id) {
|
||||
deviceClass = "pressure";
|
||||
unitMeasure = "hPa";
|
||||
valueTemplate = "{{ value_json.pressure }}";
|
||||
@ -103,7 +115,7 @@ namespace Ha {
|
||||
};
|
||||
|
||||
struct AltitudeSensor : Sensor {
|
||||
AltitudeSensor(const char* name, const char* uniqueId, const char* topic) : Sensor(name, uniqueId, topic) {
|
||||
AltitudeSensor(const char* name, const char* id) : Sensor(name, id) {
|
||||
deviceClass = "distance";
|
||||
unitMeasure = "m";
|
||||
valueTemplate = "{{ value_json.altitude }}";
|
||||
|
||||
@ -46,23 +46,23 @@ namespace Mqtt {
|
||||
}
|
||||
|
||||
Ha::Sensor sensors[] = {
|
||||
Ha::TemperatureSensor{"Living room Temperature", "living_room_temperature", "temperature"},
|
||||
Ha::PressureSensor{"Living room Pressure", "living_room_pressure", "pressure"},
|
||||
Ha::AltitudeSensor{"Living room Altitude", "living_room_altitude", "altitude"}
|
||||
Ha::TemperatureSensor{"Living room Temperature", "temperature"},
|
||||
Ha::PressureSensor{"Living room Pressure", "pressure"},
|
||||
Ha::AltitudeSensor{"Living room Altitude", "altitude"}
|
||||
};
|
||||
|
||||
Ha::Command switches[] = {
|
||||
Ha::Button{"ESP Clock Restart", "esp_clock_restart", "restart",
|
||||
Ha::Button{"ESP Clock Restart", "restart",
|
||||
[](const char* msg) {
|
||||
if (String { "PRESS" }.equals(msg)) ESP.restart();
|
||||
}
|
||||
},
|
||||
Ha::Switch{"ESP Clock Led", "esp_clock_led", "led",
|
||||
Ha::Switch{"ESP Clock Led", "led",
|
||||
[](const char* msg) {
|
||||
String{ "ON" }.equals(msg) ? digitalWrite(LED_BUILTIN, LOW) : digitalWrite(LED_BUILTIN, HIGH);
|
||||
}
|
||||
},
|
||||
Ha::Switch{"ESP Clock Format 24h", "esp_clock_format_24h", "hour_format",
|
||||
Ha::Switch{"ESP Clock Format 24h", "format_24h",
|
||||
[](const char* msg) {
|
||||
if (String{ "ON" }.equals(msg)) {
|
||||
Display::hourFormat24 = true;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user