unify sensor config

This commit is contained in:
Nicu Hodos 2023-07-08 00:34:43 +02:00
parent ea8c9a150c
commit 08ff0ba367
2 changed files with 52 additions and 56 deletions

View File

@ -4,48 +4,6 @@
namespace Ha { namespace Ha {
struct SensorConfig {
const char* name;
const char* uniqueId;
const char* stateTopic;
const char* deviceClass;
const char* unitMeasure;
const char* valueTemplate;
};
struct TemperatureConfig : SensorConfig {
TemperatureConfig(const char* name, const char* uniqueId, const char* stateTopic) {
this->name = name;
this->uniqueId = uniqueId;
this->stateTopic = stateTopic;
this->deviceClass = "temperature";
this->unitMeasure = "°C";
this->valueTemplate = "{{ value_json.temperature }}";
}
};
struct PressureConfig : SensorConfig {
PressureConfig(const char* name, const char* uniqueId, const char* stateTopic) {
this->name = name;
this->uniqueId = uniqueId;
this->stateTopic = stateTopic;
this->deviceClass = "pressure";
this->unitMeasure = "hPa";
this->valueTemplate = "{{ value_json.pressure }}";
}
};
struct AltitudeConfig : SensorConfig {
AltitudeConfig(const char* name, const char* uniqueId, const char* stateTopic) {
this->name = name;
this->uniqueId = uniqueId;
this->stateTopic = stateTopic;
this->deviceClass = "distance";
this->unitMeasure = "m";
this->valueTemplate = "{{ value_json.altitude }}";
}
};
void buildDeviceConfig(JsonDocument& jsonDoc) { void buildDeviceConfig(JsonDocument& jsonDoc) {
JsonObject device = jsonDoc.createNestedObject("device"); JsonObject device = jsonDoc.createNestedObject("device");
device["name"] = "ESP Clock"; device["name"] = "ESP Clock";
@ -56,17 +14,55 @@ namespace Ha {
mac.add(WiFi.macAddress()); mac.add(WiFi.macAddress());
} }
void buildSensorConfig(char(&output)[JSON_SIZE], SensorConfig config) { struct SensorConfig {
StaticJsonDocument<JSON_SIZE> jsonDoc; const char* name;
buildDeviceConfig(jsonDoc); const char* uniqueId;
jsonDoc["device_class"] = config.deviceClass; const char* deviceClass;
jsonDoc["name"] = config.name; const char* unitMeasure;
jsonDoc["unique_id"] = config.uniqueId; const char* valueTemplate;
jsonDoc["unit_of_measurement"] = config.unitMeasure; void buildConfig(char(&output)[JSON_SIZE], const char* stateTopic) {
jsonDoc["state_topic"] = config.stateTopic; StaticJsonDocument<JSON_SIZE> jsonDoc;
jsonDoc["value_template"] = config.valueTemplate; buildDeviceConfig(jsonDoc);
serializeJson(jsonDoc, output); jsonDoc["device_class"] = deviceClass;
} jsonDoc["name"] = name;
jsonDoc["unique_id"] = uniqueId;
jsonDoc["unit_of_measurement"] = unitMeasure;
jsonDoc["state_topic"] = stateTopic;
jsonDoc["value_template"] = valueTemplate;
serializeJson(jsonDoc, output);
}
};
struct TemperatureConfig : SensorConfig {
TemperatureConfig(const char* name, const char* uniqueId) {
this->name = name;
this->uniqueId = uniqueId;
deviceClass = "temperature";
unitMeasure = "°C";
valueTemplate = "{{ value_json.temperature }}";
}
};
struct PressureConfig : SensorConfig {
PressureConfig(const char* name, const char* uniqueId) {
this->name = name;
this->uniqueId = uniqueId;
this->deviceClass = "pressure";
this->unitMeasure = "hPa";
this->valueTemplate = "{{ value_json.pressure }}";
}
};
struct AltitudeConfig : SensorConfig {
AltitudeConfig(const char* name, const char* uniqueId) {
this->name = name;
this->uniqueId = uniqueId;
this->deviceClass = "distance";
this->unitMeasure = "m";
this->valueTemplate = "{{ value_json.altitude }}";
}
};
void buildRestartConfig(char(&output)[JSON_SIZE], const char* topic) { void buildRestartConfig(char(&output)[JSON_SIZE], const char* topic) {
StaticJsonDocument<JSON_SIZE> jsonDoc; StaticJsonDocument<JSON_SIZE> jsonDoc;

View File

@ -51,19 +51,19 @@ namespace Mqtt {
void publishTempConfig() { void publishTempConfig() {
char message[JSON_SIZE]; char message[JSON_SIZE];
Ha::buildSensorConfig(message, Ha::TemperatureConfig{ "Living room Temperature", "living_room_temperature", bmpTopic }); Ha::TemperatureConfig{ "Living room Temperature", "living_room_temperature" }.buildConfig(message, bmpTopic);
client.publish("homeassistant/sensor/esp_clock/temperature/config", 0, true, message); client.publish("homeassistant/sensor/esp_clock/temperature/config", 0, true, message);
} }
void publishPressureConfig() { void publishPressureConfig() {
char message[JSON_SIZE]; char message[JSON_SIZE];
Ha::buildSensorConfig(message, Ha::PressureConfig{ "Living room Pressure", "living_room_pressure", bmpTopic }); Ha::PressureConfig{ "Living room Pressure", "living_room_pressure" }.buildConfig(message, bmpTopic);
client.publish("homeassistant/sensor/esp_clock/pressure/config", 0, true, message); client.publish("homeassistant/sensor/esp_clock/pressure/config", 0, true, message);
} }
void publishAltitudeConfig() { void publishAltitudeConfig() {
char message[JSON_SIZE]; char message[JSON_SIZE];
Ha::buildSensorConfig(message, Ha::AltitudeConfig{ "Living room Altitude", "living_room_altitude", bmpTopic }); Ha::AltitudeConfig{ "Living room Altitude", "living_room_altitude" }.buildConfig(message, bmpTopic);
client.publish("homeassistant/sensor/esp_clock/altitude/config", 0, true, message); client.publish("homeassistant/sensor/esp_clock/altitude/config", 0, true, message);
} }