-
Notifications
You must be signed in to change notification settings - Fork 57
Added resetAutoReleaseOnDetection property to sensors #169
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ function HttpWebHookSensorAccessory(ServiceParam, CharacteristicParam, platform, | |
this.type = sensorConfig["type"]; | ||
this.autoRelease = sensorConfig["autoRelease"]; | ||
this.autoReleaseTime = sensorConfig["autoReleaseTime"] || Constants.DEFAULT_SENSOR_TIMEOUT; | ||
this.resetAutoReleaseOnDetection = sensorConfig["resetAutoReleaseOnDetection"]; | ||
|
||
this.informationService = new Service.AccessoryInformation(); | ||
this.informationService.setCharacteristic(Characteristic.Manufacturer, "HttpWebHooksPlatform"); | ||
|
@@ -79,35 +80,19 @@ HttpWebHookSensorAccessory.prototype.changeFromServer = function(urlParams) { | |
this.storage.setItemSync("http-webhook-" + this.id, urlValue); | ||
this.log.debug("cached: "+ cached); | ||
this.log.debug("cached !== urlValue: "+ (cached !== urlValue)); | ||
|
||
|
||
if (cached !== urlValue) { | ||
this.log("Change HomeKit value for " + this.type + " sensor to '%s'.", urlValue); | ||
|
||
if (this.type === "contact") { | ||
this.service.getCharacteristic(Characteristic.ContactSensorState).updateValue(urlValue ? Characteristic.ContactSensorState.CONTACT_DETECTED : Characteristic.ContactSensorState.CONTACT_NOT_DETECTED, undefined, Constants.CONTEXT_FROM_WEBHOOK); | ||
if (this.autoRelease) { | ||
setTimeout(function() { | ||
this.storage.setItemSync("http-webhook-" + this.id, true); | ||
this.service.getCharacteristic(Characteristic.ContactSensorState).updateValue(Characteristic.ContactSensorState.CONTACT_DETECTED, undefined, Constants.CONTEXT_FROM_TIMEOUTCALL); | ||
}.bind(this), this.autoReleaseTime); | ||
} | ||
} | ||
else if (this.type === "motion") { | ||
this.service.getCharacteristic(Characteristic.MotionDetected).updateValue(urlValue, undefined, Constants.CONTEXT_FROM_WEBHOOK); | ||
if (this.autoRelease) { | ||
setTimeout(function() { | ||
this.storage.setItemSync("http-webhook-" + this.id, false); | ||
this.service.getCharacteristic(Characteristic.MotionDetected).updateValue(false, undefined, Constants.CONTEXT_FROM_TIMEOUTCALL); | ||
}.bind(this), this.autoReleaseTime); | ||
} | ||
} | ||
else if (this.type === "occupancy") { | ||
this.service.getCharacteristic(Characteristic.OccupancyDetected).updateValue(urlValue ? Characteristic.OccupancyDetected.OCCUPANCY_DETECTED : Characteristic.OccupancyDetected.OCCUPANCY_NOT_DETECTED, undefined, Constants.CONTEXT_FROM_WEBHOOK); | ||
if (this.autoRelease) { | ||
setTimeout(function() { | ||
this.storage.setItemSync("http-webhook-" + this.id, false); | ||
this.service.getCharacteristic(Characteristic.OccupancyDetected).updateValue(Characteristic.OccupancyDetected.OCCUPANCY_NOT_DETECTED, undefined, Constants.CONTEXT_FROM_TIMEOUTCALL); | ||
}.bind(this), this.autoReleaseTime); | ||
} | ||
} | ||
else if (this.type === "smoke") { | ||
this.service.getCharacteristic(Characteristic.SmokeDetected).updateValue(urlValue ? Characteristic.SmokeDetected.SMOKE_DETECTED : Characteristic.SmokeDetected.SMOKE_NOT_DETECTED, undefined, Constants.CONTEXT_FROM_WEBHOOK); | ||
|
@@ -130,6 +115,9 @@ HttpWebHookSensorAccessory.prototype.changeFromServer = function(urlParams) { | |
} | ||
|
||
} | ||
|
||
this.setAutoReleaseTimeout(); | ||
|
||
return { | ||
"success" : true | ||
}; | ||
|
@@ -163,4 +151,27 @@ HttpWebHookSensorAccessory.prototype.getServices = function() { | |
return [ this.service, this.informationService ]; | ||
}; | ||
|
||
HttpWebHookSensorAccessory.prototype.resetToInitialState = function () { | ||
if (this.type === "contact") { | ||
this.storage.setItemSync("http-webhook-" + this.id, true); | ||
this.service.getCharacteristic(Characteristic.ContactSensorState).updateValue(Characteristic.ContactSensorState.CONTACT_DETECTED, undefined, Constants.CONTEXT_FROM_TIMEOUTCALL); | ||
} else if (this.type === "motion") { | ||
this.storage.setItemSync("http-webhook-" + this.id, false); | ||
this.service.getCharacteristic(Characteristic.MotionDetected).updateValue(false, undefined, Constants.CONTEXT_FROM_TIMEOUTCALL); | ||
} else if (this.type === "occupancy") { | ||
this.storage.setItemSync("http-webhook-" + this.id, false); | ||
this.service.getCharacteristic(Characteristic.OccupancyDetected).updateValue(Characteristic.OccupancyDetected.OCCUPANCY_NOT_DETECTED, undefined, Constants.CONTEXT_FROM_TIMEOUTCALL); | ||
} | ||
} | ||
|
||
HttpWebHookSensorAccessory.prototype.setAutoReleaseTimeout = function () { | ||
if (!this.autoRelease || !this.resetAutoReleaseOnDetection) return; | ||
clearTimeout(this[this.getTimeoutKey()]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clearing and resetting timeout should only be done if |
||
this[this.getTimeoutKey()] = setTimeout(this.resetToInitialState.bind(this), this.autoReleaseTime); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment above. |
||
} | ||
|
||
HttpWebHookSensorAccessory.prototype.getTimeoutKey = function() { | ||
return `autoReleaseTimeout-${this.type}-${this.id}`; | ||
} | ||
|
||
module.exports = HttpWebHookSensorAccessory; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This prevents autoreleases if not
resetAutoReleaseOnDetection
is set. The flagresetAutoReleaseOnDetection
should be handled as optional.