Merge PR #311: PhilipsHue: Fix automatic rediscovery of the bridge if it changes IP

master
Jenkins nymea 2020-11-02 18:47:53 +01:00
commit 90de922a5f
12 changed files with 131 additions and 149 deletions

View File

@ -29,6 +29,7 @@
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "huebridge.h" #include "huebridge.h"
#include <QJsonDocument>
HueBridge::HueBridge(QObject *parent) : HueBridge::HueBridge(QObject *parent) :
QObject(parent), QObject(parent),
@ -117,16 +118,6 @@ void HueBridge::setZigbeeChannel(const int &zigbeeChannel)
m_zigbeeChannel = zigbeeChannel; m_zigbeeChannel = zigbeeChannel;
} }
QList<HueLight *> HueBridge::lights() const
{
return m_lights;
}
void HueBridge::addLight(HueLight *light)
{
m_lights.append(light);
}
QPair<QNetworkRequest, QByteArray> HueBridge::createDiscoverLightsRequest() QPair<QNetworkRequest, QByteArray> HueBridge::createDiscoverLightsRequest()
{ {
QNetworkRequest request(QUrl("http://" + hostAddress().toString() + "/api/" + apiKey() + "/lights/")); QNetworkRequest request(QUrl("http://" + hostAddress().toString() + "/api/" + apiKey() + "/lights/"));

View File

@ -33,14 +33,13 @@
#include <QObject> #include <QObject>
#include <QHostAddress> #include <QHostAddress>
#include <QNetworkRequest>
#include "huelight.h"
class HueBridge : public QObject class HueBridge : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit HueBridge(QObject *parent = 0); explicit HueBridge(QObject *parent = nullptr);
QString name() const; QString name() const;
void setName(const QString &name); void setName(const QString &name);
@ -66,9 +65,6 @@ public:
int zigbeeChannel() const; int zigbeeChannel() const;
void setZigbeeChannel(const int &zigbeeChannel); void setZigbeeChannel(const int &zigbeeChannel);
QList<HueLight *> lights() const;
void addLight(HueLight *light);
QPair<QNetworkRequest, QByteArray> createDiscoverLightsRequest(); QPair<QNetworkRequest, QByteArray> createDiscoverLightsRequest();
QPair<QNetworkRequest, QByteArray> createSearchLightsRequest(const QString &deviceId); QPair<QNetworkRequest, QByteArray> createSearchLightsRequest(const QString &deviceId);
QPair<QNetworkRequest, QByteArray> createSearchSensorsRequest(); QPair<QNetworkRequest, QByteArray> createSearchSensorsRequest();
@ -84,9 +80,6 @@ private:
QString m_apiVersion; QString m_apiVersion;
QString m_softwareVersion; QString m_softwareVersion;
int m_zigbeeChannel; int m_zigbeeChannel;
QList<HueLight *> m_lights;
}; };
#endif // HUEBRIDGE_H #endif // HUEBRIDGE_H

View File

@ -30,8 +30,9 @@
#include "huedevice.h" #include "huedevice.h"
HueDevice::HueDevice(QObject *parent) : HueDevice::HueDevice(HueBridge *bridge, QObject *parent) :
QObject(parent) QObject(parent),
m_bridge(bridge)
{ {
} }
@ -46,6 +47,16 @@ void HueDevice::setId(const int &id)
m_id = id; m_id = id;
} }
QHostAddress HueDevice::hostAddress() const
{
return m_bridge->hostAddress();
}
QString HueDevice::apiKey() const
{
return m_bridge->apiKey();
}
QString HueDevice::name() const QString HueDevice::name() const
{ {
return m_name; return m_name;
@ -56,16 +67,6 @@ void HueDevice::setName(const QString &name)
m_name = name; m_name = name;
} }
QHostAddress HueDevice::hostAddress() const
{
return m_hostAddress;
}
void HueDevice::setHostAddress(const QHostAddress hostAddress)
{
m_hostAddress = hostAddress;
}
QString HueDevice::uuid() QString HueDevice::uuid()
{ {
return m_uuid; return m_uuid;
@ -76,16 +77,6 @@ void HueDevice::setUuid(const QString &uuid)
m_uuid = uuid; m_uuid = uuid;
} }
QString HueDevice::apiKey() const
{
return m_apiKey;
}
void HueDevice::setApiKey(const QString &apiKey)
{
m_apiKey = apiKey;
}
QString HueDevice::modelId() const QString HueDevice::modelId() const
{ {
return m_modelId; return m_modelId;

View File

@ -37,27 +37,27 @@
#include <QNetworkRequest> #include <QNetworkRequest>
#include <QJsonDocument> #include <QJsonDocument>
#include "huebridge.h"
class HueDevice : public QObject class HueDevice : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit HueDevice(QObject *parent = nullptr); explicit HueDevice(HueBridge *bridge, QObject *parent = nullptr);
int id() const; int id() const;
void setId(const int &id); void setId(const int &id);
QHostAddress hostAddress() const;
QString apiKey() const;
QString name() const; QString name() const;
void setName(const QString &name); void setName(const QString &name);
QHostAddress hostAddress() const;
void setHostAddress(const QHostAddress hostAddress);
QString uuid(); QString uuid();
void setUuid(const QString &uuid); void setUuid(const QString &uuid);
QString apiKey() const;
void setApiKey(const QString &apiKey);
QString modelId() const; QString modelId() const;
void setModelId(const QString &modelId); void setModelId(const QString &modelId);
@ -73,10 +73,9 @@ public:
static QString getBaseUuid(const QString &uuid); static QString getBaseUuid(const QString &uuid);
private: private:
HueBridge *m_bridge = nullptr;
int m_id; int m_id;
QString m_name; QString m_name;
QHostAddress m_hostAddress;
QString m_apiKey;
QString m_modelId; QString m_modelId;
QString m_uuid; QString m_uuid;
QString m_type; QString m_type;

View File

@ -31,8 +31,8 @@
#include "huelight.h" #include "huelight.h"
#include "extern-plugininfo.h" #include "extern-plugininfo.h"
HueLight::HueLight(QObject *parent) : HueLight::HueLight(HueBridge *bridge, QObject *parent) :
HueDevice(parent) HueDevice(bridge, parent)
{ {
} }
@ -217,9 +217,9 @@ void HueLight::processActionResponse(const QVariantList &responseList)
emit stateChanged(); emit stateChanged();
} }
QPair<QNetworkRequest, QByteArray> HueLight::createSetPowerRequest(const bool &power) QPair<QNetworkRequest, QByteArray> HueLight::createSetPowerRequest(bool power)
{ {
qCDebug(dcPhilipsHue()) << "Create power request" << power; qCDebug(dcPhilipsHue()) << "Creating power request for power" << (power ? "on" : "off");
QVariantMap requestMap; QVariantMap requestMap;
requestMap.insert("on", power); requestMap.insert("on", power);
@ -234,7 +234,7 @@ QPair<QNetworkRequest, QByteArray> HueLight::createSetPowerRequest(const bool &p
QPair<QNetworkRequest, QByteArray> HueLight::createSetColorRequest(const QColor &color) QPair<QNetworkRequest, QByteArray> HueLight::createSetColorRequest(const QColor &color)
{ {
qCDebug(dcPhilipsHue()) << "Create color request" << color.toRgb(); qCDebug(dcPhilipsHue()) << "Creating color request" << color.toRgb();
QVariantMap requestMap; QVariantMap requestMap;
requestMap.insert("hue", color.hue() * 65535 / 360); requestMap.insert("hue", color.hue() * 65535 / 360);
@ -249,9 +249,9 @@ QPair<QNetworkRequest, QByteArray> HueLight::createSetColorRequest(const QColor
return QPair<QNetworkRequest, QByteArray>(request, jsonDoc.toJson()); return QPair<QNetworkRequest, QByteArray>(request, jsonDoc.toJson());
} }
QPair<QNetworkRequest, QByteArray> HueLight::createSetBrightnessRequest(const int &brightness) QPair<QNetworkRequest, QByteArray> HueLight::createSetBrightnessRequest(int brightness)
{ {
qCDebug(dcPhilipsHue()) << "Create brightness request" << brightness; qCDebug(dcPhilipsHue()) << "Creating brightness request" << brightness;
QVariantMap requestMap; QVariantMap requestMap;
requestMap.insert("bri", brightness); requestMap.insert("bri", brightness);
@ -271,7 +271,7 @@ QPair<QNetworkRequest, QByteArray> HueLight::createSetBrightnessRequest(const in
QPair<QNetworkRequest, QByteArray> HueLight::createSetEffectRequest(const QString &effect) QPair<QNetworkRequest, QByteArray> HueLight::createSetEffectRequest(const QString &effect)
{ {
qCDebug(dcPhilipsHue()) << "Create effect request" << effect; qCDebug(dcPhilipsHue()) << "Creating effect request" << effect;
QVariantMap requestMap; QVariantMap requestMap;
if (effect == "none") { if (effect == "none") {
@ -288,9 +288,9 @@ QPair<QNetworkRequest, QByteArray> HueLight::createSetEffectRequest(const QStrin
return QPair<QNetworkRequest, QByteArray>(request, jsonDoc.toJson()); return QPair<QNetworkRequest, QByteArray>(request, jsonDoc.toJson());
} }
QPair<QNetworkRequest, QByteArray> HueLight::createSetTemperatureRequest(const int &colorTemp) QPair<QNetworkRequest, QByteArray> HueLight::createSetTemperatureRequest(int colorTemp)
{ {
qCDebug(dcPhilipsHue()) << "Create color temperature request" << colorTemp; qCDebug(dcPhilipsHue()) << "Creating color temperature request" << colorTemp;
QVariantMap requestMap; QVariantMap requestMap;
requestMap.insert("ct", colorTemp); requestMap.insert("ct", colorTemp);
@ -306,7 +306,7 @@ QPair<QNetworkRequest, QByteArray> HueLight::createSetTemperatureRequest(const i
QPair<QNetworkRequest, QByteArray> HueLight::createFlashRequest(const QString &alert) QPair<QNetworkRequest, QByteArray> HueLight::createFlashRequest(const QString &alert)
{ {
qCDebug(dcPhilipsHue()) << "Create flash request" << alert; qCDebug(dcPhilipsHue()) << "Creating flash request" << alert;
QVariantMap requestMap; QVariantMap requestMap;
if (alert == "flash") { if (alert == "flash") {

View File

@ -53,7 +53,7 @@ public:
ColorModeCT ColorModeCT
}; };
explicit HueLight(QObject *parent = 0); explicit HueLight(HueBridge* bridge, QObject *parent = nullptr);
bool power() const; bool power() const;
void setPower(const bool &power); void setPower(const bool &power);
@ -89,11 +89,11 @@ public:
void processActionResponse(const QVariantList &responseList); void processActionResponse(const QVariantList &responseList);
// create action requests // create action requests
QPair<QNetworkRequest, QByteArray> createSetPowerRequest(const bool &power); QPair<QNetworkRequest, QByteArray> createSetPowerRequest(bool power);
QPair<QNetworkRequest, QByteArray> createSetColorRequest(const QColor &color); QPair<QNetworkRequest, QByteArray> createSetColorRequest(const QColor &color);
QPair<QNetworkRequest, QByteArray> createSetBrightnessRequest(const int &brightness); QPair<QNetworkRequest, QByteArray> createSetBrightnessRequest(int brightness);
QPair<QNetworkRequest, QByteArray> createSetEffectRequest(const QString &effect); QPair<QNetworkRequest, QByteArray> createSetEffectRequest(const QString &effect);
QPair<QNetworkRequest, QByteArray> createSetTemperatureRequest(const int &colorTemp); QPair<QNetworkRequest, QByteArray> createSetTemperatureRequest(int colorTemp);
QPair<QNetworkRequest, QByteArray> createFlashRequest(const QString &alert); QPair<QNetworkRequest, QByteArray> createFlashRequest(const QString &alert);
private: private:

View File

@ -33,8 +33,8 @@
#include <QtMath> #include <QtMath>
HueMotionSensor::HueMotionSensor(QObject *parent) : HueMotionSensor::HueMotionSensor(HueBridge *bridge, QObject *parent) :
HueDevice(parent) HueDevice(bridge, parent)
{ {
m_timeout.setInterval(10000); m_timeout.setInterval(10000);
connect(&m_timeout, &QTimer::timeout, this, [this](){ connect(&m_timeout, &QTimer::timeout, this, [this](){

View File

@ -41,7 +41,7 @@ class HueMotionSensor : public HueDevice
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit HueMotionSensor(QObject *parent = nullptr); explicit HueMotionSensor(HueBridge *bridge, QObject *parent = nullptr);
virtual ~HueMotionSensor() = default; virtual ~HueMotionSensor() = default;
void setTimeout(int timeout); void setTimeout(int timeout);
@ -115,7 +115,7 @@ class HueIndoorSensor: public HueMotionSensor
{ {
Q_OBJECT Q_OBJECT
public: public:
HueIndoorSensor(QObject *parent = nullptr): HueMotionSensor(parent) {} HueIndoorSensor(HueBridge *bridge, QObject *parent = nullptr): HueMotionSensor(bridge, parent) {}
StateTypeId connectedStateTypeId() const override { return motionSensorConnectedStateTypeId; } StateTypeId connectedStateTypeId() const override { return motionSensorConnectedStateTypeId; }
StateTypeId temperatureStateTypeId() const override { return motionSensorTemperatureStateTypeId; } StateTypeId temperatureStateTypeId() const override { return motionSensorTemperatureStateTypeId; }
@ -131,7 +131,7 @@ class HueOutdoorSensor: public HueMotionSensor
{ {
Q_OBJECT Q_OBJECT
public: public:
HueOutdoorSensor(QObject *parent = nullptr): HueMotionSensor(parent) {} HueOutdoorSensor(HueBridge *bridge, QObject *parent = nullptr): HueMotionSensor(bridge, parent) {}
StateTypeId connectedStateTypeId() const override { return outdoorSensorConnectedStateTypeId; } StateTypeId connectedStateTypeId() const override { return outdoorSensorConnectedStateTypeId; }
StateTypeId temperatureStateTypeId() const override { return outdoorSensorTemperatureStateTypeId; } StateTypeId temperatureStateTypeId() const override { return outdoorSensorTemperatureStateTypeId; }

View File

@ -31,8 +31,8 @@
#include "hueremote.h" #include "hueremote.h"
#include "extern-plugininfo.h" #include "extern-plugininfo.h"
HueRemote::HueRemote(QObject *parent) : HueRemote::HueRemote(HueBridge *bridge, QObject *parent) :
HueDevice(parent) HueDevice(bridge, parent)
{ {
} }

View File

@ -44,7 +44,7 @@ class HueRemote : public HueDevice
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit HueRemote(QObject *parent = nullptr); explicit HueRemote(HueBridge *bridge, QObject *parent = nullptr);
int battery() const; int battery() const;
void setBattery(const int &battery); void setBattery(const int &battery);

View File

@ -83,20 +83,22 @@ void IntegrationPluginPhilipsHue::init()
m_zeroConfBrowser = hardwareManager()->zeroConfController()->createServiceBrowser("_hue._tcp"); m_zeroConfBrowser = hardwareManager()->zeroConfController()->createServiceBrowser("_hue._tcp");
connect(m_zeroConfBrowser, &ZeroConfServiceBrowser::serviceEntryAdded, this, [=](const ZeroConfServiceEntry &entry){ connect(m_zeroConfBrowser, &ZeroConfServiceBrowser::serviceEntryAdded, this, [=](const ZeroConfServiceEntry &entry){
foreach (Thing *thing, myThings().filterByThingClassId(bridgeThingClassId)) { qCDebug(dcPhilipsHue()) << "service entry added!" << entry;
QString thingId = thing->paramValue(bridgeThingIdParamTypeId).toString(); if (entry.protocol() != QAbstractSocket::IPv4Protocol) {
if (entry.protocol() == QAbstractSocket::IPv4Protocol) { return;
foreach (const QString &txtEntry, entry.txt()) {
QStringList parts = txtEntry.split('=');
if (parts.length() == 2 && parts.first() == "bridgeid" && parts.last().toLower() == thingId) {
thing->setParamValue(bridgeThingHostParamTypeId, entry.hostAddress().toString());
HueBridge *bridge = m_bridges.key(thing);
bridge->setHostAddress(entry.hostAddress());
}
}
}
} }
QString bridgeId = normalizeBridgeId(entry.txt("bridgeid"));
Thing *thing = bridgeForBridgeId(bridgeId);
if (!thing) {
qCDebug(dcPhilipsHue()) << "We don't know this bridge yet...";
return;
}
thing->setParamValue(bridgeThingHostParamTypeId, entry.hostAddress().toString());
pluginStorage()->beginGroup(thing->id().toString());
pluginStorage()->setValue("hostCache", entry.hostAddress().toString());
pluginStorage()->endGroup();
HueBridge *bridge = m_bridges.key(thing);
bridge->setHostAddress(entry.hostAddress());
}); });
} }
@ -130,21 +132,7 @@ void IntegrationPluginPhilipsHue::discoverThings(ThingDiscoveryInfo *info)
continue; continue;
} }
QString id; QString id = normalizeBridgeId(entry.txt("bridgeid"));
foreach (const QString &txt, entry.txt()) {
QString field = txt.split("=").first();
QString value = txt.split("=").last();
if (field == "bridgeid") {
id = value.toLower();
break;
}
}
// For some reason the UPnP api returns serial numbers withouot a "fffe" in the middle. For backwards compatiblity adjust to that
if (id.indexOf("fffe") == 6) {
id.remove(6, 4);
}
QHostAddress address = entry.hostAddress(); QHostAddress address = entry.hostAddress();
ParamList params; ParamList params;
@ -232,20 +220,13 @@ void IntegrationPluginPhilipsHue::discoverThings(ThingDiscoveryInfo *info)
} }
foreach (const QVariant &bridgeVariant, jsonDoc.toVariant().toList()) { foreach (const QVariant &bridgeVariant, jsonDoc.toVariant().toList()) {
QVariantMap bridgeMap = bridgeVariant.toMap(); QVariantMap bridgeMap = bridgeVariant.toMap();
ThingDescriptor descriptor(bridgeThingClassId, "Philips Hue Bridge", bridgeMap.value("internalipaddress").toString()); ThingDescriptor descriptor(bridgeThingClassId, "Philips Hue Bridge", bridgeMap.value("internalipaddress").toString());
ParamList params; ParamList params;
QString bridgeId = bridgeMap.value("id").toString().toLower(); QString bridgeId = normalizeBridgeId(bridgeMap.value("id").toString());
QString address = bridgeMap.value("internalipaddress").toString(); QString address = bridgeMap.value("internalipaddress").toString();
// For some reason the UPnP api returns serial numbers withouot a "fffe" in the middle. For backwards compatiblity adjust to that
if (bridgeId.indexOf("fffe") == 6) {
bridgeId.remove(6, 4);
}
params.append(Param(bridgeThingHostParamTypeId, address));
params.append(Param(bridgeThingIdParamTypeId, bridgeId)); params.append(Param(bridgeThingIdParamTypeId, bridgeId));
params.append(Param(bridgeThingHostParamTypeId, address));
descriptor.setParams(params); descriptor.setParams(params);
qCDebug(dcPhilipsHue()) << "N-UPnP: Found Hue bridge:" << bridgeId << address; qCDebug(dcPhilipsHue()) << "N-UPnP: Found Hue bridge:" << bridgeId << address;
discovery->results.append(descriptor); discovery->results.append(descriptor);
@ -305,6 +286,7 @@ void IntegrationPluginPhilipsHue::setupThing(ThingSetupInfo *info)
pluginStorage()->beginGroup(thing->id().toString()); pluginStorage()->beginGroup(thing->id().toString());
QString apiKey = pluginStorage()->value("apiKey").toString(); QString apiKey = pluginStorage()->value("apiKey").toString();
QString hostCache = pluginStorage()->value("hostCache").toString();
pluginStorage()->endGroup(); pluginStorage()->endGroup();
// For legacy reasons we might not have the api key in the pluginstorage yet. Check if there is a key in the thing params. // For legacy reasons we might not have the api key in the pluginstorage yet. Check if there is a key in the thing params.
@ -320,19 +302,35 @@ void IntegrationPluginPhilipsHue::setupThing(ThingSetupInfo *info)
info->finish(Thing::ThingErrorAuthenticationFailure, QT_TR_NOOP("Not authenticated to bridge. Please reconfigure the bridge.")); info->finish(Thing::ThingErrorAuthenticationFailure, QT_TR_NOOP("Not authenticated to bridge. Please reconfigure the bridge."));
return; return;
} }
HueBridge *bridge; HueBridge *bridge = m_bridges.key(thing);
if (m_bridges.values().contains(thing)) { if (!bridge) {
qCDebug(dcPhilipsHue()) << "Re-Discovery, not creating new bridge" << thing->name();
bridge = m_bridges.key(thing);
bridge->setApiKey(apiKey);
bridge->setHostAddress(QHostAddress(thing->paramValue(bridgeThingHostParamTypeId).toString()));
} else {
bridge = new HueBridge(this); bridge = new HueBridge(this);
bridge->setId(thing->paramValue(bridgeThingIdParamTypeId).toString());
bridge->setApiKey(apiKey);
bridge->setHostAddress(QHostAddress(thing->paramValue(bridgeThingHostParamTypeId).toString()));
m_bridges.insert(bridge, thing); m_bridges.insert(bridge, thing);
} }
bridge->setApiKey(apiKey);
QHostAddress zeroconfAddress;
foreach (const ZeroConfServiceEntry &entry, m_zeroConfBrowser->serviceEntries()) {
if (entry.protocol() == QAbstractSocket::IPv4Protocol && normalizeBridgeId(entry.txt("bridgeid")) == thing->paramValue(bridgeThingIdParamTypeId).toString()) {
zeroconfAddress = entry.hostAddress();
}
}
if (!zeroconfAddress.isNull()) {
qCDebug(dcPhilipsHue()) << "Using IP address from zeroconf:" << zeroconfAddress.toString();
bridge->setHostAddress(zeroconfAddress);
pluginStorage()->beginGroup(thing->id().toString());
pluginStorage()->setValue("hostCache", zeroconfAddress.toString());
pluginStorage()->endGroup();
} else if (!hostCache.isEmpty()) {
qCDebug(dcPhilipsHue()) << "Using last known IP:" << hostCache;
bridge->setHostAddress(QHostAddress(hostCache));
} else {
// Let's keep this for now for backward compatibility... But probably can go away at some point.
// Bridge v1 didn't have zeroconf...
QString host = thing->paramValue(bridgeThingHostParamTypeId).toString();
qCDebug(dcPhilipsHue()) << "Using IP from params:" << host;
bridge->setHostAddress(QHostAddress(host));
}
discoverBridgeDevices(bridge); discoverBridgeDevices(bridge);
return info->finish(Thing::ThingErrorNoError); return info->finish(Thing::ThingErrorNoError);
} }
@ -345,15 +343,11 @@ void IntegrationPluginPhilipsHue::setupThing(ThingSetupInfo *info)
return; return;
} }
// Hue color light // Hue color light
if (thing->thingClassId() == colorLightThingClassId) { if (thing->thingClassId() == colorLightThingClassId) {
qCDebug(dcPhilipsHue) << "Setup Hue color light" << thing->params(); qCDebug(dcPhilipsHue) << "Setup Hue color light" << thing->params();
HueBridge *bridge = m_bridges.key(myThings().findById(thing->parentId())); HueLight *hueLight = new HueLight(bridge, this);
HueLight *hueLight = new HueLight(this);
hueLight->setHostAddress(bridge->hostAddress());
hueLight->setApiKey(bridge->apiKey());
hueLight->setId(thing->paramValue(colorLightThingLightIdParamTypeId).toInt()); hueLight->setId(thing->paramValue(colorLightThingLightIdParamTypeId).toInt());
hueLight->setModelId(thing->paramValue(colorLightThingModelIdParamTypeId).toString()); hueLight->setModelId(thing->paramValue(colorLightThingModelIdParamTypeId).toString());
hueLight->setUuid(thing->paramValue(colorLightThingUuidParamTypeId).toString()); hueLight->setUuid(thing->paramValue(colorLightThingUuidParamTypeId).toString());
@ -371,10 +365,7 @@ void IntegrationPluginPhilipsHue::setupThing(ThingSetupInfo *info)
if (thing->thingClassId() == colorTemperatureLightThingClassId) { if (thing->thingClassId() == colorTemperatureLightThingClassId) {
qCDebug(dcPhilipsHue) << "Setup Hue color temperature light" << thing->params(); qCDebug(dcPhilipsHue) << "Setup Hue color temperature light" << thing->params();
HueBridge *bridge = m_bridges.key(myThings().findById(thing->parentId())); HueLight *hueLight = new HueLight(bridge, this);
HueLight *hueLight = new HueLight(this);
hueLight->setHostAddress(bridge->hostAddress());
hueLight->setApiKey(bridge->apiKey());
hueLight->setId(thing->paramValue(colorTemperatureLightThingLightIdParamTypeId).toInt()); hueLight->setId(thing->paramValue(colorTemperatureLightThingLightIdParamTypeId).toInt());
hueLight->setModelId(thing->paramValue(colorTemperatureLightThingModelIdParamTypeId).toString()); hueLight->setModelId(thing->paramValue(colorTemperatureLightThingModelIdParamTypeId).toString());
hueLight->setUuid(thing->paramValue(colorTemperatureLightThingUuidParamTypeId).toString()); hueLight->setUuid(thing->paramValue(colorTemperatureLightThingUuidParamTypeId).toString());
@ -392,10 +383,7 @@ void IntegrationPluginPhilipsHue::setupThing(ThingSetupInfo *info)
if (thing->thingClassId() == dimmableLightThingClassId) { if (thing->thingClassId() == dimmableLightThingClassId) {
qCDebug(dcPhilipsHue) << "Setup Hue white light" << thing->params(); qCDebug(dcPhilipsHue) << "Setup Hue white light" << thing->params();
HueBridge *bridge = m_bridges.key(myThings().findById(thing->parentId())); HueLight *hueLight = new HueLight(bridge, this);
HueLight *hueLight = new HueLight(this);
hueLight->setHostAddress(bridge->hostAddress());
hueLight->setApiKey(bridge->apiKey());
// Migrate thing parameters after changing param type UUIDs in 0.14. // Migrate thing parameters after changing param type UUIDs in 0.14.
QMap<QString, ParamTypeId> migrationMap; QMap<QString, ParamTypeId> migrationMap;
@ -438,10 +426,7 @@ void IntegrationPluginPhilipsHue::setupThing(ThingSetupInfo *info)
if (thing->thingClassId() == remoteThingClassId) { if (thing->thingClassId() == remoteThingClassId) {
qCDebug(dcPhilipsHue) << "Setup Hue remote" << thing->params() << thing->thingClassId(); qCDebug(dcPhilipsHue) << "Setup Hue remote" << thing->params() << thing->thingClassId();
HueBridge *bridge = m_bridges.key(myThings().findById(thing->parentId())); HueRemote *hueRemote = new HueRemote(bridge, this);
HueRemote *hueRemote = new HueRemote(this);
hueRemote->setHostAddress(bridge->hostAddress());
hueRemote->setApiKey(bridge->apiKey());
// Migrate thing parameters after changing param type UUIDs in 0.14. // Migrate thing parameters after changing param type UUIDs in 0.14.
QMap<QString, ParamTypeId> migrationMap; QMap<QString, ParamTypeId> migrationMap;
@ -481,7 +466,7 @@ void IntegrationPluginPhilipsHue::setupThing(ThingSetupInfo *info)
// Hue tap // Hue tap
if (thing->thingClassId() == tapThingClassId) { if (thing->thingClassId() == tapThingClassId) {
HueRemote *hueTap = new HueRemote(this); HueRemote *hueTap = new HueRemote(bridge, this);
hueTap->setName(thing->name()); hueTap->setName(thing->name());
hueTap->setId(thing->paramValue(tapThingSensorIdParamTypeId).toInt()); hueTap->setId(thing->paramValue(tapThingSensorIdParamTypeId).toInt());
hueTap->setModelId(thing->paramValue(tapThingModelIdParamTypeId).toString()); hueTap->setModelId(thing->paramValue(tapThingModelIdParamTypeId).toString());
@ -496,7 +481,7 @@ void IntegrationPluginPhilipsHue::setupThing(ThingSetupInfo *info)
// Hue smart button // Hue smart button
if (thing->thingClassId() == smartButtonThingClassId) { if (thing->thingClassId() == smartButtonThingClassId) {
HueRemote *smartButton = new HueRemote(this); HueRemote *smartButton = new HueRemote(bridge, this);
smartButton->setName(thing->name()); smartButton->setName(thing->name());
smartButton->setId(thing->paramValue(smartButtonThingSensorIdParamTypeId).toInt()); smartButton->setId(thing->paramValue(smartButtonThingSensorIdParamTypeId).toInt());
smartButton->setModelId(thing->paramValue(smartButtonThingModelIdParamTypeId).toString()); smartButton->setModelId(thing->paramValue(smartButtonThingModelIdParamTypeId).toString());
@ -513,7 +498,7 @@ void IntegrationPluginPhilipsHue::setupThing(ThingSetupInfo *info)
if (thing->thingClassId() == motionSensorThingClassId) { if (thing->thingClassId() == motionSensorThingClassId) {
qCDebug(dcPhilipsHue) << "Setup Hue motion sensor" << thing->params(); qCDebug(dcPhilipsHue) << "Setup Hue motion sensor" << thing->params();
HueIndoorSensor *motionSensor = new HueIndoorSensor(this); HueIndoorSensor *motionSensor = new HueIndoorSensor(bridge, this);
motionSensor->setTimeout(thing->setting(motionSensorSettingsTimeoutParamTypeId).toUInt()); motionSensor->setTimeout(thing->setting(motionSensorSettingsTimeoutParamTypeId).toUInt());
motionSensor->setUuid(thing->paramValue(motionSensorThingUuidParamTypeId).toString()); motionSensor->setUuid(thing->paramValue(motionSensorThingUuidParamTypeId).toString());
motionSensor->setModelId(thing->paramValue(motionSensorThingModelIdParamTypeId).toString()); motionSensor->setModelId(thing->paramValue(motionSensorThingModelIdParamTypeId).toString());
@ -545,7 +530,7 @@ void IntegrationPluginPhilipsHue::setupThing(ThingSetupInfo *info)
if (thing->thingClassId() == outdoorSensorThingClassId) { if (thing->thingClassId() == outdoorSensorThingClassId) {
qCDebug(dcPhilipsHue) << "Setup Hue Outdoor sensor" << thing->params(); qCDebug(dcPhilipsHue) << "Setup Hue Outdoor sensor" << thing->params();
HueMotionSensor *outdoorSensor = new HueOutdoorSensor(this); HueMotionSensor *outdoorSensor = new HueOutdoorSensor(bridge, this);
outdoorSensor->setTimeout(thing->setting(outdoorSensorSettingsTimeoutParamTypeId).toUInt()); outdoorSensor->setTimeout(thing->setting(outdoorSensorSettingsTimeoutParamTypeId).toUInt());
outdoorSensor->setUuid(thing->paramValue(outdoorSensorThingUuidParamTypeId).toString()); outdoorSensor->setUuid(thing->paramValue(outdoorSensorThingUuidParamTypeId).toString());
outdoorSensor->setModelId(thing->paramValue(outdoorSensorThingModelIdParamTypeId).toString()); outdoorSensor->setModelId(thing->paramValue(outdoorSensorThingModelIdParamTypeId).toString());
@ -796,6 +781,20 @@ void IntegrationPluginPhilipsHue::onDeviceNameChanged()
} }
} }
QString IntegrationPluginPhilipsHue::normalizeBridgeId(const QString &bridgeId)
{
// For some reason the hue apis return slightly different bridge ids:
// * UPnP api returns serial numbers withouot a "fffe" in the middle
// * N-UPnP and ZeroConf do have such a "fffe" in them.
// Let's normalize it to something that's save to compare.
QString ret = bridgeId.toLower();
if (bridgeId.indexOf("fffe") == 6) {
ret.remove(6, 4);
}
return ret;
}
void IntegrationPluginPhilipsHue::executeAction(ThingActionInfo *info) void IntegrationPluginPhilipsHue::executeAction(ThingActionInfo *info)
{ {
Thing *thing = info->thing(); Thing *thing = info->thing();
@ -1263,7 +1262,7 @@ void IntegrationPluginPhilipsHue::refreshSensors(HueBridge *bridge)
void IntegrationPluginPhilipsHue::discoverBridgeDevices(HueBridge *bridge) void IntegrationPluginPhilipsHue::discoverBridgeDevices(HueBridge *bridge)
{ {
Thing *thing = m_bridges.value(bridge); Thing *thing = m_bridges.value(bridge);
qCDebug(dcPhilipsHue) << "Discover bridge devices" << bridge->hostAddress(); qCDebug(dcPhilipsHue) << "Asking bridge for new devices" << bridge->hostAddress();
QPair<QNetworkRequest, QByteArray> lightsRequest = bridge->createDiscoverLightsRequest(); QPair<QNetworkRequest, QByteArray> lightsRequest = bridge->createDiscoverLightsRequest();
QNetworkReply *lightsReply = hardwareManager()->networkManager()->get(lightsRequest.first); QNetworkReply *lightsReply = hardwareManager()->networkManager()->get(lightsRequest.first);
@ -1279,7 +1278,7 @@ void IntegrationPluginPhilipsHue::discoverBridgeDevices(HueBridge *bridge)
void IntegrationPluginPhilipsHue::searchNewDevices(HueBridge *bridge, const QString &serialNumber) void IntegrationPluginPhilipsHue::searchNewDevices(HueBridge *bridge, const QString &serialNumber)
{ {
Thing *thing = m_bridges.value(bridge); Thing *thing = m_bridges.value(bridge);
qCDebug(dcPhilipsHue) << "Discover bridge devices" << bridge->hostAddress(); qCDebug(dcPhilipsHue) << "Triggering ZigBee scan on bridge" << bridge->hostAddress();
QPair<QNetworkRequest, QByteArray> request = bridge->createSearchLightsRequest(serialNumber); QPair<QNetworkRequest, QByteArray> request = bridge->createSearchLightsRequest(serialNumber);
QNetworkReply *reply = hardwareManager()->networkManager()->post(request.first, request.second); QNetworkReply *reply = hardwareManager()->networkManager()->post(request.first, request.second);
@ -1431,6 +1430,12 @@ void IntegrationPluginPhilipsHue::processBridgeSensorDiscoveryResponse(Thing *th
return; return;
} }
HueBridge *bridge = m_bridges.key(thing);
if (!bridge) {
qCWarning(dcPhilipsHue()) << "Received a reply for a bridge we don't have any more.";
return;
}
// Create sensors if not already added // Create sensors if not already added
QVariantMap sensorsMap = jsonDoc.toVariant().toMap(); QVariantMap sensorsMap = jsonDoc.toVariant().toMap();
QHash<QString, HueMotionSensor *> motionSensors; QHash<QString, HueMotionSensor *> motionSensors;
@ -1511,9 +1516,9 @@ void IntegrationPluginPhilipsHue::processBridgeSensorDiscoveryResponse(Thing *th
// Create an outdoor sensor // Create an outdoor sensor
HueMotionSensor *motionSensor = nullptr; HueMotionSensor *motionSensor = nullptr;
if (model == "SML001") { if (model == "SML001") {
motionSensor = new HueIndoorSensor(this); motionSensor = new HueIndoorSensor(bridge, this);
} else { } else {
motionSensor = new HueOutdoorSensor(this); motionSensor = new HueOutdoorSensor(bridge, this);
} }
motionSensor->setModelId(model); motionSensor->setModelId(model);
motionSensor->setUuid(baseUuid); motionSensor->setUuid(baseUuid);
@ -1534,9 +1539,9 @@ void IntegrationPluginPhilipsHue::processBridgeSensorDiscoveryResponse(Thing *th
// Create an outdoor sensor // Create an outdoor sensor
HueMotionSensor *motionSensor = nullptr; HueMotionSensor *motionSensor = nullptr;
if (model == "SML001") { if (model == "SML001") {
motionSensor = new HueIndoorSensor(this); motionSensor = new HueIndoorSensor(bridge, this);
} else { } else {
motionSensor = new HueOutdoorSensor(this); motionSensor = new HueOutdoorSensor(bridge, this);
} }
motionSensor->setModelId(model); motionSensor->setModelId(model);
motionSensor->setUuid(baseUuid); motionSensor->setUuid(baseUuid);
@ -1557,9 +1562,9 @@ void IntegrationPluginPhilipsHue::processBridgeSensorDiscoveryResponse(Thing *th
// Create an outdoor sensor // Create an outdoor sensor
HueMotionSensor *motionSensor = nullptr; HueMotionSensor *motionSensor = nullptr;
if (model == "SML001") { if (model == "SML001") {
motionSensor = new HueIndoorSensor(this); motionSensor = new HueIndoorSensor(bridge, this);
} else { } else {
motionSensor = new HueOutdoorSensor(this); motionSensor = new HueOutdoorSensor(bridge, this);
} }
motionSensor->setModelId(model); motionSensor->setModelId(model);
motionSensor->setUuid(baseUuid); motionSensor->setUuid(baseUuid);

View File

@ -86,6 +86,9 @@ private slots:
void networkManagerReplyReady(); void networkManagerReplyReady();
void onDeviceNameChanged(); void onDeviceNameChanged();
private:
QString normalizeBridgeId(const QString &bridgeId);
private: private:
class DiscoveryJob { class DiscoveryJob {
public: public: