Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change accessorie name #33

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# "Dummy Switches" Plugin
# "Dummy Switches and Contact" Plugin

Example config.json:

Expand Down Expand Up @@ -48,3 +48,35 @@ You may also want to create a dummy switch that turns itself on one second after
]

```

## Timed Switches

You may also want to create a timed switch that turns itself off after being on for a given time (for example, five seconds). This can be done by passing the 'time' argument in your config.json:

```
"accessories": [
{
"accessory": "DummySwitch",
"name": "My Stateful Switch 1",
"time": 5000
}
]

```


## My modified version of this Homebridge plugin creates an optional Contact Sensor to complement the switch.

This is to allow my plugin homebridge-alexa the ability to trigger routines from the contact sensor. Also, by default each DummySwitch is a Lightbulb. This can be changed by passing the 'switch' argument in your config.json:
```
"accessories": [
{
"accessory": "DummySwitch",
"name": "My Switch with Contact",
"switch": true,
"contact": true
}
]

```

51 changes: 51 additions & 0 deletions config.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"pluginAlias": "DummySwitch",
"pluginType": "accessory",
"singular": false,
"schema": {
"type": "object",
"properties": {
"name": {
"title": "Name",
"type": "string",
"required": true
},
"stateful": {
"title": "Stateful",
"type": "boolean",
"default": false,
"description": "The switch remains on instead of being automatically turned off."
},
"switch": {
"title": "Switch",
"type": "boolean",
"default": false,
"description": "The switch is shown as a Switch vs. a Lightbulb."
},
"contact": {
"title": "Contact",
"type": "boolean",
"default": false,
"description": "The switch includes a combined contact sensor."
},
"reverse": {
"title": "Reverse",
"type": "boolean",
"default": false,
"description": "The switch's default state is on."
},
"time": {
"title": "Time",
"type": "number",
"default": 1000,
"description": "The switch will turn off after this number of milliseconds. Not used if the switch is stateful."
},
"debug": {
"title": "Debug",
"type": "boolean",
"default": false,
"description": "Enables additional logging."
}
}
}
}
120 changes: 91 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,122 @@
var Service, Characteristic, HomebridgeAPI;

module.exports = function(homebridge) {

Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
HomebridgeAPI = homebridge;
homebridge.registerAccessory("homebridge-dummy", "DummySwitch", DummySwitch);
}
};


function DummySwitch(log, config) {
this.log = log;
this.name = config.name;
this.stateful = config.stateful;
this.reverse = config.reverse;
this._service = new Service.Switch(this.name);

this.contact = config['contact'] || false;
this.time = config.time ? config.time : 1000;
this.switch = config['switch'] || false;
this.timerObject = null;
this.debug = config['debug'] || false;

if (this.switch) {
this._service = new Service.Switch(this.name);
} else {
this._service = new Service.Lightbulb(this.name);
this._service
.addCharacteristic(Characteristic.Brightness);
}

this._contact = new Service.ContactSensor(this.name);

this.cacheDirectory = HomebridgeAPI.user.persistPath();
this.storage = require('node-persist');
this.storage.initSync({dir:this.cacheDirectory, forgiveParseErrors: true});

this.storage.initSync({
dir: this.cacheDirectory,
forgiveParseErrors: true
});

this._service.getCharacteristic(Characteristic.On)
.on('set', this._setOn.bind(this));

if (this.reverse) this._service.setCharacteristic(Characteristic.On, true);

if (this.stateful) {
var cachedState = this.storage.getItemSync(this.name);
if((cachedState === undefined) || (cachedState === false)) {
this._service.setCharacteristic(Characteristic.On, false);
} else {
this._service.setCharacteristic(Characteristic.On, true);
}
var cachedState = this.storage.getItemSync(this.name);
if ((cachedState === undefined) || (cachedState === false)) {
this._service.setCharacteristic(Characteristic.On, false);
this.state = false;
} else {
this._service.setCharacteristic(Characteristic.On, true);
this.state = true;
}
}
}

DummySwitch.prototype.getServices = function() {
return [this._service];
}
if (this.contact) {
return [this._service, this._contact];
} else {
return [this._service];
}
};

DummySwitch.prototype._setOn = function(on, callback) {
DummySwitch.prototype._setOn = function(on, callback, context) {
if (this.debug) {
this.log("Called to set switch to", on);
}
if (this.contact) {
this._contact.setCharacteristic(Characteristic.ContactSensorState, (on ? 1 : 0));
}

this.log("Setting switch to " + on);
if (this.state === on) {
this._service.getCharacteristic(Characteristic.On)
.emit('change', {
oldValue: on,
newValue: on,
context: context
});
} else {

this.log("Setting switch to", on);
}

if (on && !this.reverse && !this.stateful) {
if (this.timerObject) {
if (this.debug) {
this.log("Called to set state to On again. Resetting timerObject.");
}
clearTimeout(this.timerObject);
} else {
if (this.debug) {
this.log("Called to set state to On again. There is no timerObject.");
}
}
this.timerObject = setTimeout(function() {
this._service.setCharacteristic(Characteristic.On, false);
this._contact.setCharacteristic(Characteristic.ContactSensorState, 0);
}.bind(this), this.time);
} else if (!on && this.reverse && !this.stateful) {
if (this.timerObject) {
if (this.debug) {
this.log("Called to set state to Off again. Resetting timerObject.");
}
clearTimeout(this.timerObject);
} else {
if (this.debug) {
this.log("Called to set state to Off again. There is no timerObject.");
}
}
this.timerObject = setTimeout(function() {
this._service.setCharacteristic(Characteristic.On, true);
this._contact.setCharacteristic(Characteristic.ContactSensorState, 1);
}.bind(this), this.time);
}

if (on && !this.reverse && !this.stateful) {
setTimeout(function() {
this._service.setCharacteristic(Characteristic.On, false);
}.bind(this), 1000);
} else if (!on && this.reverse && !this.stateful) {
setTimeout(function() {
this._service.setCharacteristic(Characteristic.On, true);
}.bind(this), 1000);
}

if (this.stateful) {
this.storage.setItemSync(this.name, on);
this.storage.setItemSync(this.name, on);
}


this.state = on;
callback();
}
};
73 changes: 73 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"name": "homebridge-dummy",
"version": "0.3.0",
"name": "homebridge-dummy-contact",
"version": "0.3.4",
"description": "Dummy switches for Homebridge: https://github.com/nfarina/homebridge",
"license": "ISC",
"keywords": [
"homebridge-plugin"
],
"repository": {
"type": "git",
"url": "git://github.com/nfarina/homebridge-dummy.git"
"url": "git://github.com/NorthernMan54/homebridge-dummy-contact.git"
},
"bugs": {
"url": "http://github.com/nfarina/homebridge-dummy/issues"
"url": "http://github.com/NorthernMan54/homebridge-dummy-contact/issues"
},
"engines": {
"node": ">=0.12.0",
Expand Down
13 changes: 13 additions & 0 deletions publish.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#! /bin/sh

if npm audit; then
# npm run-script document
# rm *orig* *toc\.*
git add .
npm version patch -m "$1" --force
npm publish
git commit -m "$1"
git push origin master --tags
else
echo "Not publishing due to security vulnerabilites"
fi
Loading