From b9cd18a45e81e39fe152b72aebb53ae3a9053e13 Mon Sep 17 00:00:00 2001 From: Boernsman Date: Tue, 27 Oct 2020 14:01:41 +0100 Subject: [PATCH] start zone setup after parent setup has been completed --- tado/integrationplugintado.cpp | 74 ++++++++++++++++++++------------- tado/integrationplugintado.json | 12 +++--- 2 files changed, 51 insertions(+), 35 deletions(-) diff --git a/tado/integrationplugintado.cpp b/tado/integrationplugintado.cpp index 178e15a5..57232b66 100644 --- a/tado/integrationplugintado.cpp +++ b/tado/integrationplugintado.cpp @@ -100,7 +100,7 @@ void IntegrationPluginTado::setupThing(ThingSetupInfo *info) { Thing *thing = info->thing(); - if (thing->thingClassId() == tadoConnectionThingClassId) { + if (thing->thingClassId() == tadoAccountThingClassId) { qCDebug(dcTado) << "Setup tado connection" << thing->name() << thing->params(); Tado *tado; @@ -155,8 +155,18 @@ void IntegrationPluginTado::setupThing(ThingSetupInfo *info) return; } else if (thing->thingClassId() == zoneThingClassId) { - qCDebug(dcTado) << "Setup tado thermostat" << thing->params(); - return info->finish(Thing::ThingErrorNoError); + qCDebug(dcTado) << "Setup Tado zone" << thing->params(); + Thing *parentThing = myThings().findById(thing->parentId()); + if(parentThing->setupComplete()) { + return info->finish(Thing::ThingErrorNoError); + } else { + connect(parentThing, &Thing::setupStatusChanged, info, [parentThing, info]{ + if (parentThing->setupComplete()) { + info->finish(Thing::ThingErrorNoError); + } + }); + } + } else { qCWarning(dcTado()) << "Unhandled thing class in setupDevice"; return info->finish(Thing::ThingErrorThingClassNotFound); @@ -165,7 +175,7 @@ void IntegrationPluginTado::setupThing(ThingSetupInfo *info) void IntegrationPluginTado::thingRemoved(Thing *thing) { - if (thing->thingClassId() == tadoConnectionThingClassId) { + if (thing->thingClassId() == tadoAccountThingClassId) { Tado *tado = m_tadoAccounts.take(thing->id()); tado->deleteLater(); } @@ -183,11 +193,11 @@ void IntegrationPluginTado::postSetupThing(Thing *thing) connect(m_pluginTimer, &PluginTimer::timeout, this, &IntegrationPluginTado::onPluginTimer); } - if (thing->thingClassId() == tadoConnectionThingClassId) { + if (thing->thingClassId() == tadoAccountThingClassId) { Tado *tado = m_tadoAccounts.value(thing->id()); - thing->setStateValue(tadoConnectionUserDisplayNameStateTypeId, tado->username()); - thing->setStateValue(tadoConnectionLoggedInStateTypeId, true); - thing->setStateValue(tadoConnectionConnectedStateTypeId, true); + thing->setStateValue(tadoAccountUserDisplayNameStateTypeId, tado->username()); + thing->setStateValue(tadoAccountLoggedInStateTypeId, true); + thing->setStateValue(tadoAccountConnectedStateTypeId, true); tado->getHomes(); } else if (thing->thingClassId() == zoneThingClassId) { @@ -261,16 +271,22 @@ void IntegrationPluginTado::executeAction(ThingActionInfo *info) void IntegrationPluginTado::onPluginTimer() { - foreach (Thing *thing, myThings().filterByThingClassId(zoneThingClassId)) { - Tado *tado = m_tadoAccounts.value(thing->parentId()); - if (!tado){ - qCWarning(dcTado()) << "Could not find any Tado connection to Zone" << thing->name(); - continue; + Q_FOREACH(Tado *tado, m_tadoAccounts){ + ThingId accountThingId = m_tadoAccounts.key(tado); + if (!tado->authenticated()) { + pluginStorage()->beginGroup(accountThingId.toString()); + QString password = pluginStorage()->value("password").toString(); + pluginStorage()->endGroup(); + tado->getToken(password); + } else { + Q_FOREACH(Thing *thing, myThings().filterByParentId(accountThingId)) { + if (thing->thingClassId() == zoneThingClassId) { + QString homeId = thing->paramValue(zoneThingHomeIdParamTypeId).toString(); + QString zoneId = thing->paramValue(zoneThingZoneIdParamTypeId).toString(); + tado->getZoneState(homeId, zoneId); + } + } } - - QString homeId = thing->paramValue(zoneThingHomeIdParamTypeId).toString(); - QString zoneId = thing->paramValue(zoneThingZoneIdParamTypeId).toString(); - tado->getZoneState(homeId, zoneId); } } @@ -282,10 +298,14 @@ void IntegrationPluginTado::onConnectionChanged(bool connected) Thing *thing = myThings().findById(m_tadoAccounts.key(tado)); if (!thing) return; - thing->setStateValue(tadoConnectionConnectedStateTypeId, connected); + thing->setStateValue(tadoAccountConnectedStateTypeId, connected); - foreach(Thing *zoneThing, myThings().filterByParentId(thing->id())) { - zoneThing->setStateValue(zoneConnectedStateTypeId, connected); + if (!connected) { + Q_FOREACH(Thing *child, myThings().filterByParentId(thing->id())) { + if (child->thingClassId() == zoneThingClassId) { + child->setStateValue(zoneConnectedStateTypeId, connected); + } + } } } } @@ -300,17 +320,13 @@ void IntegrationPluginTado::onAuthenticationStatusChanged(bool authenticated) qCWarning(dcTado()) << "OnAuthenticationChanged no thing found by ID" << m_tadoAccounts.key(tado); return; } - thing->setStateValue(tadoConnectionLoggedInStateTypeId, authenticated); - + thing->setStateValue(tadoAccountLoggedInStateTypeId, authenticated); if (!authenticated) { - QTimer::singleShot(5000, tado, [this, tado, thing] { - if (!tado->connected()) { - pluginStorage()->beginGroup(thing->id().toString()); - QString password = pluginStorage()->value("password").toString(); - pluginStorage()->endGroup(); - tado->getToken(password); + Q_FOREACH(Thing *child, myThings().filterByParentId(thing->id())) { + if (child->thingClassId() == zoneThingClassId) { + child->setStateValue(zoneConnectedStateTypeId, authenticated); } - }); + } } } } diff --git a/tado/integrationplugintado.json b/tado/integrationplugintado.json index 29a95edd..d50901e6 100644 --- a/tado/integrationplugintado.json +++ b/tado/integrationplugintado.json @@ -10,8 +10,8 @@ "thingClasses": [ { "id": "69be7d15-5658-4442-872e-42abbd8bff81", - "name": "tadoConnection", - "displayName": "Tado Connection", + "name": "tadoAccount", + "displayName": "Tado account", "interfaces": ["account"], "createMethods": ["user"], "setupMethod": "userandpassword", @@ -19,8 +19,8 @@ { "id": "2f79bc1d-27ed-480a-b583-728363c83ea6", "name": "connected", - "displayName": "Available", - "displayNameEvent": "Available changed", + "displayName": "Connected", + "displayNameEvent": "Connected changed", "type": "bool", "defaultValue": false }, @@ -77,8 +77,8 @@ { "id": "9f45a703-6a15-447c-a77a-0df731cda48e", "name": "connected", - "displayName": "Available", - "displayNameEvent": "Available changed", + "displayName": "Connected", + "displayNameEvent": "Connected changed", "type": "bool", "defaultValue": false },