Update vacation-lighting-director.groovy
[smartapps.git] / third-party / ecobeeManageGroup.groovy
1 /**
2  *  EcobeeManageGroup
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: "ecobeeManageGroup",
22         namespace: "yracine",
23         author: "Yves Racine",
24         description: "Allows a user to create,update, and delete an ecobee group",
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 preferences {
31         section("About") {
32                 paragraph "ecobeeManageGroup, the smartapp that manages your ecobee groups ['creation', 'update', 'delete']"
33                 paragraph "Version 1.9.4" 
34                 paragraph "If you like this smartapp, please support the developer via PayPal and click on the Paypal link below " 
35                         href url: "https://www.paypal.me/ecomatiqhomes",
36                                 title:"Paypal donation..."
37                 paragraph "Copyright©2014 Yves Racine"
38                         href url:"http://github.com/yracine/device-type.myecobee", style:"embedded", required:false, title:"More information..."  
39                                 description: "http://github.com/yracine/device-type.myecobee/blob/master/README.md"
40         }
41         section("For this ecobee thermostat") {
42                 input "ecobee", "device.myEcobeeDevice", title: "Ecobee Thermostat"
43         }
44         section("Create this group or update all groups [empty for all group]") {
45                 input "groupName", "text", title: "Group Name", required: false
46         }
47         section("Or delete the group [default=false]") {
48                 input "deleteGroup", "Boolean", title: "delete?", metadata: [values: ["true", "false"]], required: false
49         }
50         section("Synchronize Vacation") {
51                 input "synchronizeVacation", "Boolean", title: "synchronize Vacation [default=false]?", metadata: [values: ["true", "false"]], required: false
52         }
53         section("Synchronize Schedule") {
54                 input "synchronizeSchedule", "Boolean", title: "synchronize Schedule [default=false]?", metadata: [values: ["true", "false"]], required: false
55         }
56         section("Synchronize SystemMode") {
57                 input "synchronizeSystemMode", "Boolean", title: "synchronize SystemMode [default=false]?", metadata: [values: ["true", "false"]], required: false
58         }
59         section("Synchronize Alerts") {
60                 input "synchronizeAlerts", "Boolean", title: "synchronize Alerts [default=false]?", metadata: [values: ["true", "false"]], required: false
61         }
62         section("Synchronize QuickSave") {
63                 input "synchronizeQuickSave", "Boolean", title: "synchronize QuickSave [default=false]?", metadata: [values: ["true", "false"]], required: false
64         }
65         section("Synchronize User Preferences") {
66                 input "synchronizeUserPreferences", "Boolean", title: "synchronize User Preferences [default=false]?", metadata: [values: ["true", "false"]], required: false
67         }
68
69         section("Notifications") {
70                 input "sendPushMessage", "enum", title: "Send a push notification?", metadata: [values: ["Yes", "No"]], required: false
71                 input "phoneNumber", "phone", title: "Send a text message?", required: false
72         }
73
74
75
76
77 }
78
79
80
81 def installed() {
82
83         ecobee.poll()
84         subscribe(app, appTouch)
85         takeAction()
86 }
87
88
89 def updated() {
90
91         unsubscribe()
92
93         ecobee.poll()
94         subscribe(app, appTouch)
95         takeAction()    
96
97
98 }
99
100 def appTouch(evt) {
101         takeAction()
102 }
103     
104
105 def takeAction() {
106         // by default,  all flags are set to false
107
108         log.debug "EcobeeManageGroup> about to take actions"
109         def syncVacationFlag = (synchronizeVacation != null) ? synchronizeVacation : 'false'
110         def syncScheduleFlag = (synchronizeSchedule != null) ? synchronizeSchedule : 'false'
111         def syncSystemModeFlag = (synchronizeSystemMode != null) ? synchronizeSystemMode : 'false'
112         def syncAlertsFlag = (synchronizeAlerts != null) ? synchronizeAlerts : 'false'
113         def syncQuickSaveFlag = (synchronizeQuickSave != null) ? synchronizeQuickSave : 'false'
114         def syncUserPreferencesFlag = (synchronizeUserPreferences != null) ? synchronizeUserPreferences : 'false'
115         def deleteGroupFlag = (deleteGroup != null) ? deleteGroup : 'false'
116
117         def groupSettings = [synchronizeVacation: "${syncVacationFlag}", synchronizeSchedule: "${syncScheduleFlag}",
118                 synchronizeSystemMode: "${syncSystemModeFlag}", synchronizeAlerts: "${syncAlertsFlag}",
119                 synchronizeQuickSave: "${syncQuickSaveFlag}", synchronizeUserPreferences: "${syncUserPreferencesFlag}"
120         ]
121
122         log.trace "ecobeeManageGroup>deleteGroupFlag=${deleteGroupFlag},  groupName = ${groupName}, groupSettings= ${groupSettings}"
123
124         if (deleteGroupFlag == 'true') {
125                 ecobee.deleteGroup(null, groupName)
126                 send("ecobeeManageGroup>groupName=${groupName} deleted")
127
128         } else if ((groupName != "") && (groupName != null)) {
129
130                 ecobee.createGroup(groupName, null, groupSettings)
131                 send("ecobeeManageGroup>groupName=${groupName} created")
132         } else {
133                 ecobee.iterateUpdateGroup(null, groupSettings)
134                 send("ecobeeManageGroup>all groups associated with thermostat updated with ${groupSettings}")
135         }
136
137 }
138
139 private send(msg) {
140         if (sendPushMessage != "No") {
141                 log.debug("sending push message")
142                 sendPush(msg)
143         }
144
145         if (phone) {
146                 log.debug("sending text message")
147                 sendSms(phone, msg)
148         }
149
150         log.debug msg
151 }