Send push notifications
This commit is contained in:
parent
8ee47e36e3
commit
2118ab0d6a
30
garage.py
30
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'}
|
||||
|
||||
|
||||
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')
|
||||
ntfy = config['push'].get('ntfy')
|
||||
topic = config['push'].get('topic')
|
||||
|
||||
|
||||
def main():
|
||||
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()
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue