Skip to content

Commit 0b6680f

Browse files
committed
feat: Added devices.updateProfile method
1 parent 4a30c88 commit 0b6680f

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

src/endpoint/devices.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ export interface DeviceUpdate {
110110
label?: string
111111
}
112112

113+
export interface DeviceProfileUpdate {
114+
profileId: string
115+
}
116+
113117
export interface DeviceCreate {
114118
label?: string
115119
locationId?: string // <^(?:([0-9a-fA-F]{32})|([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}))$>
@@ -370,6 +374,18 @@ export class DevicesEndpoint extends Endpoint {
370374
return this.client.put<Device>(id, data)
371375
}
372376

377+
/**
378+
* Update the deviceProfileId of a device. Note that currently this method can
379+
* only be called with an installedApp token with the i:deviceprofiles scope
380+
* on a device created by that app.
381+
* @param id UUID of the device
382+
* @param data the new device profile
383+
*/
384+
public updateProfile(id: string, data: DeviceProfileUpdate): Promise<Device> {
385+
return this.client.request<Device>('put',`${id}/profile`, data, undefined,
386+
{headerOverrides: {Accept: 'application/vnd.smartthings+json;v=20170916'}})
387+
}
388+
373389
/**
374390
* Returns the current values of all device attributes
375391
* @param id UUID of the device
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const request = {
2+
'url': 'https://api.smartthings.com/devices/6f5ea629-4c05-4a90-a244-cc129b0a80c3',
3+
'method': 'put',
4+
'headers': {
5+
'Content-Type': 'application/json;charset=utf-8',
6+
'Accept': 'application/json',
7+
'Authorization': 'Bearer 52991afa-66e8-4af0-8d85-5c568ed5ba7d',
8+
},
9+
'data': {
10+
'label': 'Living room light',
11+
},
12+
}
13+
const response = {
14+
'deviceId': '6f5ea629-4c05-4a90-a244-cc129b0a80c3',
15+
'name': 'color.light.100x',
16+
'label': 'Living room light',
17+
'deviceManufacturerCode': '010F-0B01-2002',
18+
'locationId': '95efee9b-6073-4871-b5ba-de6642187293',
19+
'roomId': '0fd2b1ef-1b33-4a54-9153-65aca91e9660',
20+
'components': [
21+
{
22+
'id': 'main',
23+
'label': 'string',
24+
'capabilities': [
25+
{
26+
'id': 'switch',
27+
'version': 1,
28+
},
29+
],
30+
},
31+
],
32+
'app': {
33+
'installedAppId': '0c0b935d-0616-4441-a0bf-da7aeec3dc0a',
34+
'externalId': 'Th13390',
35+
'profile': {
36+
'id': 'a7b3c264-2d22-416e-bca1-ca4b59a60aee',
37+
},
38+
},
39+
'type': 'ENDPOINT_APP',
40+
}
41+
export default {request, response}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const request = {
2+
'url': 'https://api.smartthings.com/devices/6f5ea629-4c05-4a90-a244-cc129b0a80c3/profile',
3+
'method': 'put',
4+
'headers': {
5+
'Content-Type': 'application/json;charset=utf-8',
6+
'Accept': 'application/vnd.smartthings+json;v=20170916',
7+
'Authorization': 'Bearer 52991afa-66e8-4af0-8d85-5c568ed5ba7d',
8+
},
9+
'data': {
10+
'profileId': '2b11d686-11e2-41f9-bff2-8aa40d9b944a',
11+
},
12+
}
13+
const response = {
14+
'deviceId': '6f5ea629-4c05-4a90-a244-cc129b0a80c3',
15+
'name': 'color.light.100x',
16+
'label': 'Living room light',
17+
'deviceManufacturerCode': '010F-0B01-2002',
18+
'locationId': '95efee9b-6073-4871-b5ba-de6642187293',
19+
'roomId': '0fd2b1ef-1b33-4a54-9153-65aca91e9660',
20+
'components': [
21+
{
22+
'id': 'main',
23+
'label': 'string',
24+
'capabilities': [
25+
{
26+
'id': 'switch',
27+
'version': 1,
28+
},
29+
],
30+
},
31+
],
32+
'app': {
33+
'installedAppId': '0c0b935d-0616-4441-a0bf-da7aeec3dc0a',
34+
'externalId': 'Th13390',
35+
'profile': {
36+
'id': '2b11d686-11e2-41f9-bff2-8aa40d9b944a',
37+
},
38+
},
39+
'type': 'ENDPOINT_APP',
40+
}
41+
export default {request, response}

test/unit/devices.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import createEvents from './data/devices/post_devices_385931b6-0121-4848-bcc8-54
2323
import turnOn1 from './data/devices/post_devices_385931b6-0121-4848-bcc8-54cb76436de1_commands'
2424
import create from './data/devices/post_devices'
2525
import create2 from './data/devices/post_devices_2'
26+
import update from './data/devices/put_devices'
27+
import updateProfile from './data/devices/put_devices_profile'
2628

2729

2830
const authenticator = new BearerTokenAuthenticator('52991afa-66e8-4af0-8d85-5c568ed5ba7d')
@@ -184,6 +186,26 @@ describe('Devices', () => {
184186
expect(response).toBe(create2.response)
185187
})
186188

189+
it('update name', async () => {
190+
axios.request.mockImplementationOnce(() => Promise.resolve({ status: 200, data: update.response }))
191+
const data = {
192+
'label': 'Living room light',
193+
}
194+
const response: Device = await isaClient.devices.update('6f5ea629-4c05-4a90-a244-cc129b0a80c3', data)
195+
expect(axios.request).toHaveBeenCalledWith(expectedRequest(update.request))
196+
expect(response).toStrictEqual(create.response)
197+
})
198+
199+
it('update profileId', async () => {
200+
axios.request.mockImplementationOnce(() => Promise.resolve({ status: 200, data: updateProfile.response }))
201+
const data = {
202+
'profileId': '2b11d686-11e2-41f9-bff2-8aa40d9b944a',
203+
}
204+
const response: Device = await isaClient.devices.updateProfile('6f5ea629-4c05-4a90-a244-cc129b0a80c3', data)
205+
expect(axios.request).toHaveBeenCalledWith(expectedRequest(updateProfile.request))
206+
expect(response).toStrictEqual(updateProfile.response)
207+
})
208+
187209
it('executeCommand', async () => {
188210
axios.request.mockImplementationOnce(() => Promise.resolve({ status: 200, data: turnOn1.response }))
189211
const response: Status = await client.devices.executeCommand('385931b6-0121-4848-bcc8-54cb76436de1', {

0 commit comments

Comments
 (0)