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