Skip to content

Commit

Permalink
fixed issue #23: Using Lockngo does not change the lock state
Browse files Browse the repository at this point in the history
  • Loading branch information
technyon committed Aug 6, 2022
1 parent 241d39a commit bdb377a
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 3 deletions.
1 change: 1 addition & 0 deletions MqttTopics.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define mqtt_topic_battery_lock_distance "/battery/lockDistance"

#define mqtt_topic_lock_state "/lock/state"
#define mqtt_topic_lock_binary_state "/lock/binaryState"
#define mqtt_topic_lock_trigger "/lock/trigger"
#define mqtt_topic_lock_auth_id "/lock/authorizationId"
#define mqtt_topic_lock_auth_name "/lock/authorizationName"
Expand Down
2 changes: 1 addition & 1 deletion Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n
configJSON.concat("\",\"pl_open\":\"");
configJSON.concat(openAction);
configJSON.concat("\",\"stat_t\":\"~");
configJSON.concat(mqtt_topic_lock_state);
configJSON.concat(mqtt_topic_lock_binary_state);
configJSON.concat("\",\"stat_locked\":\"");
configJSON.concat(lockedState);
configJSON.concat("\",\"stat_unlocked\":\"");
Expand Down
28 changes: 28 additions & 0 deletions NetworkLock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ void NetworkLock::initialize()

_network->setMqttPresencePath(_mqttPath);

_haEnabled = _preferences->getString(preference_mqtt_hass_discovery) != "";

_network->subscribe(_mqttPath, mqtt_topic_lock_action);
for(const auto& topic : _configTopics)
{
Expand Down Expand Up @@ -117,6 +119,11 @@ void NetworkLock::publishKeyTurnerState(const NukiLock::KeyTurnerState& keyTurne
memset(&str, 0, sizeof(str));
lockstateToString(keyTurnerState.lockState, str);
publishString(mqtt_topic_lock_state, str);

if(_haEnabled)
{
publishBinaryState(keyTurnerState.lockState);
}
}

if(_firstTunerStatePublish || keyTurnerState.trigger != lastKeyTurnerState.trigger)
Expand Down Expand Up @@ -155,6 +162,27 @@ void NetworkLock::publishKeyTurnerState(const NukiLock::KeyTurnerState& keyTurne
_firstTunerStatePublish = false;
}

void NetworkLock::publishBinaryState(NukiLock::LockState lockState)
{
switch(lockState)
{
case NukiLock::LockState::Locked:
case NukiLock::LockState::Locking:
publishString(mqtt_topic_lock_binary_state, "locked");
break;
case NukiLock::LockState::Unlocked:
case NukiLock::LockState::Unlocking:
case NukiLock::LockState::Unlatched:
case NukiLock::LockState::Unlatching:
case NukiLock::LockState::UnlockedLnga:
publishString(mqtt_topic_lock_binary_state, "unlocked");
break;
default:
break;
}
}


void NetworkLock::publishAuthorizationInfo(const uint32_t authId, const char *authName)
{
publishUInt(mqtt_topic_lock_auth_id, authId);
Expand Down
2 changes: 2 additions & 0 deletions NetworkLock.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class NetworkLock : public MqttReceiver
void update();

void publishKeyTurnerState(const NukiLock::KeyTurnerState& keyTurnerState, const NukiLock::KeyTurnerState& lastKeyTurnerState);
void publishBinaryState(NukiLock::LockState lockState);
void publishAuthorizationInfo(const uint32_t authId, const char* authName);
void publishCommandResult(const char* resultStr);
void publishBatteryReport(const NukiLock::BatteryReport& batteryReport);
Expand Down Expand Up @@ -52,6 +53,7 @@ class NetworkLock : public MqttReceiver

bool _firstTunerStatePublish = true;
unsigned long _lastMaintenanceTs = 0;
bool _haEnabled= false;

bool (*_lockActionReceivedCallback)(const char* value) = nullptr;
void (*_configUpdateReceivedCallback)(const char* path, const char* value) = nullptr;
Expand Down
26 changes: 25 additions & 1 deletion NetworkOpener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ void NetworkOpener::initialize()
_preferences->putString(preference_mqtt_opener_path, _mqttPath);
}

_haEnabled = _preferences->getString(preference_mqtt_hass_discovery) != "";

_network->subscribe(_mqttPath, mqtt_topic_lock_action);
for(const auto& topic : _configTopics)
{
Expand Down Expand Up @@ -86,6 +88,11 @@ void NetworkOpener::publishKeyTurnerState(const NukiOpener::OpenerState& keyTurn
memset(&str, 0, sizeof(str));
lockstateToString(keyTurnerState.lockState, str);
publishString(mqtt_topic_lock_state, str);

if(_haEnabled)
{
publishBinaryState(keyTurnerState.lockState);
}
}

if(_firstTunerStatePublish || keyTurnerState.trigger != lastKeyTurnerState.trigger)
Expand Down Expand Up @@ -118,6 +125,23 @@ void NetworkOpener::publishKeyTurnerState(const NukiOpener::OpenerState& keyTurn
_firstTunerStatePublish = false;
}

void NetworkOpener::publishBinaryState(NukiOpener::LockState lockState)
{
switch(lockState)
{
case NukiOpener::LockState::Locked:
case NukiOpener::LockState::RTOactive:
publishString(mqtt_topic_lock_binary_state, "locked");
break;
case NukiOpener::LockState::Open:
case NukiOpener::LockState::Opening:
publishString(mqtt_topic_lock_binary_state, "unlocked");
break;
default:
break;
}
}

void NetworkOpener::publishAuthorizationInfo(const uint32_t authId, const char *authName)
{
publishUInt(mqtt_topic_lock_auth_id, authId);
Expand Down Expand Up @@ -224,4 +248,4 @@ bool NetworkOpener::comparePrefixedPath(const char *fullPath, const char *subPat
char prefixedPath[500];
buildMqttPath(subPath, prefixedPath);
return strcmp(fullPath, prefixedPath) == 0;
}
}
2 changes: 2 additions & 0 deletions NetworkOpener.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class NetworkOpener : public MqttReceiver
void initialize();

void publishKeyTurnerState(const NukiOpener::OpenerState& keyTurnerState, const NukiOpener::OpenerState& lastKeyTurnerState);
void publishBinaryState(NukiOpener::LockState lockState);
void publishAuthorizationInfo(const uint32_t authId, const char* authName);
void publishCommandResult(const char* resultStr);
void publishBatteryReport(const NukiOpener::BatteryReport& batteryReport);
Expand Down Expand Up @@ -54,6 +55,7 @@ class NetworkOpener : public MqttReceiver
std::vector<char*> _configTopics;

bool _firstTunerStatePublish = true;
bool _haEnabled= false;

bool (*_lockActionReceivedCallback)(const char* value) = nullptr;
void (*_configUpdateReceivedCallback)(const char* path, const char* value) = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion Version.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#pragma once

#define nuki_hub_version "5.5"
#define nuki_hub_version "5.6"
Binary file modified webflash/nuki_hub.bin
Binary file not shown.

0 comments on commit bdb377a

Please sign in to comment.