updateSchedule.js
2.35 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
'use strict';
const v3 = require('../../../index').v3
, model = v3.model
;
// If using this code outside of this library the above should be replaced with
// const v3 = require('node-hue-api').v3
// , model = v3.model
// ;
// Replace this with your username for accessing the bridge
const USERNAME = require('../../../test/support/testValues').username;
v3.discovery.nupnpSearch()
.then(searchResults => {
const host = searchResults[0].ipaddress;
return v3.api.createLocal(host).connect(USERNAME);
})
.then(api => {
//
// Create a new Schedule that we will then update in a subsequent call (so that we do not mess with any existing
// schedules in the bridge).
// Finally once finished, we will remove the schedule from the bridge
//
const schedule = model.createSchedule();
schedule.name = 'Test Schedule';
schedule.description = 'A test schedule from the node-hue-api examples';
schedule.recycle = true;
schedule.localtime = model.timePatterns.createTimer().hours(1);
schedule.command = model.actions.group(0).withState(new model.lightStates.GroupLightState().off());
return api.schedules.createSchedule(schedule)
.then(created => {
console.log(`\nCreated schedule in the bridge: ${created.id}`);
console.log(created.toStringDetailed());
return created;
})
.then(created => {
// Now update the localtime of the schedule to trigger in 2 hours and 30 minutes
created.localtime = model.timePatterns.createTimer().hours(2).minutes(30);
return api.schedules.updateSchedule(created)
.then(updatedAttributes => {
console.log('\nUpdated Schedule Attributes:');
console.log(JSON.stringify(updatedAttributes, null, 2));
// Get the details of the updated schedule
return api.schedules.getSchedule(created);
});
})
.then(updatedSchedule => {
// Displaying the details of the updated schedule
console.log(`\nAttributes of the Updated Schedule:`);
console.log(updatedSchedule.toStringDetailed());
return updatedSchedule;
})
.then(scheduleToRemove => {
// Now remove the scene we just created
return api.schedules.deleteSchedule(scheduleToRemove);
});
})
.catch(err => {
console.error(`Unexpected Error: ${err.message}`);
})
;