fix hass setup
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include "MqttTopics.h"
|
||||
#include "PreferencesKeys.h"
|
||||
#include "Pins.h"
|
||||
#include "Logger.h"
|
||||
|
||||
NetworkLock::NetworkLock(Network* network, Preferences* preferences)
|
||||
: _network(network),
|
||||
@@ -82,7 +83,7 @@ void NetworkLock::onMqttDataReceived(char *&topic, byte *&payload, unsigned int
|
||||
|
||||
if(comparePrefixedPath(topic, mqtt_topic_reset) && strcmp(value, "1") == 0)
|
||||
{
|
||||
Serial.println(F("Restart requested via MQTT."));
|
||||
Log->println(F("Restart requested via MQTT."));
|
||||
delay(200);
|
||||
ESP.restart();
|
||||
}
|
||||
@@ -91,8 +92,8 @@ void NetworkLock::onMqttDataReceived(char *&topic, byte *&payload, unsigned int
|
||||
{
|
||||
if(strcmp(value, "") == 0 || strcmp(value, "--") == 0 || strcmp(value, "ack") == 0 || strcmp(value, "unknown_action") == 0) return;
|
||||
|
||||
Serial.print(F("Lock action received: "));
|
||||
Serial.println(value);
|
||||
Log->print(F("Lock action received: "));
|
||||
Log->println(value);
|
||||
bool success = false;
|
||||
if(_lockActionReceivedCallback != NULL)
|
||||
{
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include <RTOS.h>
|
||||
#include "PreferencesKeys.h"
|
||||
#include "MqttTopics.h"
|
||||
#include "Logger.h"
|
||||
#include <NukiOpenerUtils.h>
|
||||
|
||||
NukiOpenerWrapper* nukiOpenerInst;
|
||||
@@ -41,6 +42,7 @@ void NukiOpenerWrapper::initialize()
|
||||
_intervalBattery = _preferences->getInt(preference_query_interval_battery);
|
||||
_publishAuthData = _preferences->getBool(preference_publish_authdata);
|
||||
_restartBeaconTimeout = _preferences->getInt(preference_restart_ble_beacon_lost);
|
||||
_hassEnabled = _preferences->getString(preference_mqtt_hass_discovery) != "";
|
||||
|
||||
if(_intervalLockstate == 0)
|
||||
{
|
||||
@@ -55,12 +57,12 @@ void NukiOpenerWrapper::initialize()
|
||||
|
||||
_nukiOpener.setEventHandler(this);
|
||||
|
||||
Serial.print(F("Lock state interval: "));
|
||||
Serial.print(_intervalLockstate);
|
||||
Serial.print(F(" | Battery interval: "));
|
||||
Serial.print(_intervalBattery);
|
||||
Serial.print(F(" | Publish auth data: "));
|
||||
Serial.println(_publishAuthData ? "yes" : "no");
|
||||
Log->print(F("Lock state interval: "));
|
||||
Log->print(_intervalLockstate);
|
||||
Log->print(F(" | Battery interval: "));
|
||||
Log->print(_intervalBattery);
|
||||
Log->print(F(" | Publish auth data: "));
|
||||
Log->println(_publishAuthData ? "yes" : "no");
|
||||
|
||||
if(!_publishAuthData)
|
||||
{
|
||||
@@ -72,16 +74,15 @@ void NukiOpenerWrapper::update()
|
||||
{
|
||||
if (!_paired)
|
||||
{
|
||||
Serial.println(F("Nuki opener start pairing"));
|
||||
Log->println(F("Nuki opener start pairing"));
|
||||
|
||||
Nuki::AuthorizationIdType idType = _preferences->getBool(preference_register_as_app) ?
|
||||
Nuki::AuthorizationIdType::App :
|
||||
Nuki::AuthorizationIdType::Bridge;
|
||||
|
||||
if (_nukiOpener.pairNuki(idType) == NukiOpener::PairingResult::Success) {
|
||||
Serial.println(F("Nuki opener paired"));
|
||||
Log->println(F("Nuki opener paired"));
|
||||
_paired = true;
|
||||
setupHASS();
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -92,9 +93,9 @@ void NukiOpenerWrapper::update()
|
||||
|
||||
if(_restartBeaconTimeout > 0 && (millis() - _nukiOpener.getLastReceivedBeaconTs() > _restartBeaconTimeout * 1000))
|
||||
{
|
||||
Serial.print("No BLE beacon received from the opener for ");
|
||||
Serial.print((millis() - _nukiOpener.getLastReceivedBeaconTs()) / 1000);
|
||||
Serial.println(" seconds, restarting device.");
|
||||
Log->print("No BLE beacon received from the opener for ");
|
||||
Log->print((millis() - _nukiOpener.getLastReceivedBeaconTs()) / 1000);
|
||||
Log->println(" seconds, restarting device.");
|
||||
delay(200);
|
||||
ESP.restart();
|
||||
}
|
||||
@@ -118,6 +119,10 @@ void NukiOpenerWrapper::update()
|
||||
{
|
||||
_nextConfigUpdateTs = ts + _intervalConfig * 1000;
|
||||
updateConfig();
|
||||
if(_hassEnabled)
|
||||
{
|
||||
setupHASS();
|
||||
}
|
||||
}
|
||||
if(_nextRssiTs == 0 || ts > _nextRssiTs)
|
||||
{
|
||||
@@ -140,8 +145,8 @@ void NukiOpenerWrapper::update()
|
||||
|
||||
_network->publishCommandResult(resultStr);
|
||||
|
||||
Serial.print(F("Opener lock action result: "));
|
||||
Serial.println(resultStr);
|
||||
Log->print(F("Opener lock action result: "));
|
||||
Log->println(resultStr);
|
||||
|
||||
_nextLockAction = (NukiOpener::LockAction)0xff;
|
||||
if(_intervalLockstate > 10)
|
||||
@@ -176,7 +181,7 @@ void NukiOpenerWrapper::updateKeyTurnerState()
|
||||
|
||||
if(_statusUpdated && _keyTurnerState.lockState == NukiOpener::LockState::Locked && _lastKeyTurnerState.lockState == NukiOpener::LockState::Locked)
|
||||
{
|
||||
Serial.println(F("Nuki opener: Ring detected"));
|
||||
Log->println(F("Nuki opener: Ring detected"));
|
||||
_network->publishRing();
|
||||
}
|
||||
else
|
||||
@@ -187,8 +192,8 @@ void NukiOpenerWrapper::updateKeyTurnerState()
|
||||
{
|
||||
char lockStateStr[20];
|
||||
lockstateToString(_keyTurnerState.lockState, lockStateStr);
|
||||
Serial.print(F("Nuki opener state: "));
|
||||
Serial.println((int)_keyTurnerState.lockState);
|
||||
Log->print(F("Nuki opener state: "));
|
||||
Log->println((int)_keyTurnerState.lockState);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -315,38 +320,31 @@ void NukiOpenerWrapper::notify(Nuki::EventType eventType)
|
||||
|
||||
void NukiOpenerWrapper::readConfig()
|
||||
{
|
||||
Serial.print(F("Reading opener config. Result: "));
|
||||
Log->print(F("Reading opener config. Result: "));
|
||||
Nuki::CmdResult result = _nukiOpener.requestConfig(&_nukiConfig);
|
||||
_nukiConfigValid = result == Nuki::CmdResult::Success;
|
||||
Serial.println(result);
|
||||
Log->println(result);
|
||||
}
|
||||
|
||||
void NukiOpenerWrapper::readAdvancedConfig()
|
||||
{
|
||||
Serial.print(F("Reading opener advanced config. Result: "));
|
||||
Log->print(F("Reading opener advanced config. Result: "));
|
||||
Nuki::CmdResult result = _nukiOpener.requestAdvancedConfig(&_nukiAdvancedConfig);
|
||||
_nukiAdvancedConfigValid = result == Nuki::CmdResult::Success;
|
||||
Serial.println(result);
|
||||
Log->println(result);
|
||||
}
|
||||
|
||||
void NukiOpenerWrapper::setupHASS()
|
||||
{
|
||||
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->publishHASSConfig("Opener",baseTopic.c_str(),(char*)_nukiConfig.name,uidString,"deactivateRTO","activateRTO","electricStrikeActuation","locked","unlocked");
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println(F("Unable to setup HASS. Invalid config received."));
|
||||
}
|
||||
if(!_nukiConfigValid || _hassSetupCompleted) return;
|
||||
|
||||
String baseTopic = _preferences->getString(preference_mqtt_opener_path);
|
||||
char uidString[20];
|
||||
itoa(_nukiConfig.nukiId, uidString, 16);
|
||||
_network->publishHASSConfig("Opener",baseTopic.c_str(),(char*)_nukiConfig.name,uidString,"deactivateRTO","activateRTO","electricStrikeActuation","locked","unlocked");
|
||||
_hassSetupCompleted = true;
|
||||
|
||||
Log->println("HASS setup for opener completed.");
|
||||
}
|
||||
|
||||
void NukiOpenerWrapper::disableHASS()
|
||||
@@ -364,6 +362,6 @@ void NukiOpenerWrapper::disableHASS()
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println(F("Unable to disable HASS. Invalid config received."));
|
||||
Log->println(F("Unable to disable HASS. Invalid config received."));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,6 +67,8 @@ private:
|
||||
NukiOpener::AdvancedConfig _nukiAdvancedConfig = {0};
|
||||
bool _nukiConfigValid = false;
|
||||
bool _nukiAdvancedConfigValid = false;
|
||||
bool _hassEnabled = false;
|
||||
bool _hassSetupCompleted = false;
|
||||
|
||||
bool _paired = false;
|
||||
bool _statusUpdated = false;
|
||||
|
||||
@@ -47,6 +47,7 @@ void NukiWrapper::initialize()
|
||||
_publishAuthData = _preferences->getBool(preference_publish_authdata);
|
||||
_maxKeypadCodeCount = _preferences->getUInt(preference_max_keypad_code_count);
|
||||
_restartBeaconTimeout = _preferences->getInt(preference_restart_ble_beacon_lost);
|
||||
_hassEnabled = _preferences->getString(preference_mqtt_hass_discovery) != "";
|
||||
|
||||
if(_intervalLockstate == 0)
|
||||
{
|
||||
@@ -101,7 +102,6 @@ void NukiWrapper::update()
|
||||
if (_nukiLock.pairNuki(idType) == Nuki::PairingResult::Success) {
|
||||
Log->println(F("Nuki paired"));
|
||||
_paired = true;
|
||||
setupHASS();
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -138,6 +138,10 @@ void NukiWrapper::update()
|
||||
{
|
||||
_nextConfigUpdateTs = ts + _intervalConfig * 1000;
|
||||
updateConfig();
|
||||
if(_hassEnabled)
|
||||
{
|
||||
setupHASS();
|
||||
}
|
||||
}
|
||||
if(_nextRssiTs == 0 || ts > _nextRssiTs)
|
||||
{
|
||||
@@ -539,22 +543,15 @@ void NukiWrapper::readAdvancedConfig()
|
||||
|
||||
void NukiWrapper::setupHASS()
|
||||
{
|
||||
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->publishHASSConfig("SmartLock",baseTopic.c_str(),(char*)_nukiConfig.name,uidString,"lock","unlock","unlatch","locked","unlocked");
|
||||
}
|
||||
else
|
||||
{
|
||||
Log->println(F("Unable to setup HASS. Invalid config received."));
|
||||
}
|
||||
if(!_nukiConfigValid || _hassSetupCompleted) return;
|
||||
|
||||
String baseTopic = _preferences->getString(preference_mqtt_lock_path);
|
||||
char uidString[20];
|
||||
itoa(_nukiConfig.nukiId, uidString, 16);
|
||||
_network->publishHASSConfig("SmartLock",baseTopic.c_str(),(char*)_nukiConfig.name,uidString,"lock","unlock","unlatch","locked","unlocked");
|
||||
_hassSetupCompleted = true;
|
||||
|
||||
Log->println("HASS setup for lock completed.");
|
||||
}
|
||||
|
||||
void NukiWrapper::disableHASS()
|
||||
|
||||
@@ -74,6 +74,8 @@ private:
|
||||
NukiLock::AdvancedConfig _nukiAdvancedConfig = {0};
|
||||
bool _nukiConfigValid = false;
|
||||
bool _nukiAdvancedConfigValid = false;
|
||||
bool _hassEnabled = false;
|
||||
bool _hassSetupCompleted = false;
|
||||
|
||||
bool _paired = false;
|
||||
bool _statusUpdated = false;
|
||||
|
||||
9
Ota.cpp
9
Ota.cpp
@@ -1,5 +1,6 @@
|
||||
#include <Arduino.h>
|
||||
#include "Ota.h"
|
||||
#include "Logger.h"
|
||||
|
||||
#define FULL_PACKET 1436 // HTTP_UPLOAD_BUFLEN in WebServer,h
|
||||
|
||||
@@ -7,13 +8,13 @@ void Ota::updateFirmware(uint8_t* buf, size_t size)
|
||||
{
|
||||
if(!_updateStarted && size == 0)
|
||||
{
|
||||
Serial.println("OTA upload cancelled, size is 0.");
|
||||
Log->println("OTA upload cancelled, size is 0.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_updateStarted)
|
||||
{ //If it's the first packet of OTA since bootup, begin OTA
|
||||
Serial.println("BeginOTA");
|
||||
Log->println("BeginOTA");
|
||||
esp_ota_begin(esp_ota_get_next_update_partition(NULL), OTA_SIZE_UNKNOWN, &otaHandler);
|
||||
_updateStarted = true;
|
||||
}
|
||||
@@ -21,7 +22,7 @@ void Ota::updateFirmware(uint8_t* buf, size_t size)
|
||||
if (size != FULL_PACKET)
|
||||
{
|
||||
esp_ota_end(otaHandler);
|
||||
Serial.println("EndOTA");
|
||||
Log->println("EndOTA");
|
||||
if (ESP_OK == esp_ota_set_boot_partition(esp_ota_get_next_update_partition(NULL)))
|
||||
{
|
||||
delay(2000);
|
||||
@@ -29,7 +30,7 @@ void Ota::updateFirmware(uint8_t* buf, size_t size)
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("Upload Error");
|
||||
Log->println("Upload Error");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "PresenceDetection.h"
|
||||
#include "PreferencesKeys.h"
|
||||
#include "Logger.h"
|
||||
|
||||
PresenceDetection::PresenceDetection(Preferences* preferences, BleScanner::Scanner *bleScanner, Network* network)
|
||||
: _preferences(preferences),
|
||||
@@ -15,8 +16,8 @@ PresenceDetection::PresenceDetection(Preferences* preferences, BleScanner::Scann
|
||||
_preferences->putInt(preference_presence_detection_timeout, 60);
|
||||
}
|
||||
|
||||
Serial.print(F("Presence detection timeout (ms): "));
|
||||
Serial.println(_timeout);
|
||||
Log->print(F("Presence detection timeout (ms): "));
|
||||
Log->println(_timeout);
|
||||
}
|
||||
|
||||
PresenceDetection::~PresenceDetection()
|
||||
@@ -67,8 +68,8 @@ void PresenceDetection::update()
|
||||
|
||||
_csv[_csvIndex-1] = 0x00;
|
||||
|
||||
// Serial.print("Devices found: ");
|
||||
// Serial.println(_devices.size());
|
||||
// Log->print("Devices found: ");
|
||||
// Log->println(_devices.size());
|
||||
_network->publishPresenceDetection(_csv);
|
||||
}
|
||||
|
||||
@@ -117,7 +118,7 @@ void PresenceDetection::onResult(NimBLEAdvertisedDevice *device)
|
||||
std::string addressStr = device->getAddress().toString();
|
||||
char addrArrComp[13] = {0};
|
||||
|
||||
// Serial.println(addressStr.c_str());
|
||||
// Log->println(addressStr.c_str());
|
||||
|
||||
addrArrComp[0] = addressStr.at(0);
|
||||
addrArrComp[1] = addressStr.at(1);
|
||||
@@ -184,14 +185,14 @@ void PresenceDetection::onResult(NimBLEAdvertisedDevice *device)
|
||||
|
||||
// if(device->haveName())
|
||||
// {
|
||||
// Serial.print(" | ");
|
||||
// Serial.print(device->getName().c_str());
|
||||
// Log->print(" | ");
|
||||
// Log->print(device->getName().c_str());
|
||||
// if(device->haveRSSI())
|
||||
// {
|
||||
// Serial.print(" | ");
|
||||
// Serial.print(device->getRSSI());
|
||||
// Log->print(" | ");
|
||||
// Log->print(device->getRSSI());
|
||||
// }
|
||||
// }
|
||||
// Serial.println();
|
||||
// Log->println();
|
||||
|
||||
}
|
||||
@@ -868,14 +868,14 @@ void WebCfgServer::handleOtaUpload()
|
||||
_otaStartTs = millis();
|
||||
esp_task_wdt_init(30, false);
|
||||
_network->disableAutoRestarts();
|
||||
Log->print("handleFileUpload Name: "); Serial.println(filename);
|
||||
Log->print("handleFileUpload Name: "); Log->println(filename);
|
||||
} else if (upload.status == UPLOAD_FILE_WRITE) {
|
||||
_transferredSize = _transferredSize + upload.currentSize;
|
||||
Serial.println(_transferredSize);
|
||||
Log->println(_transferredSize);
|
||||
_ota.updateFirmware(upload.buf, upload.currentSize);
|
||||
} else if (upload.status == UPLOAD_FILE_END) {
|
||||
Serial.println();
|
||||
Serial.print("handleFileUpload Size: "); Serial.println(upload.totalSize);
|
||||
Log->println();
|
||||
Log->print("handleFileUpload Size: "); Log->println(upload.totalSize);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user