unify sensor config
This commit is contained in:
parent
ea8c9a150c
commit
08ff0ba367
102
include/ha.h
102
include/ha.h
@ -4,48 +4,6 @@
|
||||
|
||||
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) {
|
||||
JsonObject device = jsonDoc.createNestedObject("device");
|
||||
device["name"] = "ESP Clock";
|
||||
@ -56,17 +14,55 @@ namespace Ha {
|
||||
mac.add(WiFi.macAddress());
|
||||
}
|
||||
|
||||
void buildSensorConfig(char(&output)[JSON_SIZE], SensorConfig config) {
|
||||
StaticJsonDocument<JSON_SIZE> jsonDoc;
|
||||
buildDeviceConfig(jsonDoc);
|
||||
jsonDoc["device_class"] = config.deviceClass;
|
||||
jsonDoc["name"] = config.name;
|
||||
jsonDoc["unique_id"] = config.uniqueId;
|
||||
jsonDoc["unit_of_measurement"] = config.unitMeasure;
|
||||
jsonDoc["state_topic"] = config.stateTopic;
|
||||
jsonDoc["value_template"] = config.valueTemplate;
|
||||
serializeJson(jsonDoc, output);
|
||||
}
|
||||
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 buildRestartConfig(char(&output)[JSON_SIZE], const char* topic) {
|
||||
StaticJsonDocument<JSON_SIZE> jsonDoc;
|
||||
|
||||
@ -51,19 +51,19 @@ namespace Mqtt {
|
||||
|
||||
void publishTempConfig() {
|
||||
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);
|
||||
}
|
||||
|
||||
void publishPressureConfig() {
|
||||
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);
|
||||
}
|
||||
|
||||
void publishAltitudeConfig() {
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user