forked from tbnobody/OpenDTU
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4334e60
commit a11723b
Showing
5 changed files
with
164 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
/* | ||
* Copyright (C) 2024 Thomas Basler and others | ||
*/ | ||
#pragma once | ||
|
||
#include <ArduinoJson.h> | ||
|
||
class MqttHassPublisher { | ||
public: | ||
static void publish(const String& subtopic, const JsonDocument& payload); | ||
|
||
protected: | ||
static JsonObject createDeviceInfo(const String& name, const String& identifiers, const String& model, const String& sw_version, const bool& via_dtu); | ||
static String getDtuUniqueId(); | ||
|
||
void publishBinarySensor(const String& unique_dentifier, const String& name, const String& icon, const String& sensorId, const String& statSubTopic); | ||
|
||
virtual String configTopicPrefix(); | ||
virtual JsonObject createDeviceInfo(); | ||
|
||
private: | ||
static String getDtuUrl(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
/* | ||
* Copyright (C) 2024 Thomas Basler and others | ||
*/ | ||
#include "MqttHassPublisher.h" | ||
#include "MqttSettings.h" | ||
#include "NetworkSettings.h" | ||
#include "Utils.h" | ||
#include "Configuration.h" | ||
|
||
void MqttHassPublisher::publish(const String& subtopic, const JsonDocument& payload) | ||
{ | ||
String buffer; | ||
serializeJson(payload, buffer); | ||
|
||
String topic = Configuration.get().Mqtt.Hass.Topic; | ||
topic += subtopic; | ||
MqttSettings.publishGeneric(topic, buffer, Configuration.get().Mqtt.Hass.Retain); | ||
} | ||
|
||
|
||
JsonObject MqttHassPublisher::createDeviceInfo( | ||
const String& name, const String& identifiers, | ||
const String& model, const String& sw_version, | ||
const bool& via_dtu) | ||
{ | ||
JsonObject object; | ||
|
||
object["name"] = name; | ||
object["ids"] = identifiers; | ||
object["cu"] = getDtuUrl(), | ||
object["mf"] = "OpenDTU"; | ||
object["mdl"] = model; | ||
object["sw"] = sw_version; | ||
|
||
if (via_dtu) { | ||
object["via_device"] = getDtuUniqueId(); | ||
} | ||
|
||
return object; | ||
} | ||
|
||
void MqttHassPublisher::publishBinarySensor( | ||
const String& unique_dentifier, | ||
const String& name, const String& icon, | ||
const String& sensorId, const String& statSubTopic) | ||
{ | ||
JsonDocument root; | ||
|
||
root["name"] = name; | ||
root["uniq_id"] = unique_dentifier; | ||
root["stat_t"] = MqttSettings.getPrefix() + "/" + statSubTopic; | ||
root["pl_on"] = "1"; | ||
root["pl_off"] = "0"; | ||
|
||
if (icon != nullptr) { | ||
root["icon"] = icon; | ||
} | ||
|
||
root["dev"] = createDeviceInfo(); | ||
|
||
if (!Utils::checkJsonAlloc(root, __FUNCTION__, __LINE__)) { | ||
return; | ||
} | ||
|
||
publish( | ||
"binary_sensor/" + configTopicPrefix() + "/" + sensorId + "/config", | ||
root | ||
); | ||
} | ||
|
||
String MqttHassPublisher::getDtuUniqueId() | ||
{ | ||
return NetworkSettings.getHostname() + "_" + Utils::getChipId(); | ||
} | ||
|
||
String MqttHassPublisher::getDtuUrl() | ||
{ | ||
return String("http://") + NetworkSettings.localIP().toString(); | ||
} |