Update turn-on-by-zip-code.groovy
[smartapps.git] / third-party / ecobeeChangeMode.groovy
1 /**
2  *  ecobeeChangeMode
3  *
4  *  Copyright 2014 Yves Racine
5  *  LinkedIn profile: ca.linkedin.com/pub/yves-racine-m-sc-a/0/406/4b/
6  *
7  *  Developer retains all right, title, copyright, and interest, including all copyright, patent rights, trade secret 
8  *  in the Background technology. May be subject to consulting fees under the Agreement between the Developer and the Customer. 
9  *  Developer grants a non exclusive perpetual license to use the Background technology in the Software developed for and delivered 
10  *  to Customer under this Agreement. However, the Customer shall make no commercial use of the Background technology without
11  *  Developer's written consent.
12  *
13  *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
14  *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
15  *
16  *  Software Distribution is restricted and shall be done only with Developer's written approval.
17  *
18  * Change the mode manually (by pressing the app's play button) and automatically at the ecobee thermostat(s)
19  * If you need to set it for both Away and Home modes, you'd need to save them as 2 distinct apps
20  * Don't forget to set the app to run only for the target mode.
21  *
22  *  N.B. Requires MyEcobee device available at 
23  *          http://www.ecomatiqhomes.com/#!store/tc3yr 
24  */
25 definition(
26         name: "ecobeeChangeMode",
27         namespace: "yracine",
28         author: "Yves Racine",
29         description:
30         "Change the mode manually (by pressing the app's play button) and automatically at the ecobee thermostat(s)",
31         category: "My Apps",
32         iconUrl: "https://s3.amazonaws.com/smartapp-icons/Partner/ecobee.png",
33         iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Partner/ecobee@2x.png"
34 )
35
36 preferences {
37
38
39         page(name: "selectThermostats", title: "Thermostats", install: false , uninstall: true, nextPage: "selectProgram") {
40                 section("About") {
41                         paragraph "ecobeeChangeMode, the smartapp that sets your ecobee thermostat to a given program/climate ['Away', 'Home', 'Night']" + 
42                                 " based on ST hello mode."
43                         paragraph "Version 1.9.9" 
44                         paragraph "If you like this smartapp, please support the developer via PayPal and click on the Paypal link below " 
45                                 href url: "https://www.paypal.me/ecomatiqhomes",
46                                         title:"Paypal donation..."
47                         paragraph "Copyright©2014 Yves Racine"
48                                 href url:"http://github.com/yracine/device-type.myecobee", style:"embedded", required:false, title:"More information..."  
49                                         description: "http://github.com/yracine/device-type.myecobee/blob/master/README.md"
50                 }
51                 section("Change the following ecobee thermostat(s)...") {
52                         input "thermostats", "device.myEcobeeDevice", title: "Which thermostat(s)", multiple: true
53                 }
54                 section("Do the mode change manually only (by pressing the arrow to execute the smartapp)") {
55                         input "manualFlag", "bool", title: "Manual only [default=false]", description:"optional",required:false
56                 }
57         
58         }
59         page(name: "selectProgram", title: "Ecobee Programs", content: "selectProgram")
60         page(name: "Notifications", title: "Notifications Options", install: true, uninstall: true) {
61                 section("Notifications") {
62                         input "sendPushMessage", "enum", title: "Send a push notification?", metadata: [values: ["Yes", "No"]], required:
63                                 false
64                         input "phone", "phone", title: "Send a Text Message?", required: false
65                 }
66         section([mobileOnly:true]) {
67                         label title: "Assign a name for this SmartApp", required: false
68                 }
69         }
70 }
71
72
73 def selectProgram() {
74     def ecobeePrograms = thermostats[0].currentClimateList.toString().minus('[').minus(']').tokenize(',')
75         log.debug "programs: $ecobeePrograms"
76         def enumModes=[]
77         location.modes.each {
78                 enumModes << it.name
79         }    
80
81         return dynamicPage(name: "selectProgram", title: "Select Ecobee Program", install: false, uninstall: true, nextPage:
82                         "Notifications") {
83                 section("Select Program") {
84                         input "givenClimate", "enum", title: "Change to this program?", options: ecobeePrograms, required: true
85                 }
86                 section("When SmartThings' hello home mode changes to (ex. 'Away', 'Home')[optional]") {
87                         input "newMode", "enum", options: enumModes, multiple:true, required: false
88                 }
89                 section("Enter a delay in minutes [optional, default=immediately after ST hello mode change] ") {
90                         input "delay", "number", title: "Delay in minutes [default=immediate]", description:"no delay by default",required:false
91                 }
92
93         
94         }
95 }
96
97
98 def installed() {
99         initialize()    
100 }
101
102 def updated() {
103         unsubscribe()
104         initialize()    
105 }
106
107 private def initialize() {
108
109         if (!manualFlag) {
110                 subscribe(location, "mode", changeMode)
111         } else {
112                 takeAction()  
113         }    
114         subscribe(app, appTouch)
115 }
116
117 def appTouch(evt) {
118         log.debug ("changeMode>location.mode= $location.mode, givenClimate\7f=${givenClimate}, about to takeAction")
119
120         takeAction() 
121 }
122
123
124 def changeMode(evt) {
125         def message
126
127
128         if (delay) {
129                 try {
130                         unschedule(takeAction)
131                 } catch (e) {
132                         log.debug ("ecobeeChangeMode>exception when trying to unschedule: $e")    
133                 }    
134         }    
135     
136         Boolean foundMode=false        
137         newMode.each {
138         
139                 if (it==location.mode) {
140                         foundMode=true            
141                 }            
142         }        
143         log.debug ("changeMode>location.mode= $location.mode, newMode=${newMode}")
144         
145         if ((newMode != null) && (!foundMode)) {
146         
147                 log.debug "changeMode>location.mode= $location.mode, newMode=${newMode},foundMode=${foundMode}, not doing anything"
148                 return                  
149         }
150
151         if ((!delay) || (delay==null)) {
152                 log.debug ("changeMode>about to call takeAction()")
153                 takeAction()    
154         } else {
155                 runIn((delay*60), "takeAction")   
156         }    
157 }
158
159 private void takeAction() {
160         def message = "ecobeeChangeMode>setting ${thermostats} to ${givenClimate}.."
161         send(message)
162         log.debug (message)
163     
164         thermostats.each {
165                 it?.setThisTstatClimate(givenClimate)
166         }        
167 }
168
169
170
171
172 private send(msg) {
173         if (sendPushMessage != "No") {
174                 log.debug("sending push message")
175                 sendPush(msg)
176         }
177         if (phone) {
178                 log.debug("sending text message")
179                 sendSms(phone, msg)
180         }
181
182         log.debug msg
183 }