extract out building of hass config
This commit is contained in:
parent
391bd841e9
commit
dd99a71b37
58
include/ha.h
Normal file
58
include/ha.h
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
|
#define JSON_SIZE 512
|
||||||
|
|
||||||
|
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 }}";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void buildDeviceConfig(JsonDocument& jsonDoc) {
|
||||||
|
JsonObject device = jsonDoc.createNestedObject("device");
|
||||||
|
device["name"] = "ESP Clock";
|
||||||
|
JsonArray connections = device.createNestedArray("connections");
|
||||||
|
JsonArray mac = connections.createNestedArray();
|
||||||
|
mac.add("mac");
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,6 +1,7 @@
|
|||||||
#include <queue>
|
#include <queue>
|
||||||
#include <AsyncMqttClient.h>
|
#include <AsyncMqttClient.h>
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
|
#include "ha.h"
|
||||||
|
|
||||||
#define MQTT_HOST IPAddress(192, 168, 5, 138)
|
#define MQTT_HOST IPAddress(192, 168, 5, 138)
|
||||||
#define MQTT_PORT 1883
|
#define MQTT_PORT 1883
|
||||||
@ -30,50 +31,15 @@ namespace Mqtt {
|
|||||||
|
|
||||||
const char* bmpTopic = "homeassistant/sensor/esp_clock/state";
|
const char* bmpTopic = "homeassistant/sensor/esp_clock/state";
|
||||||
|
|
||||||
void buildDeviceConfig(JsonDocument& jsonDoc) {
|
|
||||||
JsonObject device = jsonDoc.createNestedObject("device");
|
|
||||||
device["name"] = "ESP Clock";
|
|
||||||
JsonArray connections = device.createNestedArray("connections");
|
|
||||||
JsonArray mac = connections.createNestedArray();
|
|
||||||
mac.add("mac");
|
|
||||||
mac.add("24:62:AB:09:4B:D6");
|
|
||||||
}
|
|
||||||
|
|
||||||
void buildTempConfig(JsonDocument& jsonDoc) {
|
|
||||||
buildDeviceConfig(jsonDoc);
|
|
||||||
jsonDoc["device_class"] = "temperature";
|
|
||||||
jsonDoc["name"] = "Livingroom Temperature";
|
|
||||||
jsonDoc["unique_id"] = "livingroom_temperature";
|
|
||||||
jsonDoc["unit_of_measurement"] = "°C";
|
|
||||||
jsonDoc["state_topic"] = bmpTopic;
|
|
||||||
jsonDoc["value_template"] = "{{ value_json.temperature}}";
|
|
||||||
}
|
|
||||||
|
|
||||||
void buildPressureConfig(JsonDocument& jsonDoc) {
|
|
||||||
buildDeviceConfig(jsonDoc);
|
|
||||||
jsonDoc["device_class"] = "pressure";
|
|
||||||
jsonDoc["name"] = "Livingroom Pressure";
|
|
||||||
jsonDoc["unique_id"] = "livingroom_pressure";
|
|
||||||
jsonDoc["unit_of_measurement"] = "hPa";
|
|
||||||
jsonDoc["state_topic"] = bmpTopic;
|
|
||||||
jsonDoc["value_template"] = "{{ value_json.pressure}}";
|
|
||||||
}
|
|
||||||
|
|
||||||
void publishTempConfig() {
|
void publishTempConfig() {
|
||||||
StaticJsonDocument<512> jsonDoc;
|
char message[JSON_SIZE];
|
||||||
buildTempConfig(jsonDoc);
|
Ha::buildSensorConfig(message, Ha::TemperatureConfig{"Livingroom Temperature", "livingroom_temperature", bmpTopic});
|
||||||
|
|
||||||
char message[512];
|
|
||||||
serializeJson(jsonDoc, message);
|
|
||||||
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() {
|
||||||
StaticJsonDocument<512> jsonDoc;
|
char message[JSON_SIZE];
|
||||||
buildPressureConfig(jsonDoc);
|
Ha::buildSensorConfig(message, Ha::PressureConfig{"Livingroom Pressure", "livingroom_pressure", bmpTopic});
|
||||||
|
|
||||||
char message[512];
|
|
||||||
serializeJson(jsonDoc, message);
|
|
||||||
client.publish("homeassistant/sensor/esp_clock/pressure/config", 0, true, message);
|
client.publish("homeassistant/sensor/esp_clock/pressure/config", 0, true, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user