| 546 | } |
| 547 | |
| 548 | PyObject *CUnitEx_insert(CUnitEx *self) |
| 549 | { |
| 550 | module_state *pModState = CPlugin::FindModule(); |
| 551 | if (!pModState) |
| 552 | { |
| 553 | _log.Log(LOG_ERROR, "(%s) Unable to obtain module state.", __func__); |
| 554 | Py_RETURN_NONE; |
| 555 | } |
| 556 | |
| 557 | if (pModState->pPlugin) |
| 558 | { |
| 559 | std::string sName = PyBorrowedRef(self->Name); |
| 560 | if (!(CDeviceEx*)self->Parent) |
| 561 | { |
| 562 | _log.Log(LOG_ERROR, "(%s) Unit is not associated with a Device.", __func__); |
| 563 | Py_RETURN_NONE; |
| 564 | } |
| 565 | CDeviceEx *pDevice = (CDeviceEx *)self->Parent; |
| 566 | std::string sDeviceID = PyBorrowedRef(pDevice->DeviceID); |
| 567 | if (self->ID == -1) |
| 568 | { |
| 569 | if (pModState->pPlugin->m_bDebug & PDM_DEVICE) |
| 570 | { |
| 571 | pModState->pPlugin->Debug(DEBUG_PYTHON, "(%s) Creating Unit '%s'.", pModState->pPlugin->m_Name.c_str(), sName.c_str()); |
| 572 | } |
| 573 | |
| 574 | if (!m_sql.m_bAcceptNewHardware) |
| 575 | { |
| 576 | pModState->pPlugin->Log(LOG_ERROR, "(%s) Unit creation failed, Domoticz settings prevent accepting new devices.", pModState->pPlugin->m_Name.c_str()); |
| 577 | } |
| 578 | else |
| 579 | { |
| 580 | std::vector<std::vector<std::string>> result; |
| 581 | { |
| 582 | PyAllowThreads gil; |
| 583 | result = m_sql.safe_query("SELECT Name FROM DeviceStatus WHERE (HardwareID==%d) AND (DeviceID=='%s') AND (Unit==%d)", pModState->pPlugin->m_HwdID, sDeviceID.c_str(), self->Unit); |
| 584 | } |
| 585 | if (result.empty()) |
| 586 | { |
| 587 | std::string sValue = PyBorrowedRef(self->sValue); |
| 588 | std::string sColor = _tColor(std::string(PyBorrowedRef(self->Color))).toJSONString(); // Parse the color to detect incorrectly formatted color data |
| 589 | std::string sLongName = sName; |
| 590 | std::string sDescription = PyBorrowedRef(self->Description); |
| 591 | std::string sOptionValue = ""; |
| 592 | |
| 593 | // Support weird legacy 'custom' options |
| 594 | //if ((self->SubType == sTypeCustom) && (PyDict_Size(self->Options) > 0)) |
| 595 | //{ |
| 596 | // PyBorrowedRef pValueDict = PyDict_GetItemString(self->Options, "Custom"); |
| 597 | // if (pValueDict) |
| 598 | // sOptionValue = (std::string)pValueDict; |
| 599 | //} |
| 600 | |
| 601 | if (PyDict_Size(self->Options) > 0) |
| 602 | { |
| 603 | if (self->SubType != sTypeCustom) |
| 604 | { |
| 605 | PyBorrowedRef pKeyDict, pValueDict; |
nothing calls this directly
no test coverage detected