Skip to content

Commit 6e4545a

Browse files
Merge pull request #19 from mguntli/bugfix-custom-functions
Fix function_name for actor accessories
2 parents 38afca0 + da55436 commit 6e4545a

File tree

5 files changed

+11
-9
lines changed

5 files changed

+11
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ The `devices` array contains all the accessories. You can see the accessory obje
6969
- **type** - Type of the accessory. As of now, the plugin supports 3 types: `lightbulb`, `temperaturesensor` and `humiditysensor`.
7070
- **device_id** - Device ID of the Particle Device (Core, Photon or Electron). It is defined in accessory so that you can use different Particle Devices for different accessory.
7171
- **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.
72-
- **function_name** - The name of the function that will be called when an action is triggered via HomeKit. This is only valid if the accessory is an actor (i.e. currently only `lightbulb`).
72+
- **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`).
7373

7474
**Particle Event Data Format**
7575
-------------------------------------

src/ActorAccessory.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class ActorAccessory extends Accessory {
66
constructor(log, url, accessToken, device, homebridge, ServiceType, CharacteristicType) {
77
super(log, url, accessToken, device, homebridge, ServiceType, CharacteristicType);
88

9+
this.function_name = !device.function_name ? 'power' : device.function_name;
910
this.actorService = new ServiceType(this.name);
1011
this.actorService.getCharacteristic(CharacteristicType)
1112
.on('set', this.setState.bind(this))
@@ -34,7 +35,7 @@ class ActorAccessory extends Accessory {
3435
}
3536

3637
getState(callback) {
37-
this.callParticleFunction('power', '?', (error, response, body) => {
38+
this.callParticleFunction(this.function_name, '?', (error, response, body) => {
3839
this.value = parseInt(body, 10);
3940
try {
4041
callback(null, this.value);
@@ -45,9 +46,9 @@ class ActorAccessory extends Accessory {
4546
true);
4647
}
4748

48-
setState(functionName, value, callback) {
49+
setState(value, callback) {
4950
this.value = value;
50-
this.callParticleFunction(functionName,
51+
this.callParticleFunction(this.function_name,
5152
value,
5253
(error, response, body) => this.callbackHelper(error, response, body, callback), true);
5354
}

src/LightbulbAccessory.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class LightbulbAccessory extends ActorAccessory {
99
}
1010

1111
setState(value, callback) {
12-
super.setState('power', value ? '1' : '0', callback);
12+
super.setState(value ? '1' : '0', callback);
1313
}
1414

1515
}

test/ActorAccessory-test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe('ActorAccessory.js', () => {
5050
it('should send value', () => {
5151
sinon.stub(request, 'post');
5252
const value = 17;
53-
accessory.setState('testFunctionName', value, () => {});
53+
accessory.setState(value, () => {});
5454

5555
expect(request.post).to.have.been.calledOnce;
5656
expect(request.post).to.have.been.calledWith(
@@ -71,7 +71,7 @@ describe('ActorAccessory.js', () => {
7171
sinon.stub(request, 'post', () => { accessory.callbackHelper(undefined, 200, 'body', spy); });
7272

7373
const value = 17;
74-
accessory.setState('testFunctionName', value, spy);
74+
accessory.setState(value, spy);
7575

7676
expect(spy).to.have.been.calledOnce;
7777
expect(spy.lastCall.args).to.have.length(0);
@@ -83,7 +83,7 @@ describe('ActorAccessory.js', () => {
8383
sinon.stub(request, 'post', () => { accessory.callbackHelper('some error', 200, 'body', spy); });
8484

8585
const value = 17;
86-
accessory.setState('testFunctionName', value, spy);
86+
accessory.setState(value, spy);
8787

8888
expect(spy).to.have.been.calledOnce;
8989
expect(spy.lastCall.args).to.have.length(1);

test/dummyConfig.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ const dummyConfig = {
77
{
88
name: 'Bedroom Light',
99
type: 'lightbulb',
10-
device_id: 'abcdef1234567890'
10+
device_id: 'abcdef1234567890',
11+
function_name: 'testFunctionName'
1112
},
1213
{
1314
name: 'Kitchen Light',

0 commit comments

Comments
 (0)