Skip to content

Commit

Permalink
Merge pull request #94 from Samsung/dev
Browse files Browse the repository at this point in the history
Fix: SmartThings getDevices issues
  • Loading branch information
jongsu2 authored Aug 14, 2024
2 parents 3d0b9bd + f28a88c commit 2128f36
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
42 changes: 33 additions & 9 deletions 101-SmartThings.html
Original file line number Diff line number Diff line change
Expand Up @@ -649,14 +649,15 @@ <h3>Properties</h3>
}).then(resolve, reject)
});
},
getDevices: function (token) {
getDevices: function (url, token) {
return new Promise((resolve, reject) => {
$.ajax({
url: "https://api.smartthings.com/v1/devices", headers: {
Authorization: 'Bearer ' + token
}
}).then(resolve, reject)
})
url: url,
headers: {
Authorization: 'Bearer ' + token,
},
}).then(resolve, reject);
});
},
getDeviceList: function (token, done, fail) {
return $.ajax({
Expand Down Expand Up @@ -797,6 +798,17 @@ <h3>Properties</h3>
this.validatePat = function (pat) {
return SmartThingsApi.getCapability(pat, 'switch', 1);
}
this.fetchAllDevices = (url, pat, allDevices = []) => {
return SmartThingsApi.getDevices(url, pat).then(response => {
const combinedDevices = allDevices.concat(response.items);

if (response._links && response._links.next && response._links.next.href) {
return SmartThingsProfile.fetchAllDevices(response._links.next.href, pat, combinedDevices);
} else {
return combinedDevices;
}
});
};
this.addPersonalToken = function (nodeId, pat, isRefresh) {
if (!pat) {
_mydevices[nodeId] = Promise.resolve(null);
Expand Down Expand Up @@ -824,10 +836,9 @@ <h3>Properties</h3>
return locations;
})

const getDevicesPR = SmartThingsApi.getDevices(pat)
const getDevicesPR = SmartThingsProfile.fetchAllDevices('https://api.smartthings.com/v1/devices', pat)
.then(devicesResult => {
return devicesResult.items
.map(d => {
return devicesResult.map(d => {
return {
deviceId: d.deviceId,
name: d.label || d.name,
Expand All @@ -852,6 +863,7 @@ <h3>Properties</h3>
var prs = devices.map(d => d.components).flat()
.map(cmp => cmp.capabilities).flat()
.filter(cp => !SmartThingsProfile.getCapability(cp.id + '_v' + cp.version))
.filter((d, index, self) => index === self.findIndex((t) => t.id === d.id && t.version === d.version))
.map(cp => SmartThingsApi.getCapability(pat, cp.id, cp.version))
return Promise.allSettled(prs);
});
Expand Down Expand Up @@ -1225,7 +1237,19 @@ <h3>Properties</h3>
})
})
mydevice.devices.forEach(function (device) {
let locationEl = document.querySelector(`.md_list__location[data-location-id="${device.locationId}"]`);
let roomEl = document.querySelector(`.md_list__room[data-location-id="${device.locationId}"][data-room-id="${device.roomId}"]`);

if (!locationEl) {
locationEl = createLocationEl({ locationId: device.locationId });
mdContainerEl.append(locationEl);
}

if (!roomEl) {
roomEl = createRoomEl({ locationId: device.locationId, roomId: device.roomId });
locationEl.append(roomEl);
}

roomEl.append(createDeviceEl(device));
});
} else {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-red-contrib-samsung-automation-studio-nodes",
"version": "1.1.22",
"version": "1.1.23",
"description": "Samsung Automation Studio Nodes for Node-RED",
"keywords": [
"SmartThings",
Expand Down

0 comments on commit 2128f36

Please sign in to comment.