fixed netatmo setup and added wirelessconnected interface

master
Boernsman 2020-12-16 17:03:56 +01:00
parent 319d1dad37
commit 8ea2b96ee8
6 changed files with 74 additions and 21 deletions

View File

@ -154,31 +154,28 @@ void IntegrationPluginNetatmo::setupThing(ThingSetupInfo *info)
} }
} }
OAuth2 *authentication;
if (m_pairingAuthentications.values().contains(thing->id())) { if (m_pairingAuthentications.values().contains(thing->id())) {
OAuth2 *authentication = m_pairingAuthentications.key(thing->id()); authentication = m_pairingAuthentications.key(thing->id());
m_pairingAuthentications.remove(authentication); m_pairingAuthentications.remove(authentication);
qCDebug(dcNetatmo()) << "Authenticated from pairing process, not creating a new authentication"; qCDebug(dcNetatmo()) << "Authenticated from pairing process, not creating a new authentication";
thing->setStateValue(netatmoConnectionConnectedStateTypeId, true);
thing->setStateValue(netatmoConnectionLoggedInStateTypeId, true);
thing->setStateValue(netatmoConnectionUserDisplayNameStateTypeId, authentication->username());
m_authentications.insert(authentication, thing->id());
info->finish(Thing::ThingErrorNoError);
} else { } else {
OAuth2 *authentication = new OAuth2(m_clientId, m_clientSecret, this); 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);
authentication->setScope("read_station read_thermostat write_thermostat"); authentication->setScope("read_station read_thermostat write_thermostat");
}
// Update thing connected state based on OAuth connected state if (authentication->authenticated()) {
connect(authentication, &OAuth2::authenticationChanged, thing, [this, thing, authentication](){ thing->setStateValue(netatmoConnectionConnectedStateTypeId, true);
thing->setStateValue(netatmoConnectionLoggedInStateTypeId, authentication->authenticated()); thing->setStateValue(netatmoConnectionLoggedInStateTypeId, true);
if (authentication->authenticated()) { thing->setStateValue(netatmoConnectionUserDisplayNameStateTypeId, authentication->username());
refreshData(thing, authentication->token()); m_authentications.insert(authentication, thing->id());
} refreshData(thing, authentication->token());
}); info->finish(Thing::ThingErrorNoError);
} else {
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()) {
@ -193,6 +190,14 @@ void IntegrationPluginNetatmo::setupThing(ThingSetupInfo *info)
info->finish(Thing::ThingErrorNoError); info->finish(Thing::ThingErrorNoError);
}); });
} }
// Update thing connected state based on OAuth connected state
connect(authentication, &OAuth2::authenticationChanged, thing, [this, thing, authentication](){
thing->setStateValue(netatmoConnectionLoggedInStateTypeId, authentication->authenticated());
if (authentication->authenticated()) {
refreshData(thing, authentication->token());
}
});
return; return;
} else if (thing->thingClassId() == indoorThingClassId) { } else if (thing->thingClassId() == indoorThingClassId) {
@ -457,7 +462,8 @@ void IntegrationPluginNetatmo::onIndoorStatesChanged()
thing->setStateValue(indoorHumidityStateTypeId, indoor->humidity()); thing->setStateValue(indoorHumidityStateTypeId, indoor->humidity());
thing->setStateValue(indoorCo2StateTypeId, indoor->co2()); thing->setStateValue(indoorCo2StateTypeId, indoor->co2());
thing->setStateValue(indoorNoiseStateTypeId, indoor->noise()); thing->setStateValue(indoorNoiseStateTypeId, indoor->noise());
thing->setStateValue(indoorWifiStrengthStateTypeId, indoor->wifiStrength()); thing->setStateValue(indoorSignalStrengthStateTypeId, indoor->wifiStrength());
thing->setStateValue(indoorConnectedStateTypeId, indoor->reachable());
} }
void IntegrationPluginNetatmo::onOutdoorStatesChanged() void IntegrationPluginNetatmo::onOutdoorStatesChanged()
@ -473,6 +479,7 @@ void IntegrationPluginNetatmo::onOutdoorStatesChanged()
thing->setStateValue(outdoorSignalStrengthStateTypeId, outdoor->signalStrength()); thing->setStateValue(outdoorSignalStrengthStateTypeId, outdoor->signalStrength());
thing->setStateValue(outdoorBatteryLevelStateTypeId, outdoor->battery()); thing->setStateValue(outdoorBatteryLevelStateTypeId, outdoor->battery());
thing->setStateValue(outdoorBatteryCriticalStateTypeId, outdoor->battery() < 10); thing->setStateValue(outdoorBatteryCriticalStateTypeId, outdoor->battery() < 10);
thing->setStateValue(outdoorConnectedStateTypeId, outdoor->reachable());
} }
void IntegrationPluginNetatmo::updateClientCredentials() void IntegrationPluginNetatmo::updateClientCredentials()

View File

@ -63,7 +63,7 @@
"id": "1c809049-04f2-4710-99f5-6ed379a2934f", "id": "1c809049-04f2-4710-99f5-6ed379a2934f",
"name": "indoor", "name": "indoor",
"displayName": "Indoor Station", "displayName": "Indoor Station",
"interfaces": ["temperaturesensor", "humiditysensor", "pressuresensor", "noisesensor", "co2sensor"], "interfaces": ["temperaturesensor", "humiditysensor", "pressuresensor", "noisesensor", "co2sensor", "wirelessconnectable"],
"createMethods": ["auto"], "createMethods": ["auto"],
"paramTypes": [ "paramTypes": [
{ {
@ -159,12 +159,23 @@
}, },
{ {
"id": "6ea906d4-5740-454d-a730-6fdb9fa0d624", "id": "6ea906d4-5740-454d-a730-6fdb9fa0d624",
"name": "wifiStrength", "name": "signalStrength",
"displayName": "WiFi signal strength", "displayName": "WiFi signal strength",
"displayNameEvent": "WiFi signal strength changed", "displayNameEvent": "WiFi signal strength changed",
"unit": "Percentage", "unit": "Percentage",
"type": "int", "type": "uint",
"minValue": 0,
"maxValue": 100,
"defaultValue": 0 "defaultValue": 0
},
{
"id": "d84ba5d7-5202-4786-b983-958b594c341f",
"name": "connected",
"displayName": "Connected",
"displayNameEvent": "Connected changed",
"type": "bool",
"cached": false,
"defaultValue": false
} }
] ]
}, },
@ -172,7 +183,7 @@
"id": "6cc01d62-7317-4ec4-8ac4-a4cab762c179", "id": "6cc01d62-7317-4ec4-8ac4-a4cab762c179",
"name": "outdoor", "name": "outdoor",
"displayName": "Outdoor Station", "displayName": "Outdoor Station",
"interfaces": ["temperaturesensor", "humiditysensor", "batterylevel"], "interfaces": ["temperaturesensor", "humiditysensor", "batterylevel", "wirelessconnectable"],
"createMethods": ["auto"], "createMethods": ["auto"],
"paramTypes": [ "paramTypes": [
{ {
@ -253,9 +264,20 @@
"displayName": "Signal strength", "displayName": "Signal strength",
"displayNameEvent": "Signal strength changed", "displayNameEvent": "Signal strength changed",
"unit": "Percentage", "unit": "Percentage",
"type": "int", "type": "uint",
"minValue": 0,
"maxValue": 100,
"defaultValue": 0 "defaultValue": 0
}, },
{
"id": "21d7f2b7-e45b-4986-b3da-d6e8c0421bfc",
"name": "connected",
"displayName": "Connected",
"displayNameEvent": "Connected changed",
"type": "bool",
"cached": false,
"defaultValue": false
},
{ {
"id": "15d8fae1-ba47-42e1-994d-530e8017c965", "id": "15d8fae1-ba47-42e1-994d-530e8017c965",
"name": "batteryLevel", "name": "batteryLevel",

View File

@ -94,6 +94,11 @@ int NetatmoBaseStation::wifiStrength() const
return m_wifiStrength; return m_wifiStrength;
} }
bool NetatmoBaseStation::reachable() const
{
return m_reachable;
}
void NetatmoBaseStation::updateStates(const QVariantMap &data) void NetatmoBaseStation::updateStates(const QVariantMap &data)
{ {
// check data timestamp // check data timestamp
@ -124,5 +129,10 @@ void NetatmoBaseStation::updateStates(const QVariantMap &data)
m_wifiStrength = qRound(100.0 * delta / 30.0); m_wifiStrength = qRound(100.0 * delta / 30.0);
} }
} }
// update reachable state
if (data.contains("reachable")) {
m_reachable = data.value("reachable").toBool();
}
emit statesChanged(); emit statesChanged();
} }

View File

@ -55,6 +55,7 @@ public:
int noise() const; int noise() const;
int co2() const; int co2() const;
int wifiStrength() const; int wifiStrength() const;
bool reachable() const;
void updateStates(const QVariantMap &data); void updateStates(const QVariantMap &data);
@ -73,6 +74,7 @@ private:
int m_noise; int m_noise;
int m_co2; int m_co2;
int m_wifiStrength; int m_wifiStrength;
bool m_reachable;
signals: signals:
void statesChanged(); void statesChanged();

View File

@ -90,6 +90,11 @@ int NetatmoOutdoorModule::battery() const
return m_battery; return m_battery;
} }
bool NetatmoOutdoorModule::reachable() const
{
return m_reachable;
}
void NetatmoOutdoorModule::updateStates(const QVariantMap &data) void NetatmoOutdoorModule::updateStates(const QVariantMap &data)
{ {
// check data timestamp // check data timestamp
@ -130,6 +135,11 @@ void NetatmoOutdoorModule::updateStates(const QVariantMap &data)
m_signalStrength = qRound(100.0 * delta / 30.0); m_signalStrength = qRound(100.0 * delta / 30.0);
} }
} }
// update reachable state
if (data.contains("reachable")) {
m_reachable = data.value("reachable").toBool();
}
emit statesChanged(); emit statesChanged();
} }

View File

@ -52,6 +52,7 @@ public:
double maxTemperature() const; double maxTemperature() const;
int signalStrength() const; int signalStrength() const;
int battery() const; int battery() const;
bool reachable() const;
void updateStates(const QVariantMap &data); void updateStates(const QVariantMap &data);
@ -69,6 +70,7 @@ private:
double m_maxTemperature; double m_maxTemperature;
int m_signalStrength; int m_signalStrength;
int m_battery; int m_battery;
bool m_reachable;
signals: signals:
void statesChanged(); void statesChanged();