From 08ff0ba36762e170715fbb54741e6ace9e6387be Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Sat, 8 Jul 2023 00:34:43 +0200 Subject: [PATCH] unify sensor config --- include/ha.h | 102 ++++++++++++++++++++++++------------------------- include/mqtt.h | 6 +-- 2 files changed, 52 insertions(+), 56 deletions(-) diff --git a/include/ha.h b/include/ha.h index 9b628c1..6961230 100644 --- a/include/ha.h +++ b/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 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 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 jsonDoc; diff --git a/include/mqtt.h b/include/mqtt.h index b0c4b9f..0359bb7 100644 --- a/include/mqtt.h +++ b/include/mqtt.h @@ -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); }