doorbelldingdongringringdoo.../main.py

113 lines
2.9 KiB
Python
Raw Normal View History

2021-10-15 21:43:27 +00:00
import os, logging
DEBUG = os.environ.get('DEBUG')
logging.basicConfig(
format='[%(asctime)s] %(levelname)s %(module)s/%(funcName)s - %(message)s',
2021-10-15 21:43:27 +00:00
level=logging.DEBUG if DEBUG else logging.INFO)
logging.getLogger('aiohttp').setLevel(logging.DEBUG if DEBUG else logging.WARNING)
import os
import sys
import asyncio
import aiohttp
2021-10-15 21:07:28 +00:00
import RPi.GPIO as GPIO
import time
2021-10-16 00:33:57 +00:00
from signal import *
from aiohttp import ClientSession, CookieJar
2021-10-15 19:47:23 +00:00
import settings
from pyunifiprotect.unifi_protect_server import UpvServer
2021-10-15 19:47:23 +00:00
2021-10-15 21:07:28 +00:00
RELAY_ON = False
RELAY_OFF = True
2021-10-16 00:33:57 +00:00
allow_watchdog = False
def set_relay(pin, state):
GPIO.output(pin, state)
logging.info('Set relay on pin %s to %s', pin, 'ON' if state == RELAY_ON else 'OFF')
def pulse_relay(pin):
set_relay(pin, RELAY_ON)
2021-10-16 01:05:16 +00:00
time.sleep(0.5)
2021-10-16 00:33:57 +00:00
set_relay(pin, RELAY_OFF)
2021-10-15 21:07:28 +00:00
def ring_bell(mac):
2021-10-16 00:33:57 +00:00
global allow_watchdog
2021-10-16 00:41:22 +00:00
if not allow_watchdog and not DEBUG:
logging.info('Enabling watchdog...')
allow_watchdog = True
2021-10-16 00:33:57 +00:00
2021-10-15 21:07:28 +00:00
try:
doorbell = settings.DOORBELLS[mac]
2021-10-16 00:33:57 +00:00
pulse_relay(doorbell['gpio'])
2021-10-15 21:07:28 +00:00
except KeyError:
logging.error('Doorbell %s not found!', mac)
2021-10-15 21:07:28 +00:00
def subscriber(updated):
logging.debug('Subscription: updated=%s', updated)
for _, data in updated.items():
if data['event_type'] == 'ring' and data['event_ring_on']:
logging.info('%s: %s is ringing!', data['mac'], data['name'])
2021-10-15 21:07:28 +00:00
ring_bell(data['mac'])
2021-10-16 00:33:57 +00:00
def feed_watchdog():
with open('/dev/watchdog', 'w') as wdt:
wdt.write('1')
async def ws_listener():
session = ClientSession(cookie_jar=CookieJar(unsafe=True))
unifiprotect = UpvServer(
session,
2021-10-15 19:47:23 +00:00
settings.UFP_ADDRESS,
settings.UFP_PORT,
settings.UFP_USERNAME,
settings.UFP_PASSWORD,
2021-10-15 19:47:23 +00:00
)
await unifiprotect.check_unifi_os()
await unifiprotect.update()
unsub = unifiprotect.subscribe_websocket(subscriber)
2021-10-16 00:33:57 +00:00
while True:
2021-10-16 00:41:22 +00:00
if allow_watchdog and not DEBUG:
2021-10-16 00:33:57 +00:00
feed_watchdog()
await asyncio.sleep(1)
await session.close()
unsub()
def disable_relays_on_exit(*args):
logging.info('Exiting, disabling relays...')
for _, doorbell in settings.DOORBELLS.items():
2021-10-16 00:33:57 +00:00
set_relay(doorbell['gpio'], RELAY_OFF)
logging.info('Goodbye.')
os._exit(0)
def init():
2021-10-15 21:07:28 +00:00
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
for _, doorbell in settings.DOORBELLS.items():
GPIO.setup(doorbell['gpio'], GPIO.OUT)
2021-10-16 00:33:57 +00:00
pulse_relay(doorbell['gpio'])
time.sleep(1)
logging.info('GPIO initialized')
for sig in (SIGABRT, SIGILL, SIGINT, SIGSEGV, SIGTERM):
signal(sig, disable_relays_on_exit)
logging.info('Signals initialized')
2021-10-15 21:07:28 +00:00
if __name__ == '__main__':
2021-10-16 00:41:22 +00:00
logging.info('')
logging.info('======================================')
logging.info('Boot up...')
init()
loop = asyncio.get_event_loop()
loop.run_until_complete(ws_listener())
loop.close()