Send water alerts via ntfy
This commit is contained in:
parent
97e63124d4
commit
2494b3b928
18
main.py
18
main.py
|
@ -4,6 +4,7 @@ import time
|
|||
from machine import I2C
|
||||
from net import AdafruitIO
|
||||
from net import logging
|
||||
from net import Ntfy
|
||||
from net import ntp
|
||||
from net import Server
|
||||
from net import templates
|
||||
|
@ -19,10 +20,12 @@ class SensorServer(Server):
|
|||
self.waterSensor = WaterSensor(0)
|
||||
self.mcp = MCP9808(I2C(0))
|
||||
self.aio = AdafruitIO()
|
||||
self.ntfy = Ntfy()
|
||||
self.ntp_interval_in_seconds = 60 * 60
|
||||
self.aio_interval_in_seconds = 60 * 5
|
||||
self.ntp_ticks = time.ticks_ms()
|
||||
self.aio_ticks = time.ticks_ms()
|
||||
self.isWaterPresent = False
|
||||
|
||||
ntp.sync()
|
||||
|
||||
|
@ -38,6 +41,15 @@ class SensorServer(Server):
|
|||
self.aio_ticks = ticks
|
||||
self.aio.upload(self.getReading())
|
||||
|
||||
self.updateWater()
|
||||
|
||||
def updateWater(self):
|
||||
isWaterPresent = self.waterSensor.isWaterPresent()
|
||||
|
||||
if isWaterPresent != self.isWaterPresent:
|
||||
self.isWaterPresent = isWaterPresent
|
||||
self.ntfy.message(f'Water is {self.waterTextValue()}')
|
||||
|
||||
def handlePath(self, path):
|
||||
if path == 'index.json':
|
||||
return self.getJsonData()
|
||||
|
@ -68,7 +80,7 @@ class SensorServer(Server):
|
|||
|
||||
def getState(self):
|
||||
return {
|
||||
'is-water-present': self.waterSensor.isWaterPresent(),
|
||||
'is-water-present': self.isWaterPresent,
|
||||
'temperature': self.mcp.get_temp()
|
||||
}
|
||||
|
||||
|
@ -76,10 +88,10 @@ class SensorServer(Server):
|
|||
return round(self.mcp.get_temp(), 2)
|
||||
|
||||
def waterTextValue(self):
|
||||
return 'Present' if self.waterSensor.isWaterPresent() else 'Absent'
|
||||
return 'Present' if self.isWaterPresent else 'Absent'
|
||||
|
||||
def waterCssClass(self):
|
||||
return 'water-present' if self.waterSensor.isWaterPresent() else 'water-absent'
|
||||
return 'water-present' if self.isWaterPresent else 'water-absent'
|
||||
|
||||
|
||||
def main():
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
from .adafruit_io import AdafruitIO
|
||||
from .ntfy import Ntfy
|
||||
from .server import Server
|
||||
|
|
|
@ -7,7 +7,7 @@ from .config import secrets
|
|||
|
||||
class AdafruitIO:
|
||||
def __init__(self):
|
||||
self.nickname = config['adafruit_io_nickname']
|
||||
self.hostname = config['hostname']
|
||||
|
||||
self.headers = {
|
||||
'x-aio-key': secrets['adafruit_io_key'],
|
||||
|
@ -45,7 +45,7 @@ class AdafruitIO:
|
|||
|
||||
for key, value in reading['readings'].items():
|
||||
payload['feeds'].append({
|
||||
'key': f'{self.nickname}-{key}',
|
||||
'key': f'{self.hostname}-{key}',
|
||||
'value': str(value)
|
||||
})
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
config = {
|
||||
'adafruit_io_nickname': '',
|
||||
'hostname': '',
|
||||
'adafruit_io_group': ''
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
import urequests
|
||||
|
||||
from . import logging
|
||||
from .config import config
|
||||
from .config import secrets
|
||||
|
||||
|
||||
class Ntfy:
|
||||
def __init__(self):
|
||||
self.hostname = config['hostname']
|
||||
|
||||
host = secrets['ntfy_host']
|
||||
topic = secrets['ntfy_topic']
|
||||
self.url = f'https://{host}/{topic}'
|
||||
|
||||
def message(self, message):
|
||||
result = self.sendMessage(message)
|
||||
|
||||
try:
|
||||
if result.status_code != 200:
|
||||
self.logError(result)
|
||||
return False
|
||||
|
||||
return True
|
||||
finally:
|
||||
result.close()
|
||||
|
||||
def sendMessage(self, message):
|
||||
return urequests.post(self.url, data=f"{self.hostname} - {message}")
|
||||
|
||||
def logError(self, result):
|
||||
status = result.status_code
|
||||
reason = result.reason.decode("utf-8")
|
||||
|
||||
logging.debug(f'ntfy upload issue: {status} {reason}')
|
Loading…
Reference in New Issue