From 30e60a5f3b21495fbd09c900022fdab8c01b547f Mon Sep 17 00:00:00 2001 From: NorthernMan54 Date: Wed, 20 Feb 2019 21:19:35 -0500 Subject: [PATCH 01/13] Initial --- index.js | 42 +++++++++++++++------------ package-lock.json | 73 +++++++++++++++++++++++++++++++++++++++++++++++ package.json | 6 ++-- 3 files changed, 100 insertions(+), 21 deletions(-) create mode 100644 package-lock.json diff --git a/index.js b/index.js index 56d4a03..41bdbe4 100644 --- a/index.js +++ b/index.js @@ -3,12 +3,11 @@ 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; @@ -16,47 +15,54 @@ function DummySwitch(log, config) { this.stateful = config.stateful; this.reverse = config.reverse; this._service = new Service.Switch(this.name); - + 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); + } else { + this._service.setCharacteristic(Characteristic.On, true); + } } } DummySwitch.prototype.getServices = function() { - return [this._service]; -} + return [this._service, this._contact]; +}; DummySwitch.prototype._setOn = function(on, callback) { - this.log("Setting switch to " + on); + this._contact.setCharacteristic(Characteristic.ContactSensorState, (on ? 0 : 1)); + if (on && !this.reverse && !this.stateful) { setTimeout(function() { this._service.setCharacteristic(Characteristic.On, false); + this._contact.setCharacteristic(Characteristic.ContactSensorState, 1); }.bind(this), 1000); } else if (!on && this.reverse && !this.stateful) { setTimeout(function() { this._service.setCharacteristic(Characteristic.On, true); + this._contact.setCharacteristic(Characteristic.ContactSensorState, 0); }.bind(this), 1000); } - + if (this.stateful) { - this.storage.setItemSync(this.name, on); + this.storage.setItemSync(this.name, on); } - + callback(); -} +}; diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..fca2dff --- /dev/null +++ b/package-lock.json @@ -0,0 +1,73 @@ +{ + "name": "homebridge-dummy-contact", + "version": "0.3.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "node-persist": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/node-persist/-/node-persist-2.1.0.tgz", + "integrity": "sha1-5lK784haBNrWo1PXQXYXfIORRwc=", + "requires": { + "is-absolute": "^0.2.6", + "mkdirp": "~0.5.1", + "q": "~1.1.1" + }, + "dependencies": { + "is-absolute": { + "version": "0.2.6", + "resolved": "https://registry.npmjs.org/is-absolute/-/is-absolute-0.2.6.tgz", + "integrity": "sha1-IN5p89uULvLYe5wto28XIjWxtes=", + "requires": { + "is-relative": "^0.2.1", + "is-windows": "^0.2.0" + } + }, + "is-relative": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/is-relative/-/is-relative-0.2.1.tgz", + "integrity": "sha1-0n9MfVFtF1+2ENuEu+7yPDvJeqU=", + "requires": { + "is-unc-path": "^0.1.1" + } + }, + "is-unc-path": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/is-unc-path/-/is-unc-path-0.1.2.tgz", + "integrity": "sha1-arBTpyVzwQJQ/0FqOBTDUXivObk=", + "requires": { + "unc-path-regex": "^0.1.0" + } + }, + "is-windows": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/is-windows/-/is-windows-0.2.0.tgz", + "integrity": "sha1-3hqm1j6indJIc3tp8f+LgALSEIw=" + }, + "minimist": { + "version": "0.0.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", + "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=" + }, + "mkdirp": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", + "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", + "requires": { + "minimist": "0.0.8" + } + }, + "q": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/q/-/q-1.1.2.tgz", + "integrity": "sha1-Y1fikSBnAdmfGXq4TlforRlvKok=" + }, + "unc-path-regex": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/unc-path-regex/-/unc-path-regex-0.1.2.tgz", + "integrity": "sha1-5z3T17DXxe2G+6xrCufYxqadUPo=" + } + } + } + } +} diff --git a/package.json b/package.json index 01cc251..e5a6bcb 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "homebridge-dummy", + "name": "homebridge-dummy-contact", "version": "0.3.0", "description": "Dummy switches for Homebridge: https://github.com/nfarina/homebridge", "license": "ISC", @@ -8,10 +8,10 @@ ], "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", From 78b57deca1d370c7f2d169012ef8268ec061433e Mon Sep 17 00:00:00 2001 From: NorthernMan54 Date: Wed, 20 Feb 2019 21:34:43 -0500 Subject: [PATCH 02/13] Working version --- README.md | 4 ++++ index.js | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d12bb1f..dbdbbfc 100644 --- a/README.md +++ b/README.md @@ -48,3 +48,7 @@ You may also want to create a dummy switch that turns itself on one second after ] ``` + +## My modified version of this creates a Contact Sensor to complement the switch. + +This is to allow my plugin homebridge-alexa the ability to trigger routines from the contact sensor. diff --git a/index.js b/index.js index 41bdbe4..61d95b1 100644 --- a/index.js +++ b/index.js @@ -46,17 +46,17 @@ DummySwitch.prototype.getServices = function() { DummySwitch.prototype._setOn = function(on, callback) { this.log("Setting switch to " + on); - this._contact.setCharacteristic(Characteristic.ContactSensorState, (on ? 0 : 1)); + this._contact.setCharacteristic(Characteristic.ContactSensorState, (on ? 1 : 0)); if (on && !this.reverse && !this.stateful) { setTimeout(function() { this._service.setCharacteristic(Characteristic.On, false); - this._contact.setCharacteristic(Characteristic.ContactSensorState, 1); + this._contact.setCharacteristic(Characteristic.ContactSensorState, 0); }.bind(this), 1000); } else if (!on && this.reverse && !this.stateful) { setTimeout(function() { this._service.setCharacteristic(Characteristic.On, true); - this._contact.setCharacteristic(Characteristic.ContactSensorState, 0); + this._contact.setCharacteristic(Characteristic.ContactSensorState, 1); }.bind(this), 1000); } From 112526e64506ad6b7625e88bb59e1c0933cdf79a Mon Sep 17 00:00:00 2001 From: NorthernMan54 Date: Sat, 23 Feb 2019 09:47:12 -0500 Subject: [PATCH 03/13] Sync --- index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/index.js b/index.js index 61d95b1..5a93227 100644 --- a/index.js +++ b/index.js @@ -9,12 +9,14 @@ module.exports = function(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 = new Service.ContactSensor(this.name); this.cacheDirectory = HomebridgeAPI.user.persistPath(); From f9bd851e6f6c7add898dea01d5ff37cd048c91b4 Mon Sep 17 00:00:00 2001 From: NorthernMan54 Date: Tue, 26 Feb 2019 22:42:12 -0500 Subject: [PATCH 04/13] Tweaks around events and contacts --- README.md | 5 +++-- index.js | 25 ++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index dbdbbfc..f155624 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -# "Dummy Switches" Plugin +# "Dummy Switches and Contact" Plugin Example config.json: @@ -7,7 +7,8 @@ Example config.json: "accessories": [ { "accessory": "DummySwitch", - "name": "My Switch 1" + "name": "My Switch 1", + "contact": true } ] diff --git a/index.js b/index.js index 5a93227..fde99ac 100644 --- a/index.js +++ b/index.js @@ -15,6 +15,7 @@ function DummySwitch(log, config) { this.name = config.name; this.stateful = config.stateful; this.reverse = config.reverse; + this.contact = config['contact'] || false; this._service = new Service.Switch(this.name); this._contact = new Service.ContactSensor(this.name); @@ -35,20 +36,37 @@ function DummySwitch(log, config) { 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, this._contact]; + 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) { this.log("Setting switch to " + on); - this._contact.setCharacteristic(Characteristic.ContactSensorState, (on ? 1 : 0)); + if (this.contact) { + this._contact.setCharacteristic(Characteristic.ContactSensorState, (on ? 1 : 0)); + } + + if (this.state === on) { + this._service.getCharacteristic(Characteristic.On) + .emit('change', { + oldValue: on, + newValue: on, + context: context + }); + } if (on && !this.reverse && !this.stateful) { setTimeout(function() { @@ -66,5 +84,6 @@ DummySwitch.prototype._setOn = function(on, callback) { this.storage.setItemSync(this.name, on); } + this.state = on; callback(); }; From 0778f6a990af841e2916e6962427bbc0902abd33 Mon Sep 17 00:00:00 2001 From: NorthernMan54 Date: Wed, 20 Mar 2019 21:02:23 -0400 Subject: [PATCH 05/13] Switched from switch to lightbulb to allow brightness to be controlled --- index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index fde99ac..5e876c9 100644 --- a/index.js +++ b/index.js @@ -16,7 +16,9 @@ function DummySwitch(log, config) { this.stateful = config.stateful; this.reverse = config.reverse; this.contact = config['contact'] || false; - this._service = new Service.Switch(this.name); + this._service = new Service.Lightbulb(this.name); + this._service + .addCharacteristic(Characteristic.Brightness); this._contact = new Service.ContactSensor(this.name); From d15b9a95b0e723fdc796f0eb0164d35d6cdbd5b2 Mon Sep 17 00:00:00 2001 From: NorthernMan54 Date: Sun, 23 Feb 2020 08:43:36 -0500 Subject: [PATCH 06/13] Inital --- package-lock.json | 2 +- package.json | 2 +- publish.sh | 13 +++++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100755 publish.sh diff --git a/package-lock.json b/package-lock.json index fca2dff..279aeb9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "homebridge-dummy-contact", - "version": "0.3.0", + "version": "0.3.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index e5a6bcb..a7d291e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "homebridge-dummy-contact", - "version": "0.3.0", + "version": "0.3.1", "description": "Dummy switches for Homebridge: https://github.com/nfarina/homebridge", "license": "ISC", "keywords": [ diff --git a/publish.sh b/publish.sh new file mode 100755 index 0000000..4d6ce5c --- /dev/null +++ b/publish.sh @@ -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 From abf6c8ccdfd4b9adf2c620bb30a831db0691db71 Mon Sep 17 00:00:00 2001 From: NorthernMan54 Date: Sun, 23 Feb 2020 08:48:03 -0500 Subject: [PATCH 07/13] Inital --- package-lock.json | 2 +- package.json | 2 +- test/mocks/scenes.json | 556 ----------------------------------------- 3 files changed, 2 insertions(+), 558 deletions(-) delete mode 100644 test/mocks/scenes.json diff --git a/package-lock.json b/package-lock.json index 279aeb9..98252d2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "homebridge-dummy-contact", - "version": "0.3.1", + "version": "0.3.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index a7d291e..dd86194 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "homebridge-dummy-contact", - "version": "0.3.1", + "version": "0.3.2", "description": "Dummy switches for Homebridge: https://github.com/nfarina/homebridge", "license": "ISC", "keywords": [ diff --git a/test/mocks/scenes.json b/test/mocks/scenes.json deleted file mode 100644 index 9ad7087..0000000 --- a/test/mocks/scenes.json +++ /dev/null @@ -1,556 +0,0 @@ -[ - { - "id": "33300ac17-on-0", - "name": "Feet up on 0", - "lights": [ - "1", - "2", - "3" - ], - "active": true - }, - { - "id": "0f2c67d80-on-0", - "name": "Blue rain on 0", - "lights": [ - "1", - "2", - "3" - ], - "active": true - }, - { - "id": "fd499c3e2-on-0", - "name": "Taj on 0", - "lights": [ - "1", - "2", - "3" - ], - "active": true - }, - { - "id": "3896c310d-on-0", - "name": "Jump! on 0", - "lights": [ - "1", - "2", - "3" - ], - "active": true - }, - { - "id": "797106fb9-on-0", - "name": "Hammock on 0", - "lights": [ - "1", - "2", - "3" - ], - "active": true - }, - { - "id": "64b4aa87e-on-0", - "name": "Laila on 0", - "lights": [ - "1", - "2", - "3" - ], - "active": true - }, - { - "id": "e07a35d7e-on-0", - "name": "Greece on 0", - "lights": [ - "1", - "2", - "3" - ], - "active": true - }, - { - "id": "e915785b2-on-0", - "name": "Reading on 0", - "lights": [ - "1", - "2", - "3" - ], - "active": true - }, - { - "id": "84fa88aa5-on-0", - "name": "Energize on 0", - "lights": [ - "1", - "2", - "3" - ], - "active": true - }, - { - "id": "ac637e2f0-on-0", - "name": "Relax on 0", - "lights": [ - "1", - "2", - "3" - ], - "active": true - }, - { - "id": "f5d73dbdd-on-0", - "name": "Concentrate on 0", - "lights": [ - "1", - "2", - "3" - ], - "active": true - }, - { - "id": "b0cae7c0f-on-0", - "name": "Family Room on 0", - "lights": [ - "1", - "2", - "3", - "4", - "5", - "6", - "7", - "9", - "10", - "11", - "12", - "13", - "14", - "15" - ], - "active": true - }, - { - "id": "54ae5d997-on-0", - "name": "Ski on 0", - "lights": [ - "1", - "2", - "3" - ], - "active": true - }, - { - "id": "6b6ca6a0d-on-0", - "name": "Beach on 0", - "lights": [ - "1", - "2", - "3" - ], - "active": true - }, - { - "id": "087f88f52-on-0", - "name": "Sunset on 0", - "lights": [ - "1", - "2", - "3" - ], - "active": true - }, - { - "id": "2e1dbbb4a-on-0", - "name": "Pencils on 0", - "lights": [ - "1", - "2", - "3" - ], - "active": true - }, - { - "id": "2141cb8bd-on-0", - "name": "Deep sea on 0", - "lights": [ - "1", - "2", - "3" - ], - "active": true - }, - { - "id": "f2b600f90-on-0", - "name": "Kathy on 0", - "lights": [ - "1", - "2", - "3" - ], - "active": true - }, - { - "id": "358853161-on-0", - "name": "SATCO on 0", - "lights": [ - "1", - "2", - "3" - ], - "active": true - }, - { - "id": "0df0d8410-on-0", - "name": "TV on 0", - "lights": [ - "1", - "2", - "3", - "4", - "5", - "6" - ], - "active": true - }, - { - "id": "b1ff92641-on-0", - "name": "TV on 0", - "lights": [ - "1", - "2", - "3", - "4", - "5", - "6", - "7", - "9", - "10", - "11", - "12", - "13", - "14", - "15" - ], - "active": true - }, - { - "id": "7e21bd918-on-0", - "name": "TV on 0", - "lights": [ - "1", - "2", - "3", - "4", - "5", - "6", - "7", - "9", - "10", - "11", - "12", - "13", - "14", - "15" - ], - "active": true - }, - { - "id": "672422585-on-0", - "name": "Hangout on 0", - "lights": [ - "1", - "2", - "3" - ], - "active": true - }, - { - "id": "2ef4ff39f-on-0", - "name": "Relaxing on 0", - "lights": [ - "1", - "2", - "3" - ], - "active": true - }, - { - "id": "dd3046b79-on-0", - "name": "Bedroom on 0", - "lights": [ - "1", - "2", - "3" - ], - "active": true - }, - { - "id": "02b12e930-off-0", - "name": "3 lights off", - "lights": [ - "1", - "2", - "3" - ], - "active": true - }, - { - "id": "beaaf9826-on-0", - "name": "Relaxing on 0", - "lights": [ - "1", - "2", - "3" - ], - "active": true - }, - { - "id": "f1c0e1d4b-on-0", - "name": "Hangout on 0", - "lights": [ - "1", - "2", - "3" - ], - "active": true - }, - { - "id": "OFF-TAP-1", - "name": "Tap scene 1", - "lights": [ - "1", - "2", - "3" - ], - "active": true - }, - { - "id": "TAP-2", - "name": "Tap scene 2", - "lights": [ - "1", - "2", - "3" - ], - "active": true - }, - { - "id": "TAP-3", - "name": "Tap scene 3", - "lights": [ - "1", - "2", - "3" - ], - "active": true - }, - { - "id": "TAP-4", - "name": "Tap scene 4", - "lights": [ - "1", - "2", - "3" - ], - "active": true - }, - { - "id": "5707a1666-on-0", - "name": "TV on 0", - "lights": [ - "1", - "2", - "3" - ], - "active": true - }, - { - "id": "f84ddb8fe-on-0", - "name": "Daylight on 0", - "lights": [ - "1", - "2", - "3" - ], - "active": true - }, - { - "id": "4eaf358d8-on-0", - "name": "Relax on 0", - "lights": [ - "1", - "2", - "3", - "4", - "5", - "6" - ], - "active": true - }, - { - "id": "1df07f572-off-0", - "name": "6 lights off", - "lights": [ - "1", - "2", - "3", - "4", - "5", - "6" - ], - "active": true - }, - { - "id": "442c3441f-on-0", - "name": "Dimmed on 0", - "lights": [ - "1", - "2", - "3", - "4", - "5", - "6" - ], - "active": true - }, - { - "id": "ON-DIMMER", - "name": "Dimmer scene", - "lights": [ - "1", - "2", - "3", - "4", - "5", - "6", - "7", - "8", - "9" - ], - "active": true - }, - { - "id": "fc08438b0-on-0", - "name": "Relax on 0", - "lights": [ - "1", - "2", - "3", - "4", - "5", - "6", - "9", - "10", - "11", - "12", - "13" - ], - "active": true - }, - { - "id": "0f58637a8-on-0", - "name": "Relax on 0", - "lights": [ - "1", - "2", - "3", - "4", - "5", - "6", - "7", - "9", - "10", - "11", - "12", - "13", - "14", - "15" - ], - "active": true - }, - { - "id": "0205ab766-off-0", - "name": "14 lights off", - "lights": [ - "1", - "2", - "3", - "4", - "5", - "6", - "7", - "9", - "10", - "11", - "12", - "13", - "14", - "15" - ], - "active": true - }, - { - "id": "7e1165625-on-0", - "name": "Family Room on 0", - "lights": [ - "1", - "2", - "3", - "4", - "5", - "6", - "7", - "9", - "10", - "11", - "12", - "13", - "14", - "15" - ], - "active": true - }, - { - "id": "faa918d0f-on-0", - "name": "Living Room on 0", - "lights": [ - "1", - "2", - "3", - "4", - "5", - "6", - "7", - "9", - "10", - "11", - "12", - "13", - "14", - "15" - ], - "active": true - }, - { - "id": "426146c8d-on-0", - "name": "Family Room on 0", - "lights": [ - "1", - "2", - "3", - "4", - "5", - "6", - "7", - "9", - "10", - "11", - "12", - "13", - "14", - "15" - ], - "active": true - } -] \ No newline at end of file From 69228dca31d6747bb22454acad228dc430587941 Mon Sep 17 00:00:00 2001 From: "Maarten Kossen (mpkossen)" Date: Tue, 13 Oct 2020 12:18:38 -0400 Subject: [PATCH 08/13] Add config option to have a switch instead of a lightbuld. --- index.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 5e876c9..24f6445 100644 --- a/index.js +++ b/index.js @@ -16,9 +16,15 @@ function DummySwitch(log, config) { this.stateful = config.stateful; this.reverse = config.reverse; this.contact = config['contact'] || false; - this._service = new Service.Lightbulb(this.name); - this._service - .addCharacteristic(Characteristic.Brightness); + this.switch = config['switch'] || 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); From 225ea7d7b9cd2db06eb0cccfac4bec527ec036ab Mon Sep 17 00:00:00 2001 From: "Maarten Kossen (mpkossen)" Date: Wed, 14 Oct 2020 13:31:50 -0400 Subject: [PATCH 09/13] Updated readme. --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index f155624..12bd292 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,21 @@ You may also want to create a dummy switch that turns itself on one second after ``` +## Light Bulb or Switch + +By default, a switch will appear as a light bulb in Homebridge and the Home app. You can configure your Light Bulb to appear as a Switch instead. This is more convenient when not controlling a light bulb. This can be done by passing the 'switch' argument in your config.json: + +``` + "accessories": [ + { + "accessory": "DummySwitch", + "name": "My Stateful Switch 1", + "switch": true + } + ] + +``` + ## My modified version of this creates a Contact Sensor to complement the switch. This is to allow my plugin homebridge-alexa the ability to trigger routines from the contact sensor. From faf088a4c0f9de79716dc27b60abbab7cba72e77 Mon Sep 17 00:00:00 2001 From: NorthernMan54 Date: Wed, 14 Oct 2020 18:48:11 -0400 Subject: [PATCH 10/13] config option to have a switch instead of a light bulb --- package-lock.json | 16 ++++++++-------- package.json | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index 98252d2..c04d4fa 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "homebridge-dummy-contact", - "version": "0.3.2", + "version": "0.3.3", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -45,16 +45,16 @@ "integrity": "sha1-3hqm1j6indJIc3tp8f+LgALSEIw=" }, "minimist": { - "version": "0.0.8", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", - "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=" + "version": "1.2.5", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", + "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==" }, "mkdirp": { - "version": "0.5.1", - "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", - "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", + "version": "0.5.5", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.5.tgz", + "integrity": "sha512-NKmAlESf6jMGym1++R0Ra7wvhV+wFW63FaSOFPwRahvea0gMUcGUhVeAg/0BC0wiv9ih5NYPB1Wn1UEI1/L+xQ==", "requires": { - "minimist": "0.0.8" + "minimist": "^1.2.5" } }, "q": { diff --git a/package.json b/package.json index dd86194..1d11f6a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "homebridge-dummy-contact", - "version": "0.3.2", + "version": "0.3.3", "description": "Dummy switches for Homebridge: https://github.com/nfarina/homebridge", "license": "ISC", "keywords": [ From 94b0c89499f745d29e151f4568ac190bcbdb390d Mon Sep 17 00:00:00 2001 From: ecoen66 Date: Thu, 10 Dec 2020 13:36:48 -0600 Subject: [PATCH 11/13] Added timer and schema Added adjustable timer. Added check for state change prior to logging. Added config.schema.json for Homebridge UI. --- README.md | 26 +++++++++++++++++++------- config.schema.json | 45 +++++++++++++++++++++++++++++++++++++++++++++ index.js | 31 +++++++++++++++++-------------- 3 files changed, 81 insertions(+), 21 deletions(-) create mode 100644 config.schema.json diff --git a/README.md b/README.md index 12bd292..71b281c 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,7 @@ Example config.json: "accessories": [ { "accessory": "DummySwitch", - "name": "My Switch 1", - "contact": true + "name": "My Switch 1" } ] @@ -50,21 +49,34 @@ You may also want to create a dummy switch that turns itself on one second after ``` -## Light Bulb or Switch +## Timed Switches -By default, a switch will appear as a light bulb in Homebridge and the Home app. You can configure your Light Bulb to appear as a Switch instead. This is more convenient when not controlling a light bulb. This can be done by passing the 'switch' argument in your config.json: +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", - "switch": true + "time": 5000 } ] ``` -## My modified version of this creates a Contact Sensor to complement the switch. -This is to allow my plugin homebridge-alexa the ability to trigger routines from the contact sensor. +## 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 + } + ] + +``` + diff --git a/config.schema.json b/config.schema.json new file mode 100644 index 0000000..96d06aa --- /dev/null +++ b/config.schema.json @@ -0,0 +1,45 @@ +{ + "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." + } + } + } +} diff --git a/index.js b/index.js index 24f6445..df4308f 100644 --- a/index.js +++ b/index.js @@ -16,6 +16,7 @@ function DummySwitch(log, config) { this.stateful = config.stateful; this.reverse = config.reverse; this.contact = config['contact'] || false; + this.time = config.time ? config.time : 1000; this.switch = config['switch'] || false; if (this.switch) { @@ -61,8 +62,7 @@ DummySwitch.prototype.getServices = function() { }; DummySwitch.prototype._setOn = function(on, callback, context) { - this.log("Setting switch to " + on); - + if (this.contact) { this._contact.setCharacteristic(Characteristic.ContactSensorState, (on ? 1 : 0)); } @@ -74,18 +74,21 @@ DummySwitch.prototype._setOn = function(on, callback, context) { newValue: on, context: context }); - } - - if (on && !this.reverse && !this.stateful) { - setTimeout(function() { - this._service.setCharacteristic(Characteristic.On, false); - this._contact.setCharacteristic(Characteristic.ContactSensorState, 0); - }.bind(this), 1000); - } else if (!on && this.reverse && !this.stateful) { - setTimeout(function() { - this._service.setCharacteristic(Characteristic.On, true); - this._contact.setCharacteristic(Characteristic.ContactSensorState, 1); - }.bind(this), 1000); + } else { + + this.log("Setting switch to " + on); + + if (on && !this.reverse && !this.stateful) { + 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) { + setTimeout(function() { + this._service.setCharacteristic(Characteristic.On, true); + this._contact.setCharacteristic(Characteristic.ContactSensorState, 1); + }.bind(this), this.time); + } } if (this.stateful) { From ccd9e80d2adf7c5ccf44594fcd5d3bf454495081 Mon Sep 17 00:00:00 2001 From: NorthernMan54 Date: Thu, 10 Dec 2020 16:29:57 -0500 Subject: [PATCH 12/13] Pull request from ecoen66 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index c04d4fa..f70b00c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "homebridge-dummy-contact", - "version": "0.3.3", + "version": "0.3.4", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 1d11f6a..1f12118 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "homebridge-dummy-contact", - "version": "0.3.3", + "version": "0.3.4", "description": "Dummy switches for Homebridge: https://github.com/nfarina/homebridge", "license": "ISC", "keywords": [ From 3a20e18568b7678d5f25d2ffb3af02b79e806224 Mon Sep 17 00:00:00 2001 From: ecoen66 Date: Fri, 18 Dec 2020 12:26:49 -0600 Subject: [PATCH 13/13] Debug + timer reset Added a reset of timer each subsequent time that the switch is turned on (if already on). Added Debug. --- config.schema.json | 6 ++++++ index.js | 54 +++++++++++++++++++++++++++++++++------------- 2 files changed, 45 insertions(+), 15 deletions(-) diff --git a/config.schema.json b/config.schema.json index 96d06aa..752eca9 100644 --- a/config.schema.json +++ b/config.schema.json @@ -39,6 +39,12 @@ "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." } } } diff --git a/index.js b/index.js index df4308f..2d7967e 100644 --- a/index.js +++ b/index.js @@ -18,6 +18,8 @@ function DummySwitch(log, config) { 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); @@ -62,12 +64,14 @@ DummySwitch.prototype.getServices = function() { }; 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)); } - if (this.state === on) { + if (this.state === on) { this._service.getCharacteristic(Characteristic.On) .emit('change', { oldValue: on, @@ -76,20 +80,40 @@ DummySwitch.prototype._setOn = function(on, callback, context) { }); } else { - this.log("Setting switch to " + on); - - if (on && !this.reverse && !this.stateful) { - 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) { - setTimeout(function() { - this._service.setCharacteristic(Characteristic.On, true); - this._contact.setCharacteristic(Characteristic.ContactSensorState, 1); - }.bind(this), this.time); + 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 (this.stateful) { this.storage.setItemSync(this.name, on);