make build configuration consistent
This commit is contained in:
parent
7412029272
commit
b2e049f930
93
include/ha.h
93
include/ha.h
@ -14,56 +14,6 @@ namespace Ha {
|
|||||||
mac.add(WiFi.macAddress());
|
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) {
|
void buildCommandConfig(char(&output)[JSON_SIZE], const char* name, const char* uniqueId, const char* commandTopic) {
|
||||||
StaticJsonDocument<JSON_SIZE> jsonDoc;
|
StaticJsonDocument<JSON_SIZE> jsonDoc;
|
||||||
buildDeviceConfig(jsonDoc);
|
buildDeviceConfig(jsonDoc);
|
||||||
@ -72,4 +22,47 @@ namespace Ha {
|
|||||||
jsonDoc["command_topic"] = commandTopic;
|
jsonDoc["command_topic"] = commandTopic;
|
||||||
serializeJson(jsonDoc, output);
|
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 }}";
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -51,19 +51,19 @@ namespace Mqtt {
|
|||||||
|
|
||||||
void publishTempConfig() {
|
void publishTempConfig() {
|
||||||
char message[JSON_SIZE];
|
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);
|
client.publish("homeassistant/sensor/esp_clock/temperature/config", 0, true, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
void publishPressureConfig() {
|
void publishPressureConfig() {
|
||||||
char message[JSON_SIZE];
|
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);
|
client.publish("homeassistant/sensor/esp_clock/pressure/config", 0, true, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
void publishAltitudeConfig() {
|
void publishAltitudeConfig() {
|
||||||
char message[JSON_SIZE];
|
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);
|
client.publish("homeassistant/sensor/esp_clock/altitude/config", 0, true, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user