Add input form for HASS discovery topic

This commit is contained in:
rodriguezst
2022-06-21 12:01:31 +00:00
committed by GitHub
parent 6f9b482bfb
commit 5d254d463b
3 changed files with 66 additions and 52 deletions

View File

@@ -345,64 +345,71 @@ void Network::publishPresenceDetection(char *csv)
void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* name, char* uidString, char* lockAction, char* unlockAction, char* openAction, char* lockedState, char* unlockedState)
{
String configJSON = "{\"dev\":{\"ids\":[\"nuki_";
configJSON.concat(uidString);
configJSON.concat("\"],\"mf\":\"Nuki\",\"mdl\":\"");
configJSON.concat(deviceType);
configJSON.concat("\",\"name\":\"");
configJSON.concat(name);
configJSON.concat("\"},\"~\":\"");
configJSON.concat(baseTopic);
configJSON.concat("\",\"name\":\"");
configJSON.concat(name);
configJSON.concat("\",\"unique_id\":\"");
configJSON.concat(uidString);
configJSON.concat("_lock\",\"cmd_t\":\"~");
configJSON.concat(mqtt_topic_lock_action);
configJSON.concat("\",\"pl_lock\":\"");
configJSON.concat(lockAction);
configJSON.concat("\",\"pl_unlk\":\"");
configJSON.concat(unlockAction);
configJSON.concat("\",\"pl_open\":\"");
configJSON.concat(openAction);
configJSON.concat("\",\"stat_t\":\"~");
configJSON.concat(mqtt_topic_lock_state);
configJSON.concat("\",\"stat_locked\":\"");
configJSON.concat(lockedState);
configJSON.concat("\",\"stat_unlocked\":\"");
configJSON.concat(unlockedState);
configJSON.concat("\",\"opt\":\"false\"}");
String discoveryTopic = _preferences->getString(preference_mqtt_hass_discovery);
String path = "homeassistant/lock/";
path.concat(uidString);
path.concat("/smartlock/config");
if(discoveryTopic != "")
{
String configJSON = "{\"dev\":{\"ids\":[\"nuki_";
configJSON.concat(uidString);
configJSON.concat("\"],\"mf\":\"Nuki\",\"mdl\":\"");
configJSON.concat(deviceType);
configJSON.concat("\",\"name\":\"");
configJSON.concat(name);
configJSON.concat("\"},\"~\":\"");
configJSON.concat(baseTopic);
configJSON.concat("\",\"name\":\"");
configJSON.concat(name);
configJSON.concat("\",\"unique_id\":\"");
configJSON.concat(uidString);
configJSON.concat("_lock\",\"cmd_t\":\"~");
configJSON.concat(mqtt_topic_lock_action);
configJSON.concat("\",\"pl_lock\":\"");
configJSON.concat(lockAction);
configJSON.concat("\",\"pl_unlk\":\"");
configJSON.concat(unlockAction);
configJSON.concat("\",\"pl_open\":\"");
configJSON.concat(openAction);
configJSON.concat("\",\"stat_t\":\"~");
configJSON.concat(mqtt_topic_lock_state);
configJSON.concat("\",\"stat_locked\":\"");
configJSON.concat(lockedState);
configJSON.concat("\",\"stat_unlocked\":\"");
configJSON.concat(unlockedState);
configJSON.concat("\",\"opt\":\"false\"}");
Serial.println("HASS Config:");
Serial.println(configJSON);
String path = discoveryTopic;
path.concat("/lock/");
path.concat(uidString);
path.concat("/smartlock/config");
_device->mqttClient()->publish(path.c_str(), configJSON.c_str(), true);
Serial.println("HASS Config:");
Serial.println(configJSON);
configJSON = "{\"dev\":{\"ids\":[\"nuki_";
configJSON.concat(uidString);
configJSON.concat("\"],\"mf\":\"Nuki\",\"mdl\":\"");
configJSON.concat(deviceType);
configJSON.concat("\",\"name\":\"");
configJSON.concat(name);
configJSON.concat("\"},\"~\":\"");
configJSON.concat(baseTopic);
configJSON.concat("\",\"name\":\"");
configJSON.concat(name);
configJSON.concat(" battery low\",\"unique_id\":\"");
configJSON.concat(uidString);
configJSON.concat("_battery_low\",\"dev_cla\":\"battery\",\"ent_cat\":\"diagnostic\",\"pl_off\":\"0\",\"pl_on\":\"1\",\"stat_t\":\"~");
configJSON.concat(mqtt_topic_battery_critical);
configJSON.concat("\"}");
_device->mqttClient()->publish(path.c_str(), configJSON.c_str(), true);
path = "homeassistant/binary_sensor/";
path.concat(uidString);
path.concat("/battery_low/config");
configJSON = "{\"dev\":{\"ids\":[\"nuki_";
configJSON.concat(uidString);
configJSON.concat("\"],\"mf\":\"Nuki\",\"mdl\":\"");
configJSON.concat(deviceType);
configJSON.concat("\",\"name\":\"");
configJSON.concat(name);
configJSON.concat("\"},\"~\":\"");
configJSON.concat(baseTopic);
configJSON.concat("\",\"name\":\"");
configJSON.concat(name);
configJSON.concat(" battery low\",\"unique_id\":\"");
configJSON.concat(uidString);
configJSON.concat("_battery_low\",\"dev_cla\":\"battery\",\"ent_cat\":\"diagnostic\",\"pl_off\":\"0\",\"pl_on\":\"1\",\"stat_t\":\"~");
configJSON.concat(mqtt_topic_battery_critical);
configJSON.concat("\"}");
_device->mqttClient()->publish(path.c_str(), configJSON.c_str(), true);
path = discoveryTopic;
path.concat("/binary_sensor/");
path.concat(uidString);
path.concat("/battery_low/config");
_device->mqttClient()->publish(path.c_str(), configJSON.c_str(), true);
}
}
void Network::setLockActionReceivedCallback(bool (*lockActionReceivedCallback)(const char *))

View File

@@ -13,6 +13,7 @@
#define preference_mqtt_ca "mqttca"
#define preference_mqtt_crt "mqttcrt"
#define preference_mqtt_key "mqttkey"
#define preference_mqtt_hass_discovery "hassdiscovery"
#define preference_hostname "hostname"
#define preference_network_timeout "nettmout"
#define preference_query_interval_lockstate "lockStInterval"

View File

@@ -225,6 +225,11 @@ bool WebCfgServer::processArgs(String& message)
_preferences->putString(preference_mqtt_key, value);
configChanged = true;
}
else if(key == "HASSDISCOVERY")
{
_preferences->putString(preference_mqtt_hass_discovery, value);
configChanged = true;
}
else if(key == "HOSTNAME")
{
_preferences->putString(preference_hostname, value);
@@ -493,6 +498,7 @@ void WebCfgServer::buildMqttConfigHtml(String &response)
printTextarea(response, "MQTTCA", "MQTT SSL CA Certificate (*, optional)", _preferences->getString(preference_mqtt_ca).c_str(), TLS_CA_MAX_SIZE);
printTextarea(response, "MQTTCRT", "MQTT SSL Client Certificate (*, optional)", _preferences->getString(preference_mqtt_crt).c_str(), TLS_CERT_MAX_SIZE);
printTextarea(response, "MQTTKEY", "MQTT SSL Client Key (*, optional)", _preferences->getString(preference_mqtt_key).c_str(), TLS_KEY_MAX_SIZE);
printInputField(response, "HASSDISCOVERY", "Home Assistant discovery topic (empty to disable)", _preferences->getString(preference_mqtt_hass_discovery).c_str(), 30);
printInputField(response, "NETTIMEOUT", "Network Timeout until restart (seconds; -1 to disable)", _preferences->getInt(preference_network_timeout), 5);
response.concat("</table>");
response.concat("* If no encryption is configured for the MQTT broker, leave empty.<br>");