forked from jason0x43/hubitat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jason0x43-darksky.groovy
87 lines (77 loc) · 2.44 KB
/
jason0x43-darksky.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
/**
* DarkSky weather driver
*/
metadata {
definition(name: 'DarkSky', namespace: 'jason0x43', author: 'Jason Cheatham') {
capability('Sensor')
capability('Temperature Measurement')
capability('Illuminance Measurement')
capability('Relative Humidity Measurement')
capability('Refresh')
attribute('temperatureHi', 'number')
attribute('temperatureHiTime', 'number')
attribute('temperatureLo', 'number')
attribute('temperatureLoTime', 'number')
attribute('temperature', 'number')
attribute('precipChance', 'number')
attribute('cloudCover', 'number')
}
}
preferences {
input(
name: 'apiKey',
type: 'text',
title: 'Dark Sky API Key:',
description: '',
required: true
)
input(
name: 'pollInterval',
type: 'enum',
title: 'Poll interval',
defaultValue: '30 Minutes',
options: ['10 Minutes', '30 Minutes', '1 Hour']
)
}
def installed() {
init()
}
def updated() {
init()
}
def init() {
unschedule()
if (settings.apiKey) {
def pollInterval = (settings.pollInterval ?: '30 Minutes').replace(' ', '')
"runEvery${pollInterval}"(poll)
log.debug "Scheduled poll for every ${pollInterval}"
// poll()
log.debug "${location.hubs[0]}"
}
}
def poll() {
try {
log.debug "Requesting data..."
def latitude = location.getLatitude()
def longitude = location.getLongitude()
httpGet(
"https://api.darksky.net/forecast/${settings.apiKey}/${latitude},${longitude}"
) { resp ->
def data = resp.data
def now = data.currently;
sendEvent(name: 'temperature', value: now.temperature, unit: 'F')
sendEvent(name: 'precipChance', value: now.precipProbability, unit: '%')
sendEvent(name: 'cloudCover', value: now.cloudCover, unit: '%')
def today = data.daily.data[0];
sendEvent(name: 'temperatureHi', value: today.temperatureHigh, unit: 'F')
sendEvent(name: 'temperatureHiTime', value: today.temperatureHighTime, unit: 's')
sendEvent(name: 'temperatureLo', value: today.temperatureLow, unit: 'F')
sendEvent(name: 'temperatureLoTime', value: today.temperatureLowTime, unit: 's')
}
} catch (e) {
log.error "Error updating: ${e}"
}
}
def refresh() {
poll()
}