-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHue Szenen.js
152 lines (130 loc) · 4.24 KB
/
Hue Szenen.js
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
var HueApi = require("node-hue-api").HueApi;
// Replace IP and username!!!
var host = "192.168.x.x",
username = "xxxxxxxxxx",
api = new HueApi(host, username);
var groups_ = [],
lights_ = [],
objects_ = [];
// Log JSON results
var displayResults = function(result) {
console.log('Reponse: '+JSON.stringify(result, null, 2));
};
// Parse Light Group 0 (All Lights)
var parseGroup0 = function(result) {
if (!result.lights){return} // Empty group
var id = result.id,
lights = result.lights,
name = "All Lights";
console.debug('group: '+name+', lights: '+lights);
groups_[lights] = name;
};
// Parse Light Groups
var parseGroups = function(result) {
for (var i = 0; i < result.length; i++) {
if (!result[i].lights){continue} // Empty group
var id = result[i].id,
lights = result[i].lights,
name = result[i].name;
console.debug('group: '+name+', lights: '+lights);
groups_[lights] = name;
}
};
// Parse Lights
var parseLights = function(result) {
for (var i = 0; i < result.length; i++) {
var id = result[i].id,
name = result[i].name;
console.debug('light: '+name+', id: '+id);
lights_[id] = name;
}
};
// Create States in ioBroker
var createStates = function(result) {
// Resync button
createState('PhilipsHue.Scenes.Resync', false, {role: "button", name: 'Resync Philips Hue Groups, Lights and Scenes'});
for (var i = 0; i < result.length; i++) {
if (!result[i].appdata.data){continue} // skip internal szenes
var id = result[i].id,
lights = result[i].lights,
name = result[i].name.replace(/"/g,''),
pathname = name.replace(/ /g,'_');
// Get light names
var light_names = [];
for (var j = 0; j < lights.length; j++) {
var light_name = lights_[lights[j]];
light_names.push(light_name);
}
// Room, group or lights linked with scene
var group = 'Group: '+groups_[lights] || 'Lights: '+light_names.join(", ");
// Create States and skip duplicates
if (!objects_[lights+pathname]){
console.debug('scene: '+name+', '+group);
createState('PhilipsHue.Scenes.'+pathname+'.'+id, false, {role: "button", name: 'Scene: '+name+' ('+group+')'});
objects_[lights+pathname] = true;
}
}
};
// Delete States
function deleteStates(){
console.log('Deleting current objects for scenes...');
objects_ = [];
$('javascript.0.PhilipsHue.Scenes.*').each(function (id) {
deleteState(id);
});
}
// Fetch data from Hue API
function init(){
api.getGroup(0, function(err, group0) {
if (err) throw err;
console.log('Processing group 0...');
//displayResults(group0);
parseGroup0(group0);
});
api.groups(function(err, groups) {
if (err) throw err;
console.log('Processing ' + groups.length + ' groups...');
//displayResults(groups);
parseGroups(groups);
});
api.lights(function(err, lights) {
if (err) throw err;
console.log('Processing ' + lights.lights.length + ' lights...');
//displayResults(lights);
parseLights(lights.lights);
});
api.scenes(function(err, scenes) {
if (err) throw err;
console.log('Processing ' + scenes.length + ' scenes...');
//displayResults(scenes);
createStates(scenes);
});
}
// Init on start
init();
// Activate scene
on({id: /^javascript\.0\.PhilipsHue.Scenes\./, val: true}, function (obj) {
if (obj.id == 'javascript.0.PhilipsHue.Scenes.Resync'){return}
sceneId = obj.id.split('.').pop();
console.log('Activating '+obj.name);
api.activateScene(sceneId, function(err, result) {
if (err) throw err;
displayResults(result);
});
setState(obj.id, false);
});
// Resync
on({id: 'javascript.0.PhilipsHue.Scenes.Resync', val: true}, function (obj) {
console.log('Resync triggered...');
groups_ = [];
lights_ = [];
deleteStates();
init();
});
schedule("0 3 * * *", function () {
console.log('Resync triggered...');
groups_ = [];
lights_ = [];
deleteStates();
init();
});