-
Notifications
You must be signed in to change notification settings - Fork 16
/
virtualCalendarExt.groovy
142 lines (124 loc) · 4.05 KB
/
virtualCalendarExt.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* Virtual Calendar
*
* Licensed Virtual the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
* for the specific language governing permissions and limitations under the License.
*
* Change History:
*
* Date Who What
* ---- --- ----
* 2020-11-30 thebearmay Original version 1.0.0
* 2020-12-01 thebearmay Version 1.1.0 add dayOfYear and holiday logic
*
*/
static String version() { return '1.1.0' }
metadata {
definition (
name: "Virtual Calendar Extension Device",
namespace: "thebearmay",
author: "Jean P. May, Jr.",
importUrl:"https://raw.githubusercontent.com/thebearmay/hubitat/main/virtualCalendarExt.groovy"
) {
capability "Actuator"
attribute "inputDate", "string"
attribute "calWk", "string"
attribute "dayOfWeekStr", "string"
attribute "dayOfWeek", "string"
attribute "dayOfYear", "string"
attribute "Date1", "string"
attribute "Date2", "string"
attribute "Date3", "string"
attribute "Date4", "string"
attribute "Date5", "string"
attribute "Date6", "string"
attribute "Date7", "string"
attribute "Date8", "string"
attribute "Date9", "string"
attribute "Date10", "string"
attribute "Date11", "string"
attribute "Date12", "string"
attribute "isHoliday", "bool"
command "configure", []
command "storeHoliday", ["number", "string"]
}
}
preferences {
input("debugEnable", "bool", title: "Enable debug logging?")
}
def updateValues() {
dayMap=[
1:"Sunday",
2:"Monday",
3:"Tuesday",
4:"Wednesday",
5:"Thursday",
6:"Friday",
7:"Saturday"]
dateNow = new Date()
calWk = dateNow.getAt(Calendar.WEEK_OF_YEAR)
sendEvent(name: "calWk", value: calWk)
dayOfWeek = dateNow.getAt(Calendar.DAY_OF_WEEK)
sendEvent(name: "dayOfWeek", value: dayOfWeek)
dayOfWeekStr = dayMap[dayOfWeek]
sendEvent(name: "dayOfWeekStr", value: dayOfWeekStr)
dayOfYear = dateNow[Calendar.DAY_OF_YEAR]
sendEvent(name: "dayOfYear", value: dayOfYear)
checkHoliday()
midnight=dateNow+1
midnight.clearTime()
secondsToMidnight = Math.round((midnight.getTime() - new Date().getTime())/1000)+1
if(debugEnable) log.debug "values updated...next update in $secondsToMidnight seconds"
runIn(secondsToMidnight,updateValues)
}
def storeHoliday(inx, holiDateStr) {
try {
date1 = Date.parse("yyyy-MM-dd",holiDateStr)
if(inx > 0 && inx < 13)
sendEvent(name:"Date"+inx, value: (holiDateStr))
else
log.warn "index out of bounds (1-12):$inx"
} catch (Exception e) {
log.warn "Invalid date string use format yyyy-MM-dd"
}
checkHoliday()
}
def checkHoliday(){
isHoliday = false
if(debugEnable) log.debug "checkHoliday()"
for (i=1; i<13; i++) {
holiday = device.currentValue("Date"+i)
if(debugEnable) log.debug "checkHoliday $holiday"
if (holiday != null){
date1 = Date.parse("yyyy-MM-dd",holiday)
date2 = new Date()
date2 = date2.clearTime()
if (date1 == date2){
isHoliday = true
break
}
if (debugEnable) log.debug "date1: $date1|date2: $date2"
}
}
sendEvent(name:"isHoliday", value:isHoliday)
}
def installed() {
log.trace "installed()"
}
def configure() {
if(debugEnable) log.debug "configure()"
updateValues()
}
def updated(){
log.trace "updated()"
if(debugEnable) runIn(1800,logsOff)
}
void logsOff(){
device.updateSetting("debugEnable",[value:"false",type:"bool"])
}