Skip to content

Commit da55436

Browse files
Merge branch 'master' into bugfix-custom-functions
2 parents 01139cc + 38afca0 commit da55436

11 files changed

+1422
-674
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules/
22
.nyc_output/
33
coverage/
4+
.DS_Store

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ In this version, I have made some changes from the older version. Mainly the plu
5252
"name": "Kitchen Temperature",
5353
"type": "temperaturesensor",
5454
"device_id": "<<device id>>",
55-
"event_name": "tvalue"
55+
"event_name": "tvalue",
56+
"split_character": ":"
5657
}
5758
]
5859
}
@@ -70,3 +71,10 @@ The `devices` array contains all the accessories. You can see the accessory obje
7071
- **event_name** - The name of the event to listen for sensor value update. This is only valid if the accessory is a sensor (i.e. currently `temperaturesensor` or `humiditysensor`). The plugin listens for events published from a Particle Device (using `Particle.publish`). The device firmware should publish the sensor values as a raw number.
7172
- **function_name** - The name of the particle function that will be called when an action is triggered via HomeKit. If there is no function provided, the default `power` will be used. This is only valid if the accessory is an actor (i.e. `lightbulb` or `switchaccessory`).
7273

74+
**Particle Event Data Format**
75+
-------------------------------------
76+
By default it expects the event data as "key=value".
77+
```
78+
Particle.publish("tvalue", "temperature=20.7")
79+
```
80+
In order to parse JSON format, a custom `split_character` can be configured.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "homebridge-particle-io",
3-
"version": "0.0.9",
3+
"version": "0.0.11",
44
"description": "Particle plugin for homebridge: https://github.com/nfarina/homebridge",
55
"license": "ISC",
66
"keywords": [
@@ -27,11 +27,11 @@
2727
},
2828
"engines": {
2929
"node": ">=4.3.2",
30-
"homebridge": ">=0.2.0"
30+
"homebridge": ">=0.4.0"
3131
},
3232
"dependencies": {
3333
"eventsource": "",
34-
"request": "^2.65.0"
34+
"request": "^2.85.0"
3535
},
3636
"devDependencies": {
3737
"chai": "^3.5.0",

src/AccessoryFactory.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class AccessoryFactory {
3434
}
3535

3636
createAccessory(device) {
37-
this.log("Create Accessory for device:", device)
37+
this.log('Create Accessory for device:', device);
3838
return new accessoryRegistry[device.type.toLowerCase()](
3939
this.log, this.url, this.accessToken, device, this.homebridge
4040
);

src/ActorAccessory.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,21 @@ class ActorAccessory extends Accessory {
3636

3737
getState(callback) {
3838
this.callParticleFunction(this.function_name, '?', (error, response, body) => {
39-
this.value = parseInt(body);
39+
this.value = parseInt(body, 10);
4040
try {
4141
callback(null, this.value);
42-
} catch (error) {
43-
this.log('Caught error '+ error + ' when calling homebridge callback.');
42+
} catch (err) {
43+
this.log(`Caught error ${err} when calling homebridge callback.`);
4444
}
4545
},
4646
true);
4747
}
4848

4949
setState(value, callback) {
5050
this.value = value;
51-
this.callParticleFunction(this.function_name, value, (error, response, body) => this.callbackHelper(error, response, body, callback), true);
51+
this.callParticleFunction(this.function_name,
52+
value,
53+
(error, response, body) => this.callbackHelper(error, response, body, callback), true);
5254
}
5355

5456
callbackHelper(error, response, body, callback) {

src/ColorLightbulbAccessory.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,48 @@ class ColorLightbulbAccessory extends DimmableLightbulbAccessory {
66
const Characteristic = homebridge.hap.Characteristic;
77
super(log, url, accessToken, device, homebridge);
88

9-
this.hueFunctionName = "hue"
10-
this.actorService.getCharacteristic(Characteristic.Hue)
9+
this.hueFunctionName = 'hue';
10+
this.actorService.getCharacteristic(Characteristic.Hue)
1111
.on('set', this.setHue.bind(this))
1212
.on('get', this.getHue.bind(this));
1313

14-
this.saturationFunctionName = "saturation"
14+
this.saturationFunctionName = 'saturation';
1515
this.actorService.getCharacteristic(Characteristic.Saturation)
1616
.on('set', this.setSaturation.bind(this))
1717
.on('get', this.getSaturation.bind(this));
1818
}
1919

2020
setHue(value, callback) {
2121
this.hue = value;
22-
this.callParticleFunction(this.hueFunctionName, value, (error, response, body) => this.callbackHelper(error, response, body, callback), true);
22+
this.callParticleFunction(this.hueFunctionName, value,
23+
(error, response, body) => this.callbackHelper(error, response, body, callback), true);
2324
}
2425

2526
getHue(callback) {
2627
this.callParticleFunction(this.hueFunctionName, '?', (error, response, body) => {
27-
this.hue = parseInt(body);
28+
this.hue = parseInt(body, 10);
2829
try {
2930
callback(null, this.hue);
30-
} catch (error) {
31-
this.log('Caught error '+ error + ' when calling homebridge callback.');
31+
} catch (err) {
32+
this.log(`Caught error ${err} when calling homebridge callback.`);
3233
}
3334
},
3435
true);
3536
}
3637

3738
setSaturation(value, callback) {
3839
this.saturation = value;
39-
this.callParticleFunction(this.saturationFunctionName, value, (error, response, body) => this.callbackHelper(error, response, body, callback), true);
40+
this.callParticleFunction(this.saturationFunctionName, value,
41+
(error, response, body) => this.callbackHelper(error, response, body, callback), true);
4042
}
4143

4244
getSaturation(callback) {
4345
this.callParticleFunction(this.saturationFunctionName, '?', (error, response, body) => {
44-
this.saturation = parseInt(body);
46+
this.saturation = parseInt(body, 10);
4547
try {
4648
callback(null, this.saturation);
47-
} catch (error) {
48-
this.log('Caught error '+ error + ' when calling homebridge callback.');
49+
} catch (err) {
50+
this.log(`Caught error ${err} when calling homebridge callback.`);
4951
}
5052
},
5153
true);

src/DimmableLightbulbAccessory.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,22 @@ class DimmableLightbulbAccessory extends LightbulbAccessory {
99
this.actorService.getCharacteristic(Characteristic.Brightness)
1010
.on('set', this.setBrightness.bind(this))
1111
.on('get', this.getBrightness.bind(this));
12-
this.brightnessFunctionName = "brightness"
12+
this.brightnessFunctionName = 'brightness';
1313
}
1414

1515
setBrightness(value, callback) {
1616
this.brightness = value;
17-
this.callParticleFunction(this.brightnessFunctionName, value, (error, response, body) => this.callbackHelper(error, response, body, callback), true);
17+
this.callParticleFunction(this.brightnessFunctionName, value,
18+
(error, response, body) => this.callbackHelper(error, response, body, callback), true);
1819
}
1920

2021
getBrightness(callback) {
2122
this.callParticleFunction(this.brightnessFunctionName, '?', (error, response, body) => {
22-
this.brightness = parseInt(body);
23+
this.brightness = parseInt(body, 10);
2324
try {
2425
callback(null, this.brightness);
25-
} catch (error) {
26-
this.log('Caught error '+ error + ' when calling homebridge callback.');
26+
} catch (err) {
27+
this.log(`Caught error ${err} when calling homebridge callback.`);
2728
}
2829
},
2930
true);

src/SensorAccessory.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class SensorAccessory extends Accessory {
88
this.eventName = device.event_name;
99
this.key = device.key;
1010
this.unit = null;
11+
this.split_character = !device.split_character ? '=' : device.split_character;
1112

1213
this.eventUrl = `${this.url}${this.deviceId}/events/${this.eventName}?access_token=${this.accessToken}`;
1314
this.log('Listening for events from:', this.eventUrl);
@@ -30,7 +31,7 @@ class SensorAccessory extends Accessory {
3031

3132
processEventData(e) {
3233
const data = JSON.parse(e.data);
33-
const result = this.key ? data.data.split('=')[1] : data.data;
34+
const result = this.key ? data.data.split(this.split_character)[1] : data.data;
3435

3536
if (this.services.length < 2) {
3637
return;

test/SensorAccessory-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ describe('SensorAccessory.js', () => {
3333
accessory.eventUrl.should.be.equal(
3434
'https://some.random.url.com/1234567890abcdef/events/humidity?access_token=MY_top_SECRET_access_TOKEN'
3535
);
36+
accessory.split_character.should.be.equal(device.split_character);
3637

3738
accessory.services.should.have.length(2);
3839
accessory.services[1].should.be.an.instanceOf(Service.HumiditySensor);

test/dummyConfig.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ const dummyConfig = {
2525
name: 'Kitchen Humidity',
2626
type: 'humiditysensor',
2727
device_id: '1234567890abcdef',
28-
event_name: 'humidity'
28+
event_name: 'humidity',
29+
split_character: ':'
2930
},
3031
{
3132
name: 'Kitchen Light Sensor',

0 commit comments

Comments
 (0)