From 3c0a4986f0a5b3faa2478b28531a3701d88c0cc5 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Tue, 17 Dec 2019 23:57:38 +0100 Subject: [PATCH] Implement account interface --- tuya/deviceplugintuya.cpp | 17 ++++++++++++++++- tuya/deviceplugintuya.json | 18 +++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/tuya/deviceplugintuya.cpp b/tuya/deviceplugintuya.cpp index 9e8519ac..795de219 100644 --- a/tuya/deviceplugintuya.cpp +++ b/tuya/deviceplugintuya.cpp @@ -67,6 +67,11 @@ void DevicePluginTuya::setupDevice(DeviceSetupInfo *info) if (tokenRefreshTimer->isActive()) { qCDebug(dcTuya()) << "Device already set up during pairing."; device->setStateValue(tuyaCloudConnectedStateTypeId, true); + device->setStateValue(tuyaCloudLoggedInStateTypeId, true); + pluginStorage()->beginGroup(device->id().toString()); + QString username = pluginStorage()->value("username").toString(); + pluginStorage()->endGroup(); + device->setStateValue(tuyaCloudUserDisplayNameStateTypeId, username); return info->finish(Device::DeviceErrorNoError); } @@ -142,7 +147,7 @@ void DevicePluginTuya::confirmPairing(DevicePairingInfo *info, const QString &us connect(reply, &QNetworkReply::finished, &QNetworkReply::deleteLater); qCDebug(dcTuya()) << "Pairing Tuya device"; - connect(reply, &QNetworkReply::finished, info, [this, reply, info](){ + connect(reply, &QNetworkReply::finished, info, [this, reply, info, username](){ reply->deleteLater(); QByteArray data = reply->readAll(); @@ -174,6 +179,7 @@ void DevicePluginTuya::confirmPairing(DevicePairingInfo *info, const QString &us pluginStorage()->beginGroup(info->deviceId().toString()); pluginStorage()->setValue("accessToken", result.value("access_token").toString()); pluginStorage()->setValue("refreshToken", result.value("refresh_token").toString()); + pluginStorage()->setValue("username", username); pluginStorage()->endGroup(); int timeout = result.value("expires_in").toInt(); @@ -239,6 +245,7 @@ void DevicePluginTuya::refreshAccessToken(Device *device) if (reply->error() != QNetworkReply::NoError) { qCWarning(dcTuya()) << "Error refreshing access token"; device->setStateValue(tuyaCloudConnectedStateTypeId, false); + device->setStateValue(tuyaCloudLoggedInStateTypeId, false); emit tokenRefreshed(device, false); return; } @@ -249,6 +256,7 @@ void DevicePluginTuya::refreshAccessToken(Device *device) if (error.error != QJsonParseError::NoError) { qCWarning(dcTuya()) << "Failed to parse json reply when refreshing access token" << error.errorString(); device->setStateValue(tuyaCloudConnectedStateTypeId, false); + device->setStateValue(tuyaCloudLoggedInStateTypeId, false); emit tokenRefreshed(device, false); return; } @@ -256,6 +264,7 @@ void DevicePluginTuya::refreshAccessToken(Device *device) if (jsonDoc.toVariant().toMap().isEmpty()) { qCWarning(dcTuya()) << "Empty response from Tuya server"; device->setStateValue(tuyaCloudConnectedStateTypeId, false); + device->setStateValue(tuyaCloudLoggedInStateTypeId, false); return; } @@ -271,6 +280,12 @@ void DevicePluginTuya::refreshAccessToken(Device *device) QTimer *t = m_tokenExpiryTimers.value(device->id()); t->start(tokenExpiry); device->setStateValue(tuyaCloudConnectedStateTypeId, true); + device->setStateValue(tuyaCloudLoggedInStateTypeId, true); + + pluginStorage()->beginGroup(device->id().toString()); + QString username = pluginStorage()->value("username").toString(); + pluginStorage()->endGroup(); + device->setStateValue(tuyaCloudUserDisplayNameStateTypeId, username); emit tokenRefreshed(device, true); }); diff --git a/tuya/deviceplugintuya.json b/tuya/deviceplugintuya.json index 19053191..fc835cfb 100644 --- a/tuya/deviceplugintuya.json +++ b/tuya/deviceplugintuya.json @@ -14,7 +14,7 @@ "displayName": "Tuya cloud login", "createMethods": ["user"], "setupMethod": "userandpassword", - "interfaces": [ ], + "interfaces": [ "account" ], "stateTypes": [ { "id": "c844a23a-301b-4e6c-ba18-2926a38e6bf5", @@ -24,6 +24,22 @@ "type": "bool", "defaultValue": false, "cached": false + }, + { + "id": "33e9d30c-c988-4cd4-8d39-ce84bcfa79c5", + "name": "loggedIn", + "displayName": "Logged in", + "displayNameEvent": "Logged in changed", + "type": "bool", + "defaultValue": false + }, + { + "id": "15bc38a7-0962-4f3b-a0b1-4f164958493b", + "name": "userDisplayName", + "displayName": "Username", + "displayNameEvent": "User changed", + "type": "QString", + "defaultValue": "" } ] },