2020-04-12 14:05:30 +02:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
|
|
|
"""Displays the pi-hole status (up/down) together with the number of ads that were blocked today
|
|
|
|
Parameters:
|
|
|
|
* pihole.address : pi-hole address (e.q: http://192.168.1.3)
|
|
|
|
* pihole.pwhash : pi-hole webinterface password hash (can be obtained from the /etc/pihole/SetupVars.conf file)
|
|
|
|
"""
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
2020-04-12 14:15:03 +02:00
|
|
|
import core.module
|
|
|
|
import core.widget
|
|
|
|
import core.input
|
2020-04-12 14:05:30 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-04-12 14:15:03 +02:00
|
|
|
class Module(core.module.Module):
|
|
|
|
@core.decorators.every(minutes=1)
|
2020-04-26 16:39:24 +02:00
|
|
|
def __init__(self, config, theme):
|
|
|
|
super().__init__(config, theme, core.widget.Widget(self.pihole_status))
|
2020-04-12 14:05:30 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
self._pihole_address = self.parameter("address", "")
|
|
|
|
self._pihole_pw_hash = self.parameter("pwhash", "")
|
2020-04-12 14:05:30 +02:00
|
|
|
self._pihole_status = None
|
2020-05-03 11:15:52 +02:00
|
|
|
self._ads_blocked_today = "-"
|
2020-04-12 14:05:30 +02:00
|
|
|
self.update_pihole_status()
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
core.input.register(
|
|
|
|
self, button=core.input.LEFT_MOUSE, cmd=self.toggle_pihole_status
|
|
|
|
)
|
2020-04-12 14:05:30 +02:00
|
|
|
|
|
|
|
def pihole_status(self, widget):
|
|
|
|
if self._pihole_status is None:
|
2020-05-03 11:15:52 +02:00
|
|
|
return "pi-hole unknown"
|
|
|
|
return "pi-hole {}".format(
|
|
|
|
"up {} blocked".format(self._ads_blocked_today)
|
|
|
|
if self._pihole_status
|
|
|
|
else "down"
|
|
|
|
)
|
2020-04-12 14:05:30 +02:00
|
|
|
|
|
|
|
def update_pihole_status(self):
|
|
|
|
try:
|
2020-05-03 11:15:52 +02:00
|
|
|
data = requests.get(self._pihole_address + "/admin/api.php?summary").json()
|
|
|
|
self._pihole_status = True if data["status"] == "enabled" else False
|
|
|
|
self._ads_blocked_today = data["ads_blocked_today"]
|
2020-04-12 14:15:03 +02:00
|
|
|
except Exception as e:
|
2020-04-12 14:05:30 +02:00
|
|
|
self._pihole_status = None
|
|
|
|
|
|
|
|
def toggle_pihole_status(self, widget):
|
|
|
|
if self._pihole_status is not None:
|
|
|
|
try:
|
|
|
|
req = None
|
|
|
|
if self._pihole_status:
|
2020-05-03 11:15:52 +02:00
|
|
|
req = requests.get(
|
|
|
|
self._pihole_address
|
|
|
|
+ "/admin/api.php?disable&auth="
|
|
|
|
+ self._pihole_pw_hash
|
|
|
|
)
|
2020-04-12 14:05:30 +02:00
|
|
|
else:
|
2020-05-03 11:15:52 +02:00
|
|
|
req = requests.get(
|
|
|
|
self._pihole_address
|
|
|
|
+ "/admin/api.php?enable&auth="
|
|
|
|
+ self._pihole_pw_hash
|
|
|
|
)
|
2020-04-12 14:05:30 +02:00
|
|
|
if req is not None:
|
|
|
|
if req.status_code == 200:
|
2020-05-03 11:15:52 +02:00
|
|
|
status = req.json()["status"]
|
|
|
|
self._pihole_status = False if status == "disabled" else True
|
2020-04-12 14:05:30 +02:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2020-04-12 14:15:03 +02:00
|
|
|
def update(self):
|
2020-04-12 14:05:30 +02:00
|
|
|
self.update_pihole_status()
|
|
|
|
|
|
|
|
def state(self, widget):
|
|
|
|
if self._pihole_status is None:
|
|
|
|
return []
|
|
|
|
elif self._pihole_status:
|
2020-05-03 11:15:52 +02:00
|
|
|
return ["enabled"]
|
|
|
|
return ["disabled", "warning"]
|
|
|
|
|
2020-04-12 14:15:03 +02:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|