Merge PR #417: Add more param getter functions to the python api

This commit is contained in:
Jenkins nymea 2021-07-06 17:18:17 +02:00
commit 449b5c4cd5
3 changed files with 46 additions and 2 deletions

View File

@ -97,7 +97,7 @@ static PyObject * PyThingActionInfo_paramValue(PyThingActionInfo* self, PyObject
for (int i = 0; i < PyTuple_Size(self->pyParams); i++) { for (int i = 0; i < PyTuple_Size(self->pyParams); i++) {
PyParam *pyParam = reinterpret_cast<PyParam*>(PyTuple_GetItem(self->pyParams, i)); PyParam *pyParam = reinterpret_cast<PyParam*>(PyTuple_GetItem(self->pyParams, i));
// We're intentionally converting both ids to QUuid here in order to be more flexible with different UUID notations // We're intentionally converting both ids to QUuid here in order to be more flexible with different UUID notations
ParamTypeId ptid = StateTypeId(PyUnicode_AsUTF8AndSize(pyParam->pyParamTypeId, nullptr)); ParamTypeId ptid = ParamTypeId(PyUnicode_AsUTF8AndSize(pyParam->pyParamTypeId, nullptr));
if (ptid == paramTypeId) { if (ptid == paramTypeId) {
Py_INCREF(pyParam->pyValue); Py_INCREF(pyParam->pyValue);
return pyParam->pyValue; return pyParam->pyValue;
@ -105,7 +105,7 @@ static PyObject * PyThingActionInfo_paramValue(PyThingActionInfo* self, PyObject
} }
qCWarning(dcPythonIntegrations()) << "No such ParamTypeId in action params" << paramTypeId; qCWarning(dcPythonIntegrations()) << "No such ParamTypeId in action params" << paramTypeId;
Py_RETURN_NONE; Py_RETURN_NONE;
}; }
static PyMemberDef PyThingActionInfo_members[] = { static PyMemberDef PyThingActionInfo_members[] = {
{"thing", T_OBJECT_EX, offsetof(PyThingActionInfo, pyThing), 0, "Thing this action is for"}, {"thing", T_OBJECT_EX, offsetof(PyThingActionInfo, pyThing), 0, "Thing this action is for"},

View File

@ -69,6 +69,27 @@ static void PyThingDiscoveryInfo_dealloc(PyThingDiscoveryInfo * self)
Py_TYPE(self)->tp_free(self); Py_TYPE(self)->tp_free(self);
} }
static PyObject * PyThingDiscoveryInfo_paramValue(PyThingDiscoveryInfo* self, PyObject* args) {
char *paramTypeIdStr = nullptr;
if (!PyArg_ParseTuple(args, "s", &paramTypeIdStr)) {
PyErr_SetString(PyExc_TypeError, "Invalid arguments in paramValue call. Expected: paramValue(paramTypeId)");
return nullptr;
}
ParamTypeId paramTypeId = ParamTypeId(paramTypeIdStr);
for (int i = 0; i < PyTuple_Size(self->pyParams); i++) {
PyParam *pyParam = reinterpret_cast<PyParam*>(PyTuple_GetItem(self->pyParams, i));
// We're intentionally converting both ids to QUuid here in order to be more flexible with different UUID notations
ParamTypeId ptid = ParamTypeId(PyUnicode_AsUTF8AndSize(pyParam->pyParamTypeId, nullptr));
if (ptid == paramTypeId) {
Py_INCREF(pyParam->pyValue);
return pyParam->pyValue;
}
}
qCWarning(dcPythonIntegrations()) << "No such ParamTypeId in discovery params" << paramTypeId;
Py_RETURN_NONE;
}
static PyObject * PyThingDiscoveryInfo_finish(PyThingDiscoveryInfo* self, PyObject* args) static PyObject * PyThingDiscoveryInfo_finish(PyThingDiscoveryInfo* self, PyObject* args)
{ {
int status; int status;
@ -142,6 +163,7 @@ static PyMemberDef PyThingDiscoveryInfo_members[] = {
}; };
static PyMethodDef PyThingDiscoveryInfo_methods[] = { static PyMethodDef PyThingDiscoveryInfo_methods[] = {
{ "paramValue", (PyCFunction)PyThingDiscoveryInfo_paramValue, METH_VARARGS, "Get a discovery param value"},
{ "addDescriptor", (PyCFunction)PyThingDiscoveryInfo_addDescriptor, METH_VARARGS, "Add a new descriptor to the discovery" }, { "addDescriptor", (PyCFunction)PyThingDiscoveryInfo_addDescriptor, METH_VARARGS, "Add a new descriptor to the discovery" },
{ "finish", (PyCFunction)PyThingDiscoveryInfo_finish, METH_VARARGS, "Finish a discovery" }, { "finish", (PyCFunction)PyThingDiscoveryInfo_finish, METH_VARARGS, "Finish a discovery" },
{nullptr, nullptr, 0, nullptr} // sentinel {nullptr, nullptr, 0, nullptr} // sentinel

View File

@ -89,6 +89,27 @@ static void PyThingPairingInfo_dealloc(PyThingPairingInfo * self)
Py_TYPE(self)->tp_free(self); Py_TYPE(self)->tp_free(self);
} }
static PyObject * PyThingPairingInfo_paramValue(PyThingPairingInfo* self, PyObject* args) {
char *paramTypeIdStr = nullptr;
if (!PyArg_ParseTuple(args, "s", &paramTypeIdStr)) {
PyErr_SetString(PyExc_TypeError, "Invalid arguments in paramValue call. Expected: paramValue(paramTypeId)");
return nullptr;
}
ParamTypeId paramTypeId = ParamTypeId(paramTypeIdStr);
for (int i = 0; i < PyTuple_Size(self->pyParams); i++) {
PyParam *pyParam = reinterpret_cast<PyParam*>(PyTuple_GetItem(self->pyParams, i));
// We're intentionally converting both ids to QUuid here in order to be more flexible with different UUID notations
ParamTypeId ptid = ParamTypeId(PyUnicode_AsUTF8AndSize(pyParam->pyParamTypeId, nullptr));
if (ptid == paramTypeId) {
Py_INCREF(pyParam->pyValue);
return pyParam->pyValue;
}
}
qCWarning(dcPythonIntegrations()) << "No such ParamTypeId in thing params" << paramTypeId;
Py_RETURN_NONE;
}
static PyObject * PyThingPairingInfo_finish(PyThingPairingInfo* self, PyObject* args) static PyObject * PyThingPairingInfo_finish(PyThingPairingInfo* self, PyObject* args)
{ {
int status; int status;
@ -113,6 +134,7 @@ static PyObject * PyThingPairingInfo_finish(PyThingPairingInfo* self, PyObject*
} }
static PyMethodDef PyThingPairingInfo_methods[] = { static PyMethodDef PyThingPairingInfo_methods[] = {
{ "paramValue", (PyCFunction)PyThingPairingInfo_paramValue, METH_VARARGS, "Get a param value for the thing to be paired"},
{ "finish", (PyCFunction)PyThingPairingInfo_finish, METH_VARARGS, "Finish a discovery" }, { "finish", (PyCFunction)PyThingPairingInfo_finish, METH_VARARGS, "Finish a discovery" },
{nullptr, nullptr, 0, nullptr} // sentinel {nullptr, nullptr, 0, nullptr} // sentinel
}; };