Tado: Format source using clang-format

master
Simon Stürz 2025-12-19 15:24:13 +01:00
parent 4e9c5501d6
commit 616f660c9f
4 changed files with 68 additions and 89 deletions

View File

@ -29,9 +29,9 @@
#include <network/networkaccessmanager.h> #include <network/networkaccessmanager.h>
#include <QDebug> #include <QDebug>
#include <QUrlQuery>
#include <QJsonDocument> #include <QJsonDocument>
#include <QTimer> #include <QTimer>
#include <QUrlQuery>
#include <QtMath> #include <QtMath>
namespace { namespace {
@ -43,7 +43,7 @@ void finishPendingActions(const QList<ThingActionInfo *> &actions, Thing::ThingE
} }
} }
} }
} } // namespace
IntegrationPluginTado::IntegrationPluginTado() IntegrationPluginTado::IntegrationPluginTado()
{ {
@ -91,9 +91,7 @@ void IntegrationPluginTado::queueOverlayChange(ThingActionInfo *info, const QStr
pending.dirty = true; pending.dirty = true;
pending.pendingActions.append(info); pending.pendingActions.append(info);
connect(info, &ThingActionInfo::aborted, this, [this, info]() { connect(info, &ThingActionInfo::aborted, this, [this, info]() { removePendingAction(info); });
removePendingAction(info);
});
if (!m_stateSyncTimer.isActive()) { if (!m_stateSyncTimer.isActive()) {
m_stateSyncTimer.start(); m_stateSyncTimer.start();
@ -110,10 +108,7 @@ void IntegrationPluginTado::removePendingAction(ThingActionInfo *info)
} }
} }
void IntegrationPluginTado::init() void IntegrationPluginTado::init() {}
{
}
void IntegrationPluginTado::startPairing(ThingPairingInfo *info) void IntegrationPluginTado::startPairing(ThingPairingInfo *info)
{ {
@ -128,7 +123,7 @@ void IntegrationPluginTado::startPairing(ThingPairingInfo *info)
tado->deleteLater(); tado->deleteLater();
}); });
connect(tado, &Tado::getLoginUrlFinished, info, [info, tado, this] (bool success) { connect(tado, &Tado::getLoginUrlFinished, info, [info, tado, this](bool success) {
if (!success) { if (!success) {
info->finish(Thing::ThingErrorAuthenticationFailure); info->finish(Thing::ThingErrorAuthenticationFailure);
return; return;
@ -139,8 +134,6 @@ void IntegrationPluginTado::startPairing(ThingPairingInfo *info)
m_unfinishedTadoAccounts.take(info->thingId())->deleteLater(); m_unfinishedTadoAccounts.take(info->thingId())->deleteLater();
}); });
qCDebug(dcTado()) << "Tado server is reachable. Starting the OAuth pairing process using" << tado->loginUrl(); qCDebug(dcTado()) << "Tado server is reachable. Starting the OAuth pairing process using" << tado->loginUrl();
info->setOAuthUrl(QUrl(tado->loginUrl())); info->setOAuthUrl(QUrl(tado->loginUrl()));
info->finish(Thing::ThingErrorNoError); info->finish(Thing::ThingErrorNoError);
@ -156,8 +149,8 @@ void IntegrationPluginTado::confirmPairing(ThingPairingInfo *info, const QString
qCDebug(dcTado()) << "Confirm pairing" << password; qCDebug(dcTado()) << "Confirm pairing" << password;
Tado *tado = m_unfinishedTadoAccounts.value(info->thingId()); Tado *tado = m_unfinishedTadoAccounts.value(info->thingId());
connect(tado, &Tado::connectionError, info, [info] (QNetworkReply::NetworkError error){ connect(tado, &Tado::connectionError, info, [info](QNetworkReply::NetworkError error) {
if (error != QNetworkReply::NetworkError::NoError){ if (error != QNetworkReply::NetworkError::NoError) {
qCWarning(dcTado()) << "Confirm pairing failed" << error; qCWarning(dcTado()) << "Confirm pairing failed" << error;
info->finish(Thing::ThingErrorSetupFailed, QT_TR_NOOP("A connection error occurred.")); info->finish(Thing::ThingErrorSetupFailed, QT_TR_NOOP("A connection error occurred."));
} }
@ -185,7 +178,6 @@ void IntegrationPluginTado::setupThing(ThingSetupInfo *info)
Thing *thing = info->thing(); Thing *thing = info->thing();
if (thing->thingClassId() == tadoAccountThingClassId) { if (thing->thingClassId() == tadoAccountThingClassId) {
qCDebug(dcTado) << "Setting up Tado account" << thing->name() << thing->params(); qCDebug(dcTado) << "Setting up Tado account" << thing->name() << thing->params();
Tado *tado = nullptr; Tado *tado = nullptr;
@ -205,7 +197,6 @@ void IntegrationPluginTado::setupThing(ThingSetupInfo *info)
pluginStorage()->setValue("refreshToken", tado->refreshToken()); pluginStorage()->setValue("refreshToken", tado->refreshToken());
pluginStorage()->endGroup(); pluginStorage()->endGroup();
} else { } else {
// Load refresh token // Load refresh token
pluginStorage()->beginGroup(thing->id().toString()); pluginStorage()->beginGroup(thing->id().toString());
QString refreshToken = pluginStorage()->value("refreshToken").toString(); QString refreshToken = pluginStorage()->value("refreshToken").toString();
@ -235,7 +226,7 @@ void IntegrationPluginTado::setupThing(ThingSetupInfo *info)
} }
}); });
connect(tado, &Tado::refreshTokenReceived, this, [thing, this](const QString &refreshToken){ connect(tado, &Tado::refreshTokenReceived, this, [thing, this](const QString &refreshToken) {
pluginStorage()->beginGroup(thing->id().toString()); pluginStorage()->beginGroup(thing->id().toString());
pluginStorage()->setValue("refreshToken", refreshToken); pluginStorage()->setValue("refreshToken", refreshToken);
pluginStorage()->endGroup(); pluginStorage()->endGroup();
@ -246,8 +237,8 @@ void IntegrationPluginTado::setupThing(ThingSetupInfo *info)
info->finish(Thing::ThingErrorNoError); info->finish(Thing::ThingErrorNoError);
}); });
connect(tado, &Tado::connectionError, info, [this, info] (QNetworkReply::NetworkError error) { connect(tado, &Tado::connectionError, info, [this, info](QNetworkReply::NetworkError error) {
if (error != QNetworkReply::NetworkError::NoError){ if (error != QNetworkReply::NetworkError::NoError) {
if (m_tadoAccounts.contains(info->thing()->id())) { if (m_tadoAccounts.contains(info->thing()->id())) {
Tado *tado = m_tadoAccounts.take(info->thing()->id()); Tado *tado = m_tadoAccounts.take(info->thing()->id());
tado->deleteLater(); tado->deleteLater();
@ -274,10 +265,10 @@ void IntegrationPluginTado::setupThing(ThingSetupInfo *info)
} else if (thing->thingClassId() == zoneThingClassId) { } else if (thing->thingClassId() == zoneThingClassId) {
qCDebug(dcTado) << "Setup Tado zone" << thing->params(); qCDebug(dcTado) << "Setup Tado zone" << thing->params();
Thing *parentThing = myThings().findById(thing->parentId()); Thing *parentThing = myThings().findById(thing->parentId());
if(parentThing->setupComplete()) { if (parentThing->setupComplete()) {
return info->finish(Thing::ThingErrorNoError); return info->finish(Thing::ThingErrorNoError);
} else { } else {
connect(parentThing, &Thing::setupStatusChanged, info, [parentThing, info]{ connect(parentThing, &Thing::setupStatusChanged, info, [parentThing, info] {
if (parentThing->setupComplete()) { if (parentThing->setupComplete()) {
info->finish(Thing::ThingErrorNoError); info->finish(Thing::ThingErrorNoError);
} }
@ -298,7 +289,7 @@ void IntegrationPluginTado::thingRemoved(Thing *thing)
tado->deleteLater(); tado->deleteLater();
} }
for (auto it = m_pendingOverlayChanges.begin(); it != m_pendingOverlayChanges.end(); ) { for (auto it = m_pendingOverlayChanges.begin(); it != m_pendingOverlayChanges.end();) {
if (it->accountThingId == thing->id()) { if (it->accountThingId == thing->id()) {
finishPendingActions(it->pendingActions, Thing::ThingErrorThingNotFound); finishPendingActions(it->pendingActions, Thing::ThingErrorThingNotFound);
it = m_pendingOverlayChanges.erase(it); it = m_pendingOverlayChanges.erase(it);
@ -308,7 +299,7 @@ void IntegrationPluginTado::thingRemoved(Thing *thing)
} }
QString accountPrefix = thing->id().toString() + ":"; QString accountPrefix = thing->id().toString() + ":";
for (auto it = m_pendingRequests.begin(); it != m_pendingRequests.end(); ) { for (auto it = m_pendingRequests.begin(); it != m_pendingRequests.end();) {
if (it->zoneKey.startsWith(accountPrefix)) { if (it->zoneKey.startsWith(accountPrefix)) {
finishPendingActions(it->actions, Thing::ThingErrorThingNotFound); finishPendingActions(it->actions, Thing::ThingErrorThingNotFound);
it = m_pendingRequests.erase(it); it = m_pendingRequests.erase(it);
@ -324,7 +315,7 @@ void IntegrationPluginTado::thingRemoved(Thing *thing)
PendingOverlayChange pending = m_pendingOverlayChanges.take(zoneKey); PendingOverlayChange pending = m_pendingOverlayChanges.take(zoneKey);
finishPendingActions(pending.pendingActions, Thing::ThingErrorThingNotFound); finishPendingActions(pending.pendingActions, Thing::ThingErrorThingNotFound);
} }
for (auto it = m_pendingRequests.begin(); it != m_pendingRequests.end(); ) { for (auto it = m_pendingRequests.begin(); it != m_pendingRequests.end();) {
if (it->zoneKey == zoneKey) { if (it->zoneKey == zoneKey) {
finishPendingActions(it->actions, Thing::ThingErrorThingNotFound); finishPendingActions(it->actions, Thing::ThingErrorThingNotFound);
it = m_pendingRequests.erase(it); it = m_pendingRequests.erase(it);
@ -400,7 +391,6 @@ void IntegrationPluginTado::executeAction(ThingActionInfo *info)
} }
queueOverlayChange(info, homeId, zoneId, desired); queueOverlayChange(info, homeId, zoneId, desired);
} else if (action.actionTypeId() == zoneTargetTemperatureActionTypeId) { } else if (action.actionTypeId() == zoneTargetTemperatureActionTypeId) {
double temperature = action.param(zoneTargetTemperatureActionTargetTemperatureParamTypeId).value().toDouble(); double temperature = action.param(zoneTargetTemperatureActionTargetTemperatureParamTypeId).value().toDouble();
OverlayState desired; OverlayState desired;
if (temperature <= 0) { if (temperature <= 0) {
@ -491,7 +481,7 @@ void IntegrationPluginTado::syncPendingOverlays()
void IntegrationPluginTado::onPluginTimer() void IntegrationPluginTado::onPluginTimer()
{ {
Q_FOREACH(Tado *tado, m_tadoAccounts){ Q_FOREACH (Tado *tado, m_tadoAccounts) {
ThingId accountThingId = m_tadoAccounts.key(tado); ThingId accountThingId = m_tadoAccounts.key(tado);
if (!tado->authenticated()) { if (!tado->authenticated()) {
tado->getAccessToken(); tado->getAccessToken();
@ -509,9 +499,9 @@ void IntegrationPluginTado::onPluginTimer()
void IntegrationPluginTado::onConnectionChanged(bool connected) void IntegrationPluginTado::onConnectionChanged(bool connected)
{ {
Tado *tado = static_cast<Tado*>(sender()); Tado *tado = static_cast<Tado *>(sender());
if (m_tadoAccounts.values().contains(tado)){ if (m_tadoAccounts.values().contains(tado)) {
Thing *thing = myThings().findById(m_tadoAccounts.key(tado)); Thing *thing = myThings().findById(m_tadoAccounts.key(tado));
if (!thing) if (!thing)
return; return;
@ -529,11 +519,11 @@ void IntegrationPluginTado::onConnectionChanged(bool connected)
void IntegrationPluginTado::onAuthenticationStatusChanged(bool authenticated) void IntegrationPluginTado::onAuthenticationStatusChanged(bool authenticated)
{ {
Tado *tado = static_cast<Tado*>(sender()); Tado *tado = static_cast<Tado *>(sender());
if (m_tadoAccounts.values().contains(tado)){ if (m_tadoAccounts.values().contains(tado)) {
Thing *thing = myThings().findById(m_tadoAccounts.key(tado)); Thing *thing = myThings().findById(m_tadoAccounts.key(tado));
if (!thing){ if (!thing) {
qCWarning(dcTado()) << "OnAuthenticationChanged no thing found by ID" << m_tadoAccounts.key(tado); qCWarning(dcTado()) << "OnAuthenticationChanged no thing found by ID" << m_tadoAccounts.key(tado);
return; return;
} }
@ -550,9 +540,9 @@ void IntegrationPluginTado::onAuthenticationStatusChanged(bool authenticated)
void IntegrationPluginTado::onUsernameChanged(const QString &username) void IntegrationPluginTado::onUsernameChanged(const QString &username)
{ {
Tado *tado = static_cast<Tado*>(sender()); Tado *tado = static_cast<Tado *>(sender());
if (m_tadoAccounts.values().contains(tado)){ if (m_tadoAccounts.values().contains(tado)) {
Thing *thing = myThings().findById(m_tadoAccounts.key(tado)); Thing *thing = myThings().findById(m_tadoAccounts.key(tado));
thing->setStateValue(tadoAccountUserDisplayNameStateTypeId, username); thing->setStateValue(tadoAccountUserDisplayNameStateTypeId, username);
} }
@ -565,8 +555,7 @@ void IntegrationPluginTado::onRequestExecuted(QUuid requestId, bool success)
} }
PendingRequest request = m_pendingRequests.take(requestId); PendingRequest request = m_pendingRequests.take(requestId);
finishPendingActions(request.actions, finishPendingActions(request.actions, success ? Thing::ThingErrorNoError : Thing::ThingErrorHardwareNotAvailable);
success ? Thing::ThingErrorNoError : Thing::ThingErrorHardwareNotAvailable);
if (!m_pendingOverlayChanges.contains(request.zoneKey)) { if (!m_pendingOverlayChanges.contains(request.zoneKey)) {
return; return;
@ -593,7 +582,7 @@ void IntegrationPluginTado::onRequestExecuted(QUuid requestId, bool success)
void IntegrationPluginTado::onHomesReceived(QList<Tado::Home> homes) void IntegrationPluginTado::onHomesReceived(QList<Tado::Home> homes)
{ {
qCDebug(dcTado()) << "Homes received"; qCDebug(dcTado()) << "Homes received";
Tado *tado = static_cast<Tado*>(sender()); Tado *tado = static_cast<Tado *>(sender());
foreach (Tado::Home home, homes) { foreach (Tado::Home home, homes) {
tado->getZones(home.id); tado->getZones(home.id);
} }
@ -601,16 +590,14 @@ void IntegrationPluginTado::onHomesReceived(QList<Tado::Home> homes)
void IntegrationPluginTado::onZonesReceived(const QString &homeId, QList<Tado::Zone> zones) void IntegrationPluginTado::onZonesReceived(const QString &homeId, QList<Tado::Zone> zones)
{ {
Tado *tado = static_cast<Tado*>(sender()); Tado *tado = static_cast<Tado *>(sender());
if (m_tadoAccounts.values().contains(tado)) { if (m_tadoAccounts.values().contains(tado)) {
Thing *parentDevice = myThings().findById(m_tadoAccounts.key(tado)); Thing *parentDevice = myThings().findById(m_tadoAccounts.key(tado));
qCDebug(dcTado()) << "Zones received:" << zones.count() << parentDevice->name(); qCDebug(dcTado()) << "Zones received:" << zones.count() << parentDevice->name();
ThingDescriptors descriptors; ThingDescriptors descriptors;
foreach (Tado::Zone zone, zones) { foreach (Tado::Zone zone, zones) {
ThingDescriptor descriptor(zoneThingClassId, zone.name, "Type:" + zone.type, parentDevice->id()); ThingDescriptor descriptor(zoneThingClassId, zone.name, "Type:" + zone.type, parentDevice->id());
ParamList params; ParamList params;
params.append(Param(zoneThingHomeIdParamTypeId, homeId)); params.append(Param(zoneThingHomeIdParamTypeId, homeId));
@ -630,7 +617,7 @@ void IntegrationPluginTado::onZonesReceived(const QString &homeId, QList<Tado::Z
void IntegrationPluginTado::onZoneStateReceived(const QString &homeId, const QString &zoneId, Tado::ZoneState state) void IntegrationPluginTado::onZoneStateReceived(const QString &homeId, const QString &zoneId, Tado::ZoneState state)
{ {
Tado *tado = static_cast<Tado*>(sender()); Tado *tado = static_cast<Tado *>(sender());
ThingId parentId = m_tadoAccounts.key(tado); ThingId parentId = m_tadoAccounts.key(tado);
ParamList params; ParamList params;
params.append(Param(zoneThingHomeIdParamTypeId, homeId)); params.append(Param(zoneThingHomeIdParamTypeId, homeId));
@ -639,7 +626,7 @@ void IntegrationPluginTado::onZoneStateReceived(const QString &homeId, const QSt
if (!thing) if (!thing)
return; return;
if (state.overlayIsSet) { if (state.overlayIsSet) {
if (state.overlaySettingPower) { if (state.overlaySettingPower) {
thing->setStateValue(zoneModeStateTypeId, "Manual"); thing->setStateValue(zoneModeStateTypeId, "Manual");
} else { } else {
@ -660,7 +647,7 @@ void IntegrationPluginTado::onZoneStateReceived(const QString &homeId, const QSt
void IntegrationPluginTado::onOverlayReceived(const QString &homeId, const QString &zoneId, const Tado::Overlay &overlay) void IntegrationPluginTado::onOverlayReceived(const QString &homeId, const QString &zoneId, const Tado::Overlay &overlay)
{ {
Tado *tado = static_cast<Tado*>(sender()); Tado *tado = static_cast<Tado *>(sender());
ThingId parentId = m_tadoAccounts.key(tado); ThingId parentId = m_tadoAccounts.key(tado);
ParamList params; ParamList params;
params.append(Param(zoneThingHomeIdParamTypeId, homeId)); params.append(Param(zoneThingHomeIdParamTypeId, homeId));
@ -670,7 +657,7 @@ void IntegrationPluginTado::onOverlayReceived(const QString &homeId, const QStri
return; return;
thing->setStateValue(zoneTargetTemperatureStateTypeId, overlay.temperature); thing->setStateValue(zoneTargetTemperatureStateTypeId, overlay.temperature);
if (overlay.tadoMode == "MANUAL") { if (overlay.tadoMode == "MANUAL") {
if (overlay.power) { if (overlay.power) {
thing->setStateValue(zoneModeStateTypeId, "Manual"); thing->setStateValue(zoneModeStateTypeId, "Manual");
} else { } else {

View File

@ -26,8 +26,8 @@
#define INTEGRATIONPLUGINTADO_H #define INTEGRATIONPLUGINTADO_H
#include <integrations/integrationplugin.h> #include <integrations/integrationplugin.h>
#include <plugintimer.h>
#include <network/oauth2.h> #include <network/oauth2.h>
#include <plugintimer.h>
#include <QHash> #include <QHash>
#include <QList> #include <QList>
@ -56,13 +56,15 @@ public:
void executeAction(ThingActionInfo *info) override; void executeAction(ThingActionInfo *info) override;
private: private:
struct OverlayState { struct OverlayState
{
bool deleteOverlay = false; bool deleteOverlay = false;
bool power = false; bool power = false;
double temperature = 0.0; double temperature = 0.0;
}; };
struct PendingOverlayChange { struct PendingOverlayChange
{
ThingId accountThingId; ThingId accountThingId;
QString homeId; QString homeId;
QString zoneId; QString zoneId;
@ -74,7 +76,8 @@ private:
QList<ThingActionInfo *> pendingActions; QList<ThingActionInfo *> pendingActions;
}; };
struct PendingRequest { struct PendingRequest
{
QString zoneKey; QString zoneKey;
QList<ThingActionInfo *> actions; QList<ThingActionInfo *> actions;
OverlayState sentState; OverlayState sentState;
@ -103,7 +106,7 @@ private slots:
void onRequestExecuted(QUuid requestId, bool success); void onRequestExecuted(QUuid requestId, bool success);
void onHomesReceived(QList<Tado::Home> homes); void onHomesReceived(QList<Tado::Home> homes);
void onZonesReceived(const QString &homeId, QList<Tado::Zone> zones); void onZonesReceived(const QString &homeId, QList<Tado::Zone> zones);
void onZoneStateReceived(const QString &homeId,const QString &zoneId, Tado::ZoneState sate); void onZoneStateReceived(const QString &homeId, const QString &zoneId, Tado::ZoneState sate);
void onOverlayReceived(const QString &homeId, const QString &zoneId, const Tado::Overlay &overlay); void onOverlayReceived(const QString &homeId, const QString &zoneId, const Tado::Overlay &overlay);
}; };

View File

@ -25,14 +25,14 @@
#include "tado.h" #include "tado.h"
#include "extern-plugininfo.h" #include "extern-plugininfo.h"
#include <QJsonArray>
#include <QJsonDocument> #include <QJsonDocument>
#include <QJsonObject> #include <QJsonObject>
#include <QJsonArray>
#include <QUrlQuery> #include <QUrlQuery>
Tado::Tado(NetworkAccessManager *networkManager, QObject *parent) : Tado::Tado(NetworkAccessManager *networkManager, QObject *parent)
QObject(parent), : QObject(parent)
m_networkManager(networkManager) , m_networkManager(networkManager)
{ {
m_baseControlUrl = "https://my.tado.com/api/v2"; m_baseControlUrl = "https://my.tado.com/api/v2";
m_baseAuthorizationUrl = "https://login.tado.com/oauth2"; m_baseAuthorizationUrl = "https://login.tado.com/oauth2";
@ -40,14 +40,14 @@ Tado::Tado(NetworkAccessManager *networkManager, QObject *parent) :
m_clientId = "1bb50063-6b0c-4d11-bd99-387f4a91cc46"; m_clientId = "1bb50063-6b0c-4d11-bd99-387f4a91cc46";
m_refreshTimer.setSingleShot(true); m_refreshTimer.setSingleShot(true);
connect(&m_refreshTimer, &QTimer::timeout, this, [this](){ connect(&m_refreshTimer, &QTimer::timeout, this, [this]() {
qCDebug(dcTado()) << "Refresh token..."; qCDebug(dcTado()) << "Refresh token...";
getAccessToken(); getAccessToken();
}); });
m_pollAuthenticationTimer.setSingleShot(true); m_pollAuthenticationTimer.setSingleShot(true);
m_pollAuthenticationTimer.setInterval(2000); m_pollAuthenticationTimer.setInterval(2000);
connect(&m_pollAuthenticationTimer, &QTimer::timeout, this, [this](){ connect(&m_pollAuthenticationTimer, &QTimer::timeout, this, [this]() {
qCDebug(dcTado()) << "Checking authentication status..."; qCDebug(dcTado()) << "Checking authentication status...";
requestAuthenticationToken(); requestAuthenticationToken();
}); });
@ -68,7 +68,6 @@ bool Tado::connected()
return m_connectionStatus; return m_connectionStatus;
} }
QString Tado::loginUrl() const QString Tado::loginUrl() const
{ {
return m_loginUrl; return m_loginUrl;
@ -112,7 +111,6 @@ void Tado::getLoginUrl()
QNetworkReply *reply = m_networkManager->post(request, payload); QNetworkReply *reply = m_networkManager->post(request, payload);
connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater); connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
connect(reply, &QNetworkReply::finished, this, [reply, this] { connect(reply, &QNetworkReply::finished, this, [reply, this] {
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
// Check HTTP status code // Check HTTP status code
if (status != 200 || reply->error() != QNetworkReply::NoError) { if (status != 200 || reply->error() != QNetworkReply::NoError) {
@ -157,7 +155,6 @@ void Tado::getLoginUrl()
}); });
} }
void Tado::getAccessToken() void Tado::getAccessToken()
{ {
QNetworkRequest request = QNetworkRequest(QUrl(m_baseAuthorizationUrl + "/token")); QNetworkRequest request = QNetworkRequest(QUrl(m_baseAuthorizationUrl + "/token"));
@ -175,12 +172,10 @@ void Tado::getAccessToken()
QNetworkReply *reply = m_networkManager->post(request, payload); QNetworkReply *reply = m_networkManager->post(request, payload);
connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater); connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
connect(reply, &QNetworkReply::finished, this, [reply, this] { connect(reply, &QNetworkReply::finished, this, [reply, this] {
QByteArray data = reply->readAll(); QByteArray data = reply->readAll();
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
// Check HTTP status code // Check HTTP status code
if (status != 200 || reply->error() != QNetworkReply::NoError) { if (status != 200 || reply->error() != QNetworkReply::NoError) {
emit connectionError(reply->error()); emit connectionError(reply->error());
if (reply->error() == QNetworkReply::HostNotFoundError) if (reply->error() == QNetworkReply::HostNotFoundError)
@ -216,7 +211,6 @@ void Tado::getAccessToken()
emit refreshTokenReceived(m_refreshToken); emit refreshTokenReceived(m_refreshToken);
} }
setAuthenticationStatus(true); setAuthenticationStatus(true);
// Refresh 10 sekonds before expiration // Refresh 10 sekonds before expiration
@ -232,7 +226,7 @@ void Tado::getHomes()
return; return;
} }
if(m_accessToken.isEmpty()) { if (m_accessToken.isEmpty()) {
qCWarning(dcTado()) << "Not sending request, get the access token first"; qCWarning(dcTado()) << "Not sending request, get the access token first";
return; return;
} }
@ -244,7 +238,6 @@ void Tado::getHomes()
//qCDebug(dcTado()) << "Sending request" << request.url() << request.rawHeaderList(); //qCDebug(dcTado()) << "Sending request" << request.url() << request.rawHeaderList();
connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater); connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
connect(reply, &QNetworkReply::finished, this, [reply, this] { connect(reply, &QNetworkReply::finished, this, [reply, this] {
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
// Check HTTP status code // Check HTTP status code
@ -299,7 +292,7 @@ void Tado::getZones(const QString &homeId)
return; return;
} }
if(m_accessToken.isEmpty()) { if (m_accessToken.isEmpty()) {
qCWarning(dcTado()) << "Not sending request, get the access token first"; qCWarning(dcTado()) << "Not sending request, get the access token first";
return; return;
} }
@ -312,7 +305,6 @@ void Tado::getZones(const QString &homeId)
//qCDebug(dcTado()) << "Sending request" << request.url() << request.rawHeaderList(); //qCDebug(dcTado()) << "Sending request" << request.url() << request.rawHeaderList();
connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater); connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
connect(reply, &QNetworkReply::finished, this, [reply, homeId, this] { connect(reply, &QNetworkReply::finished, this, [reply, homeId, this] {
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
// Check HTTP status code // Check HTTP status code
@ -356,7 +348,7 @@ void Tado::getZoneState(const QString &homeId, const QString &zoneId)
return; return;
} }
if(m_accessToken.isEmpty()) { if (m_accessToken.isEmpty()) {
qCWarning(dcTado()) << "Not sending request, get the access token first"; qCWarning(dcTado()) << "Not sending request, get the access token first";
return; return;
} }
@ -369,7 +361,6 @@ void Tado::getZoneState(const QString &homeId, const QString &zoneId)
//qCDebug(dcTado()) << "Sending request" << request.url() << request.rawHeaderList(); //qCDebug(dcTado()) << "Sending request" << request.url() << request.rawHeaderList();
connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater); connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
connect(reply, &QNetworkReply::finished, this, [reply, homeId, zoneId, this] { connect(reply, &QNetworkReply::finished, this, [reply, homeId, zoneId, this] {
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
// Check HTTP status code // Check HTTP status code
@ -386,7 +377,6 @@ void Tado::getZoneState(const QString &homeId, const QString &zoneId)
return; return;
} }
setConnectionStatus(true); setConnectionStatus(true);
setAuthenticationStatus(true); setAuthenticationStatus(true);
@ -416,7 +406,7 @@ void Tado::getZoneState(const QString &homeId, const QString &zoneId)
state.temperature = dataMap["insideTemperature"].toMap().value("celsius").toDouble(); state.temperature = dataMap["insideTemperature"].toMap().value("celsius").toDouble();
state.humidity = dataMap["humidity"].toMap().value("percentage").toDouble(); state.humidity = dataMap["humidity"].toMap().value("percentage").toDouble();
if (!map["overlay"].toMap().isEmpty()){ if (!map["overlay"].toMap().isEmpty()) {
state.overlayIsSet = true; state.overlayIsSet = true;
QVariantMap overlayMap = map["overlay"].toMap(); QVariantMap overlayMap = map["overlay"].toMap();
state.overlayType = map["overlayType"].toString(); state.overlayType = map["overlayType"].toString();
@ -436,7 +426,7 @@ QUuid Tado::setOverlay(const QString &homeId, const QString &zoneId, bool power,
return QUuid(); return QUuid();
} }
if(m_accessToken.isEmpty()) { if (m_accessToken.isEmpty()) {
qCWarning(dcTado()) << "Not sending request, get the access token first"; qCWarning(dcTado()) << "Not sending request, get the access token first";
return QUuid(); return QUuid();
} }
@ -454,13 +444,13 @@ QUuid Tado::setOverlay(const QString &homeId, const QString &zoneId, bool power,
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;
QNetworkReply *reply = m_networkManager->put(request, body); QNetworkReply *reply = m_networkManager->put(request, body);
connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater); connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
connect(reply, &QNetworkReply::finished, this, [homeId, zoneId, requestId, reply, this] { connect(reply, &QNetworkReply::finished, this, [homeId, zoneId, requestId, reply, this] {
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if (status != 200 || reply->error() != QNetworkReply::NoError) { if (status != 200 || reply->error() != QNetworkReply::NoError) {
emit requestExecuted(requestId, false); emit requestExecuted(requestId, false);
@ -513,7 +503,7 @@ QUuid Tado::deleteOverlay(const QString &homeId, const QString &zoneId)
return QUuid(); return QUuid();
} }
if(m_accessToken.isEmpty()) { if (m_accessToken.isEmpty()) {
qCWarning(dcTado()) << "Not sending request, get the access token first"; qCWarning(dcTado()) << "Not sending request, get the access token first";
return QUuid(); return QUuid();
} }
@ -525,11 +515,9 @@ QUuid Tado::deleteOverlay(const QString &homeId, const QString &zoneId)
QNetworkReply *reply = m_networkManager->deleteResource(request); QNetworkReply *reply = m_networkManager->deleteResource(request);
connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater); connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
connect(reply, &QNetworkReply::finished, this, [homeId, zoneId, requestId, reply, this] { connect(reply, &QNetworkReply::finished, this, [homeId, zoneId, requestId, reply, this] {
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
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);
emit connectionError(reply->error()); emit connectionError(reply->error());
if (reply->error() == QNetworkReply::HostNotFoundError) { if (reply->error() == QNetworkReply::HostNotFoundError) {
@ -590,7 +578,6 @@ void Tado::requestAuthenticationToken()
QNetworkReply *reply = m_networkManager->post(request, payload); QNetworkReply *reply = m_networkManager->post(request, payload);
connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater); connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
connect(reply, &QNetworkReply::finished, this, [reply, this] { connect(reply, &QNetworkReply::finished, this, [reply, this] {
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if (status != 200 || reply->error() != QNetworkReply::NoError) { if (status != 200 || reply->error() != QNetworkReply::NoError) {
qCDebug(dcTado()) << "Request error:" << status << "Retrying:" << m_pollAuthenticationCount << "/" << m_pollAuthenticationLimit; qCDebug(dcTado()) << "Request error:" << status << "Retrying:" << m_pollAuthenticationCount << "/" << m_pollAuthenticationLimit;
@ -643,7 +630,6 @@ void Tado::setAuthenticationStatus(bool status)
if (!status) if (!status)
m_refreshTimer.stop(); m_refreshTimer.stop();
} }
void Tado::setConnectionStatus(bool status) void Tado::setConnectionStatus(bool status)

View File

@ -25,8 +25,8 @@
#ifndef TADO_H #ifndef TADO_H
#define TADO_H #define TADO_H
#include <network/networkaccessmanager.h>
#include <integrations/thing.h> #include <integrations/thing.h>
#include <network/networkaccessmanager.h>
#include <QObject> #include <QObject>
#include <QTimer> #include <QTimer>
@ -36,21 +36,24 @@ class Tado : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
struct Zone { struct Zone
{
QString id; QString id;
QString name; QString name;
QString type; QString type;
}; };
struct Overlay { struct Overlay
bool power; {
double temperature; bool power;
QString zoneType; double temperature;
QString terminationType; QString zoneType;
QString tadoMode; QString terminationType;
QString tadoMode;
}; };
struct ZoneState { struct ZoneState
{
bool connected = false; bool connected = false;
bool power = false; bool power = false;
QString tadoMode; QString tadoMode;
@ -68,7 +71,8 @@ public:
QString overlayType; QString overlayType;
}; };
struct Home { struct Home
{
QString id; QString id;
QString name; QString name;
}; };
@ -138,10 +142,9 @@ signals:
void homesReceived(QList<Tado::Home> homes); void homesReceived(QList<Tado::Home> homes);
void zonesReceived(const QString &homeId, QList<Tado::Zone> zones); void zonesReceived(const QString &homeId, QList<Tado::Zone> zones);
void zoneStateReceived(const QString &homeId,const QString &zoneId, Tado::ZoneState sate); void zoneStateReceived(const QString &homeId, const QString &zoneId, Tado::ZoneState sate);
void overlayReceived(const QString &homeId, const QString &zoneId, const Tado::Overlay &overlay); void overlayReceived(const QString &homeId, const QString &zoneId, const Tado::Overlay &overlay);
void connectionError(QNetworkReply::NetworkError error); void connectionError(QNetworkReply::NetworkError error);
}; };
#endif // TADO_H #endif // TADO_H