Update DeviceTamperAlarm.groovy
[smartapps.git] / third-party / ecobeeManageClimate.groovy
1 /**
2  *  ecobeeManageClimate
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  *  N.B. Requires MyEcobee device available at 
18  *          http://www.ecomatiqhomes.com/#!store/tc3yr 
19  */
20 definition(
21         name: "ecobeeManageClimate",
22         namespace: "yracine",
23         author: "Yves Racine",
24         description: "Allows a user to manage ecobee's climates",
25         category: "My Apps",
26         iconUrl: "https://s3.amazonaws.com/smartapp-icons/Partner/ecobee.png",
27         iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Partner/ecobee@2x.png"
28 )
29
30
31 preferences {
32         section("About") {
33                 paragraph "ecobeeManageClimate, the smartapp that manages your ecobee climates ['creation', 'update', 'delete']" 
34                 paragraph "Version 1.9.5"
35                 paragraph "If you like this smartapp, please support the developer via PayPal and click on the Paypal link below " 
36                         href url: "https://www.paypal.me/ecomatiqhomes",
37                                 title:"Paypal donation..."
38                 paragraph "Copyright©2014 Yves Racine"
39                         href url:"http://github.com/yracine/device-type.myecobee", style:"embedded", required:false, title:"More information..."  
40                                 description: "http://github.com/yracine/device-type.myecobee/blob/master/README.md"
41         }
42
43         section("For this ecobee thermostat") {
44                 input "ecobee", "device.myEcobeeDevice", title: "Ecobee Thermostat"
45         }
46         section("Create (if not present) or update this climate") {
47                 input "climateName", "text", title: "Climate Name"
48         }
49         section("Or delete the Climate [default=false]") {
50                 input "deleteClimate", "Boolean", title: "delete?", metadata: [values: ["true", "false"]], required: false
51         }
52         section("Substitute Climate name in schedule (used for delete)") {
53                 input "subClimateName", "text", title: "Climate Name", required: false
54         }
55         section("Cool Temp [default = 75°F/23°C]") {
56                 input "givenCoolTemp", "decimal", title: "Cool Temp", required: false
57         }
58         section("Heat Temp [default=72°F/21°C]") {
59                 input "givenHeatTemp", "decimal", title: "Heat Temp", required: false
60         }
61         section("isOptimized [default=false]") {
62                 input "isOptimizedFlag", "Boolean", title: "isOptimized?", metadata: [values: ["true", "false"]], required: false
63         }
64         section("isOccupied [default=false]") {
65                 input "isOccupiedFlag", "Boolean", title: "isOccupied?", metadata: [values: ["true", "false"]], required: false
66         }
67         section("Cool Fan Mode [default=auto]") {
68                 input "givenCoolFanMode", "enum", title: "Cool Fan Mode ?", metadata: [values: ["auto", "on"]], required: false
69         }
70         section("Heat Fan Mode [default=auto]") {
71                 input "givenHeatFanMode", "enum", title: "Heat Fan Mode ?", metadata: [values: ["auto", "on"]], required: false
72         }
73         section("Notifications") {
74                 input "sendPushMessage", "enum", title: "Send a push notification?", metadata: [values: ["Yes", "No"]], required: false
75                 input "phoneNumber", "phone", title: "Send a text message?", required: false
76         }
77     
78
79 }
80
81
82
83 def installed() {
84
85         subscribe(app, appTouch)
86         takeAction()    
87
88 }
89
90
91 def updated() {
92
93
94         unsubscribe()    
95         subscribe(app, appTouch)
96         takeAction()    
97
98
99 }
100
101 def appTouch(evt) {  
102         takeAction()
103 }
104
105 def takeAction() {
106         def scale = getTemperatureScale()
107         def heatTemp, coolTemp
108         if (scale == 'C') {
109                 heatTemp = givenHeatTemp ?: 21 // by default, 21°C is the heat temp
110                 coolTemp = givenCoolTemp ?: 23 // by default, 23°C is the cool temp
111         } else {
112                 heatTemp = givenHeatTemp ?: 72 // by default, 72°F is the heat temp
113                 coolTemp = givenCoolTemp ?: 75 // by default, 75°F is the cool temp
114         }
115
116         def isOptimized = (isOptimizedFlag != null) ? isOptimizedFlag : false // by default, isOptimized flag is false
117         def isOccupied = (isOccupiedFlag != null) ? isOccupiedFlag : false // by default, isOccupied flag is false
118         def coolFanMode = givenCoolFanMode ?: 'auto' // By default, fanMode is auto
119         def heatFanMode = givenHeatFanMode ?: 'auto' // By default, fanMode is auto
120         def deleteClimateFlag = (deleteClimate != null) ? deleteClimate : 'false'
121
122         log.debug "ecobeeManageClimate> about to take actions"
123
124
125         log.trace "ecobeeManageClimate>climateName =${climateName},deleteClimateFlag =${deleteClimateFlag},subClimateName= ${subClimateName}, isOptimized=${isOptimized}" +
126                 ",isOccupied=${isOccupied},coolTemp = ${coolTemp},heatTemp = ${heatTemp},coolFanMode= ${coolFanMode}, heatFanMode= ${heatFanMode}"
127
128         if (deleteClimateFlag == 'true') {
129                 send("ecobeeManageClimate>about to delete climateName = ${climateName}")
130                 ecobee.deleteClimate("", climateName, subClimateName)
131
132         } else {
133
134                 send("ecobeeManageClimate>about to create or update climateName = ${climateName}")
135                 ecobee.updateClimate("", climateName, deleteClimateFlag, subClimateName,
136                         coolTemp, heatTemp, isOptimized, isOccupied, coolFanMode, heatFanMode)
137         }
138
139 }
140
141 private send(msg) {
142         if (sendPushMessage != "No") {
143                 log.debug("sending push message")
144                 sendPush(msg)
145         }
146
147         if (phone) {
148                 log.debug("sending text message")
149                 sendSms(phone, msg)
150         }
151
152         log.debug msg
153 }