Skip to content

Commit

Permalink
configurable lwt topic
Browse files Browse the repository at this point in the history
resolves #101
  • Loading branch information
rednblkx committed Aug 8, 2024
1 parent add0869 commit 4ebfb7f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
16 changes: 11 additions & 5 deletions data/mqtt.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ <h3>Broker Settings</h3>
<input type="text" name="mqtt-clientid" id="mqtt-clientid" placeholder="homekey_mqtt" required value="%MQTTCLIENTID%">
</div>
</div>
<div style="display: grid; gap: 8px; grid-template-columns: auto auto;">
<div style="display: grid; gap: 8px; grid-template-columns: auto auto;margin-bottom: .5rem;">
<div style="display: flex; flex-direction: column;">
<label for="mqtt-username">Username</label>
<input type="text" name="mqtt-username" id="mqtt-username" placeholder="username" required value="%MQTTUSERNAME%">
Expand All @@ -25,10 +25,16 @@ <h3>Broker Settings</h3>
<input type="password" name="mqtt-password" id="mqtt-password" placeholder="password" required value="%MQTTPASSWORD%">
</div>
</div>
<div style="margin-top: 1rem;">
<label for="mqtt-discovery-enable">Enable HASS MQTT Discovery</label>
<input type='hidden' name='mqtt-discovery-enable' value='0'>
<input type="checkbox" name="mqtt-discovery-enable" id="mqtt-discovery-enable" value="1">
<div style="display: grid; gap: 8px; grid-template-columns: auto;">
<div style="display: grid; gap: 8px; grid-template-columns: auto auto;">
<label for="mqtt-lwt-topic">MQTT LWT Topic</label>
<input type="text" name="mqtt-lwt-topic" id="mqtt-lwt-topic" placeholder="homekey_mqtt/status" required="" value="%MQTTLWTTOPIC%">
</div>
<div style="display: grid; gap: 8px; grid-template-columns: auto auto;">
<label for="mqtt-discovery-enable">Enable HASS MQTT Discovery</label>
<input type="hidden" name="mqtt-discovery-enable" value="0">
<input type="checkbox" name="mqtt-discovery-enable" id="mqtt-discovery-enable" value="1">
</div>
</div>
<h3>MQTT Core Topics</h3>
<div style="display: grid; gap: 8px; grid-template-columns: auto auto;">
Expand Down
1 change: 1 addition & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ enum customLockActions
#define MQTT_DISCOVERY true //Enable or disable discovery for home assistant tags functionality, set to true to enable.

// MQTT Topics
#define MQTT_LWT_TOPIC MQTT_CLIENTID"/status"
#define MQTT_CUSTOM_STATE_TOPIC "topic/homekey/custom_state" // MQTT Topic for publishing custom lock state
#define MQTT_CUSTOM_STATE_CTRL_TOPIC "topic/homekey/set_custom_state" // MQTT Control Topic with custom lock state
#define MQTT_AUTH_TOPIC "topic/homekey/auth" // MQTT Topic for publishing HomeKey authentication data or RFID UID
Expand Down
24 changes: 16 additions & 8 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ namespace espConfig
std::string mqttPassword = MQTT_PASSWORD;
std::string mqttClientId = MQTT_CLIENTID;
/* MQTT Topics */
std::string lwtTopic = MQTT_LWT_TOPIC;
std::string hkTopic = MQTT_AUTH_TOPIC;
std::string lockStateTopic = MQTT_STATE_TOPIC;
std::string lockStateCmd = MQTT_SET_STATE_TOPIC;
Expand All @@ -61,7 +62,7 @@ namespace espConfig
bool hassMqttDiscoveryEnabled = MQTT_DISCOVERY;
std::map<std::string, int> customLockStates = { {"C_LOCKED", C_LOCKED}, {"C_UNLOCKING", C_UNLOCKING}, {"C_UNLOCKED", C_UNLOCKED}, {"C_LOCKING", C_LOCKING}, {"C_JAMMED", C_JAMMED}, {"C_UNKNOWN", C_UNKNOWN} };
std::map<std::string, int> customLockActions = { {"UNLOCK", UNLOCK}, {"LOCK", LOCK} };
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(espConfig::mqttConfig_t, mqttBroker, mqttPort, mqttUsername, mqttPassword, mqttClientId, hkTopic, lockStateTopic, lockStateCmd, lockCStateCmd, lockTStateCmd, lockCustomStateTopic, lockCustomStateCmd, lockEnableCustomState, hassMqttDiscoveryEnabled, customLockStates, customLockActions)
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(espConfig::mqttConfig_t, mqttBroker, mqttPort, mqttUsername, mqttPassword, mqttClientId, lwtTopic, hkTopic, lockStateTopic, lockStateCmd, lockCStateCmd, lockTStateCmd, lockCustomStateTopic, lockCustomStateCmd, lockEnableCustomState, hassMqttDiscoveryEnabled, customLockStates, customLockActions)
} mqttData;

struct misc_config_t
Expand Down Expand Up @@ -459,8 +460,6 @@ void mqtt_connected_event(void* event_handler_arg, esp_event_base_t event_base,
std::string serialNumber = "HK-";
serialNumber.append(macStr);
LOG(I, "MQTT connected");
std::string statusTopic;
statusTopic.append(espConfig::mqttData.mqttClientId).append("/status");
if (espConfig::mqttData.hassMqttDiscoveryEnabled) {
json payload;
payload["topic"] = espConfig::mqttData.hkTopic.c_str();
Expand Down Expand Up @@ -508,7 +507,7 @@ void mqtt_connected_event(void* event_handler_arg, esp_event_base_t event_base,
payload["state_unlocking"] = "4";
payload["state_locking"] = "5";
payload["state_jammed"] = "2";
payload["availability_topic"] = statusTopic.c_str();
payload["availability_topic"] = espConfig::mqttData.lwtTopic.c_str();
payload["unique_id"] = id;
payload["device"] = device;
payload["retain"] = "false";
Expand All @@ -518,7 +517,7 @@ void mqtt_connected_event(void* event_handler_arg, esp_event_base_t event_base,
esp_mqtt_client_publish(client, lockConfigTopic.c_str(), bufferpub.c_str(), bufferpub.length(), 1, true);
LOG(D, "MQTT PUBLISHED DISCOVERY");
}
esp_mqtt_client_publish(client, statusTopic.c_str(), "online", 6, 1, true);
esp_mqtt_client_publish(client, espConfig::mqttData.lwtTopic.c_str(), "online", 6, 1, true);
if (espConfig::mqttData.lockEnableCustomState) {
esp_mqtt_client_subscribe(client, espConfig::mqttData.lockCustomStateCmd.c_str(), 0);
}
Expand Down Expand Up @@ -560,15 +559,13 @@ void mqtt_data_handler(void* event_handler_arg, esp_event_base_t event_base, int
* parameters.
*/
static void mqtt_app_start(void) {
std::string statusTopic;
statusTopic.append(espConfig::mqttData.mqttClientId).append("/status");
esp_mqtt_client_config_t mqtt_cfg = { };
mqtt_cfg.host = espConfig::mqttData.mqttBroker.c_str();
mqtt_cfg.port = espConfig::mqttData.mqttPort;
mqtt_cfg.client_id = espConfig::mqttData.mqttClientId.c_str();
mqtt_cfg.username = espConfig::mqttData.mqttUsername.c_str();
mqtt_cfg.password = espConfig::mqttData.mqttPassword.c_str();
mqtt_cfg.lwt_topic = statusTopic.c_str();
mqtt_cfg.lwt_topic = espConfig::mqttData.lwtTopic.c_str();
mqtt_cfg.lwt_msg = "offline";
mqtt_cfg.lwt_qos = 1;
mqtt_cfg.lwt_retain = 1;
Expand Down Expand Up @@ -726,6 +723,9 @@ String mqttHtmlProcess(const String& var) {
else if (var == "MQTTPASSWORD") {
return String(espConfig::mqttData.mqttPassword.c_str());
}
else if (var == "MQTTLWTTOPIC") {
return String(espConfig::mqttData.lwtTopic.c_str());
}
else if (var == "HKTOPIC") {
return String(espConfig::mqttData.hkTopic.c_str());
}
Expand Down Expand Up @@ -817,6 +817,9 @@ void setupWeb() {
else if (!strcmp(p->name().c_str(), "mqtt-password")) {
espConfig::mqttData.mqttPassword = p->value().c_str();
}
else if (!strcmp(p->name().c_str(), "mqtt-lwt-topic")) {
espConfig::mqttData.lwtTopic = p->value().c_str();
}
else if (!strcmp(p->name().c_str(), "mqtt-hktopic")) {
espConfig::mqttData.hkTopic = p->value().c_str();
}
Expand Down Expand Up @@ -1222,6 +1225,11 @@ void setup() {
LOG(D, "MQTTDATA - JSON(%d): %s", len, str.c_str());
try {
nlohmann::json data = nlohmann::json::parse(str);
if (!data.contains("lwtTopic") && data.contains("mqttClientId")) {
std::string lwt = data["mqttClientId"];
lwt.append("/status");
data["lwtTopic"] = lwt;
}
data.get_to<espConfig::mqttConfig_t>(espConfig::mqttData);
}
catch (const std::exception& e) {
Expand Down

0 comments on commit 4ebfb7f

Please sign in to comment.