From 3a12648dcd2054094843b693b55d2a64021efb6d Mon Sep 17 00:00:00 2001 From: Boernsman Date: Sun, 1 Nov 2020 21:16:54 +0100 Subject: [PATCH] fixed power state change on settings change --- .../integrationplugingenericthings.cpp | 55 +++++++++++-------- .../integrationplugingenericthings.h | 2 + 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/genericthings/integrationplugingenericthings.cpp b/genericthings/integrationplugingenericthings.cpp index 6ceb2b4..6424d1d 100644 --- a/genericthings/integrationplugingenericthings.cpp +++ b/genericthings/integrationplugingenericthings.cpp @@ -243,6 +243,14 @@ void IntegrationPluginGenericThings::setupThing(ThingSetupInfo *info) timer->stop(); } }); + } else if (thing->thingClassId() == thermostatThingClassId) { + thermostatCheckPowerOutputState(thing); // check the initial values + connect(thing, &Thing::settingChanged, thing, [this, thing] (const ParamTypeId ¶mTypeId, const QVariant &value) { + Q_UNUSED(value) + if (paramTypeId == thermostatSettingsTemperatureDifferenceParamTypeId) { + thermostatCheckPowerOutputState(thing); + } + }); } info->finish(Thing::ThingErrorNoError); } @@ -627,32 +635,18 @@ void IntegrationPluginGenericThings::executeAction(ThingActionInfo *info) Q_ASSERT_X(false, "executeAction", QString("Unhandled actionTypeId: %1").arg(action.actionTypeId().toString()).toUtf8()); } else if (thing->thingClassId() == thermostatThingClassId) { - if (action.actionTypeId() == temperatureSensorInputActionTypeId) { - - double targetTemperature = thing->stateValue(thermostatTargetTemperatureStateTypeId).toDouble(); - double actualTemperature = action.param(thermostatTemperatureSensorInputActionTemperatureSensorInputParamTypeId).value().toDouble(); - thing->setStateValue(temperatureSensorInputStateTypeId, actualTemperature); - double temperatureDifference = thing->setting(thermostatSettingsTemperatureDifferenceParamTypeId).toDouble(); - if (actualTemperature <= (targetTemperature-temperatureDifference)) { - thing->setStateValue(thermostatPowerStateTypeId, true); - } else if (actualTemperature >= targetTemperature) { - thing->setStateValue(thermostatPowerStateTypeId, false); - } - info->finish(Thing::ThingErrorNoError); - return; - } - if (action.actionTypeId() == thermostatTargetTemperatureActionTypeId) { - + if (action.actionTypeId() == thermostatTemperatureSensorInputActionTypeId) { + thing->setStateValue(thermostatTemperatureSensorInputStateTypeId, action.param(thermostatTemperatureSensorInputActionTemperatureSensorInputParamTypeId).value()); + } else if (action.actionTypeId() == thermostatTargetTemperatureActionTypeId) { thing->setStateValue(thermostatTargetTemperatureStateTypeId, action.param(thermostatTargetTemperatureActionTargetTemperatureParamTypeId).value()); - info->finish(Thing::ThingErrorNoError); - return; - } - if (action.actionTypeId() == thermostatPowerActionTypeId) { + } else if (action.actionTypeId() == thermostatPowerActionTypeId) { thing->setStateValue(thermostatPowerStateTypeId, action.param(thermostatPowerActionPowerParamTypeId).value()); - info->finish(Thing::ThingErrorNoError); - return; + } else { + Q_ASSERT_X(false, "executeAction", QString("Unhandled actionTypeId: %1").arg(action.actionTypeId().toString()).toUtf8()); } - Q_ASSERT_X(false, "executeAction", QString("Unhandled actionTypeId: %1").arg(action.actionTypeId().toString()).toUtf8()); + thermostatCheckPowerOutputState(thing); + info->finish(Thing::ThingErrorNoError); + return; } else { Q_ASSERT_X(false, "executeAction", QString("Unhandled thingClassId: %1").arg(thing->thingClassId().toString()).toUtf8()); } @@ -806,3 +800,18 @@ void IntegrationPluginGenericThings::moveBlindToAngle(Action action, Thing *thin qCDebug(dcGenericThings()) << "Move to angle doesn't support this thingClass"; } } + +void IntegrationPluginGenericThings::thermostatCheckPowerOutputState(Thing *thing) +{ + double targetTemperature = thing->stateValue(thermostatTargetTemperatureStateTypeId).toDouble(); + double actualTemperature = thing->stateValue(thermostatTemperatureSensorInputStateTypeId).toDouble(); + double temperatureDifference = thing->setting(thermostatSettingsTemperatureDifferenceParamTypeId).toDouble(); + if (actualTemperature <= (targetTemperature-temperatureDifference)) { + thing->setStateValue(thermostatPowerStateTypeId, true); + } else if (actualTemperature >= targetTemperature) { + thing->setStateValue(thermostatPowerStateTypeId, false); + } else { + //Keep actual state + //Possible improvement add boost action where powerState = true is forced inside the hysteresis + } +} diff --git a/genericthings/integrationplugingenericthings.h b/genericthings/integrationplugingenericthings.h index e831b8b..84f188f 100644 --- a/genericthings/integrationplugingenericthings.h +++ b/genericthings/integrationplugingenericthings.h @@ -70,6 +70,8 @@ private: void setBlindState(BlindState state, Thing *thing); void moveBlindToPercentage(Action action, Thing *thing); void moveBlindToAngle(Action action, Thing *thing); + + void thermostatCheckPowerOutputState(Thing *thing); }; #endif // INTEGRATIONPLUGINGENERICTHINGS_H