Set entities as unavailable in HA if state undefined

This commit is contained in:
iranl
2025-01-22 22:08:27 +01:00
parent c67edb3b5a
commit 13494d69c5
6 changed files with 50 additions and 7 deletions

View File

@@ -519,6 +519,8 @@ void HomeAssistantDiscovery::publishHASSDeviceConfig(char* deviceType, const cha
json["unique_id"] = String(uidString) + "_lock";
json["cmd_t"] = String("~") + String(mqtt_topic_lock_action);
json["avty"][0]["t"] = availabilityTopic;
json["avty"][1]["t"] = String("~") + String(mqtt_topic_lock_availability);
json["avty_mode"] = "all";
json["pl_lock"] = lockAction;
json["pl_unlk"] = unlockAction;
@@ -3172,7 +3174,13 @@ JsonDocument HomeAssistantDiscovery::createHassJson(const String& uidString,
json["cmd_t"] = commandTopic;
}
json["avty"]["t"] = _baseTopic + mqtt_topic_mqtt_connection_state;
json["avty"][0]["t"] = _baseTopic + mqtt_topic_mqtt_connection_state;
if (uidString != "query_lockstate")
{
json["avty"][1]["t"] = String("~") + String(mqtt_topic_lock_availability);
json["avty_mode"] = "all";
}
for(const auto& entry : additionalEntries)
{

View File

@@ -25,6 +25,7 @@
#define mqtt_topic_lock_rssi (char*)"/rssi"
#define mqtt_topic_lock_address (char*)"/address"
#define mqtt_topic_lock_retry (char*)"/retry"
#define mqtt_topic_lock_availability (char*)"/availability"
#define mqtt_topic_official_lock_action (char*)"/lockAction"
//#define mqtt_topic_official_mode (char*)"/mode"

View File

@@ -421,6 +421,15 @@ void NukiNetworkLock::publishKeyTurnerState(const NukiLock::KeyTurnerState& keyT
json["lock_state"] = str;
}
if(strcmp(str, "undefined") == 0)
{
_nukiPublisher->publishString(mqtt_topic_lock_availability, "offline", true);
}
else
{
_nukiPublisher->publishString(mqtt_topic_lock_availability, "online", true);
}
json["lockngo_state"] = keyTurnerState.lockNgoTimer != 255 ? keyTurnerState.lockNgoTimer : 0;
memset(&str, 0, sizeof(str));

View File

@@ -348,6 +348,15 @@ void NukiNetworkOpener::publishKeyTurnerState(const NukiOpener::OpenerState& key
}
}
if(strcmp(str, "undefined") == 0)
{
_nukiPublisher->publishString(mqtt_topic_lock_availability, "offline", true);
}
else
{
_nukiPublisher->publishString(mqtt_topic_lock_availability, "online", true);
}
json["lock_state"] = str;
if(keyTurnerState.nukiState == NukiOpener::State::ContinuousMode)

View File

@@ -20,6 +20,7 @@
extern const uint8_t x509_crt_imported_bundle_bin_start[] asm("_binary_x509_crt_bundle_start");
extern const uint8_t x509_crt_imported_bundle_bin_end[] asm("_binary_x509_crt_bundle_end");
extern bool timeSynced;
#ifndef NUKI_HUB_UPDATER
#include <HTTPClient.h>
@@ -736,7 +737,11 @@ void WebCfgServer::initialize()
{
if(_preferences->getBool(preference_cred_duo_approval, false))
{
if (startDuoAuth((char*)"Approve Nuki Hub export"))
if (!timeSynced)
{
return buildConfirmHtml(request, resp, "NTP time not synced yet, Duo not available, please wait for NTP to sync", 3, true);
}
else if (startDuoAuth((char*)"Approve Nuki Hub export"))
{
int duoResult = 2;
@@ -949,7 +954,11 @@ void WebCfgServer::initialize()
if(_preferences->getBool(preference_cred_duo_approval, false))
{
if (startDuoAuth((char*)"Approve Nuki Hub setting change"))
if (!timeSynced)
{
return buildConfirmHtml(request, resp, "NTP time not synced yet, Duo not available, please wait for NTP to sync", 3, true);
}
else if (startDuoAuth((char*)"Approve Nuki Hub setting change"))
{
int duoResult = 2;
@@ -1953,6 +1962,11 @@ esp_err_t WebCfgServer::buildCoredumpHtml(PsychicRequest *request, PsychicRespon
esp_err_t WebCfgServer::buildDuoHtml(PsychicRequest *request, PsychicResponse* resp)
{
if (!timeSynced)
{
return buildConfirmHtml(request, resp, "NTP time not synced yet, Duo not available, please wait for NTP to sync", 3, true);
}
bool duo = startDuoAuth((char*)"Approve Nuki Hub login");
if (!duo)

View File

@@ -90,6 +90,7 @@ RTC_NOINIT_ATTR bool disableNetwork;
RTC_NOINIT_ATTR bool wifiFallback;
RTC_NOINIT_ATTR bool ethCriticalFailure;
bool coredumpPrinted = true;
bool timeSynced = false;
int lastHTTPeventId = -1;
bool doOta = false;
@@ -182,7 +183,8 @@ uint8_t checkPartition()
}
void cbSyncTime(struct timeval *tv) {
Log->println("NTP time synched");
Log->println("NTP time synced");
timeSynced = true;
}
void networkTask(void *pvParameters)