diff --git a/main.py b/main.py index 3cac9ac..1b022b3 100644 --- a/main.py +++ b/main.py @@ -2,6 +2,8 @@ import os import sys import asyncio import aiohttp +import RPi.GPIO as GPIO +import time from aiohttp import ClientSession, CookieJar import settings @@ -11,12 +13,25 @@ from pyunifiprotect.unifi_protect_server import UpvServer import logging logger = logging.getLogger(__name__) +RELAY_ON = False +RELAY_OFF = True + +def ring_bell(mac): + try: + doorbell = settings.DOORBELLS[mac] + GPIO.output(doorbell['gpio'], RELAY_ON) + time.sleep(0.5) + GPIO.output(doorbell['gpio'], RELAY_OFF) + except KeyError: + logger.error('Doorbell %s not found!', mac) + def subscriber(updated): logger.debug('Subscription: updated=%s', updated) for _, data in updated.items(): if data['event_type'] == 'ring' and data['event_ring_on']: - logger.info('%s is ringing!', data['name']) + logger.info('%s: %s is ringing!', data['mac'], data['name']) + ring_bell(data['mac']) async def ws_listener(): session = ClientSession(cookie_jar=CookieJar(unsafe=True)) @@ -40,6 +55,14 @@ async def ws_listener(): await session.close() unsub() +def init_relays(): + GPIO.setmode(GPIO.BCM) + GPIO.setwarnings(False) + + for _, doorbell in settings.DOORBELLS.items(): + GPIO.setup(doorbell['gpio'], GPIO.OUT) + GPIO.output(doorbell['gpio'], RELAY_OFF) + if __name__ == '__main__': logging.basicConfig(level=logging.INFO) loop = asyncio.get_event_loop()