Update WindowOrDoorOpen.groovy
[smartapps.git] / official / smart-home-ventilation.groovy
1 /**
2  *      Smart Home Ventilation
3  *      Version 2.1.2 - 5/31/15
4  *
5  *      Copyright 2015 Michael Struck
6  *
7  *      Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  *      in compliance with the License. You may obtain a copy of the License at:
9  *
10  *              http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *      Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
13  *      on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
14  *      for the specific language governing permissions and limitations under the License.
15  *
16  */
17
18 definition(
19         name: "Smart Home Ventilation",
20     namespace: "MichaelStruck",
21     author: "Michael Struck",
22     description: "Allows for setting up various schedule scenarios for turning on and off home ventilation switches.",
23     category: "Convenience",
24     iconUrl: "https://raw.githubusercontent.com/MichaelStruck/SmartThings/master/Other-SmartApps/Smart-Home-Ventilation/HomeVent.png",
25     iconX2Url: "https://raw.githubusercontent.com/MichaelStruck/SmartThings/master/Other-SmartApps/Smart-Home-Ventilation/HomeVent@2x.png",
26     iconX3Url: "https://raw.githubusercontent.com/MichaelStruck/SmartThings/master/Other-SmartApps/Smart-Home-Ventilation/HomeVent@2x.png")
27
28 preferences {
29         page name: "mainPage"
30 }
31
32 //----Scheduling Pages
33 page(name: "A_Scenario") {
34         section{
35                         input "timeOnA1", title: "Schedule 1 time to turn on", "time", required: false
36                 input "timeOffA1", title: "Schedule 1 time to turn off", "time", required: false
37                 }
38         section{
39                         input "timeOnA2", title: "Schedule 2 time to turn on", "time", required: false
40                 input "timeOffA2", title: "Schedule 2 time to turn off", "time", required: false
41                 }
42         section{
43                 input "timeOnA3", title: "Schedule 3 time to turn on", "time", required: false
44                 input "timeOffA3", title: "Schedule 3 time to turn off", "time", required: false
45                 }
46         section{
47                 input "timeOnA4", title: "Schedule 4 time to turn on", "time", required: false
48                 input "timeOffA4", title: "Schedule 4 time to turn off", "time", required: false
49                 }
50                 section ("Options") {
51                 input "titleA", title: "Assign a scenario name", "text", required: false
52             input "modeA", "mode", required: false, multiple: true, title: "Run in specific mode(s)", description: "Choose Modes"
53                         input "daysA", "enum", multiple: true, title: "Run on specific day(s)", description: "Choose Days", required: false, options: ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
54                 }
55     }
56
57
58 page(name: "pageAbout", title: "About ${textAppName()}") {
59         section {
60             paragraph "${textVersion()}\n${textCopyright()}\n\n${textLicense()}\n"
61         }
62         section("Instructions") {
63             paragraph textHelp()
64         }
65 }
66
67 def mainPage() {
68         dynamicPage(name: "mainPage", title: "", install: true, uninstall: true) {
69         section("Select ventilation switches..."){
70                         input "switches", title: "Switches", "capability.switch", multiple: true
71                 }
72         section([mobileOnly:true], "Options") {
73                         label(title: "Assign a name", required: false, defaultValue: "Smart Home Ventilation")
74                         href "pageAbout", title: "About ${textAppName()}", description: "Tap to get application version, license and instructions"
75         }
76     }
77 }
78
79 // Install and initiate
80
81 def installed() {
82     log.debug "Installed with settings: ${settings}"
83     init()
84 }
85
86 def updated() {
87     unschedule()
88     turnOffSwitch() //Turn off all switches if the schedules are changed while in mid-schedule
89     unsubscribe()
90     log.debug "Updated with settings: ${settings}"
91     init()
92 }
93
94 def init() {
95     schedule ("someTime", midNight)
96     subscribe(location, "mode", locationHandler)
97     startProcess()
98 }
99
100 // Common methods
101
102 def startProcess () {
103         startDay()
104 }
105
106 def startDay() {
107     turnOnSwitch()
108 }
109
110 def incDay() {
111     turnOffSwitch()
112 }
113
114 def locationHandler(evt) {
115     def result = false
116     state.modeChange = true
117     switches.each {
118         if (it.currentValue("switch")=="on"){
119            result = true
120         }
121     }
122         if (!result) {
123         startProcess()
124     }
125 }
126
127 def midNight(){
128     startProcess()
129 }
130
131 def turnOnSwitch() {
132     switches.on()
133     log.debug "Home ventilation switches are on."
134 }
135
136 def turnOffSwitch() {
137     switches.each {
138         if (it.currentValue("switch")=="on"){
139                         it.off()
140         }
141     }
142     log.debug "Home ventilation switches are off."
143 }
144
145
146 //Version/Copyright/Information/Help
147
148 private def textAppName() {
149         def text = "Smart Home Ventilation"
150 }
151
152 private def textVersion() {
153     def text = "Version 2.1.2 (05/31/2015)"
154 }
155
156 private def textCopyright() {
157     def text = "Copyright © 2015 Michael Struck"
158 }
159
160 private def textLicense() {
161     def text =
162                 "Licensed under the Apache License, Version 2.0 (the 'License'); "+
163                 "you may not use this file except in compliance with the License. "+
164                 "You may obtain a copy of the License at"+
165                 "\n\n"+
166                 "    http://www.apache.org/licenses/LICENSE-2.0"+
167                 "\n\n"+
168                 "Unless required by applicable law or agreed to in writing, software "+
169                 "distributed under the License is distributed on an 'AS IS' BASIS, "+
170                 "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. "+
171                 "See the License for the specific language governing permissions and "+
172                 "limitations under the License."
173 }
174
175 private def textHelp() {
176         def text =
177         "Within each scenario, choose a start and end time for the ventilation fan. You can have up to 4 different " +
178         "venting scenarios, and 4 schedules within each scenario. Each scenario can be restricted to specific modes or certain days of the week. It is recommended "+
179         "that each scenario does not overlap and run in separate modes (i.e. Home, Out of town, etc). Also note that you should  " +
180         "avoid scheduling the ventilation fan at exactly midnight; the app resets itself at that time. It is suggested to start any new schedule " +
181         "at 12:15 am or later."
182 }