From 8c05586901ba6d802a225c7a72f59df03e03f9e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Mon, 21 Feb 2022 09:44:57 +0100 Subject: [PATCH] Improve password handling and clean up in case the password is not valid. --- sma/integrationpluginsma.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/sma/integrationpluginsma.cpp b/sma/integrationpluginsma.cpp index 465af7be..db6b75dd 100644 --- a/sma/integrationpluginsma.cpp +++ b/sma/integrationpluginsma.cpp @@ -186,13 +186,19 @@ void IntegrationPluginSma::confirmPairing(ThingPairingInfo *info, const QString { Q_UNUSED(username) + // On speedwire the password length has a maximum of 12 characters + if (secret.count() > 12) { + info->finish(Thing::ThingErrorInvalidParameter, QT_TR_NOOP("The password can not be longer than 12 characters.")); + return; + } + // Init with the default password QString password = "0000"; if (!secret.isEmpty()) { qCDebug(dcSma()) << "Pairing: Using password" << secret; password = secret; } else { - qCDebug(dcSma()) << "Pairing: Secret is empty. Using default password" << password; + qCDebug(dcSma()) << "Pairing: The given password is empty. Using default password" << password; } // Just store details, we'll test the login in setupDevice @@ -315,6 +321,12 @@ void IntegrationPluginSma::setupThing(ThingSetupInfo *info) connect(inverter, &SpeedwireInverter::loginFinished, info, [=](bool success){ if (!success) { qCWarning(dcSma()) << "Failed to set up inverter. Wrong password."; + + // Remove invalid password from settings + pluginStorage()->beginGroup(info->thing()->id().toString()); + pluginStorage()->remove(""); + pluginStorage()->endGroup(); + delete inverter; info->finish(Thing::ThingErrorAuthenticationFailure, QT_TR_NOOP("Failed to log in with the given password. Please try again.")); return; @@ -329,7 +341,6 @@ void IntegrationPluginSma::setupThing(ThingSetupInfo *info) // Make sure an aborted setup will clean up the object connect(info, &ThingSetupInfo::aborted, inverter, &SpeedwireInverter::deleteLater); - // Runtime connections connect(inverter, &SpeedwireInverter::reachableChanged, thing, [=](bool reachable){ thing->setStateValue(speedwireInverterConnectedStateTypeId, reachable); @@ -410,6 +421,7 @@ void IntegrationPluginSma::onConnectedChanged(bool connected) Thing *thing = m_sunnyWebBoxes.key(static_cast(sender())); if (!thing) return; + thing->setStateValue(sunnyWebBoxConnectedStateTypeId, connected); } @@ -446,6 +458,7 @@ void IntegrationPluginSma::setupRefreshTimer() } foreach (SpeedwireInverter *inverter, m_speedwireInverters) { + // Note: refresh will not be triggered if there is already a refresh process running inverter->refresh(); } });