unify sensor config
This commit is contained in:
parent
ea8c9a150c
commit
08ff0ba367
94
include/ha.h
94
include/ha.h
@ -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,18 +14,56 @@ namespace Ha {
|
|||||||
mac.add(WiFi.macAddress());
|
mac.add(WiFi.macAddress());
|
||||||
}
|
}
|
||||||
|
|
||||||
void buildSensorConfig(char(&output)[JSON_SIZE], SensorConfig config) {
|
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;
|
StaticJsonDocument<JSON_SIZE> jsonDoc;
|
||||||
buildDeviceConfig(jsonDoc);
|
buildDeviceConfig(jsonDoc);
|
||||||
jsonDoc["device_class"] = config.deviceClass;
|
jsonDoc["device_class"] = deviceClass;
|
||||||
jsonDoc["name"] = config.name;
|
jsonDoc["name"] = name;
|
||||||
jsonDoc["unique_id"] = config.uniqueId;
|
jsonDoc["unique_id"] = uniqueId;
|
||||||
jsonDoc["unit_of_measurement"] = config.unitMeasure;
|
jsonDoc["unit_of_measurement"] = unitMeasure;
|
||||||
jsonDoc["state_topic"] = config.stateTopic;
|
jsonDoc["state_topic"] = stateTopic;
|
||||||
jsonDoc["value_template"] = config.valueTemplate;
|
jsonDoc["value_template"] = valueTemplate;
|
||||||
serializeJson(jsonDoc, output);
|
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;
|
||||||
buildDeviceConfig(jsonDoc);
|
buildDeviceConfig(jsonDoc);
|
||||||
|
|||||||
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user