@@ -95,7 +95,9 @@ function LightItem(lightID, lifxLight, config) {
9595 address : lifxLight . address ,
9696 label : lifxLight . label ,
9797 reachable : true ,
98+ messageHandlerTimeout : 5000 ,
9899 maxIRLevel : 0 , // Only valid for IR capable lights
100+ getMaxIR_Error : false , // do we get an error when querying for maxIRLevel
99101 }
100102
101103 // Default state
@@ -151,7 +153,14 @@ LightItem.prototype.initialize = function initialize(callback) {
151153 ( done ) => {
152154 if ( ! ( capability & LightCapability . INFRARED ) )
153155 return done ( null , null ) ;
154- self . lifx . getMaxIR ( done ) ;
156+ self . lifx . getMaxIR ( ( err , data ) => {
157+ // getMaxIR generates an error, stop using it and use static value
158+ if ( err ) {
159+ self . info . getMaxIR_Error = true ;
160+ data = { brightness : self . info . maxIRLevel } ;
161+ }
162+ done ( null , data ) ;
163+ } ) ;
155164 } ,
156165
157166 ] , ( err , data ) => {
@@ -171,8 +180,11 @@ LightItem.prototype.initialize = function initialize(callback) {
171180 self . state = convertLifxState ( lifxInfo ) ;
172181
173182 // Infrared
174- if ( self . info . capability | LightCapability . INFRARED )
175- self . info . maxIRLevel = lifxMaxIR ;
183+ if ( self . info . capability | LightCapability . INFRARED &&
184+ _ . isPlainObject ( lifxMaxIR ) &&
185+ _ . isFinite ( lifxMaxIR . brightness ) ) {
186+ self . info . maxIRLevel = limitValue ( Math . round ( lifxMaxIR . brightness ) , 0 , 100 ) ;
187+ }
176188
177189 // Update label
178190 self . info . label = lifxInfo . label ;
@@ -201,10 +213,15 @@ LightItem.prototype.stop = function stop() {
201213
202214/**
203215 * Poll light for changes
216+ * @param {?function } callback Optional function to call when done
204217 */
205- LightItem . prototype . pollChanges = function pollChanges ( ) {
218+ LightItem . prototype . pollChanges = function pollChanges ( callback ) {
206219 var self = this ;
207220
221+ // Ensure callback is an function
222+ if ( typeof callback !== 'function' )
223+ callback = function ( ) { }
224+
208225 async . series ( [
209226 // Get state info
210227 ( done ) => {
@@ -213,21 +230,37 @@ LightItem.prototype.pollChanges = function pollChanges() {
213230
214231 // Get IR info (if we have the capability)
215232 ( done ) => {
233+ // Check capability
216234 if ( ! ( self . info . capability & LightCapability . INFRARED ) )
217235 return done ( null , null ) ;
218- self . lifx . getMaxIR ( done ) ;
236+
237+ // Check if getMaxIR returns error
238+ if ( ! self . info . getMaxIR_Error )
239+ return done ( null , { brightness : self . info . maxIRLevel } ) ;
240+
241+ // Get IR level
242+ self . lifx . getMaxIR ( ( err , data ) => {
243+ // Ignore errors
244+ if ( err ) {
245+ data = { brightness : self . info . maxIRLevel } ;
246+ }
247+ done ( null , data ) ;
248+ } ) ;
219249 }
220250 ] , ( err , data ) => {
221251 if ( err ) {
252+ callback ( err ) ;
222253 return ;
223254 }
224255
225256 var lifxState = data [ 0 ] ;
226257 var lifxMaxIR = data [ 1 ] ;
227258
228259 // We need to ignore the changes for this light until modified value has expired
229- if ( self . modified >= process . uptime ( ) )
260+ if ( self . modified >= process . uptime ( ) ) {
261+ callback ( ) ;
230262 return ;
263+ }
231264
232265 var newState = convertLifxState ( lifxState ) ;
233266 var isUpdated = false ;
@@ -237,9 +270,11 @@ LightItem.prototype.pollChanges = function pollChanges() {
237270 if ( isUpdated )
238271 self . state = newState ;
239272
240- if ( self . info . capability & LightCapability . INFRARED && _ . isFinite ( lifxMaxIR ) ) {
241- isUpdated = ( self . info . maxIRLevel !== lifxMaxIR ) || isUpdated ;
242- self . info . maxIRLevel = limitValue ( Math . round ( lifxMaxIR ) , 0 , 100 ) ;
273+ if ( self . info . capability & LightCapability . INFRARED &&
274+ _ . isPlainObject ( lifxMaxIR ) &&
275+ _ . isFinite ( lifxMaxIR . brightness ) ) {
276+ isUpdated = ( self . info . maxIRLevel !== lifxMaxIR . lifxMaxIR ) || isUpdated ;
277+ self . info . maxIRLevel = limitValue ( Math . round ( lifxMaxIR . brightness ) , 0 , 100 ) ;
243278 }
244279
245280 // Copy label
@@ -248,6 +283,8 @@ LightItem.prototype.pollChanges = function pollChanges() {
248283 // Values/state has been updated
249284 if ( isUpdated )
250285 self . emit ( 'update' ) ;
286+
287+ callback ( ) ;
251288 } ) ;
252289}
253290
0 commit comments