garage/garage.py

80 lines
1.7 KiB
Python
Raw Normal View History

2022-12-26 17:03:35 -05:00
import json
2022-12-30 14:09:56 -05:00
import requests
2022-12-26 17:03:35 -05:00
from configparser import ConfigParser
2022-12-26 17:10:16 -05:00
from gpiozero import Button
2022-12-26 17:03:35 -05:00
from signal import pause
from threading import Lock
threadLock = Lock()
stateFile = 'www/garage.json'
2022-12-30 14:38:23 -05:00
state = {'east-door': 'init', 'west-door': 'init'}
2022-12-26 17:03:35 -05:00
2022-12-30 14:09:56 -05:00
config = ConfigParser()
config.read('garage.cfg')
eastPin = config['garage'].getint('east-pin')
westPin = config['garage'].getint('west-pin')
2024-04-03 14:09:11 -04:00
ntfyList = json.loads(config['push'].get('ntfy'))
2022-12-26 17:03:35 -05:00
2022-12-30 14:09:56 -05:00
def main():
2022-12-26 17:03:35 -05:00
eastButton = Button(eastPin)
westButton = Button(westPin)
2022-12-30 14:16:17 -05:00
state['east-door'] = 'closed' if eastButton.is_pressed else 'open'
state['west-door'] = 'closed' if westButton.is_pressed else 'open'
persistState()
2022-12-26 17:19:02 -05:00
eastButton.when_pressed = eastDoorClosed
eastButton.when_released = eastDoorOpened
westButton.when_pressed = westDoorClosed
westButton.when_released = westDoorOpened
2022-12-26 17:14:02 -05:00
2022-12-26 17:03:35 -05:00
pause()
def eastDoorOpened():
2022-12-26 17:22:51 -05:00
with threadLock:
2022-12-30 14:09:56 -05:00
state['east-door'] = 'opened'
2022-12-26 17:03:35 -05:00
persistState()
2022-12-30 14:09:56 -05:00
sendNotification()
2022-12-26 17:03:35 -05:00
def eastDoorClosed():
2022-12-26 17:22:51 -05:00
with threadLock:
2022-12-30 14:09:56 -05:00
state['east-door'] = 'closed'
2022-12-26 17:03:35 -05:00
persistState()
2022-12-30 14:09:56 -05:00
sendNotification()
2022-12-26 17:03:35 -05:00
def westDoorOpened():
2022-12-26 17:22:51 -05:00
with threadLock:
2022-12-30 14:09:56 -05:00
state['west-door'] = 'opened'
2022-12-26 17:03:35 -05:00
persistState()
2022-12-30 14:09:56 -05:00
sendNotification()
2022-12-26 17:03:35 -05:00
def westDoorClosed():
2022-12-26 17:22:51 -05:00
with threadLock:
2022-12-30 14:09:56 -05:00
state['west-door'] = 'closed'
2022-12-26 17:03:35 -05:00
persistState()
2022-12-30 14:09:56 -05:00
sendNotification()
2022-12-26 17:03:35 -05:00
def persistState():
with open(stateFile, 'w') as file:
json.dump(state, file)
2022-12-30 14:09:56 -05:00
def sendNotification():
2024-04-03 14:09:11 -04:00
for ntfy in ntfyList:
requests.post(
url=f'https://{ntfy["host"]}/{ntfy["topic"]}?auth={ntfy["auth"]}',
data='update'.encode(encoding='utf-8')
)
2022-12-30 14:09:56 -05:00
2022-12-26 17:03:35 -05:00
if __name__ == '__main__':
main()