This commit is contained in:
Tanner Collin 2021-12-19 05:29:10 +00:00
parent 8a05f1aacb
commit 14051edfc5

91
main.py
View File

@ -1,10 +1,12 @@
import os, logging import os, logging
DEBUG = os.environ.get('DEBUG') DEBUG = os.environ.get('DEBUG')
NO_WATCHDOG = os.environ.get('NO_WATCHDOG')
logging.basicConfig( logging.basicConfig(
format='[%(asctime)s] %(levelname)s %(module)s/%(funcName)s - %(message)s', format='[%(asctime)s] %(levelname)s %(module)s/%(funcName)s - %(message)s',
level=logging.DEBUG if DEBUG else logging.INFO) level=logging.DEBUG if DEBUG else logging.INFO)
logging.getLogger('aiohttp').setLevel(logging.DEBUG if DEBUG else logging.WARNING) logging.getLogger('aiohttp').setLevel(logging.DEBUG if DEBUG else logging.WARNING)
import json
import os import os
import sys import sys
import asyncio import asyncio
@ -24,21 +26,30 @@ RELAY_OFF = True
allow_watchdog = False allow_watchdog = False
cooldown_time = time.time()
def set_relay(pin, state): def set_relay(pin, state):
GPIO.output(pin, state) GPIO.output(pin, state)
logging.info('Set relay on pin %s to %s', pin, 'ON' if state == RELAY_ON else 'OFF') logging.info('Set relay on pin %s to %s', pin, 'ON' if state == RELAY_ON else 'OFF')
def pulse_relay(pin): def pulse_relay(pin):
set_relay(pin, RELAY_ON) set_relay(pin, RELAY_ON)
time.sleep(0.5) time.sleep(0.5) # atomic
set_relay(pin, RELAY_OFF) set_relay(pin, RELAY_OFF)
def ring_bell(mac): def ring_bell(mac):
global allow_watchdog global allow_watchdog, cooldown_time
if not allow_watchdog and not DEBUG:
if not allow_watchdog and not DEBUG and not NO_WATCHDOG:
logging.info('Enabling watchdog...') logging.info('Enabling watchdog...')
allow_watchdog = True allow_watchdog = True
if time.time() - cooldown_time < 2:
logging.info('Cooldown skipping.')
return
cooldown_time = time.time()
try: try:
doorbell = settings.DOORBELLS[mac] doorbell = settings.DOORBELLS[mac]
pulse_relay(doorbell['gpio']) pulse_relay(doorbell['gpio'])
@ -58,44 +69,52 @@ def feed_watchdog():
wdt.write('1') wdt.write('1')
async def ws_listener(): async def ws_listener():
session = ClientSession(cookie_jar=CookieJar(unsafe=True))
unifiprotect = UpvServer(
session,
settings.UFP_ADDRESS,
settings.UFP_PORT,
settings.UFP_USERNAME,
settings.UFP_PASSWORD,
)
await unifiprotect.update()
unsub = unifiprotect.subscribe_websocket(subscriber)
logging.info('Connecting to websocket.')
await asyncio.sleep(2)
while True: while True:
session = ClientSession(cookie_jar=CookieJar(unsafe=True)) try:
updates = await unifiprotect.update()
logging.debug('')
logging.debug('Updates: %s', json.dumps(updates, indent=4))
except NvrError:
logging.error('Error updating connection. Reconnecting...')
break
unifiprotect = UpvServer( active_ws = await unifiprotect.check_ws()
session, if not active_ws:
settings.UFP_ADDRESS, logging.error('Websocket unactive. Reconnecting...')
settings.UFP_PORT, break
settings.UFP_USERNAME,
settings.UFP_PASSWORD,
)
await unifiprotect.update() if allow_watchdog:
feed_watchdog()
unsub = unifiprotect.subscribe_websocket(subscriber) await asyncio.sleep(1)
logging.info('Connecting to websocket.') await session.close()
await asyncio.sleep(2) unsub()
while True: async def connect():
try: while True:
updates = await unifiprotect.update() try:
logging.debug('Updates: %s', str(updates)) await ws_listener()
except NvrError: except NvrError as e:
logging.error('Problem connecting to Unifi Protect. Reconnecting...') logging.error('Error connecting to Unifi Protect: %s. Trying again...', str(e))
break await asyncio.sleep(3)
active_ws = await unifiprotect.check_ws()
if not active_ws:
logging.error('Websocket unactive. Reconnecting...')
break
if allow_watchdog and not DEBUG:
feed_watchdog()
await asyncio.sleep(1)
await session.close()
unsub()
def disable_relays_on_exit(*args): def disable_relays_on_exit(*args):
logging.info('Exiting, disabling relays...') logging.info('Exiting, disabling relays...')
@ -126,5 +145,5 @@ if __name__ == '__main__':
init() init()
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
loop.run_until_complete(ws_listener()) loop.run_until_complete(connect())
loop.close() loop.close()