Code by Scott שאול בן ישוע
Verified Commit 098c06f7 authored by Sha'ul ben Yeshua's avatar Sha'ul ben Yeshua 🎗
Browse files

added Pi-Hole stats

parent a7177544
/**
* Pi-hole Stats
*
* Copyright 2019 Scott Grayban
*
*/
metadata {
definition (name: "Pi-hole Stats", namespace: "sgrayban", author: "Scott Grayban") {
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
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment