diff --git a/include/MqttHandleHass.h b/include/MqttHandleHass.h index 7be812092..8f99ccdf1 100644 --- a/include/MqttHandleHass.h +++ b/include/MqttHandleHass.h @@ -60,15 +60,19 @@ class MqttHandleHassClass { void loop(); static void publish(const String& subtopic, const String& payload); + // Binary Sensor static void publishBinarySensor(JsonDocument& doc, const String& root_device, const String& unique_id_prefix, const String& name, const String& state_topic, const String& payload_on, const String& payload_off, const String& device_class, const String& category); static void publishDtuBinarySensor(const String& name, const String& state_topic, const String& payload_on, const String& payload_off, const String& device_class, const String& category); static void publishInverterBinarySensor(std::shared_ptr inv, const String& name, const String& state_topic, const String& payload_on, const String& payload_off); + // Sensor + static void publishSensor(JsonDocument& doc, const String& root_device, const String& unique_id_prefix, const String& name, const String& state_topic, const String& unit_of_measure, const String& icon, const String& device_class, const String& category); static void publishDtuSensor(const String& name, const String& state_topic, const String& unit_of_measure, const String& icon, const String& device_class, const String& category); + static void publishInverterSensor(std::shared_ptr inv, const String& name, const String& state_topic, const String& unit_of_measure, const String& icon, const String& device_class, const String& category); + static void publishInverterField(std::shared_ptr inv, const ChannelType_t type, const ChannelNum_t channel, const byteAssign_fieldDeviceClass_t fieldType, const bool clear = false); static void publishInverterButton(std::shared_ptr inv, const String& name, const String& state_topic, const String& payload, const String& icon, const String& device_class, const String& category); static void publishInverterNumber(std::shared_ptr inv, const String& name, const String& state_topic, const String& command_topic, const int16_t min, const int16_t max, float step, const String& unit_of_measure, const String& icon, const String& category); - static void publishInverterSensor(std::shared_ptr inv, const String& name, const String& state_topic, const String& unit_of_measure, const String& icon, const String& device_class, const String& category); static void createInverterInfo(JsonDocument& doc, std::shared_ptr inv); static void createDtuInfo(JsonDocument& doc); diff --git a/src/MqttHandleHass.cpp b/src/MqttHandleHass.cpp index ed5a76bbc..213ea8443 100644 --- a/src/MqttHandleHass.cpp +++ b/src/MqttHandleHass.cpp @@ -275,97 +275,6 @@ void MqttHandleHassClass::publishInverterNumber( publish(configTopic, buffer); } -void MqttHandleHassClass::publishInverterSensor(std::shared_ptr inv, const String& name, const String& state_topic, const String& unit_of_measure, const String& icon, const String& device_class, const String& category) -{ - const String serial = inv->serialString(); - - String sensorId = name; - sensorId.replace(" ", "_"); - sensorId.toLowerCase(); - - const String configTopic = "sensor/dtu_" + serial - + "/" + sensorId - + "/config"; - - const String statTopic = MqttSettings.getPrefix() + serial + "/" + state_topic; - - JsonDocument root; - - root["name"] = name; - root["uniq_id"] = serial + "_" + sensorId; - if (device_class != "") { - root["dev_cla"] = device_class; - } - if (category != "") { - root["ent_cat"] = category; - } - if (icon != "") { - root["ic"] = icon; - } - if (unit_of_measure != "") { - root["unit_of_meas"] = unit_of_measure; - } - root["stat_t"] = statTopic; - - root["avty_t"] = MqttSettings.getPrefix() + Configuration.get().Mqtt.Lwt.Topic; - - const CONFIG_T& config = Configuration.get(); - root["pl_avail"] = config.Mqtt.Lwt.Value_Online; - root["pl_not_avail"] = config.Mqtt.Lwt.Value_Offline; - - createInverterInfo(root, inv); - - if (!Utils::checkJsonAlloc(root, __FUNCTION__, __LINE__)) { - return; - } - - String buffer; - serializeJson(root, buffer); - publish(configTopic, buffer); -} - -void MqttHandleHassClass::publishDtuSensor(const String& name, const String& state_topic, const String& unit_of_measure, const String& icon, const String& device_class, const String& category) -{ - String id = name; - id.toLowerCase(); - id.replace(" ", "_"); - - JsonDocument root; - - root["name"] = name; - root["uniq_id"] = getDtuUniqueId() + "_" + id; - if (device_class != "") { - root["dev_cla"] = device_class; - } - if (category != "") { - root["ent_cat"] = category; - } - if (icon != "") { - root["ic"] = icon; - } - if (unit_of_measure != "") { - root["unit_of_meas"] = unit_of_measure; - } - root["stat_t"] = MqttSettings.getPrefix() + "dtu" + "/" + state_topic; - - root["avty_t"] = MqttSettings.getPrefix() + Configuration.get().Mqtt.Lwt.Topic; - - const CONFIG_T& config = Configuration.get(); - root["pl_avail"] = config.Mqtt.Lwt.Value_Online; - root["pl_not_avail"] = config.Mqtt.Lwt.Value_Offline; - - createDtuInfo(root); - - if (!Utils::checkJsonAlloc(root, __FUNCTION__, __LINE__)) { - return; - } - - String buffer; - const String configTopic = "sensor/" + getDtuUniqueId() + "/" + id + "/config"; - serializeJson(root, buffer); - publish(configTopic, buffer); -} - void MqttHandleHassClass::createInverterInfo(JsonDocument& root, std::shared_ptr inv) { createDeviceInfo( @@ -474,3 +383,59 @@ void MqttHandleHassClass::publishInverterBinarySensor(std::shared_ptr inv, const String& name, const String& state_topic, const String& unit_of_measure, const String& icon, const String& device_class, const String& category) +{ + const String serial = inv->serialString(); + + JsonDocument root; + createInverterInfo(root, inv); + publishSensor(root, "dtu_" + serial, serial, name, serial + "/" + state_topic, unit_of_measure, icon, device_class, category); +}