From 275f598f29ad79d007e981ceb0d0e471a6526242 Mon Sep 17 00:00:00 2001 From: "bernhard.trinnes" Date: Tue, 31 Mar 2020 22:32:38 +0200 Subject: [PATCH] added userandpassword setup method --- netatmo/integrationpluginnetatmo.cpp | 46 ++++++++++++- netatmo/integrationpluginnetatmo.h | 2 + netatmo/integrationpluginnetatmo.json | 95 +++++++++++---------------- 3 files changed, 85 insertions(+), 58 deletions(-) diff --git a/netatmo/integrationpluginnetatmo.cpp b/netatmo/integrationpluginnetatmo.cpp index 7964ed69..8e884367 100644 --- a/netatmo/integrationpluginnetatmo.cpp +++ b/netatmo/integrationpluginnetatmo.cpp @@ -52,6 +52,31 @@ void IntegrationPluginNetatmo::init() } +void IntegrationPluginNetatmo::startPairing(ThingPairingInfo *info) +{ + info->finish(Thing::ThingErrorNoError, QT_TR_NOOP("Please enter the login credentials for your Netatmo account.")); +} + +void IntegrationPluginNetatmo::confirmPairing(ThingPairingInfo *info, const QString &username, const QString &password) +{ + OAuth2 *authentication = new OAuth2("561c015d49c75f0d1cce6e13", "GuvKkdtu7JQlPD47qTTepRR9hQ0CUPAj4Tae3Ohcq", this); + authentication->setUrl(QUrl("https://api.netatmo.net/oauth2/token")); + authentication->setUsername(username); + authentication->setPassword(password); + authentication->setScope("read_station read_thermostat write_thermostat"); + + // Update thing connected state based on OAuth connected state + connect(authentication, &OAuth2::authenticationChanged, info, [info, authentication](){ + if (authentication->authenticated()) { + info->finish(Thing::ThingErrorNoError); + } else { + info->finish(Thing::ThingErrorAuthenticationFailure, "Wrong username of password"); + } + }); + authentication->startAuthentication(); + connect(info, &QObject::destroyed, authentication, &OAuth2::deleteLater); +} + void IntegrationPluginNetatmo::setupThing(ThingSetupInfo *info) { Thing *thing = info->thing(); @@ -64,10 +89,26 @@ void IntegrationPluginNetatmo::setupThing(ThingSetupInfo *info) connect(m_pluginTimer, &PluginTimer::timeout, this, &IntegrationPluginNetatmo::onPluginTimer); } + QString username; + QString password; + + ParamTypeId usernameParamTypeId = ParamTypeId("763c2c10-dee5-41c8-9f7e-ded741945e73"); + ParamTypeId passwordParamTypeId = ParamTypeId("c0d892d6-f359-4782-9d7d-8f74a3b53e3e"); + + if (pluginStorage()->childGroups().contains(thing->id().toString())) { + pluginStorage()->beginGroup(thing->id().toString()); + username = pluginStorage()->value("username").toString(); + password = pluginStorage()->value("password").toString(); + pluginStorage()->endGroup(); + } else { + username = thing->paramValue(usernameParamTypeId).toString(); + password = thing->paramValue(passwordParamTypeId).toString(); + } + OAuth2 *authentication = new OAuth2("561c015d49c75f0d1cce6e13", "GuvKkdtu7JQlPD47qTTepRR9hQ0CUPAj4Tae3Ohcq", this); authentication->setUrl(QUrl("https://api.netatmo.net/oauth2/token")); - authentication->setUsername(thing->paramValue(netatmoConnectionThingUsernameParamTypeId).toString()); - authentication->setPassword(thing->paramValue(netatmoConnectionThingPasswordParamTypeId).toString()); + authentication->setUsername(username); + authentication->setPassword(password); authentication->setScope("read_station read_thermostat write_thermostat"); m_authentications.insert(authentication, thing); @@ -78,7 +119,6 @@ void IntegrationPluginNetatmo::setupThing(ThingSetupInfo *info) refreshData(thing, authentication->token()); } }); - authentication->startAuthentication(); // Report thing setup finished when authentication reports success diff --git a/netatmo/integrationpluginnetatmo.h b/netatmo/integrationpluginnetatmo.h index d6f99ef6..d0b6ce06 100644 --- a/netatmo/integrationpluginnetatmo.h +++ b/netatmo/integrationpluginnetatmo.h @@ -51,6 +51,8 @@ public: ~IntegrationPluginNetatmo(); void init() override; + void startPairing(ThingPairingInfo *info) override; + void confirmPairing(ThingPairingInfo *info, const QString &username, const QString &secret) override; void setupThing(ThingSetupInfo *info) override; void thingRemoved(Thing *thing) override; void postSetupThing(Thing *thing) override; diff --git a/netatmo/integrationpluginnetatmo.json b/netatmo/integrationpluginnetatmo.json index ed81a47a..0276347c 100644 --- a/netatmo/integrationpluginnetatmo.json +++ b/netatmo/integrationpluginnetatmo.json @@ -13,29 +13,14 @@ "name": "netatmoConnection", "displayName": "Netatmo Connection", "interfaces": ["gateway"], + "setupMethod": "userandpassword", "createMethods": ["user"], - "paramTypes": [ - { - "id": "763c2c10-dee5-41c8-9f7e-ded741945e73", - "name": "username", - "displayName": "username", - "type": "QString", - "inputType": "TextLine" - }, - { - "id": "c0d892d6-f359-4782-9d7d-8f74a3b53e3e", - "name": "password", - "displayName": "password", - "type": "QString", - "inputType": "Password" - } - ], "stateTypes": [ { "id": "2f79bc1d-27ed-480a-b583-728363c83ea6", "name": "connected", - "displayName": "available", - "displayNameEvent": "available changed", + "displayName": "Available", + "displayNameEvent": "Available changed", "type": "bool", "defaultValue": false } @@ -58,7 +43,7 @@ { "id": "157d470a-e579-4d0e-b879-6b5bfa8e34ae", "name": "mac", - "displayName": "mac address", + "displayName": "MAC Address", "type": "QString", "inputType": "TextLine", "readOnly": true @@ -68,8 +53,8 @@ { "id": "50da9f6b-c350-401c-a72e-2e4036f3975d", "name": "updateTime", - "displayName": "last update", - "displayNameEvent": "last update changed", + "displayName": "Last update", + "displayNameEvent": "Last update changed", "unit": "UnixTime", "type": "int", "defaultValue": 0 @@ -77,8 +62,8 @@ { "id": "3cb25538-e463-40ae-92f9-8f34f0c06b92", "name": "temperature", - "displayName": "temperature", - "displayNameEvent": "temperature changed", + "displayName": "Temperature", + "displayNameEvent": "Temperature changed", "unit": "DegreeCelsius", "type": "double", "defaultValue": 0 @@ -86,8 +71,8 @@ { "id": "ae8bb713-8805-4efd-89a1-bca44a1f1690", "name": "temperatureMin", - "displayName": "temperature minimum", - "displayNameEvent": "temperature minimum changed", + "displayName": "Temperature minimum", + "displayNameEvent": "Temperature minimum changed", "unit": "DegreeCelsius", "type": "double", "defaultValue": 0 @@ -95,8 +80,8 @@ { "id": "dd30507e-037b-4c74-bcca-e04b94c7c5fe", "name": "temperatureMax", - "displayName": "temperature maximum", - "displayNameEvent": "temperature maximum changed", + "displayName": "Temperature maximum", + "displayNameEvent": "Temperature maximum changed", "unit": "DegreeCelsius", "type": "double", "defaultValue": 0 @@ -104,8 +89,8 @@ { "id": "e2db5f01-196a-48d1-8874-6b8cbfe0d8c9", "name": "humidity", - "displayName": "humidity", - "displayNameEvent": "humidity changed", + "displayName": "Humidity", + "displayNameEvent": "Humidity changed", "unit": "Percentage", "type": "double", "defaultValue": 0, @@ -115,8 +100,8 @@ { "id": "03b0a7b7-987d-4d3b-b3f0-21d9f92ad326", "name": "pressure", - "displayName": "pressure", - "displayNameEvent": "pressure changed", + "displayName": "Pressure", + "displayNameEvent": "Pressure changed", "unit": "MilliBar", "type": "double", "defaultValue": 0 @@ -124,8 +109,8 @@ { "id": "906cea9d-1daf-4e9c-90b9-e40f43052a34", "name": "noise", - "displayName": "noise", - "displayNameEvent": "noise changed", + "displayName": "Noise", + "displayNameEvent": "Noise changed", "unit": "Dezibel", "type": "double", "defaultValue": 0 @@ -133,8 +118,8 @@ { "id": "e5710bd1-79fa-4bd4-9052-8416aae909b9", "name": "co2", - "displayName": "co2", - "displayNameEvent": "co2 changed", + "displayName": "CO2", + "displayNameEvent": "CO2 changed", "unit": "PartsPerMillion", "type": "double", "defaultValue": 0 @@ -142,8 +127,8 @@ { "id": "6ea906d4-5740-454d-a730-6fdb9fa0d624", "name": "wifiStrength", - "displayName": "wifi signal strength", - "displayNameEvent": "wifi signal strength changed", + "displayName": "WiFi signal strength", + "displayNameEvent": "WiFi signal strength changed", "unit": "Percentage", "type": "int", "defaultValue": 0 @@ -167,7 +152,7 @@ { "id": "73a76c5c-84f5-4e65-8541-457e5aca9bb0", "name": "mac", - "displayName": "mac address", + "displayName": "MAC Address", "type": "QString", "inputType": "TextLine", "readOnly": true @@ -175,7 +160,7 @@ { "id": "d7a0ec46-760c-4fdc-9753-fe10c86fe1b9", "name": "baseStation", - "displayName": "base station", + "displayName": "Base station", "type": "QString", "inputType": "TextLine", "readOnly": true @@ -185,8 +170,8 @@ { "id": "154aad5c-4998-43c2-b9ee-0b997eb6dd69", "name": "updateTime", - "displayName": "last update", - "displayNameEvent": "last update changed", + "displayName": "Last update", + "displayNameEvent": "Last update changed", "unit": "UnixTime", "type": "int", "defaultValue": 0 @@ -194,8 +179,8 @@ { "id": "f98776bd-887e-4b01-a87f-3d8224180563", "name": "temperature", - "displayName": "temperature", - "displayNameEvent": "temperature changed", + "displayName": "Temperature", + "displayNameEvent": "Temperature changed", "unit": "DegreeCelsius", "type": "double", "defaultValue": 0 @@ -203,8 +188,8 @@ { "id": "b71e0c8b-3c94-421e-830e-dab97b6c104e", "name": "temperatureMin", - "displayName": "temperature minimum", - "displayNameEvent": "temperature minimum changed", + "displayName": "Temperature minimum", + "displayNameEvent": "Temperature minimum changed", "unit": "DegreeCelsius", "type": "double", "defaultValue": 0 @@ -212,8 +197,8 @@ { "id": "aae071dc-70d5-4a6a-8daa-3dca0d150bd7", "name": "temperatureMax", - "displayName": "temperature maximum", - "displayNameEvent": "temperature maximum changed", + "displayName": "Temperature maximum", + "displayNameEvent": "Temperature maximum changed", "unit": "DegreeCelsius", "type": "double", "defaultValue": 0 @@ -221,8 +206,8 @@ { "id": "7ba6ddeb-5142-4b87-9729-487fcda394df", "name": "humidity", - "displayName": "humidity", - "displayNameEvent": "humidity changed", + "displayName": "Humidity", + "displayNameEvent": "Humidity changed", "unit": "Percentage", "type": "double", "defaultValue": 0, @@ -232,8 +217,8 @@ { "id": "0faa3d08-9004-46fb-a5aa-a59b75e454cc", "name": "signalStrength", - "displayName": "signal strength", - "displayNameEvent": "signal strength changed", + "displayName": "Signal strength", + "displayNameEvent": "Signal strength changed", "unit": "Percentage", "type": "int", "defaultValue": 0 @@ -241,8 +226,8 @@ { "id": "15d8fae1-ba47-42e1-994d-530e8017c965", "name": "batteryLevel", - "displayName": "battery", - "displayNameEvent": "battery changed", + "displayName": "Battery", + "displayNameEvent": "Battery changed", "unit": "Percentage", "type": "int", "defaultValue": 0, @@ -252,8 +237,8 @@ { "id": "f8aeb144-014d-4ccb-81db-64ffc70f1c97", "name": "batteryCritical", - "displayName": "battery critical", - "displayNameEvent": "battery critical changed", + "displayName": "Battery critical", + "displayNameEvent": "Battery critical changed", "type": "bool", "defaultValue": false }