Skip to content

Commit b2d2fa2

Browse files
committed
feat: Added device includeHealth and includeStatus options
1 parent 0d70722 commit b2d2fa2

12 files changed

+757
-28
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
},
2929
"devDependencies": {
3030
"@semantic-release/git": "^9.0.0",
31-
"@types/es6-promise": "^3.3.0",
3231
"@types/jest": "^26.0.10",
3332
"@types/node": "^12.11.7",
3433
"@types/qs": "^6.9.1",

src/endpoint-client.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -163,31 +163,31 @@ export class EndpointClient {
163163
}
164164
}
165165

166-
public async get<T = any, R = AxiosResponse<T>>(path?: string, params?: HttpClientParams): Promise<T> {
167-
return this.request('get', path, undefined, params)
166+
public async get<T = any, R = AxiosResponse<T>>(path?: string, params?: HttpClientParams, options?: EndpointClientRequestOptions<T>): Promise<T> {
167+
return this.request('get', path, undefined, params, options)
168168
}
169169

170-
public post<T = any, R = AxiosResponse<T>>(path?: string, data?: any, params?: HttpClientParams): Promise<T> {
171-
return this.request('post', path, data, params)
170+
public post<T = any, R = AxiosResponse<T>>(path?: string, data?: any, params?: HttpClientParams, options?: EndpointClientRequestOptions<T>): Promise<T> {
171+
return this.request('post', path, data, params, options)
172172
}
173173

174-
public put<T = any, R = AxiosResponse<T>>(path?: string, data?: any, params?: HttpClientParams): Promise<T> {
175-
return this.request('put', path, data, params)
174+
public put<T = any, R = AxiosResponse<T>>(path?: string, data?: any, params?: HttpClientParams, options?: EndpointClientRequestOptions<T>): Promise<T> {
175+
return this.request('put', path, data, params, options)
176176
}
177177

178-
public patch<T = any, R = AxiosResponse<T>>(path?: string, data?: any, params?: HttpClientParams): Promise<T> {
179-
return this.request('patch', path, data, params)
178+
public patch<T = any, R = AxiosResponse<T>>(path?: string, data?: any, params?: HttpClientParams, options?: EndpointClientRequestOptions<T>): Promise<T> {
179+
return this.request('patch', path, data, params, options)
180180
}
181181

182-
public delete<T = any, R = AxiosResponse<T>>(path?: string, params?: HttpClientParams): Promise<T> {
183-
return this.request('delete', path, undefined, params)
182+
public delete<T = any, R = AxiosResponse<T>>(path?: string, params?: HttpClientParams, options?: EndpointClientRequestOptions<T>): Promise<T> {
183+
return this.request('delete', path, undefined, params, options)
184184
}
185185

186-
public async getPagedItems<T = any, R = AxiosResponse<T>>(path?: string, params?: HttpClientParams): Promise<T[]> {
187-
let list = await this.get<ItemsList>(path, params)
186+
public async getPagedItems<T = any, R = AxiosResponse<T>>(path?: string, params?: HttpClientParams, options?: EndpointClientRequestOptions<ItemsList>): Promise<T[]> {
187+
let list = await this.get<ItemsList>(path, params, options)
188188
const result = list.items
189189
while (list._links && list._links.next) {
190-
list = await this.get<ItemsList>(list._links.next.href)
190+
list = await this.get<ItemsList>(list._links.next.href, undefined, options)
191191
result.push(...list.items)
192192
}
193193
return result

src/endpoint/devices.ts

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ import { Links, Status, SuccessStatusValue } from '../types'
55
import {PresentationDevicePresentation} from './presentation'
66

77

8+
const HEADER_OVERRIDES = {Accept: 'application/vnd.smartthings+json;v=20170916'}
9+
810
export interface CapabilityReference {
911
id: string
1012
version?: number
13+
status?: CapabilityStatus
1114
}
1215

1316
export interface Component {
@@ -84,6 +87,11 @@ export interface ProfileIdentifier {
8487
id: string
8588
}
8689

90+
export interface HealthState {
91+
state: DeviceHealthState
92+
lastUpdatedDate?: string
93+
}
94+
8795
export interface Device {
8896
deviceId?: string
8997
name?: string
@@ -104,6 +112,7 @@ export interface Device {
104112
viper?: ViperDeviceDetails
105113
type?: DeviceIntegrationType
106114
restrictionTier?: number
115+
healthState?: HealthState
107116
}
108117

109118
export interface DeviceUpdate {
@@ -222,6 +231,16 @@ export interface DeviceListOptions {
222231
*/
223232
installedAppId?: string
224233

234+
/**
235+
* Include the device health, i.e. online/offline status in the response
236+
*/
237+
includeHealth?: boolean
238+
239+
/**
240+
* Include the device status data, i.e. the values of all attributes, in the response
241+
*/
242+
includeStatus?: boolean
243+
225244
/**
226245
* Limit the number of results to this value. By default all devices are returned
227246
*/
@@ -238,6 +257,18 @@ export interface DeviceListOptions {
238257
type?: DeviceIntegrationType | DeviceIntegrationType[]
239258
}
240259

260+
export interface DeviceGetOptions {
261+
/**
262+
* Include the device health, i.e. online/offline status in the response
263+
*/
264+
includeHealth?: boolean
265+
266+
/**
267+
* Include the device status data, i.e. the values of all attributes, in the response
268+
*/
269+
includeStatus?: boolean
270+
}
271+
241272
export interface HueSaturation {
242273
hue: number
243274
saturation: number
@@ -250,10 +281,12 @@ export class DevicesEndpoint extends Endpoint {
250281

251282
/**
252283
* Returns a list of devices matching the query options or all devices accessible by the principal (i.e. user)
253-
* if no options are specified.
284+
* if no options are specified. If the includeHealth option is set to true then the response will also contain
285+
* the health status of each device (i.e. if it is online or offline). If the includeStatus option is set to true
286+
* then the response will also include the status of all attributes (i.e. value and timestamp)
254287
*
255-
* @param options query options, capability, capabilitiesMode ('and' or 'or'), locationId, deviceId. These can
256-
* be single values or arrays.
288+
* @param options query options, capability, capabilitiesMode ('and' or 'or'), locationId, deviceId. which can
289+
* be single values or arrays, and includeHealth & includeStatus booleans
257290
*/
258291
public async list(options: DeviceListOptions = {}): Promise<Device[]> {
259292
const params: HttpClientParams = {}
@@ -271,6 +304,12 @@ export class DevicesEndpoint extends Endpoint {
271304
if ('deviceId' in options && options.deviceId) {
272305
params.deviceId = options.deviceId
273306
}
307+
if ('includeHealth' in options && options.includeHealth !== undefined) {
308+
params.includeHealth = options.includeHealth.toString()
309+
}
310+
if ('includeStatus' in options && options.includeStatus !== undefined) {
311+
params.includeStatus = options.includeStatus.toString()
312+
}
274313
if ('installedAppId' in options && options.installedAppId) {
275314
params.installedAppId = options.installedAppId
276315
}
@@ -283,7 +322,8 @@ export class DevicesEndpoint extends Endpoint {
283322
if ('type' in options && options.type) {
284323
params.type = options.type
285324
}
286-
return this.client.getPagedItems<Device>(undefined, params)
325+
return this.client.getPagedItems<Device>(undefined, params,
326+
{headerOverrides: HEADER_OVERRIDES})
287327
}
288328

289329
/**
@@ -320,9 +360,20 @@ export class DevicesEndpoint extends Endpoint {
320360
/**
321361
* Returns a description of the specified device
322362
* @param id UUID of the device
363+
* @param options optional includeHealth and includeStatus parameters.
364+
* If the includeHealth option is set to true then the response will also contain
365+
* the health status of each device (i.e. if it is online or offline). If the includeStatus option is set to true
366+
* then the response will also include the status of all attributes (i.e. value and timestamp)
323367
*/
324-
public get(id: string): Promise<Device> {
325-
return this.client.get<Device>(id)
368+
public get(id: string, options: DeviceGetOptions = {}): Promise<Device> {
369+
const params: HttpClientParams = {}
370+
if ('includeHealth' in options && options.includeHealth !== undefined) {
371+
params.includeHealth = options.includeHealth.toString()
372+
}
373+
if ('includeStatus' in options && options.includeStatus !== undefined) {
374+
params.includeStatus = options.includeStatus.toString()
375+
}
376+
return this.client.get<Device>(id, params,{headerOverrides: HEADER_OVERRIDES})
326377
}
327378

328379
/**
@@ -382,8 +433,8 @@ export class DevicesEndpoint extends Endpoint {
382433
* @param data the new device profile
383434
*/
384435
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'}})
436+
return this.client.put<Device>(`${id}/profile`, data, undefined,
437+
{headerOverrides: HEADER_OVERRIDES})
387438
}
388439

389440
/**

test/unit/data/devices/get_devices.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const request = {
33
'method': 'get',
44
'headers': {
55
'Content-Type': 'application/json;charset=utf-8',
6-
'Accept': 'application/json',
6+
'Accept': 'application/vnd.smartthings+json;v=20170916',
77
'Authorization': 'Bearer 52991afa-66e8-4af0-8d85-5c568ed5ba7d',
88
},
99
'params': {},

test/unit/data/devices/get_devices_385931b6-0121-4848-bcc8-54cb76436de1.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ const request = {
33
'method': 'get',
44
'headers': {
55
'Content-Type': 'application/json;charset=utf-8',
6-
'Accept': 'application/json',
6+
'Accept': 'application/vnd.smartthings+json;v=20170916',
77
'Authorization': 'Bearer 52991afa-66e8-4af0-8d85-5c568ed5ba7d',
88
},
9+
'params': {},
910
}
1011
const response = {
1112
'deviceId': '385931b6-0121-4848-bcc8-54cb76436de1',

0 commit comments

Comments
 (0)