Skip to content

Commit 7064d62

Browse files
author
QuickSander
committed
fix: Brightness retrieval without brightness config via RGB
1 parent 704d459 commit 7064d62

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ Following configuration is a real world example for a accessory that combines a
175175
"url":"http://192.168.0.120/api/<apikey>/lights/6/state",
176176
"body": "{\"bri\": %s}"
177177
},
178-
"http_method": "`PUT`",
178+
"http_method": "PUT",
179179
"max": 254
180180
},
181181
"color": {

index.js

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -381,12 +381,32 @@ HttpPushRgb.prototype = {
381381

382382
level = parseInt(100 / this.brightness.max * level);
383383

384-
this.log('brightness is currently at %s %', level);
384+
this.log('brightness is currently at %s%', level);
385385
callback(null, level);
386386
}
387387
}.bind(this));
388388
} else {
389-
callback(null, this.cache.brightness);
389+
if(this.color.brightness) {
390+
var url = this.color.get_url.url;
391+
this._httpRequest(url, '', 'GET', function(error, response, responseBody) {
392+
if (!this._handleHttpErrorResponse('getBrightness()', error, response, responseBody, callback)) {
393+
var rgb = responseBody;
394+
var levels = this._rgbToHsl(
395+
parseInt(rgb.substr(0,2),16),
396+
parseInt(rgb.substr(2,2),16),
397+
parseInt(rgb.substr(4,2),16)
398+
);
399+
400+
var brightness = levels[2];
401+
402+
this.log('... brightness is currently %s. RGB: %s', brightness, rgb);
403+
this.cache.brightness = brightness;
404+
callback(null, brightness);
405+
}
406+
}.bind(this));
407+
} else {
408+
callback(null, this.cache.brightness);
409+
}
390410
}
391411
},
392412

@@ -401,6 +421,8 @@ HttpPushRgb.prototype = {
401421
callback(new Error("No 'brightness' defined in configuration"));
402422
return;
403423
}
424+
425+
this.log('Caching Brightness as %s ...', level);
404426
this.cache.brightness = level;
405427

406428
// If achromatic or color.brightness is false, update brightness, otherwise, update HSL as RGB
@@ -412,11 +434,12 @@ HttpPushRgb.prototype = {
412434

413435
this._httpRequest(url, body, this.brightness.http_method, function(error, response, responseBody) {
414436
if (!this._handleHttpErrorResponse('setBrightness()', error, response, responseBody, callback)) {
415-
this.log('setBrightness() successfully set to %s %', level);
437+
this.log('setBrightness() successfully set to %s%', level);
416438
callback();
417439
}
418440
}.bind(this));
419441
} else {
442+
this.log("Setting brightness via RGB.");
420443
this._setRGB(callback);
421444
}
422445
},
@@ -445,7 +468,7 @@ HttpPushRgb.prototype = {
445468

446469
var hue = levels[0];
447470

448-
this.log('... hue is currently %s', hue);
471+
this.log('... hue is currently %s. RGB: %s', hue, rgb);
449472
this.cache.hue = hue;
450473
callback(null, hue);
451474
}
@@ -497,7 +520,7 @@ HttpPushRgb.prototype = {
497520

498521
var saturation = levels[1];
499522

500-
this.log('... saturation is currently %s', saturation);
523+
this.log('... saturation is currently %s. RGB: %s', saturation, rgb);
501524
this.cache.saturation = saturation;
502525
callback(null, saturation);
503526
}
@@ -548,12 +571,11 @@ HttpPushRgb.prototype = {
548571
var xyz = convert.rgb.xyz(rgb);
549572
var hex = convert.rgb.hex(rgb);
550573

551-
if(xyz == null || xyz.size == 0){
552-
this.log.warn("Can't read the brightness property! Ignoring the request");
574+
if(xyz == null || xyz.size == 0) {
575+
this.log.error("Failed to convert HSB to xyz values. Cached values: H:%s S:%s B:%s", this.cache.hue, this.cache.saturation, this.cache.brightness);
553576
return {url: '', body: ''};
554577
}
555578

556-
557579
var xy = {
558580
x: (xyz[0] / 100 / (xyz[0] / 100 + xyz[1] / 100 + xyz[2] / 100)).toFixed(4),
559581
y: (xyz[1] / 100 / (xyz[0] / 100 + xyz[1] / 100 + xyz[2] / 100)).toFixed(4)

test/basics.test.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ describe('Set brightness', function () {
288288

289289
// 3. Assert
290290
expect(this.homebridgeCallback.calledOnce).to.be.true;
291-
expect(this.homebridgeStub.logger.firstCall.args).deep.equals(['setBrightness() successfully set to %s %', 100]);
291+
expect(this.homebridgeStub.logger.firstCall.args).deep.equals(['Caching Brightness as %s ...', 100]);
292+
expect(this.homebridgeStub.logger.secondCall.args).deep.equals(['setBrightness() successfully set to %s%', 100]);
292293
});
293294

294295
it('sets RGB instead of brightness to Homebridge when config.color.brightness equals true', function () {
@@ -303,8 +304,12 @@ describe('Set brightness', function () {
303304

304305
// 3. Assert
305306
expect(this.homebridgeCallback.calledOnce).to.be.true; // But now inside _buildRgbRequest.
306-
expect(this.homebridgeStub.logger.firstCall.args[0]).deep.equals('_buildRgbRequest converting H:%s S:%s B:%s to RGB:%s ...');
307-
expect(this.homebridgeStub.logger.secondCall.args).deep.equals(['... _setRGB() successfully set']);
307+
expect(this.homebridgeStub.logger.secondCall.args).deep.equals(['Setting brightness via RGB.']);
308+
expect(this.homebridgeStub.logger.firstCall.args).deep.equals(['Caching Brightness as %s ...', 100]);
309+
expect(this.homebridgeStub.logger.thirdCall.args[0]).deep.equals('_buildRgbRequest converting H:%s S:%s B:%s to RGB:%s ...');
310+
expect(this.homebridgeStub.logger.getCall(3).args).deep.equals(['... _setRGB() successfully set']);
311+
expect(this.homebridgeStub.accessory._httpRequest.firstCall.args[0]).not.to.be.empty;
312+
308313
});
309314

310315
it('replies an Error object with message "Received HTTP error code 500." to Homebridge on HTTP GET device response status code 500', function () {
@@ -319,7 +324,8 @@ describe('Set brightness', function () {
319324

320325
// 3. Assert
321326
expect(this.homebridgeCallback.firstCall.args[0]).to.be.instanceOf(Error).and.have.property('message', 'Received HTTP error code '+HTTP_ERROR_STATUS_CODE+': "Dummy error"');
322-
expect(this.homebridgeStub.logger.firstCall.args).deep.equals(['setBrightness() returned HTTP error code: %s: "%s"', HTTP_ERROR_STATUS_CODE, 'Dummy error']);
327+
expect(this.homebridgeStub.logger.firstCall.args).deep.equals(['Caching Brightness as %s ...', 100]);
328+
expect(this.homebridgeStub.logger.secondCall.args).deep.equals(['setBrightness() returned HTTP error code: %s: "%s"', HTTP_ERROR_STATUS_CODE, 'Dummy error']);
323329
});
324330

325331
});
@@ -361,7 +367,7 @@ describe('Get hue', function () {
361367

362368
// 3. Assert
363369
expect(this.homebridgeCallback.firstCall.args[1]).equals(0);
364-
expect(this.homebridgeStub.logger.firstCall.args).deep.equals(['... hue is currently %s', 0]);
370+
expect(this.homebridgeStub.logger.firstCall.args).deep.equals(['... hue is currently %s. RGB: %s', 0, 'ffffff']);
365371
});
366372

367373
});
@@ -407,7 +413,7 @@ describe('Get saturation', function () {
407413

408414
// 3. Assert
409415
expect(this.homebridgeCallback.firstCall.args[1]).equals(0);
410-
expect(this.homebridgeStub.logger.firstCall.args).deep.equals(['... saturation is currently %s', 0]);
416+
expect(this.homebridgeStub.logger.firstCall.args).deep.equals(['... saturation is currently %s. RGB: %s', 0, 'ffffff']);
411417
});
412418

413419
});

0 commit comments

Comments
 (0)