Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions device_access_fn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1977,6 +1977,30 @@ bool writeZclCommand(const Resource *r, const ResourceItem *item, deCONZ::ApsCon
return result.isEnqueued;
}

/*! A function to force the correct ZCL commands for the window covering cluster to set the states open (0x00) and closed (0x01), depending on the respective resource item.
* This is relevant as only one command can be passed to a write function, but 2 are required to set the bool state correctly.
* */
bool writeCoverCommand(const Resource *r, const ResourceItem *item, deCONZ::ApsController *apsCtrl, const QVariant &cmdParameters)
{
auto map = cmdParameters.toMap();
QString command = map[QLatin1String("cmd")].toString();

if (!command.isEmpty())
{
if (item->toBool() && command != "0x00")
{
map[QLatin1String("cmd")] = "0x00";
}
else if (!item->toBool() && command != "0x01")
{
map[QLatin1String("cmd")] = "0x01";
}
}

const auto result = sendZclCommand(r, item, apsCtrl, QVariant::fromValue(map));
return result.isEnqueued;
}

ParseFunction_t DA_GetParseFunction(const QVariant &params)
{
ParseFunction_t result = nullptr;
Expand Down Expand Up @@ -2067,12 +2091,13 @@ WriteFunction_t DA_GetWriteFunction(const QVariant &params)
{
WriteFunction_t result = nullptr;

const std::array<WriteFunction, 4> functions =
const std::array<WriteFunction, 5> functions =
{
WriteFunction(QLatin1String("zcl"), 1, writeZclAttribute), // Deprecated
WriteFunction(QLatin1String("zcl:attr"), 1, writeZclAttribute),
WriteFunction(QLatin1String("zcl:cmd"), 1, writeZclCommand),
WriteFunction(QLatin1String("tuya"), 1, writeTuyaData)
WriteFunction(QLatin1String("tuya"), 1, writeTuyaData),
WriteFunction(QLatin1String("cover:openclose"), 1, writeCoverCommand)
};

QString fnName;
Expand Down
135 changes: 127 additions & 8 deletions rest_lights.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -763,13 +763,6 @@ int DeRestPluginPrivate::setLightState(const ApiRequest &req, ApiResponse &rsp)
return REQ_READY_SEND;
}

Device *device = static_cast<Device*>(taskRef.lightNode->parentResource());
Resource *rsub = nullptr;
StateChange change(StateChange::StateCallFunction, SC_WriteZclAttribute, taskRef.lightNode->haEndpoint().endpoint());
if (device && device->managed())
{
rsub = DEV_GetSubDevice(device, nullptr, taskRef.lightNode->uniqueId());
}
rsp.httpStatus = HttpStatusOk;

if (!taskRef.lightNode->isAvailable())
Expand Down Expand Up @@ -809,7 +802,25 @@ int DeRestPluginPrivate::setLightState(const ApiRequest &req, ApiResponse &rsp)
{
return setXmasLightStripState(req, rsp, taskRef, map);
}
else if (UseTuyaCluster(taskRef.lightNode->manufacturer()))

Device *device = static_cast<Device*>(taskRef.lightNode->parentResource());
Resource *rsub = nullptr;
bool devManaged = false;

if (device)
{
rsub = DEV_GetSubDevice(device, nullptr, taskRef.lightNode->uniqueId());
devManaged = device->managed();
}

StateChange change(StateChange::StateCallFunction, SC_WriteZclAttribute, taskRef.lightNode->haEndpoint().endpoint());

if (device && device->managed())
{
rsub = DEV_GetSubDevice(device, nullptr, taskRef.lightNode->uniqueId());
}

if (UseTuyaCluster(taskRef.lightNode->manufacturer()))
{
//tuya window covering
if (R_GetProductId(taskRef.lightNode).startsWith(QLatin1String("Tuya_COVD")))
Expand Down Expand Up @@ -2677,7 +2688,115 @@ int DeRestPluginPrivate::setWindowCoveringState(const ApiRequest &req, ApiRespon
rsp.httpStatus = HttpStatusBadRequest;
return REQ_READY_SEND;
}

Device *device = static_cast<Device*>(taskRef.lightNode->parentResource());
Resource *rsub = nullptr;
bool devManaged = false;

if (device)
{
rsub = DEV_GetSubDevice(device, nullptr, taskRef.lightNode->uniqueId());
devManaged = device->managed();
}

if (devManaged)
{
bool hasWriteFunction = false;
QString param;
QVariant val;
StateChange change(StateChange::StateCallFunction, SC_WriteZclAttribute, taskRef.req.dstEndpoint());

if (hasLift)
{
ResourceItem *item = taskRef.lightNode->item(RStateLift);
const auto ddfItem = DDF_GetItem(item);

if (!ddfItem.writeParameters.isNull())
{
change.addTargetValue(RStateLift, targetLiftZigBee);
val = targetLiftZigBee;
param = "lift";
hasWriteFunction = true;
}
}
else if (hasTilt)
{
ResourceItem *item = taskRef.lightNode->item(RStateTilt);
const auto ddfItem = DDF_GetItem(item);

if (!ddfItem.writeParameters.isNull())
{
change.addTargetValue(RStateTilt, targetTilt);
val = targetTilt;
param = "tilt";
hasWriteFunction = true;
}
}
else if (hasOpen)
{
ResourceItem *item = taskRef.lightNode->item(RStateOpen);
const auto ddfItem = DDF_GetItem(item);

if (!ddfItem.writeParameters.isNull())
{
change.addTargetValue(RStateOpen, targetOpen);
val = targetOpen;
param = "open";
hasWriteFunction = true;
}
}
else if (hasSpeed)
{
ResourceItem *item = taskRef.lightNode->item(RStateSpeed);
const auto ddfItem = DDF_GetItem(item);

if (!ddfItem.writeParameters.isNull())
{
change.addTargetValue(RStateSpeed, targetSpeed);
val = targetSpeed;
param = "speed";
hasWriteFunction = true;
}
}
else if (hasStop)
{
ResourceItem *item = taskRef.lightNode->item(RStateOpen);
const auto ddfItem = DDF_GetItem(item);

if (!ddfItem.writeParameters.isNull())
{
change.addTargetValue(RStateOpen, "stop");
val = "stop";
param = "stop";
hasWriteFunction = true;
}
}

if (hasWriteFunction)
{
QVariantMap rspItem;
QVariantMap rspItemState;
rspItemState[QString("/lights/%1/state/%2").arg(id).arg(param)] = val;
rspItem[QLatin1String("success")] = rspItemState;
rsp.list.append(rspItem);

if (!taskRef.lightNode->stateChanges().empty())
{
DBG_Printf(DBG_INFO, "emit event/tick: " FMT_MAC "\n", (unsigned long long)taskRef.lightNode->address().ext());
enqueueEvent({taskRef.lightNode->prefix(), REventTick, taskRef.lightNode->uniqueId(), taskRef.lightNode->address().ext()});
}

rsp.etag = taskRef.lightNode->etag;

if (rsub)
{
rsub->addStateChange(change);
}

return REQ_READY_SEND;
}
}

// Some devices invert LiftPct.
if (hasLift)
{
Expand Down