From f13ae1e6236988dfc2ff204bacea1c5bc61023a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Vanderheeren?= Date: Sun, 1 Mar 2020 18:26:53 +0100 Subject: [PATCH 01/13] Create basic-garage-door-opener.js --- abilities/basic-garage-door-opener.js | 1 + 1 file changed, 1 insertion(+) create mode 100644 abilities/basic-garage-door-opener.js diff --git a/abilities/basic-garage-door-opener.js b/abilities/basic-garage-door-opener.js new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/abilities/basic-garage-door-opener.js @@ -0,0 +1 @@ + From 2f70e47cc0858674fcdd97982cf51941a10b8531 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Vanderheeren?= Date: Sun, 1 Mar 2020 20:02:42 +0100 Subject: [PATCH 02/13] Update garage-door-openers.js --- accessories/garage-door-openers.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/accessories/garage-door-openers.js b/accessories/garage-door-openers.js index 32af54e..2f52622 100644 --- a/accessories/garage-door-openers.js +++ b/accessories/garage-door-openers.js @@ -1,10 +1,28 @@ module.exports = homebridge => { const Accessory = homebridge.hap.Accessory + const BasicGarageDoorOpenerAbility = + require('../abilities/basic-garage-door-opener')(homebridge) const GarageDoorOpenerAbility = require('../abilities/garage-door-opener')(homebridge) - const { ShellyAccessory } = require('./base')(homebridge) + const { ShellyRelayAccessory } = require('./base')(homebridge) + class Shelly1GarageDoorOpenerAccessory extends ShellyRelayAccessory { + constructor(device, index, config, log) { + super('garageDoorOpener', device, index, config, log) + + this.abilities.push(new BasicGarageDoorOpenerAbility( + 'rollerPosition', + 'rollerState', + this.setRelay.bind(this) + )) + } + + get category() { + return Accessory.Categories.GARAGE_DOOR_OPENER + } + } + class Shelly2GarageDoorOpenerAccessory extends ShellyAccessory { constructor(device, index, config, log) { super('garageDoorOpener', device, index, config, log) @@ -31,6 +49,7 @@ module.exports = homebridge => { } return { - Shelly2GarageDoorOpenerAccessory, + Shelly1GarageDoorOpenerAccessory, + Shelly2GarageDoorOpenerAccessory } } From 391064c751541139a6bff27a7c5b151b69cd1063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Vanderheeren?= Date: Sun, 1 Mar 2020 20:04:39 +0100 Subject: [PATCH 03/13] Update basic-garage-door-opener.js --- abilities/basic-garage-door-opener.js | 142 ++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) diff --git a/abilities/basic-garage-door-opener.js b/abilities/basic-garage-door-opener.js index 8b13789..47b4ca8 100644 --- a/abilities/basic-garage-door-opener.js +++ b/abilities/basic-garage-door-opener.js @@ -1 +1,143 @@ +const { handleFailedRequest } = require('../error-handlers') +module.exports = homebridge => { + const { Ability } = require('./base')(homebridge) + const Characteristic = homebridge.hap.Characteristic + const Service = homebridge.hap.Service + + class BasicGarageDoorOpenerAbility extends Ability { + /** + * @param {string} stateProperty - The device property used to indicate the + * garage door is closed. + * @param {function} setState - A function that updates the device's + * target state. Must return a Promise. + */ + constructor(stateProperty, setState) { + super() + this._stateProperty = stateProperty + this._setState = setState + this._targetState = null + } + + get state() { + return this.device[this._stateProperty] + } + + get currentState() { + const CDS = Characteristic.CurrentDoorState + + if (!!this.state) { + return CDS.CLOSED + } + + return CDS.OPEN + } + + get targetState() { + if (this._targetState !== null) { + return this._targetState + } + + const CDS = Characteristic.CurrentDoorState + const TDS = Characteristic.TargetDoorState + const cs = this.currentState + return cs === CDS.CLOSED ? TDS.OPEN : TDS.CLOSED + } + + _setupPlatformAccessory() { + super._setupPlatformAccessory() + + this.platformAccessory.addService( + new Service.GarageDoorOpener() + .setCharacteristic(Characteristic.CurrentDoorState, this.currentState) + .setCharacteristic(Characteristic.TargetDoorState, this.targetState) + .setCharacteristic(Characteristic.ObstructionDetected, false) + ) + } + + _setupEventHandlers() { + super._setupEventHandlers() + + this.platformAccessory + .getService(Service.GarageDoorOpener) + .getCharacteristic(Characteristic.TargetDoorState) + .on('set', this._targetDoorStateSetHandler.bind(this)) + + this.device + .on( + 'change:' + this._stateProperty, + this._stateChangeHandler, + this + ) + } + + /** + * Handles changes from HomeKit to the TargetDoorState characteristic. + */ + async _targetDoorStateSetHandler(newValue, callback) { + const d = this.device + + if (this.targetState === newValue) { + callback() + return + } + + this._targetState = newValue + + this.log.debug( + 'Setting', + this._switchProperty, + 'of device', + d.type, + d.id, + 'to', + true + ) + + try { + await this._setState(true) + callback() + } catch (e) { + handleFailedRequest( + this.log, + d, + e, + 'Failed to set ' + this._switchProperty + ) + callback(e) + } + } + + /** + * Handles changes from the device to the state property. + */ + _stateChangeHandler(newValue) { + this.log.debug( + this._stateProperty, + 'of device', + this.device.type, + this.device.id, + 'changed to', + newValue + ) + + this.platformAccessory + .getService(Service.GarageDoorOpener) + .getCharacteristic(Characteristic.CurrentDoorState) + .setValue(this.currentState) + } + + detach() { + this.device + .removeListener( + 'change:' + this._stateProperty, + this._stateChangeHandler, + this + ) + + super.detach() + } + } + + return BasicGarageDoorOpenerAbility +} From 73e4f612d20bb42ccb3c23d02bcd066c2d37320c Mon Sep 17 00:00:00 2001 From: Michael Vanderheeren Date: Mon, 1 Jun 2020 23:14:42 +0200 Subject: [PATCH 04/13] changed to garage-door-switch --- abilities/basic-garage-door-opener.js | 143 ----------------- abilities/garage-door-switch.js | 214 ++++++++++++++++++++++++++ accessories/factory.js | 10 ++ accessories/garage-door-openers.js | 20 +-- 4 files changed, 234 insertions(+), 153 deletions(-) delete mode 100644 abilities/basic-garage-door-opener.js create mode 100644 abilities/garage-door-switch.js diff --git a/abilities/basic-garage-door-opener.js b/abilities/basic-garage-door-opener.js deleted file mode 100644 index 47b4ca8..0000000 --- a/abilities/basic-garage-door-opener.js +++ /dev/null @@ -1,143 +0,0 @@ -const { handleFailedRequest } = require('../error-handlers') - -module.exports = homebridge => { - const { Ability } = require('./base')(homebridge) - const Characteristic = homebridge.hap.Characteristic - const Service = homebridge.hap.Service - - class BasicGarageDoorOpenerAbility extends Ability { - /** - * @param {string} stateProperty - The device property used to indicate the - * garage door is closed. - * @param {function} setState - A function that updates the device's - * target state. Must return a Promise. - */ - constructor(stateProperty, setState) { - super() - this._stateProperty = stateProperty - this._setState = setState - this._targetState = null - } - - get state() { - return this.device[this._stateProperty] - } - - get currentState() { - const CDS = Characteristic.CurrentDoorState - - if (!!this.state) { - return CDS.CLOSED - } - - return CDS.OPEN - } - - get targetState() { - if (this._targetState !== null) { - return this._targetState - } - - const CDS = Characteristic.CurrentDoorState - const TDS = Characteristic.TargetDoorState - const cs = this.currentState - return cs === CDS.CLOSED ? TDS.OPEN : TDS.CLOSED - } - - _setupPlatformAccessory() { - super._setupPlatformAccessory() - - this.platformAccessory.addService( - new Service.GarageDoorOpener() - .setCharacteristic(Characteristic.CurrentDoorState, this.currentState) - .setCharacteristic(Characteristic.TargetDoorState, this.targetState) - .setCharacteristic(Characteristic.ObstructionDetected, false) - ) - } - - _setupEventHandlers() { - super._setupEventHandlers() - - this.platformAccessory - .getService(Service.GarageDoorOpener) - .getCharacteristic(Characteristic.TargetDoorState) - .on('set', this._targetDoorStateSetHandler.bind(this)) - - this.device - .on( - 'change:' + this._stateProperty, - this._stateChangeHandler, - this - ) - } - - /** - * Handles changes from HomeKit to the TargetDoorState characteristic. - */ - async _targetDoorStateSetHandler(newValue, callback) { - const d = this.device - - if (this.targetState === newValue) { - callback() - return - } - - this._targetState = newValue - - this.log.debug( - 'Setting', - this._switchProperty, - 'of device', - d.type, - d.id, - 'to', - true - ) - - try { - await this._setState(true) - callback() - } catch (e) { - handleFailedRequest( - this.log, - d, - e, - 'Failed to set ' + this._switchProperty - ) - callback(e) - } - } - - /** - * Handles changes from the device to the state property. - */ - _stateChangeHandler(newValue) { - this.log.debug( - this._stateProperty, - 'of device', - this.device.type, - this.device.id, - 'changed to', - newValue - ) - - this.platformAccessory - .getService(Service.GarageDoorOpener) - .getCharacteristic(Characteristic.CurrentDoorState) - .setValue(this.currentState) - } - - detach() { - this.device - .removeListener( - 'change:' + this._stateProperty, - this._stateChangeHandler, - this - ) - - super.detach() - } - } - - return BasicGarageDoorOpenerAbility -} diff --git a/abilities/garage-door-switch.js b/abilities/garage-door-switch.js new file mode 100644 index 0000000..55cc255 --- /dev/null +++ b/abilities/garage-door-switch.js @@ -0,0 +1,214 @@ +const { handleFailedRequest } = require('../error-handlers') + +module.exports = homebridge => { + const { Ability } = require('./base')(homebridge) + const Characteristic = homebridge.hap.Characteristic + const Service = homebridge.hap.Service + + class GarageDoorSwitchAbility extends Ability { + /** + * @param {string} switchProperty - The device property to trigger the garage door to open or close. + * @param {string} stateProperty - The device property used to indicate the + * garage door is closed. + * @param {function} setSwitch - A function that updates the device's switch + * state. Must return a Promise. + */ + constructor(switchProperty, stateProperty, setSwitch) { + super() + this._switchProperty = switchProperty + this._stateProperty = stateProperty + this._setSwitch = setSwitch + this._targetState = null + } + + get state() { + return this.device[this._stateProperty] || 0; + } + + get isSwitchedOn() { + return this.device[this._switchProperty] || false; + } + + get currentState() { + const CDS = Characteristic.CurrentDoorState + + if (this.state > 0) { + return CDS.CLOSED + } + + return CDS.OPEN + } + + get targetState() { + if (this._targetState !== null) { + return this._targetState + } + + const CDS = Characteristic.CurrentDoorState + const TDS = Characteristic.TargetDoorState + const cs = this.currentState + return cs === CDS.OPEN || cs === CDS.OPENING ? TDS.OPEN : TDS.CLOSED + } + + _setupPlatformAccessory() { + const CDS = Characteristic.CurrentDoorState + const TDS = Characteristic.TargetDoorState + super._setupPlatformAccessory() + + // This is the initial setup of the garage door + this.platformAccessory.addService( + new Service.GarageDoorOpener() + .setCharacteristic(CDS, this.currentState) + .setCharacteristic(TDS, TDS.CLOSED) + .setCharacteristic(Characteristic.ObstructionDetected, false) + ) + } + + _setupEventHandlers() { + super._setupEventHandlers() + + // This is the handler to catch HomeKit events + this.platformAccessory + .getService(Service.GarageDoorOpener) + .getCharacteristic(Characteristic.TargetDoorState) + .on('set', this._targetDoorStateSetHandler.bind(this)) + + this.platformAccessory + .getService(Service.GarageDoorOpener) + .getCharacteristic(Characteristic.CurrentDoorState) + .on('get', this.getState.bind(this)); + + this.platformAccessory + .getService(Service.GarageDoorOpener) + .getCharacteristic(Characteristic.TargetDoorState) + .on('get', this.getState.bind(this)); + + // This is the handler to catch device events + // This one is always correct! + this.device + .on( + 'change:' + this._stateProperty, + this._stateChangeHandler, + this + ) + } + + /** + * Handles changes from HomeKit to the TargetDoorState characteristic. + */ + async _targetDoorStateSetHandler(newValue, callback, context) { + const d = this.device + + // If the context is shelly then this is an internal update + // to ensure that homekit is in sync with the current status + // If not, we really trigger the switch + if (context === 'shelly') { + callback() + return + } + + this._targetState = newValue + + this.log.debug( + 'Target homekit state is', + newValue + ) + + this.log.debug( + 'Setting', + this._switchProperty, + 'of device', + d.type, + d.id, + 'to', + true + ) + + try { + await this._setSwitch(true) + callback() + this.updateGarageDoorState() + } catch (e) { + handleFailedRequest( + this.log, + d, + e, + 'Failed to set ' + this._switchProperty + ) + callback(e) + } + } + + /** + * Handles changes from the device to the state property. + * This means either the garage door just closed or it has started to open. + */ + _stateChangeHandler(newValue) { + this.log.debug( + this._stateProperty, + 'of device', + this.device.type, + this.device.id, + 'changed to', + newValue + ) + + this.updateGarageDoorState() + } + + updateGarageDoorState() { + const CDS = Characteristic.CurrentDoorState + const TDS = Characteristic.TargetDoorState + + if (this.currentState === CDS.CLOSED) { + this.platformAccessory + .getService(Service.GarageDoorOpener) + .getCharacteristic(TDS) + .setValue(TDS.CLOSED, null, 'shelly') + this.platformAccessory + .getService(Service.GarageDoorOpener) + .setCharacteristic(CDS, CDS.CLOSING) + + setTimeout(() => { + this.platformAccessory + .getService(Service.GarageDoorOpener) + .setCharacteristic(CDS, CDS.CLOSED) + }, 1000) + } else { + this.platformAccessory + .getService(Service.GarageDoorOpener) + .getCharacteristic(TDS) + .setValue(TDS.OPEN, null, 'shelly') + this.platformAccessory + .getService(Service.GarageDoorOpener) + .setCharacteristic(CDS, CDS.OPENING) + + setTimeout(() => { + this.platformAccessory + .getService(Service.GarageDoorOpener) + .setCharacteristic(CDS, CDS.OPEN) + }, 1000) + } + } + + getState(callback) { + const CDS = Characteristic.CurrentDoorState + const TDS = Characteristic.TargetDoorState + + callback(null, this.currentState); + } + + detach() { + this.device + .removeListener( + 'change:' + this._stateProperty, + this._stateChangeHandler, + this + ) + + super.detach() + } + } + + return GarageDoorSwitchAbility +} diff --git a/accessories/factory.js b/accessories/factory.js index f43d173..b8a0741 100644 --- a/accessories/factory.js +++ b/accessories/factory.js @@ -5,6 +5,7 @@ module.exports = homebridge => { } = require('./doors')(homebridge) const { + Shelly1GarageDoorSwitchAccessory, Shelly2GarageDoorOpenerAccessory, } = require('./garage-door-openers')(homebridge) @@ -165,6 +166,7 @@ module.exports = homebridge => { } else if (accessoryType === 'valve') { return new ShellyRelayValveAccessory(this.device, ...opts) } + return new ShellyRelaySwitchAccessory(this.device, ...opts) } } @@ -427,6 +429,14 @@ module.exports = homebridge => { get numberOfPowerMeters() { return 0 } + + _createAccessory(accessoryType, ...opts) { + if (accessoryType === 'garageDoorOpener') { + return new Shelly1GarageDoorSwitchAccessory(this.device, ...opts) + } + + return super._createAccessory(accessoryType, ...opts) + } } FACTORIES.set('SHSW-1', Shelly1Factory) diff --git a/accessories/garage-door-openers.js b/accessories/garage-door-openers.js index 2f52622..4d328ad 100644 --- a/accessories/garage-door-openers.js +++ b/accessories/garage-door-openers.js @@ -1,19 +1,19 @@ module.exports = homebridge => { const Accessory = homebridge.hap.Accessory - const BasicGarageDoorOpenerAbility = - require('../abilities/basic-garage-door-opener')(homebridge) + const GarageDoorSwitchAbility = + require('../abilities/garage-door-switch')(homebridge) const GarageDoorOpenerAbility = require('../abilities/garage-door-opener')(homebridge) - const { ShellyRelayAccessory } = require('./base')(homebridge) + const { ShellyAccessory, ShellyRelayAccessory } = require('./base')(homebridge) - class Shelly1GarageDoorOpenerAccessory extends ShellyRelayAccessory { + class Shelly1GarageDoorSwitchAccessory extends ShellyRelayAccessory { constructor(device, index, config, log) { - super('garageDoorOpener', device, index, config, log) + super('garageDoorSwitch', device, index, config, log) - this.abilities.push(new BasicGarageDoorOpenerAbility( - 'rollerPosition', - 'rollerState', + this.abilities.push(new GarageDoorSwitchAbility( + 'relay' + index, + 'input' + index, this.setRelay.bind(this) )) } @@ -22,7 +22,7 @@ module.exports = homebridge => { return Accessory.Categories.GARAGE_DOOR_OPENER } } - + class Shelly2GarageDoorOpenerAccessory extends ShellyAccessory { constructor(device, index, config, log) { super('garageDoorOpener', device, index, config, log) @@ -49,7 +49,7 @@ module.exports = homebridge => { } return { - Shelly1GarageDoorOpenerAccessory, + Shelly1GarageDoorSwitchAccessory, Shelly2GarageDoorOpenerAccessory } } From 3ee601782e38bf4673ee6cddc597bb775e0ad1a5 Mon Sep 17 00:00:00 2001 From: Michael Vanderheeren Date: Mon, 1 Jun 2020 23:15:17 +0200 Subject: [PATCH 05/13] added the garage door switch --- abilities/garage-door-switch.js | 16 +++++++--------- accessories/garage-door-openers.js | 5 ++++- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/abilities/garage-door-switch.js b/abilities/garage-door-switch.js index 55cc255..f0985a3 100644 --- a/abilities/garage-door-switch.js +++ b/abilities/garage-door-switch.js @@ -7,7 +7,8 @@ module.exports = homebridge => { class GarageDoorSwitchAbility extends Ability { /** - * @param {string} switchProperty - The device property to trigger the garage door to open or close. + * @param {string} switchProperty - The device property to trigger the + * garage door to open or close. * @param {string} stateProperty - The device property used to indicate the * garage door is closed. * @param {function} setSwitch - A function that updates the device's switch @@ -22,11 +23,11 @@ module.exports = homebridge => { } get state() { - return this.device[this._stateProperty] || 0; + return this.device[this._stateProperty] || 0 } get isSwitchedOn() { - return this.device[this._switchProperty] || false; + return this.device[this._switchProperty] || false } get currentState() { @@ -76,12 +77,12 @@ module.exports = homebridge => { this.platformAccessory .getService(Service.GarageDoorOpener) .getCharacteristic(Characteristic.CurrentDoorState) - .on('get', this.getState.bind(this)); + .on('get', this.getState.bind(this)) this.platformAccessory .getService(Service.GarageDoorOpener) .getCharacteristic(Characteristic.TargetDoorState) - .on('get', this.getState.bind(this)); + .on('get', this.getState.bind(this)) // This is the handler to catch device events // This one is always correct! @@ -192,10 +193,7 @@ module.exports = homebridge => { } getState(callback) { - const CDS = Characteristic.CurrentDoorState - const TDS = Characteristic.TargetDoorState - - callback(null, this.currentState); + callback(null, this.currentState) } detach() { diff --git a/accessories/garage-door-openers.js b/accessories/garage-door-openers.js index 4d328ad..f8ad241 100644 --- a/accessories/garage-door-openers.js +++ b/accessories/garage-door-openers.js @@ -5,7 +5,10 @@ module.exports = homebridge => { require('../abilities/garage-door-switch')(homebridge) const GarageDoorOpenerAbility = require('../abilities/garage-door-opener')(homebridge) - const { ShellyAccessory, ShellyRelayAccessory } = require('./base')(homebridge) + const { + ShellyAccessory, + ShellyRelayAccessory + } = require('./base')(homebridge) class Shelly1GarageDoorSwitchAccessory extends ShellyRelayAccessory { constructor(device, index, config, log) { From 4dbba6acabd6da3454e2dc155149d5f5f1f81f6e Mon Sep 17 00:00:00 2001 From: Michael Vanderheereneren Date: Sun, 7 Mar 2021 20:52:08 +0100 Subject: [PATCH 06/13] Change from garageDoorSwitch to GarageDoorOpener --- accessories/garage-door-openers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/accessories/garage-door-openers.js b/accessories/garage-door-openers.js index f8ad241..0b28763 100644 --- a/accessories/garage-door-openers.js +++ b/accessories/garage-door-openers.js @@ -12,7 +12,7 @@ module.exports = homebridge => { class Shelly1GarageDoorSwitchAccessory extends ShellyRelayAccessory { constructor(device, index, config, log) { - super('garageDoorSwitch', device, index, config, log) + super('garageDoorOpener', device, index, config, log) this.abilities.push(new GarageDoorSwitchAbility( 'relay' + index, From 4325eae9e1cbadc3fcf4a53e83b38caa651eaa4f Mon Sep 17 00:00:00 2001 From: Michael Vanderheereneren Date: Sun, 7 Mar 2021 20:56:21 +0100 Subject: [PATCH 07/13] Remove unused property isSwitchedOn from garageDoorSwitch --- abilities/garage-door-switch.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/abilities/garage-door-switch.js b/abilities/garage-door-switch.js index f0985a3..17db72a 100644 --- a/abilities/garage-door-switch.js +++ b/abilities/garage-door-switch.js @@ -26,10 +26,6 @@ module.exports = homebridge => { return this.device[this._stateProperty] || 0 } - get isSwitchedOn() { - return this.device[this._switchProperty] || false - } - get currentState() { const CDS = Characteristic.CurrentDoorState From cecb408c73cee959f17b69674518dfedde5fde25 Mon Sep 17 00:00:00 2001 From: Michael Vanderheereneren Date: Sun, 7 Mar 2021 21:03:18 +0100 Subject: [PATCH 08/13] Remove unnecessary get handlers --- abilities/garage-door-switch.js | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/abilities/garage-door-switch.js b/abilities/garage-door-switch.js index 17db72a..91546db 100644 --- a/abilities/garage-door-switch.js +++ b/abilities/garage-door-switch.js @@ -70,16 +70,6 @@ module.exports = homebridge => { .getCharacteristic(Characteristic.TargetDoorState) .on('set', this._targetDoorStateSetHandler.bind(this)) - this.platformAccessory - .getService(Service.GarageDoorOpener) - .getCharacteristic(Characteristic.CurrentDoorState) - .on('get', this.getState.bind(this)) - - this.platformAccessory - .getService(Service.GarageDoorOpener) - .getCharacteristic(Characteristic.TargetDoorState) - .on('get', this.getState.bind(this)) - // This is the handler to catch device events // This one is always correct! this.device From ad9abf9612a21d98332e50625c0f1f82cad6673d Mon Sep 17 00:00:00 2001 From: Michael Vanderheereneren Date: Sun, 7 Mar 2021 21:27:55 +0100 Subject: [PATCH 09/13] Fix reference to error-handlers module --- abilities/garage-door-switch.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/abilities/garage-door-switch.js b/abilities/garage-door-switch.js index 91546db..1a6489f 100644 --- a/abilities/garage-door-switch.js +++ b/abilities/garage-door-switch.js @@ -1,4 +1,4 @@ -const { handleFailedRequest } = require('../error-handlers') +const { handleFailedRequest } = require('../util/error-handlers') module.exports = homebridge => { const { Ability } = require('./base')(homebridge) From c96e0e9440ac109a7307f6c02c95c1c855d87d0c Mon Sep 17 00:00:00 2001 From: Michael Vanderheereneren Date: Sun, 7 Mar 2021 21:49:46 +0100 Subject: [PATCH 10/13] Adapt to the way services setup is handled now --- abilities/garage-door-switch.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/abilities/garage-door-switch.js b/abilities/garage-door-switch.js index 1a6489f..fa0099a 100644 --- a/abilities/garage-door-switch.js +++ b/abilities/garage-door-switch.js @@ -22,6 +22,10 @@ module.exports = homebridge => { this._targetState = null } + get service() { + return this.platformAccessory.getService(Service.GarageDoorOpener) + } + get state() { return this.device[this._stateProperty] || 0 } From 2c312d4353a7f636ee53e17aa2a2bdbda150e50b Mon Sep 17 00:00:00 2001 From: Michael Vanderheereneren Date: Sun, 7 Mar 2021 22:02:30 +0100 Subject: [PATCH 11/13] Adapt to the way services setup is handled now - continued --- abilities/garage-door-switch.js | 37 ++++++++++++--------------------- 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/abilities/garage-door-switch.js b/abilities/garage-door-switch.js index fa0099a..c9c551d 100644 --- a/abilities/garage-door-switch.js +++ b/abilities/garage-door-switch.js @@ -51,26 +51,21 @@ module.exports = homebridge => { return cs === CDS.OPEN || cs === CDS.OPENING ? TDS.OPEN : TDS.CLOSED } - _setupPlatformAccessory() { + _createService() { const CDS = Characteristic.CurrentDoorState const TDS = Characteristic.TargetDoorState - super._setupPlatformAccessory() - - // This is the initial setup of the garage door - this.platformAccessory.addService( - new Service.GarageDoorOpener() - .setCharacteristic(CDS, this.currentState) - .setCharacteristic(TDS, TDS.CLOSED) - .setCharacteristic(Characteristic.ObstructionDetected, false) - ) + + return new Service.GarageDoorOpener() + .setCharacteristic(CDS, this.currentState) + .setCharacteristic(TDS, TDS.CLOSED) + .setCharacteristic(Characteristic.ObstructionDetected, false) } _setupEventHandlers() { super._setupEventHandlers() // This is the handler to catch HomeKit events - this.platformAccessory - .getService(Service.GarageDoorOpener) + this.service(Service.GarageDoorOpener) .getCharacteristic(Characteristic.TargetDoorState) .on('set', this._targetDoorStateSetHandler.bind(this)) @@ -152,31 +147,25 @@ module.exports = homebridge => { const TDS = Characteristic.TargetDoorState if (this.currentState === CDS.CLOSED) { - this.platformAccessory - .getService(Service.GarageDoorOpener) + this.service(Service.GarageDoorOpener) .getCharacteristic(TDS) .setValue(TDS.CLOSED, null, 'shelly') - this.platformAccessory - .getService(Service.GarageDoorOpener) + this.service(Service.GarageDoorOpener) .setCharacteristic(CDS, CDS.CLOSING) setTimeout(() => { - this.platformAccessory - .getService(Service.GarageDoorOpener) + this.service(Service.GarageDoorOpener) .setCharacteristic(CDS, CDS.CLOSED) }, 1000) } else { - this.platformAccessory - .getService(Service.GarageDoorOpener) + this.service(Service.GarageDoorOpener) .getCharacteristic(TDS) .setValue(TDS.OPEN, null, 'shelly') - this.platformAccessory - .getService(Service.GarageDoorOpener) + this.service(Service.GarageDoorOpener) .setCharacteristic(CDS, CDS.OPENING) setTimeout(() => { - this.platformAccessory - .getService(Service.GarageDoorOpener) + this.service(Service.GarageDoorOpener) .setCharacteristic(CDS, CDS.OPEN) }, 1000) } From 83969d8b142ccdb18a770a03f78401b626752d9a Mon Sep 17 00:00:00 2001 From: Michael Vanderheereneren Date: Sun, 7 Mar 2021 22:20:47 +0100 Subject: [PATCH 12/13] Adapt to the way services setup is handled now - continued --- abilities/garage-door-switch.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/abilities/garage-door-switch.js b/abilities/garage-door-switch.js index c9c551d..958c715 100644 --- a/abilities/garage-door-switch.js +++ b/abilities/garage-door-switch.js @@ -65,7 +65,7 @@ module.exports = homebridge => { super._setupEventHandlers() // This is the handler to catch HomeKit events - this.service(Service.GarageDoorOpener) + this.service .getCharacteristic(Characteristic.TargetDoorState) .on('set', this._targetDoorStateSetHandler.bind(this)) @@ -147,25 +147,25 @@ module.exports = homebridge => { const TDS = Characteristic.TargetDoorState if (this.currentState === CDS.CLOSED) { - this.service(Service.GarageDoorOpener) + this.service .getCharacteristic(TDS) .setValue(TDS.CLOSED, null, 'shelly') - this.service(Service.GarageDoorOpener) + this.service .setCharacteristic(CDS, CDS.CLOSING) setTimeout(() => { - this.service(Service.GarageDoorOpener) + this.service .setCharacteristic(CDS, CDS.CLOSED) }, 1000) } else { - this.service(Service.GarageDoorOpener) + this.service .getCharacteristic(TDS) .setValue(TDS.OPEN, null, 'shelly') - this.service(Service.GarageDoorOpener) + this.service .setCharacteristic(CDS, CDS.OPENING) setTimeout(() => { - this.service(Service.GarageDoorOpener) + this.service .setCharacteristic(CDS, CDS.OPEN) }, 1000) } From 7a85268a4771d7acfa0aaea38dc5c22e4006ae75 Mon Sep 17 00:00:00 2001 From: Michael Vanderheereneren Date: Mon, 8 Mar 2021 08:55:47 +0100 Subject: [PATCH 13/13] Remove calling updateGarageDoorState when we set the switch --- abilities/garage-door-switch.js | 1 - 1 file changed, 1 deletion(-) diff --git a/abilities/garage-door-switch.js b/abilities/garage-door-switch.js index 958c715..e8f1286 100644 --- a/abilities/garage-door-switch.js +++ b/abilities/garage-door-switch.js @@ -113,7 +113,6 @@ module.exports = homebridge => { try { await this._setSwitch(true) callback() - this.updateGarageDoorState() } catch (e) { handleFailedRequest( this.log,