Compare commits

..

3 Commits

Author SHA1 Message Date
eed4d6f63b Add example settings file 2021-10-15 15:10:12 -06:00
8b6838ac3b Freeze requirements 2021-10-15 15:08:34 -06:00
d9499faaba Integrate GPIO relays 2021-10-15 15:07:28 -06:00
3 changed files with 54 additions and 1 deletions

25
main.py
View File

@ -2,6 +2,8 @@ import os
import sys import sys
import asyncio import asyncio
import aiohttp import aiohttp
import RPi.GPIO as GPIO
import time
from aiohttp import ClientSession, CookieJar from aiohttp import ClientSession, CookieJar
import settings import settings
@ -11,12 +13,25 @@ from pyunifiprotect.unifi_protect_server import UpvServer
import logging import logging
logger = logging.getLogger(__name__) 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): def subscriber(updated):
logger.debug('Subscription: updated=%s', updated) logger.debug('Subscription: updated=%s', updated)
for _, data in updated.items(): for _, data in updated.items():
if data['event_type'] == 'ring' and data['event_ring_on']: 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(): async def ws_listener():
session = ClientSession(cookie_jar=CookieJar(unsafe=True)) session = ClientSession(cookie_jar=CookieJar(unsafe=True))
@ -40,6 +55,14 @@ async def ws_listener():
await session.close() await session.close()
unsub() 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__': if __name__ == '__main__':
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()

15
requirements.txt Normal file
View File

@ -0,0 +1,15 @@
aiohttp==3.7.4.post0
async-timeout==3.0.1
asyncio==3.4.3
attrs==21.2.0
chardet==4.0.0
click==8.0.3
idna==3.3
multidict==5.2.0
Pillow==8.4.0
PyJWT==2.2.0
python-dotenv==0.19.1
pyunifiprotect==0.33.0
typer==0.4.0
typing-extensions==3.10.0.2
yarl==1.7.0

15
settings.py.example Normal file
View File

@ -0,0 +1,15 @@
UFP_USERNAME = 'admin'
UFP_PASSWORD = 'password'
UFP_ADDRESS = '192.168.whatever'
UFP_PORT = 443
DOORBELLS = {
'123456780ABC': {
name: 'Front Door',
gpio: 26,
},
'123456780ABC': {
name: 'Side Door',
gpio: 19,
},
}