powerfox: Set current power to 0 if not connected

master
Simon Stürz 2023-02-02 00:56:15 +01:00
parent bc059d4efd
commit f7dd90e65b
2 changed files with 14 additions and 6 deletions

View File

@ -158,17 +158,14 @@ void IntegrationPluginPowerfox::postSetupThing(Thing */*thing*/)
query.addQueryItem("unit", "kWh"); query.addQueryItem("unit", "kWh");
// Can be called at max once per 3 secs. Not sure if that's per account or per meter ID yet. Assuming per meter ID for now. // Can be called at max once per 3 secs. Not sure if that's per account or per meter ID yet. Assuming per meter ID for now.
QNetworkReply *reply = request(account, "/" + powerMeter->paramValue(powerMeterThingIdParamTypeId).toString() + "/current", query); QNetworkReply *reply = request(account, "/" + powerMeter->paramValue(powerMeterThingIdParamTypeId).toString() + "/current", query);
connect(reply, &QNetworkReply::finished, powerMeter, [account, powerMeter, reply](){ connect(reply, &QNetworkReply::finished, powerMeter, [this, account, powerMeter, reply](){
if (reply->error() == QNetworkReply::AuthenticationRequiredError) { if (reply->error() == QNetworkReply::AuthenticationRequiredError) {
account->setStateValue(accountConnectedStateTypeId, false);
account->setStateValue(accountLoggedInStateTypeId, false); account->setStateValue(accountLoggedInStateTypeId, false);
markAsDisconnected(powerMeter);
} }
if (reply->error() != QNetworkReply::NoError) { if (reply->error() != QNetworkReply::NoError) {
qCWarning(dcPowerfox()) << "Failed to poll power meter:" << reply->error() << reply->errorString(); qCWarning(dcPowerfox()) << "Failed to poll power meter:" << reply->error() << reply->errorString();
powerMeter->setStateValue(powerMeterConnectedStateTypeId, false); markAsDisconnected(powerMeter);
powerMeter->setStateValue(powerMeterCurrentPowerStateTypeId, 0);
powerMeter->setStateValue(powerMeterCurrentPhaseAStateTypeId, 0);
powerMeter->setStateValue(powerMeterVoltagePhaseAStateTypeId, 0);
return; return;
} }
@ -230,3 +227,13 @@ QNetworkReply *IntegrationPluginPowerfox::request(Thing *thing, const QString &p
return reply; return reply;
} }
void IntegrationPluginPowerfox::markAsDisconnected(Thing *thing)
{
qCDebug(dcPowerfox()) << "Mark thing as disconnected" << thing;
thing->setStateValue(powerMeterConnectedStateTypeId, false);
thing->setStateValue(powerMeterCurrentPowerStateTypeId, 0);
thing->setStateValue(powerMeterCurrentPhaseAStateTypeId, 0);
thing->setStateValue(powerMeterVoltagePhaseAStateTypeId, 0);
}

View File

@ -69,6 +69,7 @@ public:
private: private:
QNetworkReply *request(Thing *thing, const QString &path, const QUrlQuery &query = QUrlQuery()); QNetworkReply *request(Thing *thing, const QString &path, const QUrlQuery &query = QUrlQuery());
void markAsDisconnected(Thing *thing);
private: private:
PluginTimer *m_pollTimer = nullptr; PluginTimer *m_pollTimer = nullptr;