Advertisement
nurChrisYT

SzenenSkript

Jun 22nd, 2019
1,281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. var HueApi = require("node-hue-api").HueApi;
  2.  
  3. // Replace IP and username!!! IP=Bridge IP/Username=aus dem hue controller kopieren!!!
  4. var host = "192.168.10.23",
  5. username = "ytTzQ4FfD2QNqXRW5jpgnHMA-GcLshxXmr-oiSuT",
  6. api = new HueApi(host, username);
  7.  
  8. var groups_ = [],
  9. lights_ = [],
  10. objects_ = [];
  11.  
  12. // Log JSON results
  13. var displayResults = function(result) {
  14. console.log('Reponse: '+JSON.stringify(result, null, 2));
  15. };
  16.  
  17. // Parse Light Group 0 (All Lights)
  18. var parseGroup0 = function(result) {
  19. if (!result.lights){return} // Empty group
  20.  
  21. var id = result.id,
  22. lights = result.lights,
  23. name = "All Lights";
  24. console.debug('group: '+name+', lights: '+lights);
  25. groups_[lights] = name;
  26. };
  27.  
  28. // Parse Light Groups
  29. var parseGroups = function(result) {
  30. for (var i = 0; i < result.length; i++) {
  31. if (!result[i].lights){continue} // Empty group
  32.  
  33. var id = result[i].id,
  34. lights = result[i].lights,
  35. name = result[i].name;
  36. console.debug('group: '+name+', lights: '+lights);
  37. groups_[lights] = name;
  38. }
  39. };
  40.  
  41. // Parse Lights
  42. var parseLights = function(result) {
  43. for (var i = 0; i < result.length; i++) {
  44. var id = result[i].id,
  45. name = result[i].name;
  46. console.debug('light: '+name+', id: '+id);
  47. lights_[id] = name;
  48. }
  49. };
  50.  
  51. // Create States in ioBroker
  52. var createStates = function(result) {
  53. // Resync button
  54. createState('PhilipsHue.Scenes.Resync', false, {role: "button", name: 'Resync Philips Hue Groups, Lights and Scenes'});
  55.  
  56. for (var i = 0; i < result.length; i++) {
  57. if (!result[i].appdata.data){continue} // skip internal szenes
  58.  
  59. var id = result[i].id,
  60. lights = result[i].lights,
  61. name = result[i].name.replace(/"/g,''),
  62. pathname = name.replace(/ /g,'_');
  63.  
  64. // Get light names
  65. var light_names = [];
  66. for (var j = 0; j < lights.length; j++) {
  67. var light_name = lights_[lights[j]];
  68. light_names.push(light_name);
  69. }
  70.  
  71. // Room, group or lights linked with scene
  72. var group = 'Group: '+groups_[lights] || 'Lights: '+light_names.join(", ");
  73.  
  74. // Create States and skip duplicates
  75. if (!objects_[lights+pathname]){
  76. console.debug('scene: '+name+', '+group);
  77. createState('PhilipsHue.Scenes.'+pathname+'.'+id, false, {role: "button", name: 'Scene: '+name+' ('+group+')'});
  78. objects_[lights+pathname] = true;
  79. }
  80. }
  81. };
  82.  
  83. // Delete States
  84. function deleteStates(){
  85. console.log('Deleting current objects for scenes...');
  86. objects_ = [];
  87. $('javascript.0.PhilipsHue.Scenes.*').each(function (id) {
  88. deleteState(id);
  89. });
  90. }
  91.  
  92. // Fetch data from Hue API
  93. function init(){
  94. api.getGroup(0, function(err, group0) {
  95. if (err) throw err;
  96. console.log('Processing group 0...');
  97. //displayResults(group0);
  98. parseGroup0(group0);
  99. });
  100. api.groups(function(err, groups) {
  101. if (err) throw err;
  102. console.log('Processing ' + groups.length + ' groups...');
  103. //displayResults(groups);
  104. parseGroups(groups);
  105. });
  106.  
  107. api.lights(function(err, lights) {
  108. if (err) throw err;
  109. console.log('Processing ' + lights.lights.length + ' lights...');
  110. //displayResults(lights);
  111. parseLights(lights.lights);
  112. });
  113.  
  114. api.scenes(function(err, scenes) {
  115. if (err) throw err;
  116. console.log('Processing ' + scenes.length + ' scenes...');
  117. //displayResults(scenes);
  118. createStates(scenes);
  119. });
  120. }
  121.  
  122. // Init on start
  123. init();
  124.  
  125. // Activate scene
  126. on({id: /^javascript\.0\.PhilipsHue.Scenes\./, val: true}, function (obj) {
  127. if (obj.id == 'javascript.0.PhilipsHue.Scenes.Resync'){return}
  128. sceneId = obj.id.split('.').pop();
  129. console.log('Activating '+obj.name);
  130. api.activateScene(sceneId, function(err, result) {
  131. if (err) throw err;
  132. displayResults(result);
  133. });
  134. setState(obj.id, false);
  135. });
  136.  
  137. // Resync
  138. on({id: 'javascript.0.PhilipsHue.Scenes.Resync', val: true}, function (obj) {
  139. console.log('Resync triggered...');
  140. groups_ = [];
  141. lights_ = [];
  142. deleteStates();
  143. init();
  144. });
  145.  
  146. schedule("0 3 * * *", function () {
  147. console.log('Resync triggered...');
  148. groups_ = [];
  149. lights_ = [];
  150. deleteStates();
  151. init();
  152. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement