make build configuration consistent

This commit is contained in:
Nicu Hodos 2023-07-08 01:01:11 +02:00
parent 7412029272
commit b2e049f930
2 changed files with 46 additions and 53 deletions

View File

@ -14,56 +14,6 @@ namespace Ha {
mac.add(WiFi.macAddress());
}
struct SensorConfig {
const char* name;
const char* uniqueId;
const char* deviceClass;
const char* unitMeasure;
const char* valueTemplate;
void buildConfig(char(&output)[JSON_SIZE], const char* stateTopic) {
StaticJsonDocument<JSON_SIZE> jsonDoc;
buildDeviceConfig(jsonDoc);
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 buildCommandConfig(char(&output)[JSON_SIZE], const char* name, const char* uniqueId, const char* commandTopic) {
StaticJsonDocument<JSON_SIZE> jsonDoc;
buildDeviceConfig(jsonDoc);
@ -72,4 +22,47 @@ namespace Ha {
jsonDoc["command_topic"] = commandTopic;
serializeJson(jsonDoc, output);
}
struct SensorConfig {
const char* deviceClass;
const char* unitMeasure;
const char* valueTemplate;
void buildConfig(char(&output)[JSON_SIZE], const char* name, const char* uniqueId, const char* stateTopic) {
StaticJsonDocument<JSON_SIZE> jsonDoc;
buildDeviceConfig(jsonDoc);
jsonDoc["name"] = name;
jsonDoc["unique_id"] = uniqueId;
jsonDoc["state_topic"] = stateTopic;
jsonDoc["device_class"] = deviceClass;
jsonDoc["unit_of_measurement"] = unitMeasure;
jsonDoc["value_template"] = valueTemplate;
serializeJson(jsonDoc, output);
}
};
struct TemperatureConfig : SensorConfig {
TemperatureConfig() {
deviceClass = "temperature";
unitMeasure = "°C";
valueTemplate = "{{ value_json.temperature }}";
}
};
struct PressureConfig : SensorConfig {
PressureConfig() {
deviceClass = "pressure";
unitMeasure = "hPa";
valueTemplate = "{{ value_json.pressure }}";
}
};
struct AltitudeConfig : SensorConfig {
AltitudeConfig() {
deviceClass = "distance";
unitMeasure = "m";
valueTemplate = "{{ value_json.altitude }}";
}
};
}

View File

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