diff --git a/zigbee-generic-lights/integrationpluginzigbeegenericlights.cpp b/zigbee-generic-lights/integrationpluginzigbeegenericlights.cpp index 1165a4ce..d928a55d 100644 --- a/zigbee-generic-lights/integrationpluginzigbeegenericlights.cpp +++ b/zigbee-generic-lights/integrationpluginzigbeegenericlights.cpp @@ -163,7 +163,7 @@ void IntegrationPluginZigbeeGenericLights::setupThing(ThingSetupInfo *info) // Get the node for this thing QUuid networkUuid = thing->paramValue(m_networkUuidParamTypeIds.value(thing->thingClassId())).toUuid(); ZigbeeAddress zigbeeAddress = ZigbeeAddress(thing->paramValue(m_ieeeAddressParamTypeIds.value(thing->thingClassId())).toString()); - ZigbeeNode *node = hardwareManager()->zigbeeResource()->getNode(networkUuid, zigbeeAddress); + ZigbeeNode *node = hardwareManager()->zigbeeResource()->claimNode(this, networkUuid, zigbeeAddress); if (!node) { qCWarning(dcZigbeeGenericLights()) << "Zigbee node for" << info->thing()->name() << "not found.´"; info->finish(Thing::ThingErrorHardwareNotAvailable); diff --git a/zigbee-generic/integrationpluginzigbeegeneric.cpp b/zigbee-generic/integrationpluginzigbeegeneric.cpp index 50ad31cf..76c5cc05 100644 --- a/zigbee-generic/integrationpluginzigbeegeneric.cpp +++ b/zigbee-generic/integrationpluginzigbeegeneric.cpp @@ -33,6 +33,8 @@ #include "plugininfo.h" #include "hardware/zigbee/zigbeehardwareresource.h" +#include "zcl/hvac/zigbeeclusterthermostat.h" + #include IntegrationPluginZigbeeGeneric::IntegrationPluginZigbeeGeneric() @@ -78,6 +80,19 @@ bool IntegrationPluginZigbeeGeneric::handleNode(ZigbeeNode *node, const QUuid &n return handled; } +void IntegrationPluginZigbeeGeneric::handleRemoveNode(ZigbeeNode *node, const QUuid &networkUuid) +{ + Q_UNUSED(networkUuid) + Thing *thing = m_zigbeeNodes.key(node); + if (thing) { + qCDebug(dcZigbeeGeneric()) << node << "for" << thing << "has left the network."; + emit autoThingDisappeared(thing->id()); + + // Removing it from our map to prevent a loop that would ask the zigbee network to remove this node (see thingRemoved()) + m_zigbeeNodes.remove(thing); + } +} + void IntegrationPluginZigbeeGeneric::init() { hardwareManager()->zigbeeResource()->registerHandler(this, ZigbeeHardwareResource::HandlerTypeCatchAll); @@ -94,13 +109,15 @@ void IntegrationPluginZigbeeGeneric::setupThing(ThingSetupInfo *info) QUuid networkUuid = thing->paramValue(m_networkUuidParamTypeIds.value(thing->thingClassId())).toUuid(); qCDebug(dcZigbeeGeneric()) << "Nework uuid:" << networkUuid; ZigbeeAddress zigbeeAddress = ZigbeeAddress(thing->paramValue(m_ieeeAddressParamTypeIds.value(thing->thingClassId())).toString()); - ZigbeeNode *node = hardwareManager()->zigbeeResource()->getNode(networkUuid, zigbeeAddress); + ZigbeeNode *node = hardwareManager()->zigbeeResource()->claimNode(this, networkUuid, zigbeeAddress); if (!node) { qCWarning(dcZigbeeGeneric()) << "Zigbee node for" << info->thing()->name() << "not found.´"; info->finish(Thing::ThingErrorHardwareNotAvailable); return; } + m_zigbeeNodes.insert(thing, node); + ZigbeeNodeEndpoint *endpoint = node->getEndpoint(0x01); if (!endpoint) { qCWarning(dcZigbeeGeneric()) << "Zigbee endpoint 1 not found on" << thing->name(); @@ -127,7 +144,8 @@ void IntegrationPluginZigbeeGeneric::setupThing(ThingSetupInfo *info) // Type specific setup if (thing->thingClassId() == thermostatThingClassId) { // TODO: Thermostat cluster is missing - // ZigbeeClusterThermostat *thermostatCluster = endpoint->inputCluster(ZigbeeClusterLibrary::ClusterIdThermostat); +// ZigbeeClusterThermostat *thermostatCluster = endpoint->outputCluster(ZigbeeClusterLibrary::ClusterIdThermostat); +// thermostatCluster->attribute(ZigbeeClusterLibrary::ClusterId); } @@ -142,5 +160,9 @@ void IntegrationPluginZigbeeGeneric::executeAction(ThingActionInfo *info) void IntegrationPluginZigbeeGeneric::thingRemoved(Thing *thing) { - Q_UNUSED(thing) + ZigbeeNode *node = m_zigbeeNodes.take(thing); + if (node) { + QUuid networkUuid = thing->paramValue(m_networkUuidParamTypeIds.value(thing->thingClassId())).toUuid(); + hardwareManager()->zigbeeResource()->removeNodeFromNetwork(networkUuid, node); + } } diff --git a/zigbee-generic/integrationpluginzigbeegeneric.h b/zigbee-generic/integrationpluginzigbeegeneric.h index feb7f821..39f755eb 100644 --- a/zigbee-generic/integrationpluginzigbeegeneric.h +++ b/zigbee-generic/integrationpluginzigbeegeneric.h @@ -49,6 +49,7 @@ public: QString name() const override; bool handleNode(ZigbeeNode *node, const QUuid &networkUuid) override; + void handleRemoveNode(ZigbeeNode *node, const QUuid &networkUuid) override; void init() override; void setupThing(ThingSetupInfo *info) override; @@ -56,6 +57,7 @@ public: void thingRemoved(Thing *thing) override; private: + QHash m_zigbeeNodes; QHash m_ieeeAddressParamTypeIds; QHash m_networkUuidParamTypeIds; diff --git a/zigbee-lumi/integrationpluginzigbeelumi.cpp b/zigbee-lumi/integrationpluginzigbeelumi.cpp index ed1e33be..fb099283 100644 --- a/zigbee-lumi/integrationpluginzigbeelumi.cpp +++ b/zigbee-lumi/integrationpluginzigbeelumi.cpp @@ -161,7 +161,7 @@ void IntegrationPluginZigbeeLumi::setupThing(ThingSetupInfo *info) // Get the node for this thing QUuid networkUuid = thing->paramValue(m_networkUuidParamTypeIds.value(thing->thingClassId())).toUuid(); ZigbeeAddress zigbeeAddress = ZigbeeAddress(thing->paramValue(m_zigbeeAddressParamTypeIds.value(thing->thingClassId())).toString()); - ZigbeeNode *node = hardwareManager()->zigbeeResource()->getNode(networkUuid, zigbeeAddress); + ZigbeeNode *node = hardwareManager()->zigbeeResource()->claimNode(this, networkUuid, zigbeeAddress); if (!node) { qCWarning(dcZigbeeLumi()) << "Zigbee node for" << info->thing()->name() << "not found.´"; info->finish(Thing::ThingErrorHardwareNotAvailable);