From d38516f77971bdeac34640bdd054a178d33fac10 Mon Sep 17 00:00:00 2001 From: Maria Jesus Cresci Date: Thu, 30 Mar 2017 14:15:37 -0700 Subject: [PATCH] MSA-1866: Test for starting with this --- .../mcresci/motionjc.src/motionjc.groovy | 36 ++++++ devicetypes/mcresci/switch.src/switch.groovy | 48 ++++++++ devicetypes/mcresci/testjc.src/testjc.groovy | 47 ++++++++ smartapps/mcrescii/jcapp.src/jcapp.groovy | 110 ++++++++++++++++++ 4 files changed, 241 insertions(+) create mode 100644 devicetypes/mcresci/motionjc.src/motionjc.groovy create mode 100644 devicetypes/mcresci/switch.src/switch.groovy create mode 100644 devicetypes/mcresci/testjc.src/testjc.groovy create mode 100644 smartapps/mcrescii/jcapp.src/jcapp.groovy diff --git a/devicetypes/mcresci/motionjc.src/motionjc.groovy b/devicetypes/mcresci/motionjc.src/motionjc.groovy new file mode 100644 index 0000000..c873133 --- /dev/null +++ b/devicetypes/mcresci/motionjc.src/motionjc.groovy @@ -0,0 +1,36 @@ +/** + * motionJC + * + * Copyright 2017 Maria Jesus Cresci + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License + * for the specific language governing permissions and limitations under the License. + * + */ +metadata { + definition (name: "motionJC", namespace: "mcresci", author: "Maria Jesus Cresci") { + capability "Motion Sensor" + } + + + simulator { + // TODO: define status and reply messages here + } + + tiles { + // TODO: define your main and details tiles here + } +} + +// parse events into attributes +def parse(String description) { + log.debug "Parsing '${description}'" + // TODO: handle 'motion' attribute + +} \ No newline at end of file diff --git a/devicetypes/mcresci/switch.src/switch.groovy b/devicetypes/mcresci/switch.src/switch.groovy new file mode 100644 index 0000000..08e17a3 --- /dev/null +++ b/devicetypes/mcresci/switch.src/switch.groovy @@ -0,0 +1,48 @@ +/** + * Switch + * + * Copyright 2017 Maria Jesus Cresci + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License + * for the specific language governing permissions and limitations under the License. + * + */ +metadata { + definition (name: "Switch", namespace: "mcresci", author: "Maria Jesus Cresci") { + capability "Switch" + } + + } + + + simulator { + // TODO: define status and reply messages here + } + + tiles{ + + } + +// parse events into attributes +def parse(String description) { + log.debug "Parsing '${description}'" + // TODO: handle 'switch' attribute + +} + +// handle commands +def on() { + log.debug "Executing 'on'" + // TODO: handle 'on' command +} + +def off() { + log.debug "Executing 'off'" + // TODO: handle 'off' command +} \ No newline at end of file diff --git a/devicetypes/mcresci/testjc.src/testjc.groovy b/devicetypes/mcresci/testjc.src/testjc.groovy new file mode 100644 index 0000000..34fd923 --- /dev/null +++ b/devicetypes/mcresci/testjc.src/testjc.groovy @@ -0,0 +1,47 @@ +/** + * testJC + * + * Copyright 2017 Maria Jesus Cresci + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License + * for the specific language governing permissions and limitations under the License. + * + */ +metadata { + definition (name: "testJC", namespace: "mcresci", author: "Maria Jesus Cresci") { + capability "Switch" + } + + + simulator { + // TODO: define status and reply messages here + } + + tiles { + // TODO: define your main and details tiles here + } +} + +// parse events into attributes +def parse(String description) { + log.debug "Parsing '${description}'" + // TODO: handle 'switch' attribute + +} + +// handle commands +def on() { + log.debug "Executing 'on'" + theswitch.on() +} + +def off() { + log.debug "Executing 'off'" + theswitch.off() +} \ No newline at end of file diff --git a/smartapps/mcrescii/jcapp.src/jcapp.groovy b/smartapps/mcrescii/jcapp.src/jcapp.groovy new file mode 100644 index 0000000..46c597b --- /dev/null +++ b/smartapps/mcrescii/jcapp.src/jcapp.groovy @@ -0,0 +1,110 @@ +/** + * jcApp + * + * Copyright 2017 Maria Jesus Cresci + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License + * for the specific language governing permissions and limitations under the License. + * + */ +definition( + name: "jcApp", + namespace: "mcrescii", + author: "Maria Jesus Cresci", + description: "Description test", + category: "", + iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png", + iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png", + iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png") + + +preferences { + section("Turn on when motion detected:") { + input "themotion", "capability.motionSensor", required: true, title: "Where?" + } + section("Send Push Notification?") { + input "sendPush", "bool", required: false, + title: "Send Push Notification when motion?" + } + section("Turn off when there's been no movement for") { + input "minutes", "number", required: true, title: "Minutes?" + } + section("Turn on this light") { + input "theswitch", "capability.switch", required: true + } +} + +def installed() { + initialize() +} + +def updated() { + unsubscribe() + initialize() +} + +def initialize() { + subscribe(themotion, "motion.active", motionDetectedHandler) + subscribe(themotion, "motion.inactive", motionStoppedHandler) + subscribe(theswitch, "switch.off", switchOFFHandler) + subscribe(theswitch, "switch.on", switchONHandler) +} + +def switchOFFHandler(evt){ + log.debug "switchOFFHandler called: $evt" + if (sendPush) { + sendPush("Switch OFF") + } +} + +def switchONHandler(evt){ + log.debug "switchOFFHandler called: $evt" + if (sendPush) { + sendPush("Switch ON") + } +} + +def motionDetectedHandler(evt) { + log.debug "motionDetectedHandler called: $evt" + theswitch.on() + if (sendPush) { + sendPush("Atenti, se detecto movimiento!") + } +} + +def motionStoppedHandler(evt) { + log.debug "motionStoppedHandler called: $evt" + runIn(60 * minutes, checkMotion) +} + +def checkMotion() { + log.debug "In checkMotion scheduled method" + + def motionState = themotion.currentState("motion") + + if (motionState.value == "inactive") { + // get the time elapsed between now and when the motion reported inactive + def elapsed = now() - motionState.date.time + + // elapsed time is in milliseconds, so the threshold must be converted to milliseconds too + def threshold = 1000 * 60 * minutes + + if (elapsed >= threshold) { + log.debug "Motion has stayed inactive long enough since last check ($elapsed ms): turning switch off" + theswitch.off() + } else { + log.debug "Motion has not stayed inactive long enough since last check ($elapsed ms): doing nothing" + } + } else { + // Motion active; just log it and do nothing + log.debug "Motion is active, do nothing and wait for inactive" + } +} + +// TODO: implement event handlers \ No newline at end of file