forked from SmartThingsCommunity/smartthings-core-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodes.ts
107 lines (95 loc) · 3.25 KB
/
modes.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
import { Endpoint } from '../endpoint'
import { EndpointClient, EndpointClientConfig } from '../endpoint-client'
import { Status, SuccessStatusValue } from '../types'
export interface ModeRequest {
/**
* A name provided by the User. Unique per location, updatable.
*/
label?: string
}
export interface Mode extends ModeRequest {
/**
*
* Globally unique id for the mode.
*/
id: string
/**
* A name provided when the mode was created. The name is unique per location, and can not be updated.
*/
name?: string
}
export class ModesEndpoint extends Endpoint {
constructor(config: EndpointClientConfig) {
super(new EndpointClient('locations', config))
}
/**
* Returns a list of the modes defined for a location
* @param locationId UUID of the location. If the client is configured with a locationId this parameter is
* not necessary.
*/
public list(locationId?: string): Promise<Mode[]> {
return this.client.getPagedItems<Mode>(`${this.locationId(locationId)}/modes`)
}
/**
* Returns a specific mode
* @param id UUID of the mode
* @param locationId UUID of the location. If the client is configured with a locationId this parameter is
* not necessary.
*/
public async get(id: string, locationId?: string): Promise<Mode> {
const list = await this.list(locationId)
if (list) {
const item = list.find(it => it.id === id)
if (item) {
return item
}
}
throw Error(`Mode ${id} not found`)
}
/**
* Returns the currently active mode of a location
* @param locationId UUID of the location. If the client is configured with a locationId this parameter is
* not necessary.
*/
public getCurrent(locationId?: string): Promise<Mode> {
return this.client.get<Mode>(`${this.locationId(locationId)}/modes/current`)
}
/**
* Sets the currently active mode of a location
* @param id UUID of the mode
* @param locationId UUID of the location. If the client is configured with a locationId this parameter is
* not necessary.
*/
public setCurrent(id: string, locationId?: string): Promise<Mode> {
return this.client.put(`${this.locationId(locationId)}/modes/current`, {modeId: id})
}
/**
* Create a new mode in a location
* @param data definition specifying the name of the new mode
* @param locationId UUID of the location. If the client is configured with a locationId this parameter is
* not necessary.
*/
public create(data: ModeRequest, locationId?: string): Promise<Mode> {
return this.client.post(`${this.locationId(locationId)}/modes`, data)
}
/**
* Updates the name of a mode
* @param id UUID of the mode
* @param data definition specifying the new mode name
* @param locationId UUID of the location. If the client is configured with a locationId this parameter is
* not necessary.
*/
public update(id: string, data: ModeRequest, locationId?: string): Promise<Mode> {
return this.client.put(`${this.locationId(locationId)}/modes/${id}`, data)
}
/**
* Delete a mode
* @param id UUID of the mode
* @param locationId UUID of the location. If the client is configured with a locationId this parameter is
* not necessary.
*/
public async delete(id: string, locationId?: string): Promise<Status> {
await this.client.delete(`${this.locationId(locationId)}/modes/${id}`)
return SuccessStatusValue
}
}