added account interface
This commit is contained in:
parent
3207d5c8a4
commit
f3f1d06fd5
@ -67,7 +67,8 @@ void IntegrationPluginNetatmo::startPairing(ThingPairingInfo *info)
|
|||||||
//The server replies usually 404 not found on this request
|
//The server replies usually 404 not found on this request
|
||||||
//int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
//int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||||
if (reply->error() == QNetworkReply::NetworkError::HostNotFoundError) {
|
if (reply->error() == QNetworkReply::NetworkError::HostNotFoundError) {
|
||||||
info->finish(Thing::ThingErrorHardwareNotAvailable, QT_TR_NOOP("Netatmo server is not reachable."));
|
qCWarning(dcNetatmo()) << "Netatmo server is not reachable";
|
||||||
|
info->finish(Thing::ThingErrorAuthenticationFailure, QT_TR_NOOP("Netatmo server is not reachable."));
|
||||||
} else {
|
} else {
|
||||||
info->finish(Thing::ThingErrorNoError, QT_TR_NOOP("Please enter the login credentials for your Netatmo account."));
|
info->finish(Thing::ThingErrorNoError, QT_TR_NOOP("Please enter the login credentials for your Netatmo account."));
|
||||||
}
|
}
|
||||||
@ -88,13 +89,15 @@ void IntegrationPluginNetatmo::confirmPairing(ThingPairingInfo *info, const QStr
|
|||||||
pluginStorage()->setValue("username", username);
|
pluginStorage()->setValue("username", username);
|
||||||
pluginStorage()->setValue("password", password);
|
pluginStorage()->setValue("password", password);
|
||||||
pluginStorage()->endGroup();
|
pluginStorage()->endGroup();
|
||||||
|
m_authentications.insert(authentication, info->thingId());
|
||||||
info->finish(Thing::ThingErrorNoError);
|
info->finish(Thing::ThingErrorNoError);
|
||||||
} else {
|
} else {
|
||||||
|
qCWarning(dcNetatmo()) << "Wrong username or password";
|
||||||
info->finish(Thing::ThingErrorAuthenticationFailure, QT_TR_NOOP("Wrong username or password"));
|
info->finish(Thing::ThingErrorAuthenticationFailure, QT_TR_NOOP("Wrong username or password"));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
authentication->startAuthentication();
|
authentication->startAuthentication();
|
||||||
connect(info, &QObject::destroyed, authentication, &OAuth2::deleteLater);
|
connect(info, &ThingPairingInfo::aborted, authentication, &OAuth2::deleteLater);
|
||||||
}
|
}
|
||||||
|
|
||||||
void IntegrationPluginNetatmo::setupThing(ThingSetupInfo *info)
|
void IntegrationPluginNetatmo::setupThing(ThingSetupInfo *info)
|
||||||
@ -102,7 +105,7 @@ void IntegrationPluginNetatmo::setupThing(ThingSetupInfo *info)
|
|||||||
Thing *thing = info->thing();
|
Thing *thing = info->thing();
|
||||||
|
|
||||||
if (thing->thingClassId() == netatmoConnectionThingClassId) {
|
if (thing->thingClassId() == netatmoConnectionThingClassId) {
|
||||||
qCDebug(dcNetatmo) << "Setup netatmo connection" << thing->name() << thing->params();
|
qCDebug(dcNetatmo) << "Setup Netatmo connection" << thing->name() << thing->params();
|
||||||
|
|
||||||
QString username;
|
QString username;
|
||||||
QString password;
|
QString password;
|
||||||
@ -131,19 +134,26 @@ void IntegrationPluginNetatmo::setupThing(ThingSetupInfo *info)
|
|||||||
thing->setParamValue(ParamTypeId("c0d892d6-f359-4782-9d7d-8f74a3b53e3e"), "");
|
thing->setParamValue(ParamTypeId("c0d892d6-f359-4782-9d7d-8f74a3b53e3e"), "");
|
||||||
}
|
}
|
||||||
|
|
||||||
OAuth2 *authentication;
|
if (username.isEmpty() || password.isEmpty()) {
|
||||||
if (m_authentications.values().contains(thing)) {
|
qCWarning(dcNetatmo()) << "Setup failed because of missing login credentials";
|
||||||
qCDebug(dcNetatmo()) << "Re-Discovery, not creating a new authentication";
|
info->finish(Thing::ThingErrorAuthenticationFailure, QT_TR_NOOP("Error, login credentials not found."));
|
||||||
authentication = m_authentications.key(thing);
|
return;
|
||||||
authentication->setUsername(username);
|
}
|
||||||
authentication->setPassword(password);
|
if (m_clientId.isEmpty() || m_clientSecret.isEmpty()) {
|
||||||
|
qCWarning(dcNetatmo()) << "Setup failed because of missing client credentials";
|
||||||
|
info->finish(Thing::ThingErrorAuthenticationFailure, QT_TR_NOOP("Error, client credentials are not set."));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_authentications.values().contains(thing->id())) {
|
||||||
|
OAuth2 *authentication = m_authentications.key(thing->id());
|
||||||
|
qCDebug(dcNetatmo()) << "Authenticated from discovery, not creating a new authentication";
|
||||||
|
thing->setStateValue(netatmoConnectionConnectedStateTypeId, true);
|
||||||
|
thing->setStateValue(netatmoConnectionLoggedInStateTypeId, true);
|
||||||
|
thing->setStateValue(netatmoConnectionUserDisplayNameStateTypeId, authentication->username());
|
||||||
|
info->finish(Thing::ThingErrorNoError);
|
||||||
} else {
|
} else {
|
||||||
if (m_clientId.isEmpty() || m_clientSecret.isEmpty()) {
|
OAuth2 *authentication = new OAuth2(m_clientId, m_clientSecret, this);
|
||||||
qCWarning(dcNetatmo()) << "Setup failed because of missing client credentials";
|
|
||||||
info->finish(Thing::ThingErrorAuthenticationFailure, QT_TR_NOOP("Error, client credentials are not set."));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
authentication = new OAuth2(m_clientId, m_clientSecret, this);
|
|
||||||
authentication->setUrl(QUrl("https://api.netatmo.net/oauth2/token"));
|
authentication->setUrl(QUrl("https://api.netatmo.net/oauth2/token"));
|
||||||
authentication->setUsername(username);
|
authentication->setUsername(username);
|
||||||
authentication->setPassword(password);
|
authentication->setPassword(password);
|
||||||
@ -151,28 +161,33 @@ void IntegrationPluginNetatmo::setupThing(ThingSetupInfo *info)
|
|||||||
|
|
||||||
// Update thing connected state based on OAuth connected state
|
// Update thing connected state based on OAuth connected state
|
||||||
connect(authentication, &OAuth2::authenticationChanged, thing, [this, thing, authentication](){
|
connect(authentication, &OAuth2::authenticationChanged, thing, [this, thing, authentication](){
|
||||||
|
//FIXME connected states should only change if the server is not reachable
|
||||||
thing->setStateValue(netatmoConnectionConnectedStateTypeId, authentication->authenticated());
|
thing->setStateValue(netatmoConnectionConnectedStateTypeId, authentication->authenticated());
|
||||||
|
thing->setStateValue(netatmoConnectionLoggedInStateTypeId, authentication->authenticated());
|
||||||
if (authentication->authenticated()) {
|
if (authentication->authenticated()) {
|
||||||
refreshData(thing, authentication->token());
|
refreshData(thing, authentication->token());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
authentication->startAuthentication();
|
||||||
authentication->startAuthentication();
|
|
||||||
|
|
||||||
// Report thing setup finished when authentication reports success
|
// Report thing setup finished when authentication reports success
|
||||||
connect(authentication, &OAuth2::authenticationChanged, info, [this, info, thing, authentication](){
|
connect(authentication, &OAuth2::authenticationChanged, info, [this, info, thing, authentication](){
|
||||||
if (!authentication->authenticated()) {
|
if (!authentication->authenticated()) {
|
||||||
authentication->deleteLater();
|
authentication->deleteLater();
|
||||||
info->finish(Thing::ThingErrorAuthenticationFailure, QT_TR_NOOP("Error logging in to Netatmo server."));
|
info->finish(Thing::ThingErrorAuthenticationFailure, QT_TR_NOOP("Error logging in to Netatmo server."));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
m_authentications.insert(authentication, thing);
|
thing->setStateValue(netatmoConnectionConnectedStateTypeId, true);
|
||||||
info->finish(Thing::ThingErrorNoError);
|
thing->setStateValue(netatmoConnectionLoggedInStateTypeId, true);
|
||||||
});
|
thing->setStateValue(netatmoConnectionUserDisplayNameStateTypeId, authentication->username());
|
||||||
|
m_authentications.insert(authentication, thing->id());
|
||||||
|
info->finish(Thing::ThingErrorNoError);
|
||||||
|
});
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
|
|
||||||
} else if (thing->thingClassId() == indoorThingClassId) {
|
} else if (thing->thingClassId() == indoorThingClassId) {
|
||||||
qCDebug(dcNetatmo) << "Setup netatmo indoor base station" << thing->params();
|
qCDebug(dcNetatmo) << "Setup Netatmo indoor base station" << thing->params();
|
||||||
NetatmoBaseStation *indoor = new NetatmoBaseStation(thing->paramValue(indoorThingNameParamTypeId).toString(),
|
NetatmoBaseStation *indoor = new NetatmoBaseStation(thing->paramValue(indoorThingNameParamTypeId).toString(),
|
||||||
thing->paramValue(indoorThingMacParamTypeId).toString(),
|
thing->paramValue(indoorThingMacParamTypeId).toString(),
|
||||||
this);
|
this);
|
||||||
@ -184,7 +199,6 @@ void IntegrationPluginNetatmo::setupThing(ThingSetupInfo *info)
|
|||||||
} else if (thing->thingClassId() == outdoorThingClassId) {
|
} else if (thing->thingClassId() == outdoorThingClassId) {
|
||||||
qCDebug(dcNetatmo) << "Setup netatmo outdoor module" << thing->params();
|
qCDebug(dcNetatmo) << "Setup netatmo outdoor module" << thing->params();
|
||||||
|
|
||||||
|
|
||||||
// Migrate thing parameters after changing param type UUIDs in 0.14.
|
// Migrate thing parameters after changing param type UUIDs in 0.14.
|
||||||
QMap<QString, ParamTypeId> migrationMap;
|
QMap<QString, ParamTypeId> migrationMap;
|
||||||
migrationMap.insert("a97d256c-e159-4aa0-bc71-6bd7cd0688b3", outdoorThingNameParamTypeId);
|
migrationMap.insert("a97d256c-e159-4aa0-bc71-6bd7cd0688b3", outdoorThingNameParamTypeId);
|
||||||
@ -224,7 +238,7 @@ void IntegrationPluginNetatmo::setupThing(ThingSetupInfo *info)
|
|||||||
void IntegrationPluginNetatmo::thingRemoved(Thing *thing)
|
void IntegrationPluginNetatmo::thingRemoved(Thing *thing)
|
||||||
{
|
{
|
||||||
if (thing->thingClassId() == netatmoConnectionThingClassId) {
|
if (thing->thingClassId() == netatmoConnectionThingClassId) {
|
||||||
OAuth2 * authentication = m_authentications.key(thing);
|
OAuth2 * authentication = m_authentications.key(thing->id());
|
||||||
m_authentications.remove(authentication);
|
m_authentications.remove(authentication);
|
||||||
authentication->deleteLater();
|
authentication->deleteLater();
|
||||||
} else if (thing->thingClassId() == indoorThingClassId) {
|
} else if (thing->thingClassId() == indoorThingClassId) {
|
||||||
@ -385,7 +399,7 @@ Thing *IntegrationPluginNetatmo::findOutdoorDevice(const QString &macAddress)
|
|||||||
void IntegrationPluginNetatmo::onPluginTimer()
|
void IntegrationPluginNetatmo::onPluginTimer()
|
||||||
{
|
{
|
||||||
foreach (OAuth2 *authentication, m_authentications.keys()) {
|
foreach (OAuth2 *authentication, m_authentications.keys()) {
|
||||||
Thing *thing = m_authentications.value(authentication);
|
Thing *thing = myThings().findById(m_authentications.value(authentication));
|
||||||
if (!thing) {
|
if (!thing) {
|
||||||
qCWarning(dcNetatmo()) << "Authentication without an associated Netatmo connection thing" << authentication->username();
|
qCWarning(dcNetatmo()) << "Authentication without an associated Netatmo connection thing" << authentication->username();
|
||||||
m_authentications.remove(authentication);
|
m_authentications.remove(authentication);
|
||||||
|
|||||||
@ -61,7 +61,7 @@ private:
|
|||||||
QHash<QString, QVariantMap> m_indoorStationInitData;
|
QHash<QString, QVariantMap> m_indoorStationInitData;
|
||||||
QHash<QString, QVariantMap> m_outdoorStationInitData;
|
QHash<QString, QVariantMap> m_outdoorStationInitData;
|
||||||
|
|
||||||
QHash<OAuth2 *, Thing *> m_authentications;
|
QHash<OAuth2 *, ThingId> m_authentications;
|
||||||
QHash<NetatmoBaseStation *, Thing *> m_indoorDevices;
|
QHash<NetatmoBaseStation *, Thing *> m_indoorDevices;
|
||||||
QHash<NetatmoOutdoorModule *, Thing *> m_outdoorDevices;
|
QHash<NetatmoOutdoorModule *, Thing *> m_outdoorDevices;
|
||||||
|
|
||||||
|
|||||||
@ -29,7 +29,7 @@
|
|||||||
"id": "728d5a67-27a3-400e-b83c-2765f5196f69",
|
"id": "728d5a67-27a3-400e-b83c-2765f5196f69",
|
||||||
"name": "netatmoConnection",
|
"name": "netatmoConnection",
|
||||||
"displayName": "Netatmo Connection",
|
"displayName": "Netatmo Connection",
|
||||||
"interfaces": ["gateway"],
|
"interfaces": ["account"],
|
||||||
"setupMethod": "userandpassword",
|
"setupMethod": "userandpassword",
|
||||||
"createMethods": ["user"],
|
"createMethods": ["user"],
|
||||||
"stateTypes": [
|
"stateTypes": [
|
||||||
@ -40,6 +40,22 @@
|
|||||||
"displayNameEvent": "Available changed",
|
"displayNameEvent": "Available changed",
|
||||||
"type": "bool",
|
"type": "bool",
|
||||||
"defaultValue": false
|
"defaultValue": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "d1ca8579-5d5a-4372-9139-4b083efead2e",
|
||||||
|
"name": "loggedIn",
|
||||||
|
"displayName": "Logged in",
|
||||||
|
"displayNameEvent": "Logged inchanged",
|
||||||
|
"type": "bool",
|
||||||
|
"defaultValue": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "b9c98ae6-3687-4279-8fda-bc02c7b7ea38",
|
||||||
|
"name": "userDisplayName",
|
||||||
|
"displayName": "Username",
|
||||||
|
"displayNameEvent": "Username changed",
|
||||||
|
"type": "QString",
|
||||||
|
"defaultValue": ""
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user