Send push notifications

This commit is contained in:
Mike Cifelli 2022-12-30 14:09:56 -05:00
parent 8ee47e36e3
commit 2118ab0d6a
2 changed files with 27 additions and 15 deletions

View File

@ -1,4 +1,5 @@
import json import json
import requests
import sys import sys
from configparser import ConfigParser from configparser import ConfigParser
@ -8,19 +9,19 @@ from signal import signal
from signal import SIGTERM from signal import SIGTERM
from threading import Lock from threading import Lock
state = {"east-door": "opened", "west-door": "opened"}
threadLock = Lock() threadLock = Lock()
stateFile = 'www/garage.json' 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(): 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() persistState()
eastButton = Button(eastPin) eastButton = Button(eastPin)
@ -36,26 +37,30 @@ def main():
def eastDoorOpened(): def eastDoorOpened():
with threadLock: with threadLock:
state["east-door"] = "opened" state['east-door'] = 'opened'
persistState() persistState()
sendNotification()
def eastDoorClosed(): def eastDoorClosed():
with threadLock: with threadLock:
state["east-door"] = "closed" state['east-door'] = 'closed'
persistState() persistState()
sendNotification()
def westDoorOpened(): def westDoorOpened():
with threadLock: with threadLock:
state["west-door"] = "opened" state['west-door'] = 'opened'
persistState() persistState()
sendNotification()
def westDoorClosed(): def westDoorClosed():
with threadLock: with threadLock:
state["west-door"] = "closed" state['west-door'] = 'closed'
persistState() persistState()
sendNotification()
def persistState(): def persistState():
@ -63,5 +68,12 @@ def persistState():
json.dump(state, file) json.dump(state, file)
def sendNotification():
requests.post(
url=f'https://{ntfy}/{topic}',
data='update'.encode(encoding='utf-8')
)
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -1,7 +1,7 @@
[garage] [garage]
east-pin=1 east-pin=1
west-pin=1 west-pin=2
[push] [push]
east-uri=uri ntfy=ntfy.sh
west-uri=uri topic=topic