-
Notifications
You must be signed in to change notification settings - Fork 54
/
devices.ts
587 lines (522 loc) · 16.8 KB
/
devices.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
import { Endpoint } from '../endpoint'
import { EndpointClient, EndpointClientConfig, HttpClientParams } from '../endpoint-client'
import { ConfigEntry} from './installedapps'
import { Links, Status, SuccessStatusValue } from '../types'
import {PresentationDevicePresentation} from './presentation'
export interface CapabilityReference {
id: string
version?: number
}
export interface Component {
id?: string // <^[-_!.~'()*0-9a-zA-Z]{1,36}$>
label?: string
capabilities: CapabilityReference[]
}
export interface AppDeviceDetails {
installedAppId?: string // <^[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}$>
externalId?: string // <= 64 characters
profileId?: string
}
export enum DeviceNetworkSecurityLevel {
UNKNOWN = 'UNKNOWN',
ZWAVE_LEGACY_NON_SECURE = 'ZWAVE_LEGACY_NON_SECURE',
ZWAVE_S0_LEGACY = 'ZWAVE_S0_LEGACY',
ZWAVE_S0_FALLBACK = 'ZWAVE_S0_FALLBACK',
ZWAVE_S2_UNAUTHENTICATED = 'ZWAVE_S2_UNAUTHENTICATED',
ZWAVE_S2_AUTHENTICATED = 'ZWAVE_S2_AUTHENTICATED',
ZWAVE_S2_ACCESS_CONTROL = 'ZWAVE_S2_ACCESS_CONTROL',
ZWAVE_S2_FAILED = 'ZWAVE_S2_FAILED',
ZWAVE_S0_FAILED = 'ZWAVE_S0_FAILED',
ZWAVE_S2_DOWNGRADE = 'ZWAVE_S2_DOWNGRADE',
ZWAVE_S0_DOWNGRADE = 'ZWAVE_S0_DOWNGRADE'
}
export interface DthDeviceDetails {
deviceTypeId: 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}))$>
deviceTypeName: string
deviceNetworkType?: string
completedSetup: boolean
networkSecurityLevel?: DeviceNetworkSecurityLevel
hubId?: string
installedGroovyAppId?: 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}))$>
}
export interface IrDeviceDetails {
parentDeviceId?: string // <^[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}$>
profileId?: string // <^[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}$>
ocfDeviceType?: string
irCode?: string
functionCodes?: { default: string }
childDevices?: IrDeviceDetails[]
metadata?: object
}
export interface ViperDeviceDetails {
uniqueIdentifier?: string
manufacturerName?: string
modelName?: string
swVersion?: string
hwVersion?: string
}
export enum DeviceIntegrationType {
BLE = 'BLE',
BLE_D2D = 'BLE_D2D',
DTH = 'DTH',
ENDPOINT_APP = 'ENDPOINT_APP',
HUB = 'HUB',
IR = 'IR',
IR_OCF = 'IR_OCF',
MQTT = 'MQTT',
OCF = 'OCF',
PENGYOU = 'PENGYOU',
VIDEO = 'VIDEO',
VIPER = 'VIPER',
WATCH = 'WATCH',
}
export interface ProfileIdentifier {
id: string
}
export interface Device {
deviceId?: string
name?: string
label?: string
manufacturerName?: string
presentationId?: string
deviceManufacturerCode?: string
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}))$>
roomId?: 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}))$>
deviceTypeId?: 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}))$>
components?: Component[]
childDevices?: Device[]
profile?: ProfileIdentifier
app?: AppDeviceDetails
dth?: DthDeviceDetails
ir?: IrDeviceDetails
irOcf?: IrDeviceDetails
viper?: ViperDeviceDetails
type?: DeviceIntegrationType
restrictionTier?: number
}
export interface DeviceUpdate {
label?: string
}
export interface DeviceCreate {
label?: string
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}))$>
app?: AppDeviceDetails
profileId?: undefined
}
export interface AlternateDeviceCreate {
label?: string
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}))$>
installedAppId?: string // <^[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}$>
externalId?: string // <= 64 characters
profileId?: string
app?: undefined
}
export interface DeviceList {
items: Device[]
_links: Links
}
interface AttributeState {
value?: unknown
unit?: string
data?: { [name: string]: object }
timestamp?: string // date-time ("Will always be 0 time-zone offset" whatever that means)
}
export interface CapabilityStatus {
[attributeName: string]: AttributeState
}
export interface ComponentStatus {
[attributeName: string]: CapabilityStatus
}
export interface DeviceStatus {
components?: { [componentId: string]: ComponentStatus }
}
export interface DeviceEvent {
component: string
capability: string
attribute: string
value: unknown
unit?: string
data?: { [name: string]: object }
}
export interface DeviceEventList {
deviceEvents: DeviceEvent[]
}
export enum DeviceHealthState {
ONLINE = 'ONLINE',
OFFLINE = 'OFFLINE',
UNKNOWN = 'UNKNOWN',
}
export interface DeviceHealth {
deviceId: string
state: DeviceHealthState
lastUpdatedDate?: string
}
export interface Command {
component: string
capability: string
command: string
arguments?: (object | string | number)[]
}
export interface CommandRequest {
capability: string
command: string
arguments?: (object | string | number)[]
}
export interface CommandList {
commands: Command[]
}
export interface DeviceListOptions {
/**
* Capability ID (for example, 'switchLevel') or array of capability IDs
*/
capability?: string | string[]
/**
* Whether to AND or OR the capability IDs when more than one is specified
*/
capabilitiesMode?: 'and' | 'or'
/**
* Location UUID or array of location UUIDs
*/
locationId?: string | string[]
/**
* Device UUID or array of device UUIDs
*/
deviceId?: string | string[]
/**
* Limit the number of results to this value. By default all devices are returned
*/
max?: number
/**
* Page number for when a max number of results has been specified, starting with 1
*/
page?: number
/**
* Device Type
*/
type?: DeviceIntegrationType | DeviceIntegrationType[]
}
export interface HueSaturation {
hue: number
saturation: number
}
export class DevicesEndpoint extends Endpoint {
constructor(config: EndpointClientConfig) {
super(new EndpointClient('devices', config))
}
/**
* Returns a list of devices matching the query options or all devices accessible by the principal (i.e. user)
* if no options are specified.
*
* @param options query options, capability, capabilitiesMode ('and' or 'or'), locationId, deviceId. These can
* be single values or arrays.
*/
public async list(options: DeviceListOptions = {}): Promise<Device[]> {
const params: HttpClientParams = {}
if ('capability' in options && options.capability) {
params.capability = options.capability
}
if ('capabilitiesMode' in options && options.capabilitiesMode) {
params.capabilitiesMode = options.capabilitiesMode
}
if ('locationId' in options && options.locationId) {
params.locationId = options.locationId
} else if (this.client.config.locationId) {
params.locationId = this.client.config.locationId
}
if ('deviceId' in options && options.deviceId) {
params.deviceId = options.deviceId
}
if ('max' in options && options.max) {
params.max = options.max
}
if ('page' in options && options.page) {
params.page = options.page
}
if ('type' in options && options.type) {
params.type = options.type
}
return this.client.getPagedItems<Device>(undefined, params)
}
/**
* Returns all devices in the location specified in the client configuration. Throws an error if no location is
* specified in the client config. For use only in SmartApps.
* @deprecated use list() instead
*/
public listInLocation(): Promise<Device[]> {
if (this.client.config.locationId) {
return this.list({locationId: this.client.config.locationId})
}
return Promise.reject(Error('Location ID not defined'))
}
/**
* Returns all devices accessible by the principal (i.e. user)
* @deprecated use list() instead
*/
public listAll(): Promise<Device[]> {
return this.list()
}
/**
* Returns devices with the specified capability.
* @deprecated use list({capability: 'switch'} instead
*/
public findByCapability(capability: string): Promise<Device[]> {
if (this.client.config.locationId) {
return this.list({locationId: this.locationId(), capability: capability})
}
return Promise.reject(Error('Location ID not defined'))
}
/**
* Returns a description of the specified device
* @param id UUID of the device
*/
public get(id: string): Promise<Device> {
return this.client.get<Device>(id)
}
/**
* Deletes the specified device
* @param id UUID of the device
*/
public delete(id: string): Promise<Device> {
return this.client.delete<Device>(id)
}
/**
* Install a device.
* @param definition the device definition. If the client configuration specifies a locationId and installedAppId
* then these values don't need to be included in the definition.
*/
public create(definition: DeviceCreate | AlternateDeviceCreate): Promise<Device> {
let data
if (definition.app) {
data = {
label: definition.label,
locationId: this.locationId(definition.locationId),
app: {
installedAppId: this.installedAppId(),
...definition.app,
},
}
} else if (definition.profileId) {
data = {
label: definition.label,
locationId: this.locationId(definition.locationId),
app: {
installedAppId: this.installedAppId(definition.installedAppId),
profileId: definition.profileId,
externalId: definition.externalId,
},
}
}
return this.client.post<Device>('', data)
}
/**
* Update a device. Currently only the device label can be changed
* @param id UUID of the device
* @param data new device definition with the label specified
*/
public update(id: string, data: DeviceUpdate): Promise<Device> {
return this.client.put<Device>(id, data)
}
/**
* Returns the current values of all device attributes
* @param id UUID of the device
*/
public getStatus(id: string): Promise<DeviceStatus> {
return this.client.get<DeviceStatus>(`${id}/status`)
}
/**
* Returns the current values of all device attributes
* @deprecated use getStatus instead
*/
public getState(id: string): Promise<DeviceStatus> {
return this.client.get<DeviceStatus>(`${id}/status`)
}
/**
* Gets the attribute values of the specified component of the device
* @param id UUID of the device
* @param componentId alphanumeric component ID, e.g. 'main'
*/
public getComponentStatus(id: string, componentId: string): Promise<ComponentStatus> {
return this.client.get<ComponentStatus>(`${id}/components/${componentId}/status`)
}
/**
* Gets the attribute values of the specified component of the device
* @deprecated use getComponentStatus instead
*/
public getComponentState(id: string, componentId: string): Promise<ComponentStatus> {
return this.client.get<ComponentStatus>(`${id}/components/${componentId}/status`)
}
/**
* Gets the attribute values of the specified component capability
* @param id UUID of the device
* @param componentId alphanumeric component ID, e.g. 'main'
* @param capabilityId alphanumeric capability ID, e.g. 'switchLevel'
*/
public getCapabilityStatus(id: string, componentId: string, capabilityId: string): Promise<CapabilityStatus> {
return this.client.get<CapabilityStatus>(`${id}/components/${componentId}/capabilities/${capabilityId}/status`)
}
/**
* Gets the attribute values of the specified ccomponent capability
* @deprecated use getCapabilityStatus instead
*/
public getCapabilityState(id: string, componentId: string, capabilityId: string): Promise<CapabilityStatus> {
return this.client.get<CapabilityStatus>(`${id}/components/${componentId}/capabilities/${capabilityId}/status`)
}
/**
* Returns the health status of the device
* @param id UUID of the device
*/
public getHealth(id: string): Promise<DeviceHealth> {
return this.client.get<DeviceHealth>(`${id}/health`).catch(reason => {
if (reason.statusCode === 404) {
return {
deviceId: id,
state: DeviceHealthState.UNKNOWN,
}
}
return Promise.reject(reason)
})
}
/**
* Sends the specified list of commands to the device
* @param id UUID of the device
* @param commands list of commands
*/
public async executeCommands(id: string, commands: Command[]): Promise<Status> {
await this.client.post(`${id}/commands`, { commands })
return SuccessStatusValue
}
/**
* Sends the specified command to the device
* @param id UUID of the device
* @param command a single device command
*/
public async executeCommand(id: string, command: Command): Promise<Status> {
await this.executeCommands(id, [command])
return SuccessStatusValue
}
/**
* Sends the specified commands to the device
* @deprecated use executeCommands instead
*/
public async postCommands(id: string, commands: CommandList): Promise<Status> {
await this.client.post(`${id}/commands`, commands)
return SuccessStatusValue
}
/**
* Sends the specified command or commands to the device and component defined in the specified config entry. The
* end result is the same as calling the executeCommand method, but this method accepts a SmartApp config entry
* for convenience
* @param item installedApp config entry specifying the device UUID and component
* @param capabilityIdOrCmdList either a capability ID or list of commands. If a list of commands is specified
* then the command and args parameters are not required.
* @param command the command name. Required when a capability ID has been specified in the previous parameter
* @param args list of arguments. Required when a capability ID has been specified and the command has arguments
*/
public async sendCommand(item: ConfigEntry, capabilityIdOrCmdList: string | CommandRequest[], command?: string, args?: (object | string | number)[]): Promise<Status> {
let commands
const {deviceConfig} = item
if (deviceConfig) {
if (Array.isArray(capabilityIdOrCmdList)) {
commands = capabilityIdOrCmdList.map(it => {
return {
component: deviceConfig.componentId,
capability: it.capability,
command: it.command,
arguments: it.arguments || [],
}
})
} else {
commands = [
{
component: deviceConfig.componentId,
capability: capabilityIdOrCmdList,
command,
arguments: args || [],
},
]
}
const body = {commands}
await this.client.post(`${deviceConfig.deviceId}/commands`, body)
return SuccessStatusValue
}
return Promise.reject(Error('Device config not found'))
}
/**
* Sends the specified command or commands to the devices and components defined in the specified config entry list. The
* end result is the same as calling the executeCommand method, but this method accepts a SmartApp config entry
* for convenience
* @param items a list of the installedApp config entries specifying device UUIDs and component IDs
* @param capabilityIdOrCmdList either a capability ID or list of commands. If a list of commands is specified
* then the command and args parameters are not required.
* @param command the command name. Required when a capability ID has been specified in the previous parameter
* @param args list of arguments. Required when a capability ID has been specified and the command has arguments
*/
public sendCommands(items: ConfigEntry[], capabilityIdOrCmdList: string | CommandRequest[],
command: string, args?: (object | string | number)[]): Promise<Status[]> {
const results = []
if (items) {
for (const it of items) {
results.push(this.sendCommand(it, capabilityIdOrCmdList, command, args))
}
}
return Promise.all(results)
}
/**
* Creates events for the specified device
* @param id UUID of the device
* @param deviceEvents list of events
*/
public async createEvents(id: string, deviceEvents: DeviceEvent[]): Promise<Status> {
await this.client.post(`${id}/events`, { deviceEvents })
return SuccessStatusValue
}
/**
* @deprecated use createEvents instead
* @param id
* @param deviceEvents
*/
public sendEvents(id: string, deviceEvents: DeviceEventList): void {
this.client.post(`${id}/events`, deviceEvents)
}
/**
* Get a device presentation. If mnmn is omitted the default SmartThingsCommunity mnmn is used.
* @param deviceId UUID of the device
*/
public getPresentation(deviceId: string): Promise<PresentationDevicePresentation> {
return this.client.get<PresentationDevicePresentation>('/presentation', { deviceId })
}
/**
* Convenience function that returns hue and saturation of the named color
* @deprecated
*/
public namedColor(color: string, sat = 100): HueSaturation {
let hueColor = 0
const saturation = sat
switch (color) {
case 'Blue':
hueColor = 70
break
case 'Green':
hueColor = 39
break
case 'Yellow':
hueColor = 25
break
case 'Orange':
hueColor = 10
break
case 'Purple':
hueColor = 75
break
case 'Pink':
hueColor = 83
break
case 'Red':
hueColor = 100
break
default:
hueColor = 0
}
return {hue: hueColor, saturation}
}
}