Remove retained MQTT messages when changing HASS config
This commit is contained in:
22
Network.cpp
22
Network.cpp
@@ -412,6 +412,28 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n
|
||||
}
|
||||
}
|
||||
|
||||
void Network::removeHASSConfig(char* uidString)
|
||||
{
|
||||
String discoveryTopic = _preferences->getString(preference_mqtt_hass_discovery);
|
||||
|
||||
if(discoveryTopic != "")
|
||||
{
|
||||
String path = discoveryTopic;
|
||||
path.concat("/lock/");
|
||||
path.concat(uidString);
|
||||
path.concat("/smartlock/config");
|
||||
|
||||
_device->mqttClient()->publish(path.c_str(), NULL, 0U, true);
|
||||
|
||||
path = discoveryTopic;
|
||||
path.concat("/binary_sensor/");
|
||||
path.concat(uidString);
|
||||
path.concat("/battery_low/config");
|
||||
|
||||
_device->mqttClient()->publish(path.c_str(), NULL, 0U, true);
|
||||
}
|
||||
}
|
||||
|
||||
void Network::setLockActionReceivedCallback(bool (*lockActionReceivedCallback)(const char *))
|
||||
{
|
||||
_lockActionReceivedCallback = lockActionReceivedCallback;
|
||||
|
||||
@@ -37,6 +37,7 @@ public:
|
||||
void publishAdvancedConfig(const NukiLock::AdvancedConfig& config);
|
||||
void publishPresenceDetection(char* csv);
|
||||
void publishHASSConfig(char* deviceType, const char* baseTopic, char* name, char* uidString, char* lockAction, char* unlockAction, char* openAction, char* lockedState, char* unlockedState);
|
||||
void removeHASSConfig(char* uidString);
|
||||
|
||||
void setLockActionReceivedCallback(bool (*lockActionReceivedCallback)(const char* value));
|
||||
void setConfigUpdateReceivedCallback(void (*configUpdateReceivedCallback)(const char* path, const char* value));
|
||||
|
||||
@@ -171,6 +171,11 @@ void NetworkOpener::publishHASSConfig(char* deviceType, const char* baseTopic, c
|
||||
_network->publishHASSConfig(deviceType, baseTopic, name, uidString, lockAction, unlockAction, openAction, lockedState, unlockedState);
|
||||
}
|
||||
|
||||
void NetworkOpener::removeHASSConfig(char* uidString)
|
||||
{
|
||||
_network->removeHASSConfig(uidString);
|
||||
}
|
||||
|
||||
void NetworkOpener::setLockActionReceivedCallback(bool (*lockActionReceivedCallback)(const char *))
|
||||
{
|
||||
_lockActionReceivedCallback = lockActionReceivedCallback;
|
||||
|
||||
@@ -27,6 +27,7 @@ public:
|
||||
void publishConfig(const NukiOpener::Config& config);
|
||||
void publishAdvancedConfig(const NukiOpener::AdvancedConfig& config);
|
||||
void publishHASSConfig(char* deviceType, const char* baseTopic, char* name, char* uidString, char* lockAction, char* unlockAction, char* openAction, char* lockedState, char* unlockedState);
|
||||
void removeHASSConfig(char* uidString);
|
||||
|
||||
void setLockActionReceivedCallback(bool (*lockActionReceivedCallback)(const char* value));
|
||||
void setConfigUpdateReceivedCallback(void (*configUpdateReceivedCallback)(const char* path, const char* value));
|
||||
|
||||
@@ -320,3 +320,23 @@ void NukiOpenerWrapper::setupHASS()
|
||||
Serial.println(F("Unable to setup HASS. Invalid config received."));
|
||||
}
|
||||
}
|
||||
|
||||
void NukiOpenerWrapper::disableHASS()
|
||||
{
|
||||
if(!_nukiConfigValid) // only ask for config once to save battery life
|
||||
{
|
||||
Nuki::CmdResult result = _nukiOpener.requestConfig(&_nukiConfig);
|
||||
_nukiConfigValid = result == Nuki::CmdResult::Success;
|
||||
}
|
||||
if (_nukiConfigValid)
|
||||
{
|
||||
String baseTopic = _preferences->getString(preference_mqtt_opener_path);
|
||||
char uidString[20];
|
||||
itoa(_nukiConfig.nukiId, uidString, 16);
|
||||
_network->removeHASSConfig(uidString);
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println(F("Unable to disable HASS. Invalid config received."));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ public:
|
||||
void setPin(const uint16_t pin);
|
||||
|
||||
void unpair();
|
||||
|
||||
void disableHASS();
|
||||
|
||||
const NukiOpener::OpenerState& keyTurnerState();
|
||||
const bool isPaired();
|
||||
|
||||
@@ -342,3 +342,23 @@ void NukiWrapper::setupHASS()
|
||||
Serial.println(F("Unable to setup HASS. Invalid config received."));
|
||||
}
|
||||
}
|
||||
|
||||
void NukiWrapper::disableHASS()
|
||||
{
|
||||
if(!_nukiConfigValid) // only ask for config once to save battery life
|
||||
{
|
||||
Nuki::CmdResult result = _nukiLock.requestConfig(&_nukiConfig);
|
||||
_nukiConfigValid = result == Nuki::CmdResult::Success;
|
||||
}
|
||||
if (_nukiConfigValid)
|
||||
{
|
||||
String baseTopic = _preferences->getString(preference_mqtt_lock_path);
|
||||
char uidString[20];
|
||||
itoa(_nukiConfig.nukiId, uidString, 16);
|
||||
_network->removeHASSConfig(uidString);
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println(F("Unable to disable HASS. Invalid config received."));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ public:
|
||||
void setPin(const uint16_t pin);
|
||||
|
||||
void unpair();
|
||||
|
||||
void disableHASS();
|
||||
|
||||
const NukiLock::KeyTurnerState& keyTurnerState();
|
||||
const bool isPaired();
|
||||
|
||||
@@ -227,6 +227,15 @@ bool WebCfgServer::processArgs(String& message)
|
||||
}
|
||||
else if(key == "HASSDISCOVERY")
|
||||
{
|
||||
// Previous HASS config has to be disabled first (remove retained MQTT messages)
|
||||
if ( _nuki != nullptr )
|
||||
{
|
||||
_nuki->disableHASS();
|
||||
}
|
||||
if ( _nukiOpener != nullptr )
|
||||
{
|
||||
_nukiOpener->disableHASS();
|
||||
}
|
||||
_preferences->putString(preference_mqtt_hass_discovery, value);
|
||||
configChanged = true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user