From a6829c0924c96437ba6e11667e9f098943c3780a Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Fri, 23 Nov 2018 10:45:08 +0100 Subject: [PATCH 1/3] fix hue naming Current master, if renaming a hue light is reset after a nymea restart because the plugin reads the light name from the bridge on every start and overwrites the nymea configured name again. Also drops the redundant name device parameter. --- philipshue/devicepluginphilipshue.cpp | 36 +++++----------------- philipshue/devicepluginphilipshue.json | 42 -------------------------- 2 files changed, 8 insertions(+), 70 deletions(-) diff --git a/philipshue/devicepluginphilipshue.cpp b/philipshue/devicepluginphilipshue.cpp index bfd1e752..776df38c 100644 --- a/philipshue/devicepluginphilipshue.cpp +++ b/philipshue/devicepluginphilipshue.cpp @@ -118,7 +118,6 @@ DeviceManager::DeviceSetupStatus DevicePluginPhilipsHue::setupDevice(Device *dev m_unconfiguredBridges.removeAll(b); qCDebug(dcPhilipsHue) << "Setup unconfigured Hue Bridge" << b->name(); // set data which was not known during discovery - device->setParamValue(bridgeDeviceNameParamTypeId, b->name()); device->setParamValue(bridgeDeviceApiKeyParamTypeId, b->apiKey()); device->setParamValue(bridgeDeviceZigbeeChannelParamTypeId, b->zigbeeChannel()); device->setParamValue(bridgeDeviceIdParamTypeId, b->id()); @@ -137,7 +136,6 @@ DeviceManager::DeviceSetupStatus DevicePluginPhilipsHue::setupDevice(Device *dev bridge->setId(device->paramValue(bridgeDeviceIdParamTypeId).toString()); bridge->setApiKey(device->paramValue(bridgeDeviceApiKeyParamTypeId).toString()); bridge->setHostAddress(QHostAddress(device->paramValue(bridgeDeviceHostParamTypeId).toString())); - bridge->setName(device->paramValue(bridgeDeviceNameParamTypeId).toString()); bridge->setMacAddress(device->paramValue(bridgeDeviceMacParamTypeId).toString()); bridge->setZigbeeChannel(device->paramValue(bridgeDeviceZigbeeChannelParamTypeId).toInt()); m_bridges.insert(bridge, device); @@ -155,7 +153,6 @@ DeviceManager::DeviceSetupStatus DevicePluginPhilipsHue::setupDevice(Device *dev hueLight->setHostAddress(bridge->hostAddress()); hueLight->setApiKey(bridge->apiKey()); hueLight->setId(device->paramValue(colorLightDeviceLightIdParamTypeId).toInt()); - hueLight->setName(device->paramValue(colorLightDeviceNameParamTypeId).toString()); hueLight->setModelId(device->paramValue(colorLightDeviceModelIdParamTypeId).toString()); hueLight->setUuid(device->paramValue(colorLightDeviceUuidParamTypeId).toString()); hueLight->setType(device->paramValue(colorLightDeviceTypeParamTypeId).toString()); @@ -163,10 +160,8 @@ DeviceManager::DeviceSetupStatus DevicePluginPhilipsHue::setupDevice(Device *dev connect(hueLight, &HueLight::stateChanged, this, &DevicePluginPhilipsHue::lightStateChanged); m_lights.insert(hueLight, device); - device->setName(hueLight->name()); - refreshLight(device); - setLightName(device, device->paramValue(colorLightDeviceNameParamTypeId).toString()); + setLightName(device, device->name()); return DeviceManager::DeviceSetupStatusSuccess; } @@ -180,7 +175,6 @@ DeviceManager::DeviceSetupStatus DevicePluginPhilipsHue::setupDevice(Device *dev hueLight->setHostAddress(bridge->hostAddress()); hueLight->setApiKey(bridge->apiKey()); hueLight->setId(device->paramValue(colorTemperatureLightDeviceLightIdParamTypeId).toInt()); - hueLight->setName(device->paramValue(colorTemperatureLightDeviceNameParamTypeId).toString()); hueLight->setModelId(device->paramValue(colorTemperatureLightDeviceModelIdParamTypeId).toString()); hueLight->setUuid(device->paramValue(colorTemperatureLightDeviceUuidParamTypeId).toString()); hueLight->setType(device->paramValue(colorTemperatureLightDeviceTypeParamTypeId).toString()); @@ -188,10 +182,8 @@ DeviceManager::DeviceSetupStatus DevicePluginPhilipsHue::setupDevice(Device *dev connect(hueLight, &HueLight::stateChanged, this, &DevicePluginPhilipsHue::lightStateChanged); m_lights.insert(hueLight, device); - device->setName(hueLight->name()); - refreshLight(device); - setLightName(device, device->paramValue(colorTemperatureLightDeviceNameParamTypeId).toString()); + setLightName(device, device->name()); return DeviceManager::DeviceSetupStatusSuccess; } @@ -205,19 +197,16 @@ DeviceManager::DeviceSetupStatus DevicePluginPhilipsHue::setupDevice(Device *dev hueLight->setHostAddress(bridge->hostAddress()); hueLight->setApiKey(bridge->apiKey()); hueLight->setId(device->paramValue(dimmableLightDeviceLightIdParamTypeId).toInt()); - hueLight->setName(device->paramValue(dimmableLightDeviceNameParamTypeId).toString()); hueLight->setModelId(device->paramValue(dimmableLightDeviceModelIdParamTypeId).toString()); hueLight->setUuid(device->paramValue(dimmableLightDeviceUuidParamTypeId).toString()); hueLight->setType(device->paramValue(dimmableLightDeviceTypeParamTypeId).toString()); connect(hueLight, &HueLight::stateChanged, this, &DevicePluginPhilipsHue::lightStateChanged); - device->setName(hueLight->name()); - m_lights.insert(hueLight, device); refreshLight(device); - setLightName(device, device->paramValue(dimmableLightDeviceNameParamTypeId).toString()); + setLightName(device, device->name()); return DeviceManager::DeviceSetupStatusSuccess; } @@ -230,13 +219,10 @@ DeviceManager::DeviceSetupStatus DevicePluginPhilipsHue::setupDevice(Device *dev hueRemote->setHostAddress(bridge->hostAddress()); hueRemote->setApiKey(bridge->apiKey()); hueRemote->setId(device->paramValue(remoteDeviceSensorIdParamTypeId).toInt()); - hueRemote->setName(device->paramValue(remoteDeviceNameParamTypeId).toString()); hueRemote->setModelId(device->paramValue(remoteDeviceModelIdParamTypeId).toString()); hueRemote->setType(device->paramValue(remoteDeviceTypeParamTypeId).toString()); hueRemote->setUuid(device->paramValue(remoteDeviceUuidParamTypeId).toString()); - device->setName(hueRemote->name()); - connect(hueRemote, &HueRemote::stateChanged, this, &DevicePluginPhilipsHue::remoteStateChanged); connect(hueRemote, &HueRemote::buttonPressed, this, &DevicePluginPhilipsHue::onRemoteButtonEvent); @@ -772,7 +758,6 @@ void DevicePluginPhilipsHue::onUpnpDiscoveryFinished() if (upnpDevice.modelDescription().contains("Philips")) { DeviceDescriptor descriptor(bridgeDeviceClassId, "Philips Hue Bridge", upnpDevice.hostAddress().toString()); ParamList params; - params.append(Param(bridgeDeviceNameParamTypeId, upnpDevice.friendlyName())); params.append(Param(bridgeDeviceHostParamTypeId, upnpDevice.hostAddress().toString())); params.append(Param(bridgeDeviceApiKeyParamTypeId, QString())); params.append(Param(bridgeDeviceMacParamTypeId, QString())); @@ -895,7 +880,6 @@ void DevicePluginPhilipsHue::processNUpnpResponse(const QByteArray &data) QVariantMap bridgeMap = bridgeVariant.toMap(); DeviceDescriptor descriptor(bridgeDeviceClassId, "Philips Hue Bridge", bridgeMap.value("internalipaddress").toString()); ParamList params; - params.append(Param(bridgeDeviceNameParamTypeId, "Philips hue")); params.append(Param(bridgeDeviceHostParamTypeId, bridgeMap.value("internalipaddress").toString())); params.append(Param(bridgeDeviceApiKeyParamTypeId, QString())); params.append(Param(bridgeDeviceMacParamTypeId, QString())); @@ -945,9 +929,8 @@ void DevicePluginPhilipsHue::processBridgeLightDiscoveryResponse(Device *device, continue; if (lightMap.value("type").toString() == "Dimmable light") { - DeviceDescriptor descriptor(dimmableLightDeviceClassId, "Philips Hue White Light", lightMap.value("name").toString(), device->id()); + DeviceDescriptor descriptor(dimmableLightDeviceClassId, lightMap.value("name").toString(), "Philips Hue White Light", device->id()); ParamList params; - params.append(Param(dimmableLightDeviceNameParamTypeId, lightMap.value("name").toString())); params.append(Param(dimmableLightDeviceModelIdParamTypeId, model)); params.append(Param(dimmableLightDeviceTypeParamTypeId, lightMap.value("type").toString())); params.append(Param(dimmableLightDeviceUuidParamTypeId, uuid)); @@ -957,9 +940,8 @@ void DevicePluginPhilipsHue::processBridgeLightDiscoveryResponse(Device *device, qCDebug(dcPhilipsHue) << "Found new dimmable light" << lightMap.value("name").toString() << model; } else if (lightMap.value("type").toString() == "Color temperature light") { - DeviceDescriptor descriptor(colorTemperatureLightDeviceClassId, "Philips Hue White Light", lightMap.value("name").toString(), device->id()); + DeviceDescriptor descriptor(colorTemperatureLightDeviceClassId, lightMap.value("name").toString(), "Philips Hue Color Temperature Light", device->id()); ParamList params; - params.append(Param(colorTemperatureLightDeviceNameParamTypeId, lightMap.value("name").toString())); params.append(Param(colorTemperatureLightDeviceModelIdParamTypeId, model)); params.append(Param(colorTemperatureLightDeviceTypeParamTypeId, lightMap.value("type").toString())); params.append(Param(colorTemperatureLightDeviceUuidParamTypeId, uuid)); @@ -969,9 +951,8 @@ void DevicePluginPhilipsHue::processBridgeLightDiscoveryResponse(Device *device, qCDebug(dcPhilipsHue) << "Found new color temperature light" << lightMap.value("name").toString() << model; } else { - DeviceDescriptor descriptor(colorLightDeviceClassId, "Philips Hue Light", lightMap.value("name").toString(), device->id()); + DeviceDescriptor descriptor(colorLightDeviceClassId, lightMap.value("name").toString(), "Philips Hue Color Light", device->id()); ParamList params; - params.append(Param(colorLightDeviceNameParamTypeId, lightMap.value("name").toString())); params.append(Param(colorLightDeviceModelIdParamTypeId, model)); params.append(Param(colorLightDeviceTypeParamTypeId, lightMap.value("type").toString())); params.append(Param(colorLightDeviceUuidParamTypeId, uuid)); @@ -1023,9 +1004,8 @@ void DevicePluginPhilipsHue::processBridgeSensorDiscoveryResponse(Device *device continue; if (sensorMap.value("type").toString() == "ZLLSwitch") { - DeviceDescriptor descriptor(remoteDeviceClassId, "Philips Hue Remote", sensorMap.value("name").toString(), device->id()); + DeviceDescriptor descriptor(remoteDeviceClassId, sensorMap.value("name").toString(), "Philips Hue Remote", device->id()); ParamList params; - params.append(Param(remoteDeviceNameParamTypeId, sensorMap.value("name").toString())); params.append(Param(remoteDeviceModelIdParamTypeId, model)); params.append(Param(remoteDeviceTypeParamTypeId, sensorMap.value("type").toString())); params.append(Param(remoteDeviceUuidParamTypeId, uuid)); @@ -1034,7 +1014,7 @@ void DevicePluginPhilipsHue::processBridgeSensorDiscoveryResponse(Device *device emit autoDevicesAppeared(remoteDeviceClassId, {descriptor}); qCDebug(dcPhilipsHue) << "Found new remote" << sensorMap.value("name").toString() << model; } else if (sensorMap.value("type").toString() == "ZGPSwitch") { - DeviceDescriptor descriptor(tapDeviceClassId, "Hue Tap", sensorMap.value("name").toString(), device->id()); + DeviceDescriptor descriptor(tapDeviceClassId, sensorMap.value("name").toString(), "Philips Hue Tap", device->id()); ParamList params; params.append(Param(tapDeviceUuidParamTypeId, uuid)); params.append(Param(tapDeviceModelIdParamTypeId, model)); diff --git a/philipshue/devicepluginphilipshue.json b/philipshue/devicepluginphilipshue.json index 3e9f5fcc..972c593b 100644 --- a/philipshue/devicepluginphilipshue.json +++ b/philipshue/devicepluginphilipshue.json @@ -24,13 +24,6 @@ "setupMethod": "pushButton", "pairingInfo": "Please press the button on the Hue Bridge within 30 seconds before you continue", "paramTypes": [ - { - "id": "275b76f2-0174-4f26-b3ec-5f108baf8bdd", - "name": "name", - "displayName": "name", - "type" : "QString", - "inputType": "TextLine" - }, { "id": "8bf5776a-d5a6-4600-8b27-481f0d803a8f", "name": "apiKey", @@ -163,13 +156,6 @@ "primaryActionTypeId": "90aaffe5-6a76-47d2-a14a-550f60390245", "createMethods": ["auto"], "paramTypes": [ - { - "id": "0d71522c-08c0-4a66-bb5e-e31eb2dd41e5", - "name": "name", - "displayName": "name", - "type" : "QString", - "inputType": "TextLine" - }, { "id": "095a463b-f59e-46b1-989a-a71f9cbe3e30", "name": "modelId", @@ -312,13 +298,6 @@ ], "createMethods": ["auto"], "paramTypes": [ - { - "id": "1fcb0645-f552-4d81-9cc3-69ad5bbe2a85", - "name": "name", - "displayName": "name", - "type" : "QString", - "inputType": "TextLine" - }, { "id": "e944795b-7863-488c-805b-a112b836bfbf", "name": "modelId", @@ -433,13 +412,6 @@ "primaryActionTypeId": "90aaffe5-6a76-47d2-a14a-550f60390245", "createMethods": ["auto"], "paramTypes": [ - { - "id": "0d71522c-08c0-4a66-bb5e-e31eb2dd41e5", - "name": "name", - "displayName": "name", - "type" : "QString", - "inputType": "TextLine" - }, { "id": "095a463b-f59e-46b1-989a-a71f9cbe3e30", "name": "modelId", @@ -538,13 +510,6 @@ "criticalStateTypeId": "19bb8d10-1b28-4ba3-99b7-a634138dcfde", "createMethods": ["auto"], "paramTypes": [ - { - "id": "0d71522c-08c0-4a66-bb5e-e31eb2dd41e5", - "name": "name", - "displayName": "name", - "type" : "QString", - "inputType": "TextLine" - }, { "id": "095a463b-f59e-46b1-989a-a71f9cbe3e30", "name": "modelId", @@ -682,13 +647,6 @@ "interfaces": ["simplemultibutton", "connectable"], "createMethods": ["auto"], "paramTypes": [ - { - "id": "3d450f00-b521-4a2c-985f-046fad5122cb", - "name": "name", - "displayName": "name", - "type" : "QString", - "inputType": "TextLine" - }, { "id": "62d92175-db3a-4da2-a72b-f58f34cb6911", "name": "modelId", From 665eda1353b1825f8729ef87302316be696e665f Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Fri, 30 Nov 2018 23:34:34 +0100 Subject: [PATCH 2/3] don't add duplicate lights --- philipshue/devicepluginphilipshue.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/philipshue/devicepluginphilipshue.cpp b/philipshue/devicepluginphilipshue.cpp index 776df38c..1844f278 100644 --- a/philipshue/devicepluginphilipshue.cpp +++ b/philipshue/devicepluginphilipshue.cpp @@ -1368,6 +1368,11 @@ bool DevicePluginPhilipsHue::lightAlreadyAdded(const QString &uuid) return true; } } + if (device->deviceClassId() == colorTemperatureLightDeviceClassId) { + if (device->paramValue(colorTemperatureLightDeviceUuidParamTypeId).toString() == uuid) { + return true; + } + } } return false; } From 3433b8f8ab4a795790b41910124c89eda5bff1f0 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Sat, 1 Dec 2018 00:27:24 +0100 Subject: [PATCH 3/3] only set device name when the user changes it --- philipshue/devicepluginphilipshue.cpp | 59 +++++++++++++++++---------- philipshue/devicepluginphilipshue.h | 6 ++- 2 files changed, 41 insertions(+), 24 deletions(-) diff --git a/philipshue/devicepluginphilipshue.cpp b/philipshue/devicepluginphilipshue.cpp index 1844f278..915b123d 100644 --- a/philipshue/devicepluginphilipshue.cpp +++ b/philipshue/devicepluginphilipshue.cpp @@ -110,6 +110,7 @@ DeviceManager::DeviceError DevicePluginPhilipsHue::discoverDevices(const DeviceC DeviceManager::DeviceSetupStatus DevicePluginPhilipsHue::setupDevice(Device *device) { + connect(device, &Device::nameChanged, this, &DevicePluginPhilipsHue::onDeviceNameChanged); // hue bridge if (device->deviceClassId() == bridgeDeviceClassId) { // unconfigured bridges (from pairing) @@ -161,7 +162,6 @@ DeviceManager::DeviceSetupStatus DevicePluginPhilipsHue::setupDevice(Device *dev m_lights.insert(hueLight, device); refreshLight(device); - setLightName(device, device->name()); return DeviceManager::DeviceSetupStatusSuccess; } @@ -183,7 +183,6 @@ DeviceManager::DeviceSetupStatus DevicePluginPhilipsHue::setupDevice(Device *dev m_lights.insert(hueLight, device); refreshLight(device); - setLightName(device, device->name()); return DeviceManager::DeviceSetupStatusSuccess; } @@ -206,7 +205,6 @@ DeviceManager::DeviceSetupStatus DevicePluginPhilipsHue::setupDevice(Device *dev m_lights.insert(hueLight, device); refreshLight(device); - setLightName(device, device->name()); return DeviceManager::DeviceSetupStatusSuccess; } @@ -252,7 +250,7 @@ DeviceManager::DeviceSetupStatus DevicePluginPhilipsHue::setupDevice(Device *dev void DevicePluginPhilipsHue::deviceRemoved(Device *device) { abortRequests(m_lightRefreshRequests, device); - abortRequests(m_lightSetNameRequests, device); + abortRequests(m_setNameRequests, device); abortRequests(m_bridgeRefreshRequests, device); abortRequests(m_lightsRefreshRequests, device); abortRequests(m_sensorsRefreshRequests, device); @@ -310,6 +308,8 @@ DeviceManager::DeviceSetupStatus DevicePluginPhilipsHue::confirmPairing(const Pa void DevicePluginPhilipsHue::networkManagerReplyReady() { QNetworkReply *reply = static_cast(sender()); + reply->deleteLater(); + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); // qCDebug(dcPhilipsHue()) << "Hue reply:" << status << reply->error() << reply->errorString(); @@ -322,7 +322,6 @@ void DevicePluginPhilipsHue::networkManagerReplyReady() if (status != 200 || reply->error() != QNetworkReply::NoError) { qCWarning(dcPhilipsHue) << "Request error:" << status << reply->errorString(); pairingInfo->deleteLater(); - reply->deleteLater(); return; } processPairingResponse(pairingInfo, reply->readAll()); @@ -333,7 +332,6 @@ void DevicePluginPhilipsHue::networkManagerReplyReady() // check HTTP status code if (status != 200 || reply->error() != QNetworkReply::NoError) { qCWarning(dcPhilipsHue) << "Request error:" << status << reply->errorString(); - reply->deleteLater(); pairingInfo->deleteLater(); return; } @@ -344,7 +342,6 @@ void DevicePluginPhilipsHue::networkManagerReplyReady() // check HTTP status code if (status != 200 || reply->error() != QNetworkReply::NoError) { qCWarning(dcPhilipsHue) << "N-UPNP discovery error:" << status << reply->errorString(); - reply->deleteLater(); return; } processNUpnpResponse(reply->readAll()); @@ -356,7 +353,6 @@ void DevicePluginPhilipsHue::networkManagerReplyReady() if (status != 200 || reply->error() != QNetworkReply::NoError) { qCWarning(dcPhilipsHue) << "Bridge light discovery error:" << status << reply->errorString(); bridgeReachableChanged(device, false); - reply->deleteLater(); return; } processBridgeLightDiscoveryResponse(device, reply->readAll()); @@ -368,7 +364,6 @@ void DevicePluginPhilipsHue::networkManagerReplyReady() if (status != 200 || reply->error() != QNetworkReply::NoError) { qCWarning(dcPhilipsHue) << "Bridge sensor discovery error:" << status << reply->errorString(); bridgeReachableChanged(device, false); - reply->deleteLater(); return; } processBridgeSensorDiscoveryResponse(device, reply->readAll()); @@ -380,7 +375,6 @@ void DevicePluginPhilipsHue::networkManagerReplyReady() if (status != 200 || reply->error() != QNetworkReply::NoError) { qCWarning(dcPhilipsHue) << "Bridge search new devices error:" << status << reply->errorString(); bridgeReachableChanged(device, false); - reply->deleteLater(); return; } discoverBridgeDevices(m_bridges.key(device)); @@ -394,7 +388,6 @@ void DevicePluginPhilipsHue::networkManagerReplyReady() qCWarning(dcPhilipsHue) << "Refresh Hue Bridge request error:" << status << reply->errorString(); bridgeReachableChanged(device, false); } - reply->deleteLater(); return; } processBridgeRefreshResponse(device, reply->readAll()); @@ -406,7 +399,6 @@ void DevicePluginPhilipsHue::networkManagerReplyReady() if (status != 200 || reply->error() != QNetworkReply::NoError) { qCWarning(dcPhilipsHue) << "Refresh Hue Light request error:" << status << reply->errorString(); bridgeReachableChanged(device, false); - reply->deleteLater(); return; } processLightRefreshResponse(device, reply->readAll()); @@ -420,7 +412,6 @@ void DevicePluginPhilipsHue::networkManagerReplyReady() qCWarning(dcPhilipsHue) << "Refresh Hue lights request error:" << status << reply->errorString(); bridgeReachableChanged(device, false); } - reply->deleteLater(); return; } processLightsRefreshResponse(device, reply->readAll()); @@ -434,7 +425,6 @@ void DevicePluginPhilipsHue::networkManagerReplyReady() qCWarning(dcPhilipsHue) << "Refresh Hue sensors request error:" << status << reply->errorString(); bridgeReachableChanged(device, false); } - reply->deleteLater(); return; } processSensorsRefreshResponse(device, reply->readAll()); @@ -447,26 +437,34 @@ void DevicePluginPhilipsHue::networkManagerReplyReady() qCWarning(dcPhilipsHue) << "Execute Hue Light action request error:" << status << reply->errorString(); bridgeReachableChanged(actionInfo.first, false); emit actionExecutionFinished(actionInfo.second, DeviceManager::DeviceErrorHardwareNotAvailable); - reply->deleteLater(); return; } processActionResponse(actionInfo.first, actionInfo.second, reply->readAll()); - } else if (m_lightSetNameRequests.contains(reply)) { - Device *device = m_lightSetNameRequests.take(reply); + } else if (m_setNameRequests.contains(reply)) { + Device *device = m_setNameRequests.take(reply); // check HTTP status code if (status != 200 || reply->error() != QNetworkReply::NoError) { qCWarning(dcPhilipsHue) << "Set name of Hue Light request error:" << status << reply->errorString(); bridgeReachableChanged(device, false); - reply->deleteLater(); return; } processSetNameResponse(device, reply->readAll()); } else { qCWarning(dcPhilipsHue()) << "Unhandled bridge reply" << reply->error() << reply->readAll(); } - reply->deleteLater(); +} + +void DevicePluginPhilipsHue::onDeviceNameChanged() +{ + Device *device = static_cast(sender()); + if (m_lights.values().contains(device)) { + setLightName(device); + } + if (m_remotes.values().contains(device)) { + setRemoteName(device); + } } DeviceManager::DeviceError DevicePluginPhilipsHue::executeAction(Device *device, const Action &action) @@ -845,12 +843,12 @@ void DevicePluginPhilipsHue::searchNewDevices(HueBridge *bridge, const QString & m_bridgeSearchDevicesRequests.insert(reply, device); } -void DevicePluginPhilipsHue::setLightName(Device *device, const QString &name) +void DevicePluginPhilipsHue::setLightName(Device *device) { HueLight *light = m_lights.key(device); QVariantMap requestMap; - requestMap.insert("name", name); + requestMap.insert("name", device->name()); QJsonDocument jsonDoc = QJsonDocument::fromVariant(requestMap); QNetworkRequest request(QUrl("http://" + light->hostAddress().toString() + "/api/" + light->apiKey() + @@ -859,7 +857,24 @@ void DevicePluginPhilipsHue::setLightName(Device *device, const QString &name) QNetworkReply *reply = hardwareManager()->networkManager()->put(request,jsonDoc.toJson()); connect(reply, &QNetworkReply::finished, this, &DevicePluginPhilipsHue::networkManagerReplyReady); - m_lightSetNameRequests.insert(reply, device); + m_setNameRequests.insert(reply, device); +} + +void DevicePluginPhilipsHue::setRemoteName(Device *device) +{ + HueRemote *remote = m_remotes.key(device); + + QVariantMap requestMap; + requestMap.insert("name", device->name()); + QJsonDocument jsonDoc = QJsonDocument::fromVariant(requestMap); + + QNetworkRequest request(QUrl("http://" + remote->hostAddress().toString() + "/api/" + remote->apiKey() + + "/sensors/" + QString::number(remote->id()))); + request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); + + QNetworkReply *reply = hardwareManager()->networkManager()->put(request,jsonDoc.toJson()); + connect(reply, &QNetworkReply::finished, this, &DevicePluginPhilipsHue::networkManagerReplyReady); + m_setNameRequests.insert(reply, device); } void DevicePluginPhilipsHue::processNUpnpResponse(const QByteArray &data) diff --git a/philipshue/devicepluginphilipshue.h b/philipshue/devicepluginphilipshue.h index 7146b71e..5b4ef521 100644 --- a/philipshue/devicepluginphilipshue.h +++ b/philipshue/devicepluginphilipshue.h @@ -63,6 +63,7 @@ private slots: private slots: void onUpnpDiscoveryFinished(); void networkManagerReplyReady(); + void onDeviceNameChanged(); private: PluginTimer *m_pluginTimer1Sec = nullptr; @@ -77,7 +78,7 @@ private: QList m_discoveryRequests; QHash m_lightRefreshRequests; - QHash m_lightSetNameRequests; + QHash m_setNameRequests; QHash m_bridgeRefreshRequests; QHash m_lightsRefreshRequests; QHash m_sensorsRefreshRequests; @@ -100,7 +101,8 @@ private: void discoverBridgeDevices(HueBridge *bridge); void searchNewDevices(HueBridge *bridge, const QString &serialNumber); - void setLightName(Device *device, const QString &name); + void setLightName(Device *device); + void setRemoteName(Device *device); void processNUpnpResponse(const QByteArray &data); void processBridgeLightDiscoveryResponse(Device *device, const QByteArray &data);