Skip to content

Commit 2128f36

Browse files
authored
Merge pull request #94 from Samsung/dev
Fix: SmartThings getDevices issues
2 parents 3d0b9bd + f28a88c commit 2128f36

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

101-SmartThings.html

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -649,14 +649,15 @@ <h3>Properties</h3>
649649
}).then(resolve, reject)
650650
});
651651
},
652-
getDevices: function (token) {
652+
getDevices: function (url, token) {
653653
return new Promise((resolve, reject) => {
654654
$.ajax({
655-
url: "https://api.smartthings.com/v1/devices", headers: {
656-
Authorization: 'Bearer ' + token
657-
}
658-
}).then(resolve, reject)
659-
})
655+
url: url,
656+
headers: {
657+
Authorization: 'Bearer ' + token,
658+
},
659+
}).then(resolve, reject);
660+
});
660661
},
661662
getDeviceList: function (token, done, fail) {
662663
return $.ajax({
@@ -797,6 +798,17 @@ <h3>Properties</h3>
797798
this.validatePat = function (pat) {
798799
return SmartThingsApi.getCapability(pat, 'switch', 1);
799800
}
801+
this.fetchAllDevices = (url, pat, allDevices = []) => {
802+
return SmartThingsApi.getDevices(url, pat).then(response => {
803+
const combinedDevices = allDevices.concat(response.items);
804+
805+
if (response._links && response._links.next && response._links.next.href) {
806+
return SmartThingsProfile.fetchAllDevices(response._links.next.href, pat, combinedDevices);
807+
} else {
808+
return combinedDevices;
809+
}
810+
});
811+
};
800812
this.addPersonalToken = function (nodeId, pat, isRefresh) {
801813
if (!pat) {
802814
_mydevices[nodeId] = Promise.resolve(null);
@@ -824,10 +836,9 @@ <h3>Properties</h3>
824836
return locations;
825837
})
826838

827-
const getDevicesPR = SmartThingsApi.getDevices(pat)
839+
const getDevicesPR = SmartThingsProfile.fetchAllDevices('https://api.smartthings.com/v1/devices', pat)
828840
.then(devicesResult => {
829-
return devicesResult.items
830-
.map(d => {
841+
return devicesResult.map(d => {
831842
return {
832843
deviceId: d.deviceId,
833844
name: d.label || d.name,
@@ -852,6 +863,7 @@ <h3>Properties</h3>
852863
var prs = devices.map(d => d.components).flat()
853864
.map(cmp => cmp.capabilities).flat()
854865
.filter(cp => !SmartThingsProfile.getCapability(cp.id + '_v' + cp.version))
866+
.filter((d, index, self) => index === self.findIndex((t) => t.id === d.id && t.version === d.version))
855867
.map(cp => SmartThingsApi.getCapability(pat, cp.id, cp.version))
856868
return Promise.allSettled(prs);
857869
});
@@ -1225,7 +1237,19 @@ <h3>Properties</h3>
12251237
})
12261238
})
12271239
mydevice.devices.forEach(function (device) {
1240+
let locationEl = document.querySelector(`.md_list__location[data-location-id="${device.locationId}"]`);
12281241
let roomEl = document.querySelector(`.md_list__room[data-location-id="${device.locationId}"][data-room-id="${device.roomId}"]`);
1242+
1243+
if (!locationEl) {
1244+
locationEl = createLocationEl({ locationId: device.locationId });
1245+
mdContainerEl.append(locationEl);
1246+
}
1247+
1248+
if (!roomEl) {
1249+
roomEl = createRoomEl({ locationId: device.locationId, roomId: device.roomId });
1250+
locationEl.append(roomEl);
1251+
}
1252+
12291253
roomEl.append(createDeviceEl(device));
12301254
});
12311255
} else {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-red-contrib-samsung-automation-studio-nodes",
3-
"version": "1.1.22",
3+
"version": "1.1.23",
44
"description": "Samsung Automation Studio Nodes for Node-RED",
55
"keywords": [
66
"SmartThings",

0 commit comments

Comments
 (0)