Update keep-me-cozy-ii.groovy
[smartapps.git] / official / thermostat.groovy
1 /**
2  *  Copyright 2017 SmartThings
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  *  in compliance with the License. You may obtain a copy of the License at:
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
10  *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
11  *  for the specific language governing permissions and limitations under the License.
12  *
13  *  Thermostats
14  *
15  *  Author: Juan Pablo Risso
16  *  Date: 2017-12-05
17  *
18  */
19
20 definition(
21                 name: "Thermostats",
22                 namespace: "smartthings",
23                 author: "SmartThings",
24                 description: "Receive notifications when anything happens in your home.",
25                 category: "SmartSolutions",
26                 iconUrl: "https://s3.amazonaws.com/smartapp-icons/SafetyAndSecurity/Cat-SafetyAndSecurity.png",
27                 iconX2Url: "https://s3.amazonaws.com/smartapp-icons/SafetyAndSecurity/Cat-SafetyAndSecurity@2x.png",
28                 singleInstance: true
29 )
30
31 preferences {
32   page name: "mainPage", install: true, uninstall: true
33 }
34
35 def mainPage() {
36   dynamicPage(name:"mainPage") {
37         section("Choose one or more, when..."){
38                 input "smokeDevices", "capability.smokeDetector", title: "Smoke Detected", required: false, multiple: true
39                 input "carbonMonoxideDevices", "capability.carbonMonoxideDetector", title: "Carbon Monoxide Detected", required: false, multiple: true
40         }
41         section("Turn off these thermostats"){
42                 input "thermostatDevices", "capability.thermostat", title: "Thermostats", required: true, multiple: true
43         }
44         section("Send this message (optional, sends standard status message if not specified)"){
45                 input "messageText", "text", title: "Message Text", required: false
46         }
47
48         if (location.contactBookEnabled || phone) {
49                 section("Via a push notification and/or an SMS message"){
50                         input("recipients", "contact", title: "Send notifications to") {
51                                 input "phone", "phone", title: "Enter a phone number to get SMS", required: false
52                                 paragraph "If outside the US please make sure to enter the proper country code"
53                                 input "pushAndPhone", "enum", title: "Notify me via Push Notification", required: false, options: ["Yes", "No"]
54                         }
55                 }
56         } else {
57                 section("Via a push notification"){
58                         input "pushAndPhone", "enum", title: "Notify me via Push Notification", required: false, options: ["Yes", "No"]
59                 }
60         }
61         section("Minimum time between messages (optional, defaults to every message)") {
62                 input "frequency", "decimal", title: "Minutes", required: false
63         }
64   }
65 }
66
67 def installed() {
68         log.debug "Installed with settings: ${settings}"
69         subscribeToEvents()
70 }
71
72 def updated() {
73         log.debug "Updated with settings: ${settings}"
74         unsubscribe()
75         subscribeToEvents()
76 }
77
78 def subscribeToEvents() {
79         subscribe(smokeDevices, "smoke.detected", eventHandler)
80         subscribe(smokeDevices, "smoke.tested", eventHandler)
81         subscribe(smokeDevices, "carbonMonoxide.detected", eventHandler)
82         subscribe(carbonMonoxideDevices, "carbonMonoxide.detected", eventHandler)
83 }
84
85 def eventHandler(evt) {
86         log.debug "Notify got evt ${evt}"
87         // Turn off thermostat
88         thermostatDevices*.setThermostatMode("off")
89         if (frequency) {
90                 def lastTime = state[evt.deviceId]
91                 if (lastTime == null || now() - lastTime >= frequency * 60000) {
92                         sendMessage(evt)
93                 }
94         }
95         else {
96                 sendMessage(evt)
97         }
98 }
99
100 private sendMessage(evt) {
101         String msg = messageText
102         Map options = [:]
103
104         if (!messageText) {
105                 msg = '{{ triggerEvent.descriptionText }}'
106                 options = [translatable: true, triggerEvent: evt]
107         }
108         log.debug "$evt.name:$evt.value, pushAndPhone:$pushAndPhone, '$msg'"
109
110         if (location.contactBookEnabled) {
111                 sendNotificationToContacts(msg, recipients, options)
112         } else {
113                 if (phone) {
114                         options.phone = phone
115                         if (pushAndPhone != 'No') {
116                                 log.debug 'Sending push and SMS'
117                                 options.method = 'both'
118                         } else {
119                                 log.debug 'Sending SMS'
120                                 options.method = 'phone'
121                         }
122                 } else if (pushAndPhone != 'No') {
123                         log.debug 'Sending push'
124                         options.method = 'push'
125                 } else {
126                         log.debug 'Sending nothing'
127                         options.method = 'none'
128                 }
129                 sendNotification(msg, options)
130         }
131         if (frequency) {
132                 state[evt.deviceId] = now()
133         }
134 }