From 2118ab0d6a3a7bcd730e0adad827a473e6410448 Mon Sep 17 00:00:00 2001 From: Mike Cifelli <1836280-mike-cifelli@users.noreply.gitlab.com> Date: Fri, 30 Dec 2022 14:09:56 -0500 Subject: [PATCH] Send push notifications --- garage.py | 36 ++++++++++++++++++++++++------------ garage.sample.cfg | 6 +++--- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/garage.py b/garage.py index 848d83c..754f09b 100644 --- a/garage.py +++ b/garage.py @@ -1,4 +1,5 @@ import json +import requests import sys from configparser import ConfigParser @@ -8,19 +9,19 @@ from signal import signal from signal import SIGTERM from threading import Lock -state = {"east-door": "opened", "west-door": "opened"} threadLock = Lock() stateFile = 'www/garage.json' +state = {'east-door': 'opened', 'west-door': 'opened'} + +config = ConfigParser() +config.read('garage.cfg') +eastPin = config['garage'].getint('east-pin') +westPin = config['garage'].getint('west-pin') +ntfy = config['push'].get('ntfy') +topic = config['push'].get('topic') def main(): - config = ConfigParser() - config.read('garage.cfg') - eastPin = config['garage'].getint('east-pin') - westPin = config['garage'].getint('west-pin') - # eastUri = config['push'].getint('east-uri') - # westUri = config['push'].getint('west-uri') - persistState() eastButton = Button(eastPin) @@ -36,26 +37,30 @@ def main(): def eastDoorOpened(): with threadLock: - state["east-door"] = "opened" + state['east-door'] = 'opened' persistState() + sendNotification() def eastDoorClosed(): with threadLock: - state["east-door"] = "closed" + state['east-door'] = 'closed' persistState() + sendNotification() def westDoorOpened(): with threadLock: - state["west-door"] = "opened" + state['west-door'] = 'opened' persistState() + sendNotification() def westDoorClosed(): with threadLock: - state["west-door"] = "closed" + state['west-door'] = 'closed' persistState() + sendNotification() def persistState(): @@ -63,5 +68,12 @@ def persistState(): json.dump(state, file) +def sendNotification(): + requests.post( + url=f'https://{ntfy}/{topic}', + data='update'.encode(encoding='utf-8') + ) + + if __name__ == '__main__': main() diff --git a/garage.sample.cfg b/garage.sample.cfg index 1ca14c9..0204f18 100644 --- a/garage.sample.cfg +++ b/garage.sample.cfg @@ -1,7 +1,7 @@ [garage] east-pin=1 -west-pin=1 +west-pin=2 [push] -east-uri=uri -west-uri=uri +ntfy=ntfy.sh +topic=topic