Code by Scott שאול בן ישוע
Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Sha'ul ben Yeshua
Hubitat Public Repo
Commits
a9c9eb4b
Verified
Commit
a9c9eb4b
authored
Nov 10, 2019
by
Sha'ul ben Yeshua
🎗
Browse files
added Pi-Hole stats
parents
7eaa3c51
7fbff675
Pipeline
#203
passed with stage
in 2 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
public/Pi-hole Stats.groovy
deleted
100644 → 0
View file @
7eaa3c51
/**
* Pi-hole Stats
*
* Copyright 2019 Scott Grayban
*
*/
metadata
{
definition
(
name:
"Pi-hole Stats"
,
namespace:
"sgrayban"
,
author:
"Scott Grayban"
,
importUrl:
"http://sgrayban.borgnet.online:8081/hubitat-public-repo/Pi-hole%20Stats.groovy"
)
{
capability
"Switch"
capability
"Refresh"
command
"poll"
command
"refresh"
attribute
"LastRefresh"
,
"string"
attribute
"combined"
,
"string"
attribute
"QueriesToday"
,
"number"
attribute
"BlockedAds"
,
"number"
attribute
"UniqueClients"
,
"number"
}
preferences
{
def
refreshRate
=
[:]
refreshRate
<<
[
"1 min"
:
"Refresh every minute"
]
refreshRate
<<
[
"5 min"
:
"Refresh every 5 minutes"
]
refreshRate
<<
[
"15 min"
:
"Refresh every 15 minutes"
]
refreshRate
<<
[
"30 min"
:
"Refresh every 30 minutes"
]
input
(
"refresh_Rate"
,
"enum"
,
title:
"Device Refresh Rate"
,
options:
refreshRate
,
defaultValue:
"1 min"
)
input
"locale"
,
"enum"
,
title:
"Choose refresh date format"
,
required:
true
,
defaultValue:
""
,
options:
[
US:
"US MM/DD/YYYY"
,
UK:
"UK DD/MM/YYYY"
]
//RK
input
name:
"debugOutput"
,
type:
"bool"
,
title:
"Enable debug logging?"
,
defaultValue:
true
input
name:
"deviceIP"
,
type:
"text"
,
title:
"Pi-home IP address"
,
required:
true
input
name:
"apiToken"
,
type:
"text"
,
title:
"API token"
,
required:
true
}
}
private
getPort
()
{
return
80
}
private
getApiPath
()
{
"/admin/api.php"
}
def
installed
()
{
log
.
debug
"Installed with settings: ${settings}"
}
def
uninstalled
()
{
log
.
debug
"uninstalled()"
}
private
logDebug
(
msg
)
{
if
(
settings
?.
debugOutput
||
settings
?.
debugOutput
==
null
)
{
log
.
debug
"$msg"
}
}
def
updated
()
{
log
.
debug
"Updated with settings: ${settings}"
unschedule
()
switch
(
refresh_Rate
)
{
case
"1 min"
:
runEvery1Minute
(
autorefresh
)
break
case
"5 min"
:
runEvery5Minutes
(
autorefresh
)
break
case
"15 min"
:
runEvery15Minutes
(
autorefresh
)
break
default:
runEvery30Minutes
(
autorefresh
)
}
refresh
()
}
def
autorefresh
()
{
if
(
locale
==
"UK"
)
{
state
.
LastRefresh
=
new
Date
().
format
(
"dd/MM/YYYY @ HH:mm:ss"
,
location
.
timeZone
)
sendEvent
(
name:
"LastRefresh"
,
value:
state
.
LastRefresh
,
descriptionText:
"Last refresh performed"
)
}
if
(
locale
==
"US"
)
{
state
.
LastRefresh
=
new
Date
().
format
(
"MMM dd, yyyy @ HH:mm:ss"
,
location
.
timeZone
)
sendEvent
(
name:
"LastRefresh"
,
value:
state
.
LastRefresh
,
descriptionText:
"Last refresh performed"
)
}
logDebug
"Executing ${refresh_Rate} minute(s) AUTO REFRESH"
poll
()
}
def
refresh
()
{
poll
()
}
def
poll
()
{
if
(
locale
==
"UK"
)
{
state
.
LastRefresh
=
new
Date
().
format
(
"dd/MM/YYYY @ HH:mm:ss"
,
location
.
timeZone
)
sendEvent
(
name:
"LastRefresh"
,
value:
state
.
LastRefresh
,
descriptionText:
"Last refresh performed"
)
}
if
(
locale
==
"US"
)
{
state
.
LastRefresh
=
new
Date
().
format
(
"MMM dd yyyy @ HH:mm:ss"
,
location
.
timeZone
)
sendEvent
(
name:
"LastRefresh"
,
value:
state
.
LastRefresh
,
descriptionText:
"Last refresh performed"
)
}
def
path
=
getApiPath
()
def
hostAddress
=
"${deviceIP}:${port}"
def
headers
=
[:]
headers
.
put
(
"HOST"
,
hostAddress
)
def
hubAction
=
new
hubitat
.
device
.
HubAction
(
method:
"GET"
,
path:
path
,
headers:
headers
,
null
,
[
callback
:
parse
]
)
sendHubCommand
(
hubAction
)
}
def
parse
(
response
)
{
log
.
debug
"Parsing '${response}'"
def
json
=
response
.
json
log
.
debug
"Received '${json}'"
if
(
json
.
FTLnotrunning
)
{
return
}
if
(
json
.
status
==
"enabled"
)
{
sendEvent
(
name:
"switch"
,
value:
"on"
)
}
if
(
json
.
status
==
"disabled"
)
{
sendEvent
(
name:
"switch"
,
value:
"off"
)
}
if
(
json
.
dns_queries_today
){
if
(
json
.
dns_queries_today
.
toInteger
()
>=
0
)
{
def
combinedValue
=
"Queries today: "
+
json
.
dns_queries_today
+
" Blocked: "
+
json
.
ads_blocked_today
+
"\nClients: "
+
json
.
unique_clients
sendEvent
(
name:
"combined"
,
value:
combinedValue
,
unit:
""
)
}
}
sendEvent
(
name:
'lastupdate'
,
value:
lastUpdated
(
now
()),
unit:
""
)
}
def
on
()
{
doSwitch
(
"enable"
)
}
def
off
()
{
doSwitch
(
"disable"
)
}
def
doSwitch
(
toggle
)
{
if
(
deviceIP
==
null
)
{
log
.
debug
"IP address missing in preferences"
return
}
def
path
=
getApiPath
()
+
"?"
+
toggle
+
"&auth="
+
apiToken
def
hostAddress
=
"${deviceIP}:${port}"
def
headers
=
[:]
headers
.
put
(
"HOST"
,
hostAddress
)
def
hubAction
=
new
hubitat
.
device
.
HubAction
(
method:
"GET"
,
path:
path
,
headers:
headers
,
null
,
[
callback
:
parse
]
)
sendHubCommand
(
hubAction
)
}
def
lastUpdated
(
time
)
{
def
timeNow
=
now
()
def
lastUpdate
=
""
if
(
location
.
timeZone
==
null
)
{
logDebug
"Cannot set update time : location not defined in app"
}
else
{
lastUpdate
=
new
Date
(
timeNow
).
format
(
"MMM dd, yyyy @ HH:mm"
,
location
.
timeZone
)
}
return
lastUpdate
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment