From 6bd868f4a0fa3a7637914cdbbcc855888d271c7c Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Thu, 15 Mar 2018 11:46:07 +0100 Subject: [PATCH] switch philips hue plugin to use new parenting features --- philipshue/devicepluginphilipshue.cpp | 50 +++++--------- philipshue/devicepluginphilipshue.json | 92 -------------------------- philipshue/huedevice.cpp | 10 --- philipshue/huedevice.h | 6 -- 4 files changed, 18 insertions(+), 140 deletions(-) diff --git a/philipshue/devicepluginphilipshue.cpp b/philipshue/devicepluginphilipshue.cpp index b1d4073d..902a897f 100644 --- a/philipshue/devicepluginphilipshue.cpp +++ b/philipshue/devicepluginphilipshue.cpp @@ -150,16 +150,15 @@ DeviceManager::DeviceSetupStatus DevicePluginPhilipsHue::setupDevice(Device *dev if (device->deviceClassId() == hueLightDeviceClassId) { qCDebug(dcPhilipsHue) << "Setup Hue color light" << device->params(); + HueBridge *bridge = m_bridges.key(myDevices().findById(device->parentId())); HueLight *hueLight = new HueLight(this); + hueLight->setHostAddress(bridge->hostAddress()); + hueLight->setApiKey(bridge->apiKey()); hueLight->setId(device->paramValue(hueLightLightIdParamTypeId).toInt()); - hueLight->setHostAddress(QHostAddress(device->paramValue(hueLightHostParamTypeId).toString())); hueLight->setName(device->paramValue(hueLightNameParamTypeId).toString()); - hueLight->setApiKey(device->paramValue(hueLightApiKeyParamTypeId).toString()); hueLight->setModelId(device->paramValue(hueLightModelIdParamTypeId).toString()); hueLight->setUuid(device->paramValue(hueLightUuidParamTypeId).toString()); hueLight->setType(device->paramValue(hueLightTypeParamTypeId).toString()); - hueLight->setBridgeId(DeviceId(device->paramValue(hueLightBridgeParamTypeId).toString())); - device->setParentId(hueLight->bridgeId()); connect(hueLight, &HueLight::stateChanged, this, &DevicePluginPhilipsHue::lightStateChanged); m_lights.insert(hueLight, device); @@ -176,16 +175,15 @@ DeviceManager::DeviceSetupStatus DevicePluginPhilipsHue::setupDevice(Device *dev if (device->deviceClassId() == hueWhiteLightDeviceClassId) { qCDebug(dcPhilipsHue) << "Setup Hue white light" << device->params(); + HueBridge *bridge = m_bridges.key(myDevices().findById(device->parentId())); HueLight *hueLight = new HueLight(this); + hueLight->setHostAddress(bridge->hostAddress()); + hueLight->setApiKey(bridge->apiKey()); hueLight->setId(device->paramValue(hueWhiteLightLightIdParamTypeId).toInt()); - hueLight->setHostAddress(QHostAddress(device->paramValue(hueWhiteLightHostParamTypeId).toString())); hueLight->setName(device->paramValue(hueWhiteLightNameParamTypeId).toString()); - hueLight->setApiKey(device->paramValue(hueWhiteLightApiKeyParamTypeId).toString()); hueLight->setModelId(device->paramValue(hueWhiteLightModelIdParamTypeId).toString()); hueLight->setUuid(device->paramValue(hueWhiteLightUuidParamTypeId).toString()); hueLight->setType(device->paramValue(hueWhiteLightTypeParamTypeId).toString()); - hueLight->setBridgeId(DeviceId(device->paramValue(hueWhiteLightBridgeParamTypeId).toString())); - device->setParentId(hueLight->bridgeId()); connect(hueLight, &HueLight::stateChanged, this, &DevicePluginPhilipsHue::lightStateChanged); @@ -202,16 +200,15 @@ DeviceManager::DeviceSetupStatus DevicePluginPhilipsHue::setupDevice(Device *dev if (device->deviceClassId() == hueRemoteDeviceClassId) { qCDebug(dcPhilipsHue) << "Setup Hue remote" << device->params() << device->deviceClassId(); + HueBridge *bridge = m_bridges.key(myDevices().findById(device->parentId())); HueRemote *hueRemote = new HueRemote(this); + hueRemote->setHostAddress(bridge->hostAddress()); + hueRemote->setApiKey(bridge->apiKey()); hueRemote->setId(device->paramValue(hueRemoteSensorIdParamTypeId).toInt()); - hueRemote->setHostAddress(QHostAddress(device->paramValue(hueRemoteHostParamTypeId).toString())); hueRemote->setName(device->paramValue(hueRemoteNameParamTypeId).toString()); - hueRemote->setApiKey(device->paramValue(hueRemoteApiKeyParamTypeId).toString()); hueRemote->setModelId(device->paramValue(hueRemoteModelIdParamTypeId).toString()); hueRemote->setType(device->paramValue(hueRemoteTypeParamTypeId).toString()); hueRemote->setUuid(device->paramValue(hueRemoteUuidParamTypeId).toString()); - hueRemote->setBridgeId(DeviceId(device->paramValue(hueRemoteBridgeParamTypeId).toString())); - device->setParentId(hueRemote->bridgeId()); device->setName(hueRemote->name()); @@ -227,9 +224,7 @@ DeviceManager::DeviceSetupStatus DevicePluginPhilipsHue::setupDevice(Device *dev HueRemote *hueTap = new HueRemote(this); hueTap->setName(device->name()); hueTap->setId(device->paramValue(hueTapSensorIdParamTypeId).toInt()); - hueTap->setBridgeId(DeviceId(device->paramValue(hueTapBridgeParamTypeId).toString())); hueTap->setModelId(device->paramValue(hueTapModelIdParamTypeId).toString()); - device->setParentId(hueTap->bridgeId()); connect(hueTap, &HueRemote::stateChanged, this, &DevicePluginPhilipsHue::remoteStateChanged); connect(hueTap, &HueRemote::buttonPressed, this, &DevicePluginPhilipsHue::onRemoteButtonEvent); @@ -883,12 +878,9 @@ void DevicePluginPhilipsHue::processBridgeLightDiscoveryResponse(Device *device, // check if this is a white light if (model == "LWB004" || model == "LWB006" || model == "LWB007") { - DeviceDescriptor descriptor(hueWhiteLightDeviceClassId, "Philips Hue White Light", lightMap.value("name").toString()); + DeviceDescriptor descriptor(hueWhiteLightDeviceClassId, "Philips Hue White Light", lightMap.value("name").toString(), device->id()); ParamList params; params.append(Param(hueWhiteLightNameParamTypeId, lightMap.value("name").toString())); - params.append(Param(hueWhiteLightApiKeyParamTypeId, device->paramValue(hueBridgeBridgeApiKeyParamTypeId).toString())); - params.append(Param(hueWhiteLightBridgeParamTypeId, device->id().toString())); - params.append(Param(hueWhiteLightHostParamTypeId, device->paramValue(hueBridgeBridgeHostParamTypeId).toString())); params.append(Param(hueWhiteLightModelIdParamTypeId, model)); params.append(Param(hueWhiteLightTypeParamTypeId, lightMap.value("type").toString())); params.append(Param(hueWhiteLightUuidParamTypeId, uuid)); @@ -899,12 +891,9 @@ void DevicePluginPhilipsHue::processBridgeLightDiscoveryResponse(Device *device, qCDebug(dcPhilipsHue) << "Found new white light" << lightMap.value("name").toString() << model; } else { - DeviceDescriptor descriptor(hueLightDeviceClassId, "Philips Hue Light", lightMap.value("name").toString()); + DeviceDescriptor descriptor(hueLightDeviceClassId, "Philips Hue Light", lightMap.value("name").toString(), device->id()); ParamList params; params.append(Param(hueLightNameParamTypeId, lightMap.value("name").toString())); - params.append(Param(hueLightApiKeyParamTypeId, device->paramValue(hueBridgeBridgeApiKeyParamTypeId).toString())); - params.append(Param(hueLightBridgeParamTypeId, device->id().toString())); - params.append(Param(hueLightHostParamTypeId, device->paramValue(hueBridgeBridgeHostParamTypeId).toString())); params.append(Param(hueLightModelIdParamTypeId, model)); params.append(Param(hueLightTypeParamTypeId, lightMap.value("type").toString())); params.append(Param(hueLightUuidParamTypeId, uuid)); @@ -955,12 +944,9 @@ void DevicePluginPhilipsHue::processBridgeSensorDiscoveryResponse(Device *device continue; if (model == "RWL021" || model == "RWL020") { - DeviceDescriptor descriptor(hueRemoteDeviceClassId, "Philips Hue Remote", sensorMap.value("name").toString()); + DeviceDescriptor descriptor(hueRemoteDeviceClassId, "Philips Hue Remote", sensorMap.value("name").toString(), device->id()); ParamList params; params.append(Param(hueRemoteNameParamTypeId, sensorMap.value("name").toString())); - params.append(Param(hueRemoteApiKeyParamTypeId, device->paramValue(hueBridgeBridgeApiKeyParamTypeId).toString())); - params.append(Param(hueRemoteBridgeParamTypeId, device->id().toString())); - params.append(Param(hueRemoteHostParamTypeId, device->paramValue(hueBridgeBridgeHostParamTypeId).toString())); params.append(Param(hueRemoteModelIdParamTypeId, model)); params.append(Param(hueRemoteTypeParamTypeId, sensorMap.value("type").toString())); params.append(Param(hueRemoteUuidParamTypeId, uuid)); @@ -969,9 +955,8 @@ void DevicePluginPhilipsHue::processBridgeSensorDiscoveryResponse(Device *device emit autoDevicesAppeared(hueRemoteDeviceClassId, {descriptor}); qCDebug(dcPhilipsHue) << "Found new remote" << sensorMap.value("name").toString() << model; } else if (model == "ZGPSWITCH") { - DeviceDescriptor descriptor(hueTapDeviceClassId, "Hue Tap", sensorMap.value("name").toString()); + DeviceDescriptor descriptor(hueTapDeviceClassId, "Hue Tap", sensorMap.value("name").toString(), device->id()); ParamList params; - params.append(Param(hueTapBridgeParamTypeId, device->id().toString())); params.append(Param(hueTapUuidParamTypeId, uuid)); params.append(Param(hueTapModelIdParamTypeId, model)); params.append(Param(hueTapSensorIdParamTypeId, sensorId)); @@ -1079,7 +1064,7 @@ void DevicePluginPhilipsHue::processLightsRefreshResponse(Device *device, const QVariantMap lightMap = lightsMap.value(lightId).toMap(); // get the light of this bridge foreach (HueLight *light, m_lights.keys()) { - if (light->id() == lightId.toInt() && light->bridgeId() == device->id()) { + if (light->id() == lightId.toInt() && m_lights.value(light)->parentId() == device->id()) { light->updateStates(lightMap.value("state").toMap()); } } @@ -1108,7 +1093,8 @@ void DevicePluginPhilipsHue::processSensorsRefreshResponse(Device *device, const foreach (const QString &sensorId, sensorsMap.keys()) { QVariantMap sensorMap = sensorsMap.value(sensorId).toMap(); foreach (HueRemote *remote, m_remotes.keys()) { - if (remote->id() == sensorId.toInt() && remote->bridgeId() == device->id()) { + if (remote->id() == sensorId.toInt() && m_remotes.value(remote)->parentId() == device->id()) { + qCDebug(dcPhilipsHue) << "update remote" << remote->id() << remote->name(); remote->updateStates(sensorMap.value("state").toMap(), sensorMap.value("config").toMap()); } } @@ -1275,7 +1261,7 @@ void DevicePluginPhilipsHue::bridgeReachableChanged(Device *device, const bool & device->setStateValue(hueBridgeConnectedStateTypeId, false); foreach (HueLight *light, m_lights.keys()) { - if (light->bridgeId() == device->id()) { + if (m_lights.value(light)->parentId() == device->id()) { light->setReachable(false); if (m_lights.value(light)->deviceClassId() == hueLightDeviceClassId) { m_lights.value(light)->setStateValue(hueLightConnectedStateTypeId, false); @@ -1286,7 +1272,7 @@ void DevicePluginPhilipsHue::bridgeReachableChanged(Device *device, const bool & } foreach (HueRemote *remote, m_remotes.keys()) { - if (remote->bridgeId() == device->id()) { + if (m_remotes.value(remote)->parentId() == device->id()) { remote->setReachable(false); if (m_remotes.value(remote)->deviceClassId() == hueRemoteDeviceClassId) { m_remotes.value(remote)->setStateValue(hueRemoteConnectedStateTypeId, false); diff --git a/philipshue/devicepluginphilipshue.json b/philipshue/devicepluginphilipshue.json index 81b3bbfd..e553ab38 100644 --- a/philipshue/devicepluginphilipshue.json +++ b/philipshue/devicepluginphilipshue.json @@ -162,13 +162,6 @@ "type" : "QString", "inputType": "TextLine" }, - { - "id": "33b01ab8-3811-4a06-ab3c-0a0ef2faa3ef", - "name": "bridge", - "displayName": "bridge", - "type" : "QString", - "readOnly": true - }, { "id": "095a463b-f59e-46b1-989a-a71f9cbe3e30", "name": "modelId", @@ -190,22 +183,6 @@ "type" : "QString", "readOnly": true }, - { - "id": "99458d90-d171-4600-ba08-4818a4aaed68", - "name": "apiKey", - "displayName": "api key", - "type" : "QString", - "inputType": "TextLine", - "readOnly": true - }, - { - "id": "56d14f2f-1b20-4004-a333-87ac09fa54d2", - "name": "host", - "displayName": "host address", - "type" : "QString", - "inputType": "IPv4Address", - "readOnly": true - }, { "id": "491dc012-ccf2-4d3a-9f18-add98f7374af", "name": "lightId", @@ -336,13 +313,6 @@ "type" : "QString", "inputType": "TextLine" }, - { - "id": "33b01ab8-3811-4a06-ab3c-0a0ef2faa3ef", - "name": "bridge", - "displayName": "bridge", - "type" : "QString", - "readOnly": true - }, { "id": "095a463b-f59e-46b1-989a-a71f9cbe3e30", "name": "modelId", @@ -364,22 +334,6 @@ "type" : "QString", "readOnly": true }, - { - "id": "99458d90-d171-4600-ba08-4818a4aaed68", - "name": "apiKey", - "displayName": "api key", - "type" : "QString", - "inputType": "TextLine", - "readOnly": true - }, - { - "id": "56d14f2f-1b20-4004-a333-87ac09fa54d2", - "name": "host", - "displayName": "host address", - "type" : "QString", - "inputType": "IPv4Address", - "readOnly": true - }, { "id": "491dc012-ccf2-4d3a-9f18-add98f7374af", "name": "lightId", @@ -464,13 +418,6 @@ "type" : "QString", "inputType": "TextLine" }, - { - "id": "33b01ab8-3811-4a06-ab3c-0a0ef2faa3ef", - "name": "bridge", - "displayName": "bridge", - "type" : "QString", - "readOnly": true - }, { "id": "095a463b-f59e-46b1-989a-a71f9cbe3e30", "name": "modelId", @@ -492,22 +439,6 @@ "type" : "QString", "readOnly": true }, - { - "id": "99458d90-d171-4600-ba08-4818a4aaed68", - "name": "apiKey", - "displayName": "api key", - "type" : "QString", - "inputType": "TextLine", - "readOnly": true - }, - { - "id": "56d14f2f-1b20-4004-a333-87ac09fa54d2", - "name": "host", - "displayName": "host address", - "type" : "QString", - "inputType": "IPv4Address", - "readOnly": true - }, { "id": "2ddb571b-149f-4f08-a76a-78b7d3aa27e0", "name": "sensorId", @@ -631,13 +562,6 @@ "type" : "QString", "inputType": "TextLine" }, - { - "id": "c9e7483b-2f4e-4280-a437-8471f4dc9b22", - "name": "bridge", - "displayName": "bridge", - "type" : "QString", - "readOnly": true - }, { "id": "62d92175-db3a-4da2-a72b-f58f34cb6911", "name": "modelId", @@ -659,22 +583,6 @@ "type" : "QString", "readOnly": true }, - { - "id": "fb507641-9c78-4a28-937d-711775454e90", - "name": "apiKey", - "displayName": "api key", - "type" : "QString", - "inputType": "TextLine", - "readOnly": true - }, - { - "id": "6be56a1b-f64a-4188-a052-1fab11e01718", - "name": "host", - "displayName": "host address", - "type" : "QString", - "inputType": "IPv4Address", - "readOnly": true - }, { "id": "5eca2b24-8986-4487-bc12-50e91d023d97", "name": "sensorId", diff --git a/philipshue/huedevice.cpp b/philipshue/huedevice.cpp index 3f4ae5c5..a0503579 100644 --- a/philipshue/huedevice.cpp +++ b/philipshue/huedevice.cpp @@ -48,16 +48,6 @@ void HueDevice::setName(const QString &name) m_name = name; } -DeviceId HueDevice::bridgeId() const -{ - return m_bridgeId; -} - -void HueDevice::setBridgeId(const DeviceId &bridgeId) -{ - m_bridgeId = bridgeId; -} - QHostAddress HueDevice::hostAddress() const { return m_hostAddress; diff --git a/philipshue/huedevice.h b/philipshue/huedevice.h index 6679e9f3..94811667 100644 --- a/philipshue/huedevice.h +++ b/philipshue/huedevice.h @@ -29,8 +29,6 @@ #include #include -#include "typeutils.h" - class HueDevice : public QObject { Q_OBJECT @@ -43,9 +41,6 @@ public: QString name() const; void setName(const QString &name); - DeviceId bridgeId() const; - void setBridgeId(const DeviceId &bridgeId); - QHostAddress hostAddress() const; void setHostAddress(const QHostAddress hostAddress); @@ -74,7 +69,6 @@ private: QString m_apiKey; QString m_modelId; QString m_uuid; - DeviceId m_bridgeId; QString m_type; QString m_softwareVersion;