Add HA discovery topics (#440)

This commit is contained in:
iranl
2024-07-31 13:14:09 +02:00
committed by GitHub
parent 41430179f3
commit d132e76ae2
5 changed files with 124 additions and 6 deletions

View File

@@ -104,6 +104,7 @@
#define mqtt_topic_restart_reason_esp "/maintenance/restartReasonNukiEsp" #define mqtt_topic_restart_reason_esp "/maintenance/restartReasonNukiEsp"
#define mqtt_topic_mqtt_connection_state "/maintenance/mqttConnectionState" #define mqtt_topic_mqtt_connection_state "/maintenance/mqttConnectionState"
#define mqtt_topic_network_device "/maintenance/networkDevice" #define mqtt_topic_network_device "/maintenance/networkDevice"
#define mqtt_hybrid_state "/maintenance/hybridConnected"
#define mqtt_topic_presence "/presence/devices" #define mqtt_topic_presence "/presence/devices"

View File

@@ -1015,6 +1015,105 @@ void NukiNetwork::publishHASSConfig(char* deviceType, const char* baseTopic, cha
{ (char*)"stat_on", (char*)"1" }, { (char*)"stat_on", (char*)"1" },
{ (char*)"stat_off", (char*)"0" }}); { (char*)"stat_off", (char*)"0" }});
// Network device
publishHassTopic("sensor",
"network_device",
uidString,
"_network_device",
"Network device",
name,
baseTopic,
_lockPath + mqtt_topic_network_device,
deviceType,
"",
"",
"diagnostic",
"",
{ { (char*)"en", (char*)"true" }});
// Nuki Hub Webserver enabled
publishHassTopic("switch",
"webserver",
uidString,
"_webserver",
"Nuki Hub webserver enabled",
name,
baseTopic,
_lockPath + mqtt_topic_webserver_state,
deviceType,
"",
"",
"diagnostic",
_lockPath + mqtt_topic_webserver_action,
{ { (char*)"pl_on", (char*)"1" },
{ (char*)"pl_off", (char*)"0" },
{ (char*)"stat_on", (char*)"1" },
{ (char*)"stat_off", (char*)"0" }});
// Uptime
publishHassTopic("sensor",
"uptime",
uidString,
"_uptime",
"Uptime",
name,
baseTopic,
_lockPath + mqtt_topic_uptime,
deviceType,
"",
"",
"diagnostic",
"",
{ { (char*)"en", (char*)"true" }});
if(_preferences->getBool(preference_mqtt_log_enabled, false))
{
// MQTT Log
publishHassTopic("sensor",
"mqtt_log",
uidString,
"_mqtt_log",
"MQTT Log",
name,
baseTopic,
_lockPath + mqtt_topic_log,
deviceType,
"",
"",
"diagnostic",
"",
{ { (char*)"en", (char*)"true" }});
}
else
{
removeHassTopic((char*)"sensor", (char*)"mqtt_log", uidString);
}
if(_preferences->getBool(preference_official_hybrid, false))
{
// Hybrid connected
publishHassTopic("binary_sensor",
"hybrid_connected",
uidString,
"_hybrid_connected",
"Hybrid connected",
name,
baseTopic,
_lockPath + mqtt_hybrid_state,
deviceType,
"",
"",
"diagnostic",
"",
{ {(char*)"pl_on", (char*)"1"},
{(char*)"pl_off", (char*)"0"},
{ (char*)"en", (char*)"true" }});
}
else
{
removeHassTopic((char*)"binary_sensor", (char*)"hybrid_connected", uidString);
}
// Firmware version // Firmware version
publishHassTopic("sensor", publishHassTopic("sensor",
"firmware_version", "firmware_version",
@@ -3437,6 +3536,11 @@ void NukiNetwork::removeHASSConfig(char* uidString)
removeHassTopic((char*)"number", (char*)"unlocked_position_offset_degrees", uidString); removeHassTopic((char*)"number", (char*)"unlocked_position_offset_degrees", uidString);
removeHassTopic((char*)"switch", (char*)"pairing_enabled", uidString); removeHassTopic((char*)"switch", (char*)"pairing_enabled", uidString);
removeHassTopic((char*)"switch", (char*)"auto_unlatch", uidString); removeHassTopic((char*)"switch", (char*)"auto_unlatch", uidString);
removeHassTopic((char*)"sensor", (char*)"network_device", uidString);
removeHassTopic((char*)"switch", (char*)"webserver", uidString);
removeHassTopic((char*)"sensor", (char*)"uptime", uidString);
removeHassTopic((char*)"sensor", (char*)"mqtt_log", uidString);
removeHassTopic((char*)"binary_sensor", (char*)"hybrid_connected", uidString);
} }
void NukiNetwork::removeHASSConfigTopic(char *deviceType, char *name, char *uidString) void NukiNetwork::removeHASSConfigTopic(char *deviceType, char *name, char *uidString)

View File

@@ -864,6 +864,11 @@ void NukiWrapper::onConfigUpdateReceivedCallback(const char *value)
nukiInst->onConfigUpdateReceived(value); nukiInst->onConfigUpdateReceived(value);
} }
bool NukiWrapper::offConnected()
{
return _network->_offConnected;
}
Nuki::AdvertisingMode NukiWrapper::advertisingModeToEnum(const char *str) Nuki::AdvertisingMode NukiWrapper::advertisingModeToEnum(const char *str)
{ {
if(strcmp(str, "Automatic") == 0) return Nuki::AdvertisingMode::Automatic; if(strcmp(str, "Automatic") == 0) return Nuki::AdvertisingMode::Automatic;
@@ -972,6 +977,7 @@ void NukiWrapper::onOfficialUpdateReceived(const char *topic, const char *value)
Log->print(F("Connected: ")); Log->print(F("Connected: "));
Log->println((strcmp(value, "true") == 0 ? 1 : 0)); Log->println((strcmp(value, "true") == 0 ? 1 : 0));
_network->_offConnected = (strcmp(value, "true") == 0 ? 1 : 0); _network->_offConnected = (strcmp(value, "true") == 0 ? 1 : 0);
_network->publishBool(mqtt_hybrid_state, _network->_offConnected, true);
if(!_network->_offConnected) _nextHybridLockStateUpdateTs = (esp_timer_get_time() / 1000) + _intervalHybridLockstate * 1000; if(!_network->_offConnected) _nextHybridLockStateUpdateTs = (esp_timer_get_time() / 1000) + _intervalHybridLockstate * 1000;
else _nextHybridLockStateUpdateTs = 0; else _nextHybridLockStateUpdateTs = 0;

View File

@@ -37,6 +37,7 @@ public:
const bool isPaired() const; const bool isPaired() const;
const bool hasKeypad() const; const bool hasKeypad() const;
bool hasDoorSensor() const; bool hasDoorSensor() const;
bool offConnected();
const BLEAddress getBleAddress() const; const BLEAddress getBleAddress() const;
std::string firmwareVersion() const; std::string firmwareVersion() const;

View File

@@ -2000,6 +2000,12 @@ void WebCfgServer::buildHtml(String& response)
{ {
String lockState = pinStateToString(_preferences->getInt(preference_lock_pin_status, 4)); String lockState = pinStateToString(_preferences->getInt(preference_lock_pin_status, 4));
printParameter(response, "Nuki Lock PIN status", lockState.c_str(), "", "lockPin"); printParameter(response, "Nuki Lock PIN status", lockState.c_str(), "", "lockPin");
if(_preferences->getBool(preference_official_hybrid, false))
{
String offConnected = _nuki->offConnected() ? "Yes": "No";
printParameter(response, "Nuki Lock hybrid mode connected", offConnected.c_str(), "", "lockHybrid");
}
} }
} }
if(_nukiOpener != nullptr) if(_nukiOpener != nullptr)