-
-
Notifications
You must be signed in to change notification settings - Fork 46
Add Shelly1 as GarageDoorOpener - actually Garage Door Switch #239
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
Open
MichaelVdheeren
wants to merge
13
commits into
alexryd:master
Choose a base branch
from
MichaelVdheeren:garage-door-shelly-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f13ae1e
Create basic-garage-door-opener.js
MichaelVdheeren 2f70e47
Update garage-door-openers.js
MichaelVdheeren 391064c
Update basic-garage-door-opener.js
MichaelVdheeren 73e4f61
changed to garage-door-switch
MichaelVdheeren 3ee6017
added the garage door switch
MichaelVdheeren 4dbba6a
Change from garageDoorSwitch to GarageDoorOpener
MichaelVdheeren 4325eae
Remove unused property isSwitchedOn from garageDoorSwitch
MichaelVdheeren cecb408
Remove unnecessary get handlers
MichaelVdheeren ad9abf9
Fix reference to error-handlers module
MichaelVdheeren c96e0e9
Adapt to the way services setup is handled now
MichaelVdheeren 2c312d4
Adapt to the way services setup is handled now - continued
MichaelVdheeren 83969d8
Adapt to the way services setup is handled now - continued
MichaelVdheeren 7a85268
Remove calling updateGarageDoorState when we set the switch
MichaelVdheeren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| const { handleFailedRequest } = require('../util/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 service() { | ||
| return this.platformAccessory.getService(Service.GarageDoorOpener) | ||
| } | ||
|
|
||
| get state() { | ||
| return this.device[this._stateProperty] || 0 | ||
| } | ||
|
|
||
| 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 | ||
| } | ||
|
|
||
| _createService() { | ||
| const CDS = Characteristic.CurrentDoorState | ||
| const TDS = Characteristic.TargetDoorState | ||
|
|
||
| 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.service | ||
| .getCharacteristic(Characteristic.TargetDoorState) | ||
| .on('set', this._targetDoorStateSetHandler.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() | ||
| } 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.service | ||
| .getCharacteristic(TDS) | ||
| .setValue(TDS.CLOSED, null, 'shelly') | ||
| this.service | ||
| .setCharacteristic(CDS, CDS.CLOSING) | ||
|
|
||
| setTimeout(() => { | ||
| this.service | ||
| .setCharacteristic(CDS, CDS.CLOSED) | ||
| }, 1000) | ||
| } else { | ||
| this.service | ||
| .getCharacteristic(TDS) | ||
| .setValue(TDS.OPEN, null, 'shelly') | ||
| this.service | ||
| .setCharacteristic(CDS, CDS.OPENING) | ||
|
|
||
| setTimeout(() => { | ||
| this.service | ||
| .setCharacteristic(CDS, CDS.OPEN) | ||
| }, 1000) | ||
| } | ||
| } | ||
|
|
||
| getState(callback) { | ||
| callback(null, this.currentState) | ||
| } | ||
|
|
||
| detach() { | ||
| this.device | ||
| .removeListener( | ||
| 'change:' + this._stateProperty, | ||
| this._stateChangeHandler, | ||
| this | ||
| ) | ||
|
|
||
| super.detach() | ||
| } | ||
| } | ||
|
|
||
| return GarageDoorSwitchAbility | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.