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

@@ -475,7 +475,7 @@ void HomeAssistantDiscovery::publishHASSConfig(char *deviceType, const char *bas
removeHASSConfigTopic((char*)"sensor", (char*)"last_action_authorization", uidString); removeHASSConfigTopic((char*)"sensor", (char*)"last_action_authorization", uidString);
} }
else else
{ {
removeHASSConfigTopic((char*)"sensor", (char*)"rolling_log", uidString); removeHASSConfigTopic((char*)"sensor", (char*)"rolling_log", uidString);
} }
if(hasKeypad) if(hasKeypad)
@@ -519,6 +519,8 @@ void HomeAssistantDiscovery::publishHASSDeviceConfig(char* deviceType, const cha
json["unique_id"] = String(uidString) + "_lock"; json["unique_id"] = String(uidString) + "_lock";
json["cmd_t"] = String("~") + String(mqtt_topic_lock_action); json["cmd_t"] = String("~") + String(mqtt_topic_lock_action);
json["avty"][0]["t"] = availabilityTopic; 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_lock"] = lockAction;
json["pl_unlk"] = unlockAction; json["pl_unlk"] = unlockAction;
@@ -1878,7 +1880,7 @@ void HomeAssistantDiscovery::publishHASSConfigAdditionalLockEntities(char *devic
{ {
removeHassTopic((char*)"switch", (char*)"auto_update_enabled", uidString); removeHassTopic((char*)"switch", (char*)"auto_update_enabled", uidString);
} }
// Motor speed // Motor speed
if((int)advancedLockConfigAclPrefs[23] == 1) if((int)advancedLockConfigAclPrefs[23] == 1)
{ {
@@ -1895,7 +1897,7 @@ void HomeAssistantDiscovery::publishHASSConfigAdditionalLockEntities(char *devic
{ {
removeHassTopic((char*)"select", (char*)"motor_speed", uidString); removeHassTopic((char*)"select", (char*)"motor_speed", uidString);
} }
if((int)advancedLockConfigAclPrefs[24] == 1) if((int)advancedLockConfigAclPrefs[24] == 1)
{ {
// Slow speed during night mode enabled // Slow speed during night mode enabled
@@ -3172,7 +3174,13 @@ JsonDocument HomeAssistantDiscovery::createHassJson(const String& uidString,
json["cmd_t"] = commandTopic; 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) for(const auto& entry : additionalEntries)
{ {

View File

@@ -25,6 +25,7 @@
#define mqtt_topic_lock_rssi (char*)"/rssi" #define mqtt_topic_lock_rssi (char*)"/rssi"
#define mqtt_topic_lock_address (char*)"/address" #define mqtt_topic_lock_address (char*)"/address"
#define mqtt_topic_lock_retry (char*)"/retry" #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_lock_action (char*)"/lockAction"
//#define mqtt_topic_official_mode (char*)"/mode" //#define mqtt_topic_official_mode (char*)"/mode"

View File

@@ -420,6 +420,15 @@ void NukiNetworkLock::publishKeyTurnerState(const NukiLock::KeyTurnerState& keyT
lockstateToString((NukiLock::LockState)_nukiOfficial->getOffState(), str); lockstateToString((NukiLock::LockState)_nukiOfficial->getOffState(), str);
json["lock_state"] = str; 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; json["lockngo_state"] = keyTurnerState.lockNgoTimer != 255 ? keyTurnerState.lockNgoTimer : 0;

View File

@@ -347,6 +347,15 @@ void NukiNetworkOpener::publishKeyTurnerState(const NukiOpener::OpenerState& key
publishState(keyTurnerState); publishState(keyTurnerState);
} }
} }
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; json["lock_state"] = str;

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_start[] asm("_binary_x509_crt_bundle_start");
extern const uint8_t x509_crt_imported_bundle_bin_end[] asm("_binary_x509_crt_bundle_end"); extern const uint8_t x509_crt_imported_bundle_bin_end[] asm("_binary_x509_crt_bundle_end");
extern bool timeSynced;
#ifndef NUKI_HUB_UPDATER #ifndef NUKI_HUB_UPDATER
#include <HTTPClient.h> #include <HTTPClient.h>
@@ -736,7 +737,11 @@ void WebCfgServer::initialize()
{ {
if(_preferences->getBool(preference_cred_duo_approval, false)) 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; int duoResult = 2;
@@ -949,7 +954,11 @@ void WebCfgServer::initialize()
if(_preferences->getBool(preference_cred_duo_approval, false)) 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; int duoResult = 2;
@@ -1953,6 +1962,11 @@ esp_err_t WebCfgServer::buildCoredumpHtml(PsychicRequest *request, PsychicRespon
esp_err_t WebCfgServer::buildDuoHtml(PsychicRequest *request, PsychicResponse* resp) 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"); bool duo = startDuoAuth((char*)"Approve Nuki Hub login");
if (!duo) if (!duo)

View File

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