iot-firmware/src/espmega_iot_homeassistant.c...

29 lines
2.0 KiB
Plaintext

#include "espmega_iot_homeassistant.hpp"
void publishDiscoveryPayload(const char* component, const char* config) {
String topic = "homeassistant/" + String(component) + "/config";
mqttClient.publish(topic.c_str(), config);
}
void publishDiscoveryPayload(const char* component, const char* name, const char* deviceClass, const char* unitOfMeasurement, const char* stateTopic, const char* commandTopic, const char* availabilityTopic, const char* payloadAvailable, const char* payloadNotAvailable, const char* uniqueId) {
String config = "{";
config += "\"name\":\"" + String(name) + "\",";
config += "\"device_class\":\"" + String(deviceClass) + "\",";
config += "\"unit_of_measurement\":\"" + String(unitOfMeasurement) + "\",";
config += "\"state_topic\":\"" + String(stateTopic) + "\",";
config += "\"command_topic\":\"" + String(commandTopic) + "\",";
config += "\"availability_topic\":\"" + String(availabilityTopic) + "\",";
config += "\"payload_available\":\"" + String(payloadAvailable) + "\",";
config += "\"payload_not_available\":\"" + String(payloadNotAvailable) + "\",";
config += "\"unique_id\":\"" + String(uniqueId) + "\"";
config += "}";
String topic = "homeassistant/" + String(component) + "/config";
mqttClient.publish(topic.c_str(), config.c_str());
}
// Example usage:
publishDiscoveryPayload("sensor", "temperature", "temperature", "°C", "home/temperature", "home/temperature/set", "home/availability", "online", "offline", "temperature_sensor");
publishDiscoveryPayload("binary_sensor", "motion", "motion", "", "home/motion", "home/motion/set", "home/availability", "online", "offline", "motion_sensor");
publishDiscoveryPayload("climate", "thermostat", "temperature", "°C", "home/thermostat", "home/thermostat/set", "home/availability", "online", "offline", "thermostat");
publishDiscoveryPayload("switch", "light", "switch", "", "home/light", "home/light/set", "home/availability", "online", "offline", "light_switch");