Update hue-mood-lighting.groovy
[smartapps.git] / third-party / ecobeeSetClimate.groovy
1 /**
2  *  ecobeeSetClimate
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  * You may want to create multiple instances of this smartapp (and rename them in SmartSetup) for each time
19  * you want to set a different Climate at a given day and time during the week.*
20  *
21  *  N.B. Requires MyEcobee device available at 
22  *          http://www.ecomatiqhomes.com/#!store/tc3yr 
23  */
24 definition(
25         name: "ecobeeSetClimate",
26         namespace: "yracine",
27         author: "Yves Racine",
28         description: "This script allows an ecobee user to set a Climate at a given day & time",
29         category: "My Apps",
30         iconUrl: "https://s3.amazonaws.com/smartapp-icons/Partner/ecobee.png",
31         iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Partner/ecobee@2x.png"
32 )
33
34
35
36 preferences {
37
38         page(name: "selectThermostats", title: "Thermostats", install: false, uninstall: true, nextPage: "selectProgram") {
39                 section("About") {
40                         paragraph "ecobeeSetClimate, the smartapp that sets your ecobee thermostat to a given climate at a given day & time"
41                         paragraph "Version 1.2" 
42                         paragraph "If you like this smartapp, please support the developer via PayPal and click on the Paypal link below " 
43                                 href url: "https://www.paypal.me/ecomatiqhomes",
44                                         title:"Paypal donation..."
45                         paragraph "Copyright©2014 Yves Racine"
46                                 href url:"http://github.com/yracine/device-type.myecobee", style:"embedded", required:false, title:"More information..."  
47                                         description: "http://github.com/yracine/device-type.myecobee/blob/master/README.md"
48                 }
49                 section("Set the ecobee thermostat(s)") {
50                         input "ecobee", "device.myEcobeeDevice", title: "Which ecobee thermostat(s)?", multiple: true
51
52                 }
53                 section("Configuration") {
54                         input "dayOfWeek", "enum",
55                                 title: "Which day of the week?",
56                                 multiple: false,
57                                 metadata: [
58                                         values: [
59                                                 'All Week',
60                                                 'Monday to Friday',
61                                                 'Saturday & Sunday',
62                                                 'Monday',
63                                                 'Tuesday',
64                                                 'Wednesday',
65                                                 'Thursday',
66                                                 'Friday',
67                                                 'Saturday',
68                                                 'Sunday'
69                                         ]
70                                 ]
71                         input "begintime", "time", title: "Beginning time"
72                 }
73
74         }
75         page(name: "selectProgram", title: "Ecobee Programs", content: "selectProgram")
76         page(name: "Notifications", title: "Notifications Options", install: true, uninstall: true) {
77                 section("Notifications") {
78                         input "sendPushMessage", "enum", title: "Send a push notification?", metadata: [values: ["Yes", "No"]], required:
79                                 false
80                         input "phone", "phone", title: "Send a Text Message?", required: false
81                 }
82                 section([mobileOnly:true]) {
83                         label title: "Assign a name for this SmartApp", required: false
84                         mode title: "Set for specific mode(s)", required: false
85                 }
86         }
87 }
88
89
90 def selectProgram() {
91         def ecobeePrograms = ecobee.currentClimateList.toString().minus('[').minus(']').tokenize(',')
92         log.debug "programs: $ecobeePrograms"
93
94
95         return dynamicPage(name: "selectProgram", title: "Select Ecobee Program", install: false, uninstall: true, nextPage:
96                 "Notifications") {
97                 section("Select Program") {
98                         input "givenClimate", "enum", title: "Which program?", options: ecobeePrograms, required: true
99                 }
100         }
101 }
102
103
104
105 def installed() {
106         // subscribe to these events
107         initialize()
108 }
109
110 def updated() {
111         // we have had an update
112         // remove everything and reinstall
113         unsubscribe()
114         unschedule()    
115         initialize()
116 }
117
118 def initialize() {
119
120         log.debug "Scheduling setClimate for day " + dayOfWeek + " at begin time " + begintime
121         subscribe(ecobee, "climateList", climateListHandler)
122
123         schedule(begintime, setClimate)
124         subscribe(app, setClimateNow)    
125
126 }
127 def climateListHandler(evt) {
128         log.debug "thermostat's Climates List: $evt.value, $settings"
129 }
130
131 def setClimateNow(evt) {
132         setClimate()
133 }
134
135 def setClimate() {
136         def climateName = (givenClimate ?: 'Home').capitalize()
137
138
139         def doChange = IsRightDayForChange()
140
141         // If we have hit the condition to schedule this then lets do it
142
143         if (doChange == true) {
144                 log.debug "setClimate, location.mode = $location.mode, newMode = $newMode, location.modes = $location.modes"
145
146                 ecobee.each {
147                         it.setThisTstatClimate(climateName)
148                 }            
149                 send("ecobeeSetClimate>set ${ecobee} to ${climateName} program as requested")
150         } else {
151                 log.debug "climate change to ${climateName} not scheduled for today."
152         }
153         log.debug "End of Fcn"
154 }
155
156
157 def IsRightDayForChange() {
158
159         def makeChange = false
160         String currentDay = new Date().format("E", location.timeZone)
161     
162         // Check the condition under which we want this to run now
163         // This set allows the most flexibility.
164         if (dayOfWeek == 'All Week') {
165                 makeChange = true
166         } else if ((dayOfWeek == 'Monday' || dayOfWeek == 'Monday to Friday') && currentDay == 'Mon') {
167                 makeChange = true
168         } else if ((dayOfWeek == 'Tuesday' || dayOfWeek == 'Monday to Friday') && currentDay == 'Tue') {
169                 makeChange = true
170         } else if ((dayOfWeek == 'Wednesday' || dayOfWeek == 'Monday to Friday') && currentDay == 'Wed') {
171                 makeChange = true
172         } else if ((dayOfWeek == 'Thursday' || dayOfWeek == 'Monday to Friday') && currentDay == 'Thu') {
173                 makeChange = true
174         } else if ((dayOfWeek == 'Friday' || dayOfWeek == 'Monday to Friday') &&  currentDay == 'Fri') {
175                 makeChange = true
176         } else if ((dayOfWeek == 'Saturday' || dayOfWeek == 'Saturday & Sunday') && currentDay == 'Sat') {
177                 makeChange = true
178         } else if ((dayOfWeek == 'Sunday' || dayOfWeek == 'Saturday & Sunday') && currentDay == 'Sun' ) {
179                 makeChange = true
180         }
181
182
183         // some debugging in order to make sure things are working correclty
184         log.debug "Calendar DOW: " + currentDay
185         log.debug "SET DOW: " + dayOfWeek
186
187         return makeChange
188 }
189
190
191 private send(msg) {
192         if (sendPushMessage != "No") {
193                 log.debug("sending push message")
194                 sendPush(msg)
195         }
196
197         if (phoneNumber) {
198                 log.debug("sending text message")
199                 sendSms(phoneNumber, msg)
200         }
201
202         log.debug msg
203 }
204
205
206
207 // catchall
208 def event(evt) {
209         log.debug "value: $evt.value, event: $evt, settings: $settings, handlerName: ${evt.handlerName}"
210 }