master
Tanner Collin 2 years ago
parent 8a05f1aacb
commit 14051edfc5
  1. 85
      main.py

@ -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():
while True: session = ClientSession(cookie_jar=CookieJar(unsafe=True))
session = ClientSession(cookie_jar=CookieJar(unsafe=True))
unifiprotect = UpvServer(
session,
settings.UFP_ADDRESS,
settings.UFP_PORT,
settings.UFP_USERNAME,
settings.UFP_PASSWORD,
)
unifiprotect = UpvServer( await unifiprotect.update()
session,
settings.UFP_ADDRESS,
settings.UFP_PORT,
settings.UFP_USERNAME,
settings.UFP_PASSWORD,
)
await unifiprotect.update() unsub = unifiprotect.subscribe_websocket(subscriber)
unsub = unifiprotect.subscribe_websocket(subscriber) logging.info('Connecting to websocket.')
await asyncio.sleep(2)
logging.info('Connecting to websocket.') while True:
await asyncio.sleep(2) 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
while True: active_ws = await unifiprotect.check_ws()
try: if not active_ws:
updates = await unifiprotect.update() logging.error('Websocket unactive. Reconnecting...')
logging.debug('Updates: %s', str(updates)) break
except NvrError:
logging.error('Problem connecting to Unifi Protect. Reconnecting...')
break
active_ws = await unifiprotect.check_ws() if allow_watchdog:
if not active_ws: feed_watchdog()
logging.error('Websocket unactive. Reconnecting...')
break
if allow_watchdog and not DEBUG: await asyncio.sleep(1)
feed_watchdog()
await asyncio.sleep(1) await session.close()
unsub()
await session.close() async def connect():
unsub() while True:
try:
await ws_listener()
except NvrError as e:
logging.error('Error connecting to Unifi Protect: %s. Trying again...', str(e))
await asyncio.sleep(3)
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()

Loading…
Cancel
Save