Schedules.js
2.84 KB
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
'use strict';
const schedulesApi = require('./http/endpoints/schedules')
, ApiDefinition = require('./http/ApiDefinition')
, util = require('../util')
;
/**
* @typedef {import('@peter-murray/hue-bridge-model').model.Schedule} Schedule
*
* @type {Schedules}
*/
module.exports = class Schedules extends ApiDefinition {
constructor(hueApi) {
super(hueApi);
}
/**
* Gets all the Schedules from the bridge.
* @returns {Promise<Array<Schedule>>} A Promise that will resolve to an Array of Schedules.
*/
getAll() {
return this.execute(schedulesApi.getAll);
}
/**
* @deprecated Use getSchedule(id) instead
* @param id {int | Schedule}
* @returns {Promise<Schedule>}
*/
get(id) {
util.deprecatedFunction('5.x', 'schedules.get(id)', 'Use schedules.getSchedule(id) instead.');
return this.getSchedule(id);
}
/**
* Gets a specific Schedule from the Bridge.
* @param id {int | Schedule} The id or Schedule instance to retrieve from the bridge.
* @returns {Promise<Schedule>} A Promise that will resolve to the actual schedule instance.
*/
getSchedule(id) {
return this.execute(schedulesApi.getScheduleAttributes, {id: id});
}
/**
*
* @param name
* @returns {Promise<Schedule[]>}
*/
getScheduleByName(name) {
return this.getAll()
.then(schedules => {
return schedules.filter(schedule => schedule.name === name);
});
}
/**
* Creates a new Schedule on the bridge.
* @param schedule {Schedule} The instance to create on the bridge.
* @returns {PromiseLike<Schedule> | Promise<Schedule>} The resultant Schedule instance that was created.
*/
createSchedule(schedule) {
const self = this;
return self.execute(schedulesApi.createSchedule, {schedule: schedule})
.then(result => {
return self.getSchedule(result.id);
});
}
/**
* Updates a Schedule.
* @param schedule {Schedule} The schedule with updated attributes to be saved to the Bridge.
* @returns {Promise<Object>} A promise that will resolve to an Object of the keys that were the attrubutes updated
* and a Boolean value that indicates if it was updated.
*/
updateSchedule(schedule) {
return this.execute(schedulesApi.setScheduleAttributes, {id: schedule, schedule: schedule});
}
// /**
// * @deprecated Use udpateSchedule(schedule) instead.
// * @param id
// * @param schedule
// * @returns {Promise<boolean>}
// */
// update(id, schedule) {
// return this.execute(schedulesApi.setScheduleAttributes, {id: id, schedule: schedule});
// }
/**
* Deletes an existing Schedule.
* @param id {int | Schedule} The id or Schedule instance to delete.
* @returns {Promise<boolean>} A Promise that will resolve to a boolean indicating success.
*/
deleteSchedule(id) {
return this.execute(schedulesApi.deleteSchedule, {id: id});
}
};