Disable Pihole from Hubitat

Hi all,

I'm wondering if anyone else has successfully implemented a way to temporarily disable ad blocking from a PiHole through the trigger of a virtual switch in Hubitat. I used to be able to accomplish this using the API in PiHole version 5, but the API for PiHole version 6 is completely rewritten and a bit over my head. The documentation for the PiHole API is here http://pi.hole/api/docs/ . I've asked ChatGPT to help create a Hubitat app where I turn on a virtual switch and it sends the appropriate API command to the PiHole (app code below).

definition(
    name: "Pi-hole API Controller",
    namespace: "user",
    author: "ChatGPT",
    description: "Disables Pi-hole blocking via v6 API",
    category: "Convenience",
    iconUrl: "",
    iconX2Url: ""
)

preferences {
    section("Pi-hole Settings") {
        input "piHoleIP", "text", title: "Pi-hole IP Address (e.g., 192.168.1.100)", required: true
        input "piHolePassword", "password", title: "Pi-hole Password", required: true
        input "disableDuration", "number", title: "Disable Time (seconds)", defaultValue: 3600
    }
    section("Control") {
        input "triggerSwitch", "capability.switch", title: "Trigger Switch", required: true
    }
}

def installed() {
    initialize()
}

def updated() {
    unsubscribe()
    initialize()
}

def initialize() {
    subscribe(triggerSwitch, "switch.on", eventHandler)
}

def eventHandler(evt) {
    log.debug "Trigger received, starting Pi-hole disable sequence."
    authenticateToPiHole()
}

def authenticateToPiHole() {
    def params = [
        uri: "http://${piHoleIP}:80/api/auth",
        requestContentType: 'application/json',
        contentType: 'application/json',
        body: "{\"password\":\"${piHolePassword}\"}"
    ]

    try {
        httpPost(params) { resp ->
            if (resp.data?.session?.sid) {
                def sid = resp.data.session.sid
                log.debug "Authenticated. SID: ${sid}"
                disableBlocking(sid)
            } else {
                log.error "Authentication failed: ${resp.data}"
                resetTriggerSwitch()
            }
        }
    } catch (e) {
        log.error "Error authenticating: ${e.message}"
        resetTriggerSwitch()
    }
}

def disableBlocking(sid) {
    def params = [
        uri: "http://${piHoleIP}/api/dns/blocking?sid=${sid}",
        requestContentType: 'application/json',
        contentType: 'application/json',
        body: [blocking: false, timer: disableDuration]
    ]

    try {
        httpPost(params) { resp ->
            if (resp.status == 200) {
                log.info "Blocking disabled for ${disableDuration} seconds."
                logoutPiHole(sid)
            } else {
                log.error "Failed to disable blocking: ${resp.status}"
                resetTriggerSwitch()
            }
        }
    } catch (e) {
        log.error "Error disabling blocking: ${e.message}"
        resetTriggerSwitch()
    }
}

def logoutPiHole(sid) {
    def params = [
        uri: "http://${piHoleIP}/api/auth?sid=${sid}"
    ]

    try {
        httpDelete(params) { resp ->
            log.debug "Logged out of Pi-hole session."
            resetTriggerSwitch()
        }
    } catch (e) {
        log.error "Error logging out: ${e.message}"
        resetTriggerSwitch()
    }
}

def resetTriggerSwitch() {
    runIn(2, turnOffTriggerSwitch)
}

def turnOffTriggerSwitch() {
    if (triggerSwitch.currentSwitch == "on") {
        triggerSwitch.off()
        log.debug "Trigger switch turned off."
    }
}

However, with this app I keep getting this log in Hubitat.

Any help would be very much appreciated! Thanks!

2 Likes

Thanks so much!

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.