Update hvac-auto-off.smartapp.groovy
[smartapps.git] / third-party / ecobeeControlPlug.groovy
1 /**
2  *  ecobeeControlPlug
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  *  N.B. Requires MyEcobee device available at 
19  *          http://www.ecomatiqhomes.com/#!store/tc3yr 
20  */
21
22 definition(
23         name: "ecobeeControlPlug",
24         namespace: "yracine",
25         author: "Yves Racine",
26         description: "Control a plug attached to an ecobee device",
27         category: "My Apps",
28         iconUrl: "https://s3.amazonaws.com/smartapp-icons/Partner/ecobee.png",
29         iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Partner/ecobee@2x.png"
30 )
31
32
33 preferences {
34         section("About") {
35                 paragraph "ecobeeControlPlug, the smartapp that controls your ecobee connected sensor or plug"
36                 paragraph "Version 1.1.4" 
37                 paragraph "If you like this smartapp, please support the developer via PayPal and click on the Paypal link below " 
38                         href url: "https://www.paypal.me/ecomatiqhomes",
39                                 title:"Paypal donation..."
40                 paragraph "Copyright©2014 Yves Racine"
41                         href url:"http://github.com/yracine/device-type.myecobee", style:"embedded", required:false, title:"More information..."  
42                                 description: "http://github.com/yracine/device-type.myecobee/blob/master/README.md"
43         }
44
45         section("For this Ecobee thermostat") {
46                 input "ecobee", "device.myEcobeeDevice", title: "Ecobee Thermostat"
47         }
48         section("Control this SmartPlug Name") {
49                 input "plugName", "text", title: "SmartPlug Name"
50         }
51         section("Target control State") {
52                 input "plugState", "enum", title: "Control State?", metadata: [values: ["on", "off", "resume", ]]
53         }
54         section("Hold Type") {
55                 input "givenHoldType", "enum", title: "Hold Type?", metadata: [values: ["dateTime", "nextTransition", "indefinite"]]
56         }
57         section("For 'dateTime' holdType, Start date for the hold (format = DD-MM-YYYY)") {
58                 input "givenStartDate", "text", title: "Beginning Date", required: false
59         }
60         section("For 'dateTime' holdType, Start time for the hold (HH:MM,24HR)") {
61                 input "givenStartTime", "text", title: "Beginning time", required: false
62         }
63         section("For 'dateTime' holdType, End date for the hold (format = DD-MM-YYYY)") {
64                 input "givenEndDate", "text", title: "End Date", required: false
65         }
66         section("For 'dateTime' holdType, End time for the hold (HH:MM,24HR)") {
67                 input "givenEndTime", "text", title: "End time", required: false
68         }
69
70
71 }
72
73
74 def installed() {
75
76         initialize()
77 }
78
79
80 def updated() {
81
82         unsubscribe()   
83         initialize()
84
85
86 }
87
88 def initialize() {
89         ecobee.poll()
90         subscribe(app, appTouch)
91 }
92
93 private void sendMsgWithDelay() {
94
95         if (state?.msg) {
96                 send state.msg
97         }
98 }
99
100 def appTouch(evt) {
101         log.debug "ecobeeControlPlug> about to take actions"
102         def plugSettings = [holdType: "${givenHoldType}"]
103
104
105         if (givenHoldType == "dateTime") {
106
107                 if ((!givenStartDate) || (!givenEndDate) || (!givenStartTime) || (!givenEndTime)) {
108                         state?.msg="ecobeeControlPlug>holdType=dateTime and dates/times are not valid for controlling plugName ${plugName}"
109                         log.error state.msg
110                         runIn(30, "sendMsgWithDelay")
111                         return
112                 }
113                 plugSettings = [holdType: "dateTime", startDate: "${givenStartDate}", startTime: "${givenStartTime}", endDate: "${givenEndDate}", endTime: "${givenEndTime}"]
114         }
115         log.debug("About to call controlPlug for thermostatId=${thermostatId}, plugName=${plugName}")
116         ecobee.controlPlug("", plugName, plugState, plugSettings)
117 }
118
119
120 private send(msg) {
121         if (sendPushMessage != "No") {
122                 log.debug("sending push message")
123                 sendPush(msg)
124         }
125
126         if (phoneNumber) {
127                 log.debug("sending text message")
128                 sendSms(phoneNumber, msg)
129         }
130
131         log.debug msg
132 }