Skip to content

Commit 5395c36

Browse files
author
QuickSander
committed
feat: Prepare code for bodyRegEx for color status. Added color unttests.
1 parent 7999800 commit 5395c36

File tree

3 files changed

+145
-9
lines changed

3 files changed

+145
-9
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,17 @@ Following configuration is a real world example for a accessory that combines a
270270
### Color object
271271
| Key | Description |
272272
| --- | --- |
273-
| `status` | URL to get RGB current colour (HEX value) |
274-
| `url` | URL to set the RGB colour value (HEX value) |
273+
| `status` | URL to get RGB current colour (HEX value) or a [color status object](#color-status-object). |
274+
| `url` | URL to set the RGB colour value (HEX value) or a [color url object](#color-url-object). |
275275
| `brightness` | Whether or not the plugin should include brightness data in `color` HEX data (`true` or `false`). When `true` brightness will be controllable in HomeKit but will be changed through changing RGB values. |
276276
| `http_method` _(optional)_ | The brightness specific HTTP method for set requests. If omitted defaults to `http_method` as specified in the root structure |
277277

278+
#### Color status object
279+
| Key | Description |
280+
| --- | --- |
281+
| `url` | URL to retrieve color status |
282+
| `bodyRegEx` _(optional)_ | _Not yet supported_ |
283+
278284
#### Color url object
279285
| Key | Description |
280286
| --- | --- |

index.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,23 @@ function HttpPushRgb(log, config) {
143143

144144
// Color handling
145145
if (typeof config.color === 'object') {
146-
this.color = {"set_url": {}};
146+
this.color = {"set_url": {}, "get_url": {}};
147147
if (typeof config.color.url === 'object') {
148148
this.color.set_url.url = config.color.url.url || this.color.status;
149-
this.color.set_url.body = config.color.url.body
149+
this.color.set_url.body = config.color.url.body;
150150
} else {
151151
this.color.set_url.url = config.color.url || this.color.status;
152152
this.color.set_url.body = '';
153153
}
154-
this.color.status = config.color.status;
154+
155+
if (typeof config.color.status === 'object') {
156+
this.color.get_url.url = config.color.status.url;
157+
this.color.get_url.bodyRegEx = config.color.status.bodyRegEx || '';
158+
} else {
159+
this.color.get_url.url = config.color.status;
160+
this.color.get_url.bodyRegEx = '';
161+
}
162+
155163
this.color.http_method = config.color.http_method || this.http_method;
156164
this.color.brightness = config.color.brightness;
157165
this.cache.hue = 0;
@@ -418,12 +426,12 @@ HttpPushRgb.prototype = {
418426
* @param {function} callback The callback that handles the response.
419427
*/
420428
getHue: function(callback) {
421-
if (this.color && typeof this.color.status !== 'string') {
429+
if (this.color && typeof this.color.get_url.url !== 'string') {
422430
this.log.warn("Ignoring getHue request; problem with 'color' variables.");
423431
callback(new Error("There was a problem parsing the 'color.status' section of your configuration."));
424432
return;
425433
}
426-
var url = this.color.status;
434+
var url = this.color.get_url.url;
427435

428436
this._httpRequest(url, '', 'GET', function(error, response, responseBody) {
429437
if (!this._handleHttpErrorResponse('getHue()', error, response, responseBody, callback)) {
@@ -470,12 +478,12 @@ HttpPushRgb.prototype = {
470478
* @param {function} callback The callback that handles the response.
471479
*/
472480
getSaturation: function(callback) {
473-
if (this.color && typeof this.color.status !== 'string') {
481+
if (this.color && typeof this.color.get_url.url !== 'string') {
474482
this.log.warn("Ignoring getSaturation request; problem with 'color' variables.");
475483
callback(new Error("There was a problem parsing the 'color' section of your configuration."));
476484
return;
477485
}
478-
var url = this.color.status;
486+
var url = this.color.get_url.url;
479487

480488
this._httpRequest(url, '', 'GET', function(error, response, responseBody) {
481489
if (!this._handleHttpErrorResponse('getSaturation()', error, response, responseBody, callback)) {

test/basics.test.js

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,39 @@ describe('Homebridge plugin creation', function () {
122122
expect(this.homebridgeStub.accessory.switch.status.bodyRegEx).to.eql(new RegExp(/1/));
123123
});
124124

125+
it('sets color.get_url on legacy color.status config string', function () {
126+
// 1. Arrange
127+
// Default TestConfig sets color.status URL already.
128+
129+
// 2. Act
130+
sut(this.homebridgeStub);
131+
132+
// 3. Assert
133+
expect(this.homebridgeStub.accessory.color.get_url.url).to.eql("http://localhost:8080/color/status");
134+
});
135+
136+
it('sets color.set_url on legacy color.url config string', function () {
137+
// 1. Arrange
138+
// Default TestConfig sets color.url URL already.
139+
140+
// 2. Act
141+
sut(this.homebridgeStub);
142+
143+
// 3. Assert
144+
expect(this.homebridgeStub.accessory.color.set_url.url).to.eql("http://localhost:8080/color/set/%s");
145+
});
146+
147+
it('sets color.set_url on new style color.url config object', function () {
148+
// 1. Arrange
149+
this.testConfig.color.url = {'url': "http://example.com" };
150+
151+
// 2. Act
152+
sut(this.homebridgeStub);
153+
154+
// 3. Assert
155+
expect(this.homebridgeStub.accessory.color.set_url.url).to.eql("http://example.com");
156+
});
157+
125158
});
126159

127160

@@ -275,6 +308,95 @@ describe('Set brightness', function () {
275308

276309
});
277310

311+
// -----------------------------------------------------------------------------
312+
describe('Get hue', function () {
313+
314+
beforeEach(function () {
315+
// 1. Arrange
316+
this.testConfig = new TestConfig();
317+
318+
// This will also make sure to reset the embedded Sinon stubs.
319+
this.homebridgeStub = new (require('./homebridge.stub.js'))(this.testConfig);
320+
sut(this.homebridgeStub);
321+
322+
this.homebridgeStub.accessory._httpRequest = sinon.stub();
323+
this.homebridgeCallback = sinon.stub();
324+
325+
// 2. Act
326+
// Allow getHue to create HTTP response callback
327+
this.homebridgeStub.accessory.getHue(this.homebridgeCallback);
328+
});
329+
330+
331+
it('sends HTTP GET request with correct URL', function () {
332+
// 1. Arrange
333+
var url = this.testConfig.color.status;
334+
335+
// 3. Assert
336+
expect(this.homebridgeStub.accessory._httpRequest.firstCall.args[0]).equals(url);
337+
expect(this.homebridgeStub.accessory._httpRequest.firstCall.args[1]).to.be.empty; // Body empty.
338+
expect(this.homebridgeStub.accessory._httpRequest.firstCall.args[2]).equals('GET');
339+
});
340+
341+
it('replies "0" to Homebridge on valid HTTP GET device response "ffffff"', function () {
342+
// 2. Act
343+
// Call collected HTTP response callback to simulate device response.
344+
this.homebridgeStub.accessory._httpRequest.firstCall.callback(undefined, {statusCode: 200}, 'ffffff');
345+
346+
// 3. Assert
347+
expect(this.homebridgeCallback.firstCall.args[1]).equals(0);
348+
expect(this.homebridgeStub.logger.firstCall.args).deep.equals(['... hue is currently %s', 0]);
349+
});
350+
351+
});
352+
353+
354+
// -----------------------------------------------------------------------------
355+
describe('Get saturation', function () {
356+
357+
beforeEach(function () {
358+
// 1. Arrange
359+
this.testConfig = new TestConfig();
360+
361+
// This will also make sure to reset the embedded Sinon stubs.
362+
this.homebridgeStub = new (require('./homebridge.stub.js'))(this.testConfig);
363+
sut(this.homebridgeStub);
364+
365+
this.homebridgeStub.accessory._httpRequest = sinon.stub();
366+
this.homebridgeCallback = sinon.stub();
367+
368+
// 2. Act
369+
// Allow getSaturation to create HTTP response callback
370+
this.homebridgeStub.accessory.getSaturation(this.homebridgeCallback);
371+
});
372+
373+
374+
it('sends HTTP GET request with correct URL', function () {
375+
// 1. Arrange
376+
var url = this.testConfig.color.status;
377+
378+
// 2. Act
379+
this.homebridgeStub.accessory.getSaturation(this.homebridgeCallback);
380+
381+
// 3. Assert
382+
expect(this.homebridgeStub.accessory._httpRequest.firstCall.args[0]).equals(url);
383+
expect(this.homebridgeStub.accessory._httpRequest.firstCall.args[1]).to.be.empty; // Body empty.
384+
expect(this.homebridgeStub.accessory._httpRequest.firstCall.args[2]).equals('GET');
385+
});
386+
387+
it('replies "0" to Homebridge on valid HTTP GET device response "ffffff"', function () {
388+
// 2. Act
389+
// Call collected HTTP response callback to simulate device response.
390+
this.homebridgeStub.accessory._httpRequest.firstCall.callback(undefined, {statusCode: 200}, 'ffffff');
391+
392+
// 3. Assert
393+
expect(this.homebridgeCallback.firstCall.args[1]).equals(0);
394+
expect(this.homebridgeStub.logger.firstCall.args).deep.equals(['... saturation is currently %s', 0]);
395+
});
396+
397+
});
398+
399+
278400
// -----------------------------------------------------------------------------
279401
describe('Get services', function () {
280402

0 commit comments

Comments
 (0)