fixed re-authorization
This commit is contained in:
parent
a5b74dc4cc
commit
1291311d0c
@ -45,7 +45,18 @@ IntegrationPluginTado::IntegrationPluginTado()
|
|||||||
|
|
||||||
void IntegrationPluginTado::startPairing(ThingPairingInfo *info)
|
void IntegrationPluginTado::startPairing(ThingPairingInfo *info)
|
||||||
{
|
{
|
||||||
info->finish(Thing::ThingErrorNoError, QT_TR_NOOP("Please enter the login credentials."));
|
// Checking the internet connection
|
||||||
|
NetworkAccessManager *network = hardwareManager()->networkManager();
|
||||||
|
QNetworkReply *reply = network->get(QNetworkRequest(QUrl("https://my.tado.com/api/v2")));
|
||||||
|
connect(reply, &QNetworkReply::finished, this, [reply, info] {
|
||||||
|
reply->deleteLater();
|
||||||
|
|
||||||
|
if (reply->error() == QNetworkReply::NetworkError::HostNotFoundError) {
|
||||||
|
info->finish(Thing::ThingErrorHardwareNotAvailable, QT_TR_NOOP("Tado server is not reachable."));
|
||||||
|
} else {
|
||||||
|
info->finish(Thing::ThingErrorNoError, QT_TR_NOOP("Please enter the login credentials for your Tado account."));
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void IntegrationPluginTado::confirmPairing(ThingPairingInfo *info, const QString &username, const QString &password)
|
void IntegrationPluginTado::confirmPairing(ThingPairingInfo *info, const QString &username, const QString &password)
|
||||||
@ -164,14 +175,14 @@ void IntegrationPluginTado::executeAction(ThingActionInfo *info)
|
|||||||
QString homeId = thing->paramValue(zoneThingHomeIdParamTypeId).toString();
|
QString homeId = thing->paramValue(zoneThingHomeIdParamTypeId).toString();
|
||||||
QString zoneId = thing->paramValue(zoneThingZoneIdParamTypeId).toString();
|
QString zoneId = thing->paramValue(zoneThingZoneIdParamTypeId).toString();
|
||||||
if (action.actionTypeId() == zoneModeActionTypeId) {
|
if (action.actionTypeId() == zoneModeActionTypeId) {
|
||||||
QUuid requestId;
|
QUuid requestId;
|
||||||
if (action.param(zoneModeActionModeParamTypeId).value().toString() == "Tado") {
|
if (action.param(zoneModeActionModeParamTypeId).value().toString() == "Tado") {
|
||||||
requestId = tado->deleteOverlay(homeId, zoneId);
|
requestId = tado->deleteOverlay(homeId, zoneId);
|
||||||
} else if (action.param(zoneModeActionModeParamTypeId).value().toString() == "Off") {
|
} else if (action.param(zoneModeActionModeParamTypeId).value().toString() == "Off") {
|
||||||
requestId = tado->setOverlay(homeId, zoneId, false, thing->stateValue(zoneTargetTemperatureStateTypeId).toDouble());
|
requestId = tado->setOverlay(homeId, zoneId, false, thing->stateValue(zoneTargetTemperatureStateTypeId).toDouble());
|
||||||
} else {
|
} else {
|
||||||
if(thing->stateValue(zoneTargetTemperatureStateTypeId).toDouble() <= 5.0) {
|
if(thing->stateValue(zoneTargetTemperatureStateTypeId).toDouble() <= 5.0) {
|
||||||
requestId = tado->setOverlay(homeId, zoneId, true, 5);
|
requestId = tado->setOverlay(homeId, zoneId, true, 5);
|
||||||
} else {
|
} else {
|
||||||
requestId = tado->setOverlay(homeId, zoneId, true, thing->stateValue(zoneTargetTemperatureStateTypeId).toDouble());
|
requestId = tado->setOverlay(homeId, zoneId, true, thing->stateValue(zoneTargetTemperatureStateTypeId).toDouble());
|
||||||
}
|
}
|
||||||
@ -270,6 +281,17 @@ void IntegrationPluginTado::onAuthenticationStatusChanged(bool authenticated)
|
|||||||
if (!thing)
|
if (!thing)
|
||||||
return;
|
return;
|
||||||
thing->setStateValue(tadoConnectionLoggedInStateTypeId, authenticated);
|
thing->setStateValue(tadoConnectionLoggedInStateTypeId, authenticated);
|
||||||
|
|
||||||
|
if (!authenticated) {
|
||||||
|
QTimer::singleShot(5000, [this, tado, thing] {
|
||||||
|
if (!tado->connected()) {
|
||||||
|
pluginStorage()->beginGroup(thing->id().toString());
|
||||||
|
QString password = pluginStorage()->value("password").toString();
|
||||||
|
pluginStorage()->endGroup();
|
||||||
|
tado->getToken(password);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
106
tado/tado.cpp
106
tado/tado.cpp
@ -56,6 +56,16 @@ QString Tado::username()
|
|||||||
return m_username;
|
return m_username;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Tado::authenticated()
|
||||||
|
{
|
||||||
|
return m_authenticationStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Tado::connected()
|
||||||
|
{
|
||||||
|
return m_connectionStatus;
|
||||||
|
}
|
||||||
|
|
||||||
void Tado::getToken(const QString &password)
|
void Tado::getToken(const QString &password)
|
||||||
{
|
{
|
||||||
QNetworkRequest request;
|
QNetworkRequest request;
|
||||||
@ -78,15 +88,15 @@ void Tado::getToken(const QString &password)
|
|||||||
// Check HTTP status code
|
// Check HTTP status code
|
||||||
if (status != 200 || reply->error() != QNetworkReply::NoError) {
|
if (status != 200 || reply->error() != QNetworkReply::NoError) {
|
||||||
if (reply->error() == QNetworkReply::HostNotFoundError) {
|
if (reply->error() == QNetworkReply::HostNotFoundError) {
|
||||||
emit connectionChanged(false);
|
setConnectionStatus(false);
|
||||||
}
|
}
|
||||||
if (status == 401) {
|
if (status == 401 || status == 400) {
|
||||||
emit authenticationStatusChanged(false);
|
setAuthenticationStatus(false);
|
||||||
}
|
}
|
||||||
qCWarning(dcTado()) << "Request error:" << status << reply->errorString();
|
qCWarning(dcTado()) << "Request error:" << status << reply->errorString();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
emit connectionChanged(true);
|
setConnectionStatus(true);
|
||||||
|
|
||||||
QJsonParseError error;
|
QJsonParseError error;
|
||||||
QJsonDocument data = QJsonDocument::fromJson(reply->readAll(), &error);
|
QJsonDocument data = QJsonDocument::fromJson(reply->readAll(), &error);
|
||||||
@ -111,15 +121,15 @@ void Tado::getToken(const QString &password)
|
|||||||
token.expires = obj["expires_in"].toInt();
|
token.expires = obj["expires_in"].toInt();
|
||||||
m_refreshTimer->start((token.expires - 10)*1000);
|
m_refreshTimer->start((token.expires - 10)*1000);
|
||||||
} else {
|
} else {
|
||||||
qCWarning(dcTado()) << "Received response doesn't contain an expire time";
|
qCWarning(dcTado()) << "Received response doesn't contain an expire time";
|
||||||
}
|
}
|
||||||
token.scope = obj["scope"].toString();
|
token.scope = obj["scope"].toString();
|
||||||
token.jti = obj["jti"].toString();
|
token.jti = obj["jti"].toString();
|
||||||
emit authenticationStatusChanged(true);
|
setAuthenticationStatus(true);
|
||||||
emit tokenReceived(token);
|
emit tokenReceived(token);
|
||||||
} else {
|
} else {
|
||||||
qCWarning(dcTado()) << "Received response isn't an object" << data.toJson();
|
qCWarning(dcTado()) << "Received response isn't an object" << data.toJson();
|
||||||
emit authenticationStatusChanged(false);
|
setAuthenticationStatus(false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -139,16 +149,16 @@ void Tado::getHomes()
|
|||||||
// Check HTTP status code
|
// Check HTTP status code
|
||||||
if (status != 200 || reply->error() != QNetworkReply::NoError) {
|
if (status != 200 || reply->error() != QNetworkReply::NoError) {
|
||||||
if (reply->error() == QNetworkReply::HostNotFoundError) {
|
if (reply->error() == QNetworkReply::HostNotFoundError) {
|
||||||
emit connectionChanged(false);
|
setConnectionStatus(false);
|
||||||
}
|
}
|
||||||
if (status == 401) {
|
if (status == 401 || status == 400) {
|
||||||
emit authenticationStatusChanged(false);
|
setAuthenticationStatus(false);
|
||||||
}
|
}
|
||||||
qCWarning(dcTado()) << "Request error:" << status << reply->errorString();
|
qCWarning(dcTado()) << "Request error:" << status << reply->errorString();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
emit connectionChanged(true);
|
setConnectionStatus(true);
|
||||||
emit authenticationStatusChanged(true);
|
setAuthenticationStatus(true);
|
||||||
|
|
||||||
QJsonParseError error;
|
QJsonParseError error;
|
||||||
QJsonDocument data = QJsonDocument::fromJson(reply->readAll(), &error);
|
QJsonDocument data = QJsonDocument::fromJson(reply->readAll(), &error);
|
||||||
@ -184,16 +194,16 @@ void Tado::getZones(const QString &homeId)
|
|||||||
// Check HTTP status code
|
// Check HTTP status code
|
||||||
if (status != 200 || reply->error() != QNetworkReply::NoError) {
|
if (status != 200 || reply->error() != QNetworkReply::NoError) {
|
||||||
if (reply->error() == QNetworkReply::HostNotFoundError) {
|
if (reply->error() == QNetworkReply::HostNotFoundError) {
|
||||||
emit connectionChanged(false);
|
setConnectionStatus(false);
|
||||||
}
|
}
|
||||||
if (status == 401) {
|
if (status == 401 || status == 400) {
|
||||||
emit authenticationStatusChanged(false);
|
setAuthenticationStatus(false);
|
||||||
}
|
}
|
||||||
qCWarning(dcTado()) << "Request error:" << status << reply->errorString();
|
qCWarning(dcTado()) << "Request error:" << status << reply->errorString();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
emit connectionChanged(true);
|
setConnectionStatus(true);
|
||||||
emit authenticationStatusChanged(true);
|
setAuthenticationStatus(true);
|
||||||
|
|
||||||
QJsonParseError error;
|
QJsonParseError error;
|
||||||
QJsonDocument data = QJsonDocument::fromJson(reply->readAll(), &error);
|
QJsonDocument data = QJsonDocument::fromJson(reply->readAll(), &error);
|
||||||
@ -230,17 +240,17 @@ void Tado::getZoneState(const QString &homeId, const QString &zoneId)
|
|||||||
// Check HTTP status code
|
// Check HTTP status code
|
||||||
if (status != 200 || reply->error() != QNetworkReply::NoError) {
|
if (status != 200 || reply->error() != QNetworkReply::NoError) {
|
||||||
if (reply->error() == QNetworkReply::HostNotFoundError) {
|
if (reply->error() == QNetworkReply::HostNotFoundError) {
|
||||||
emit connectionChanged(false);
|
setConnectionStatus(false);
|
||||||
}
|
}
|
||||||
if (status == 401) {
|
if (status == 401 || status == 400) {
|
||||||
emit authenticationStatusChanged(false);
|
setAuthenticationStatus(false);
|
||||||
}
|
}
|
||||||
qCWarning(dcTado()) << "Request error:" << status << reply->errorString();
|
qCWarning(dcTado()) << "Request error:" << status << reply->errorString();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
emit connectionChanged(true);
|
setConnectionStatus(true);
|
||||||
emit authenticationStatusChanged(true);
|
setAuthenticationStatus(true);
|
||||||
|
|
||||||
QJsonParseError error;
|
QJsonParseError error;
|
||||||
QJsonDocument data = QJsonDocument::fromJson(reply->readAll(), &error);
|
QJsonDocument data = QJsonDocument::fromJson(reply->readAll(), &error);
|
||||||
@ -291,9 +301,9 @@ QUuid Tado::setOverlay(const QString &homeId, const QString &zoneId, bool power,
|
|||||||
QByteArray body;
|
QByteArray body;
|
||||||
QByteArray powerString;
|
QByteArray powerString;
|
||||||
if (power)
|
if (power)
|
||||||
powerString = "ON";
|
powerString = "ON";
|
||||||
else
|
else
|
||||||
powerString = "OFF";
|
powerString = "OFF";
|
||||||
|
|
||||||
body.append("{\"setting\":{\"type\":\"HEATING\",\"power\":\""+ powerString + "\",\"temperature\":{\"celsius\":" + QVariant(targetTemperature).toByteArray() + "}},\"termination\":{\"type\":\"MANUAL\"}}");
|
body.append("{\"setting\":{\"type\":\"HEATING\",\"power\":\""+ powerString + "\",\"temperature\":{\"celsius\":" + QVariant(targetTemperature).toByteArray() + "}},\"termination\":{\"type\":\"MANUAL\"}}");
|
||||||
//qCDebug(dcTado()) << "Sending request" << body;
|
//qCDebug(dcTado()) << "Sending request" << body;
|
||||||
@ -306,10 +316,10 @@ QUuid Tado::setOverlay(const QString &homeId, const QString &zoneId, bool power,
|
|||||||
if (status != 200 || reply->error() != QNetworkReply::NoError) {
|
if (status != 200 || reply->error() != QNetworkReply::NoError) {
|
||||||
emit requestExecuted(requestId, false);
|
emit requestExecuted(requestId, false);
|
||||||
if (reply->error() == QNetworkReply::HostNotFoundError) {
|
if (reply->error() == QNetworkReply::HostNotFoundError) {
|
||||||
emit connectionChanged(false);
|
setConnectionStatus(false);
|
||||||
}
|
}
|
||||||
if (status == 401) { //Unauthorized
|
if (status == 401 || status == 400) { //Unauthorized
|
||||||
emit authenticationStatusChanged(false);
|
setAuthenticationStatus(false);
|
||||||
} else if (status == 422) { //Unprocessable Entity
|
} else if (status == 422) { //Unprocessable Entity
|
||||||
qCWarning(dcTado()) << "Unprocessable Entity, probably a value out of range";
|
qCWarning(dcTado()) << "Unprocessable Entity, probably a value out of range";
|
||||||
} else {
|
} else {
|
||||||
@ -317,8 +327,8 @@ QUuid Tado::setOverlay(const QString &homeId, const QString &zoneId, bool power,
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
emit authenticationStatusChanged(true);
|
setAuthenticationStatus(true);
|
||||||
emit connectionChanged(true);
|
setConnectionStatus(true);
|
||||||
emit requestExecuted(requestId, true);
|
emit requestExecuted(requestId, true);
|
||||||
|
|
||||||
QJsonParseError error;
|
QJsonParseError error;
|
||||||
@ -358,10 +368,10 @@ QUuid Tado::deleteOverlay(const QString &homeId, const QString &zoneId)
|
|||||||
if (status < 200 || status > 210 || reply->error() != QNetworkReply::NoError) {
|
if (status < 200 || status > 210 || reply->error() != QNetworkReply::NoError) {
|
||||||
emit requestExecuted(requestId ,false);
|
emit requestExecuted(requestId ,false);
|
||||||
if (reply->error() == QNetworkReply::HostNotFoundError) {
|
if (reply->error() == QNetworkReply::HostNotFoundError) {
|
||||||
emit connectionChanged(false);
|
setConnectionStatus(false);
|
||||||
}
|
}
|
||||||
if (status == 401) { //Unauthorized
|
if (status == 401 || status == 400) { //Unauthorized
|
||||||
emit authenticationStatusChanged(false);
|
setAuthenticationStatus(false);
|
||||||
} else if (status == 422) { //Unprocessable Entity
|
} else if (status == 422) { //Unprocessable Entity
|
||||||
qCWarning(dcTado()) << "Unprocessable Entity, probably a value out of range";
|
qCWarning(dcTado()) << "Unprocessable Entity, probably a value out of range";
|
||||||
} else {
|
} else {
|
||||||
@ -370,8 +380,8 @@ QUuid Tado::deleteOverlay(const QString &homeId, const QString &zoneId)
|
|||||||
qCWarning(dcTado()) << "Request error:" << status << reply->errorString();
|
qCWarning(dcTado()) << "Request error:" << status << reply->errorString();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
emit authenticationStatusChanged(true);
|
setAuthenticationStatus(true);
|
||||||
emit connectionChanged(true);
|
setConnectionStatus(true);
|
||||||
emit requestExecuted(requestId, true);
|
emit requestExecuted(requestId, true);
|
||||||
|
|
||||||
QJsonParseError error;
|
QJsonParseError error;
|
||||||
@ -396,6 +406,26 @@ QUuid Tado::deleteOverlay(const QString &homeId, const QString &zoneId)
|
|||||||
return requestId;
|
return requestId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Tado::setAuthenticationStatus(bool status)
|
||||||
|
{
|
||||||
|
if (m_authenticationStatus != status) {
|
||||||
|
m_authenticationStatus = status;
|
||||||
|
emit authenticationStatusChanged(status);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!status) {
|
||||||
|
m_refreshTimer->stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Tado::setConnectionStatus(bool status)
|
||||||
|
{
|
||||||
|
if (m_connectionStatus != status) {
|
||||||
|
m_connectionStatus = status;
|
||||||
|
emit connectionChanged(status);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Tado::onRefreshTimer()
|
void Tado::onRefreshTimer()
|
||||||
{
|
{
|
||||||
QNetworkRequest request;
|
QNetworkRequest request;
|
||||||
@ -416,16 +446,16 @@ void Tado::onRefreshTimer()
|
|||||||
// Check HTTP status code
|
// Check HTTP status code
|
||||||
if (status != 200 || reply->error() != QNetworkReply::NoError) {
|
if (status != 200 || reply->error() != QNetworkReply::NoError) {
|
||||||
if (reply->error() == QNetworkReply::HostNotFoundError) {
|
if (reply->error() == QNetworkReply::HostNotFoundError) {
|
||||||
emit connectionChanged(false);
|
setConnectionStatus(false);
|
||||||
}
|
}
|
||||||
if (status == 400 || status == 401) {
|
if (status == 400 || status == 401) {
|
||||||
emit authenticationStatusChanged(false);
|
setAuthenticationStatus(false);
|
||||||
}
|
}
|
||||||
qCWarning(dcTado()) << "Request error:" << status << reply->errorString();
|
qCWarning(dcTado()) << "Request error:" << status << reply->errorString();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
emit connectionChanged(true);
|
setConnectionStatus(true);
|
||||||
emit authenticationStatusChanged(true);
|
setAuthenticationStatus(true);
|
||||||
|
|
||||||
QJsonParseError error;
|
QJsonParseError error;
|
||||||
QJsonDocument data = QJsonDocument::fromJson(reply->readAll(), &error);
|
QJsonDocument data = QJsonDocument::fromJson(reply->readAll(), &error);
|
||||||
|
|||||||
@ -93,6 +93,8 @@ public:
|
|||||||
|
|
||||||
void setUsername(const QString &username);
|
void setUsername(const QString &username);
|
||||||
QString username();
|
QString username();
|
||||||
|
bool authenticated();
|
||||||
|
bool connected();
|
||||||
|
|
||||||
void getToken(const QString &password);
|
void getToken(const QString &password);
|
||||||
void getHomes();
|
void getHomes();
|
||||||
@ -114,6 +116,11 @@ private:
|
|||||||
QString m_refreshToken;
|
QString m_refreshToken;
|
||||||
QTimer *m_refreshTimer = nullptr;
|
QTimer *m_refreshTimer = nullptr;
|
||||||
|
|
||||||
|
bool m_authenticationStatus = false;
|
||||||
|
bool m_connectionStatus = false;
|
||||||
|
void setAuthenticationStatus(bool status);
|
||||||
|
void setConnectionStatus(bool status);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void connectionChanged(bool connected);
|
void connectionChanged(bool connected);
|
||||||
void authenticationStatusChanged(bool authenticated);
|
void authenticationStatusChanged(bool authenticated);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user