Update FireCO2Alarm.groovy
[smartapps.git] / official / shabbat-and-holiday-modes.groovy
1 /**
2  *  HebcalModes
3  *
4  *  Author: danielbarak@live.com
5  *  Date: 2014-02-21
6  */
7
8 // Automatically generated. Make future change here.
9 definition(
10     name: "Shabbat and Holiday Modes",
11     namespace: "ShabbatHolidayMode",
12     author: "danielbarak@live.com",
13     description: "Changes the mode at candle lighting and back after havdalah.  Uses the HebCal.com API to look for days that are shabbat or chag and pull real time candle lighting and havdalah times to change modes automatically",
14     category: "My Apps",
15     iconUrl: "http://upload.wikimedia.org/wikipedia/commons/thumb/4/49/Star_of_David.svg/200px-Star_of_David.svg.png",
16     iconX2Url: "http://upload.wikimedia.org/wikipedia/commons/thumb/4/49/Star_of_David.svg/200px-Star_of_David.svg.png",
17     iconX3Url: "http://upload.wikimedia.org/wikipedia/commons/thumb/4/49/Star_of_David.svg/200px-Star_of_David.svg.png")
18
19 preferences {
20         
21         section("At Candlelighting Change Mode To:") 
22     {
23                 input "startMode", "mode", title: "Mode?"
24         }
25     section("At Havdalah Change Mode To:") 
26     {
27                 input "endMode", "mode", title: "Mode?"
28         }
29         section("Havdalah Offset (Usually 50 or 72)") {
30                 input "havdalahOffset", "number", title: "Minutes After Sundown", required:true
31         } 
32         section("Your ZipCode") {
33                 input "zipcode", "text", title: "ZipCode", required:true
34         }
35     section( "Notifications" ) {
36         input "sendPushMessage", "enum", title: "Send a push notification?", metadata:[values:["Yes","No"]], required:false
37         input "phone", "phone", title: "Send a Text Message?", required: false
38     }
39     /**/
40 }
41
42 def installed() {
43         log.debug "Installed with settings: ${settings}"
44         initialize()
45 }
46
47 def updated() {
48         log.debug "Updated with settings: ${settings}"
49         unsubscribe()
50         initialize()
51 }
52
53 def initialize() {
54     poll();
55     schedule("0 0 8 1/1 * ? *", poll) 
56 }
57
58 //Check hebcal for today's candle lighting or havdalah
59 def poll()
60 {
61         
62     unschedule("endChag")
63     unschedule("setChag")
64         Hebcal_WebRequest()
65
66 }//END def poll()
67
68
69
70 /**********************************************
71 // HEBCAL FUNCTIONS
72 -----------------------------------------------*/
73
74 //This function is the web request and response parse
75 def Hebcal_WebRequest(){
76
77 def today = new Date().format("yyyy-MM-dd")
78 //def today = "2014-11-14"
79 def zip = settings.zip as String
80 def locale = getWeatherFeature("geolookup", zip)
81 def timezone = TimeZone.getTimeZone(locale.location.tz_long)
82 def hebcal_date
83 def hebcal_category
84 def hebcal_title
85 def candlelighting
86 def candlelightingLocalTime
87 def havdalah
88 def havdalahLocalTime
89 def pushMessage
90 def testmessage
91 def urlRequest = "http://www.hebcal.com/hebcal/?v=1&cfg=json&nh=off&nx=off&year=now&month=now&mf=off&c=on&zip=${zipcode}&m=${havdalahOffset}&s=off&D=off&d=off&o=off&ss=off"
92 log.trace "${urlRequest}"
93
94 def hebcal = { response ->
95         hebcal_date = response.data.items.date
96         hebcal_category = response.data.items.category
97         hebcal_title = response.data.items.title
98     
99     for (int i = 0; i < hebcal_date.size; i++) 
100     {
101         if(hebcal_date[i].split("T")[0]==today)
102         {
103                 if(hebcal_category[i]=="candles")
104                 {
105                         candlelightingLocalTime = HebCal_GetTime12(hebcal_title[i])
106                 pushMessage = "Candle Lighting is at ${candlelightingLocalTime}"
107                 candlelightingLocalTime = HebCal_GetTime24(hebcal_date[i])
108                                 candlelighting = timeToday(candlelightingLocalTime, timezone)  
109                
110                                 sendMessage(pushMessage)
111                         schedule(candlelighting, setChag)     
112                 log.debug pushMessage
113                 }//END if(hebcal_category=="candles")
114     
115                 else if(hebcal_category[i]=="havdalah")
116                 {
117                         havdalahLocalTime = HebCal_GetTime12(hebcal_title[i])
118                 pushMessage = "Havdalah is at ${havdalahLocalTime}"
119                 havdalahLocalTime = HebCal_GetTime24(hebcal_date[i])
120                                 havdalah = timeToday(havdalahLocalTime, timezone)
121                 testmessage = "Scheduling for ${havdalah}"
122                         schedule(havdalah, endChag)      
123                 log.debug pushMessage
124                 log.debug testmessage
125                 }//END if(hebcal_category=="havdalah"){
126         }//END if(hebcal_date[i].split("T")[0]==today)
127         
128     }//END for (int i = 0; i < hebcal_date.size; i++)
129  }//END def hebcal = { response ->
130 httpGet(urlRequest, hebcal);
131 }//END def queryHebcal()
132
133
134 //This function gets candle lighting time
135 def HebCal_GetTime12(hebcal_title){
136 def returnTime = hebcal_title.split(":")[1] + ":" + hebcal_title.split(":")[2] + " "
137 return returnTime
138 }//END def HebCal_GetTime12()
139
140 //This function gets candle lighting time
141 def HebCal_GetTime24(hebcal_date){
142 def returnTime = hebcal_date.split("T")[1]
143 returnTime = returnTime.split("-")[0]
144 return returnTime
145 }//END def HebCal_GetTime12()
146
147 /*-----------------------------------------------
148  END OF HEBCAL FUNCTIONS
149 -----------------------------------------------*/
150 def setChag()
151 {
152         
153         if (location.mode != startMode) 
154         {
155                 if (location.modes?.find{it.name == startMode}) 
156         {
157                         setLocationMode(startMode)
158                         //sendMessage("Changed the mode to '${startMode}'")
159             def dayofweek = new Date().format("EEE")
160                 if(dayofweek=='Fri'){
161                                 sendMessage("Shabbat Shalom!")
162                 }
163                 else{
164                         sendMessage("Chag Sameach!")
165                 }
166             
167                 }//END if (location.modes?.find{it.name == startMode})
168                 else 
169         {
170                         sendMessage("Tried to change to undefined mode '${startMode}'")
171                 }//END else
172         }//END if (location.mode != newMode)  
173     
174     unschedule("setChag")
175 }//END def setChag()
176
177
178 def endChag()
179 {
180         
181         if (location.mode != endMode) 
182         {
183                 if (location.modes?.find{it.name == endMode}) 
184         {
185                         setLocationMode(endMode)
186                         sendMessage("Changed the mode to '${endMode}'")
187                 }//END if (location.modes?.find{it.name == endMode})
188                 else 
189         {
190                         sendMessage("Tried to change to undefined mode '${endMode}'")
191                 }//END else
192         }//END if (location.mode != endMode)
193     
194         //sendMessage("Shavuah Tov!")
195     unschedule("endChag")
196 }//END def setChag()
197
198 def sendMessage(msg){
199 if ( sendPushMessage != "No" ) {
200         log.debug( "sending push message" )
201         //sendPush( msg )
202     }
203
204     if ( phone ) {
205         log.debug( "sending text message" )
206         sendSms( phone, msg )
207     }
208 }//END def sendMessage(msg)