Add outer loop to ws listener

This commit is contained in:
Tanner Collin 2021-11-29 04:49:17 +00:00
parent 6ecb570abf
commit 0053b78e41

27
main.py
View File

@ -17,6 +17,7 @@ from aiohttp import ClientSession, CookieJar
import settings import settings
from pyunifiprotect.unifi_protect_server import UpvServer from pyunifiprotect.unifi_protect_server import UpvServer
from pyunifiprotect.exceptions import NvrError
RELAY_ON = False RELAY_ON = False
RELAY_OFF = True RELAY_OFF = True
@ -44,10 +45,10 @@ def ring_bell(mac):
except KeyError: except KeyError:
logging.error('Doorbell %s not found!', mac) logging.error('Doorbell %s not found!', mac)
def subscriber(updated): def subscriber(updates):
logging.debug('Subscription: updated=%s', updated) logging.debug('Subscription: updates=%s', updates)
for _, data in updated.items(): for _, data in updates.items():
if data['event_type'] == 'ring' and data['event_ring_on']: if data['event_type'] == 'ring' and data['event_ring_on']:
logging.info('%s: %s is ringing!', data['mac'], data['name']) logging.info('%s: %s is ringing!', data['mac'], data['name'])
ring_bell(data['mac']) ring_bell(data['mac'])
@ -57,6 +58,7 @@ 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( unifiprotect = UpvServer(
@ -73,8 +75,25 @@ async def ws_listener():
unsub = unifiprotect.subscribe_websocket(subscriber) unsub = unifiprotect.subscribe_websocket(subscriber)
while True: while True:
try:
updates = await unifiprotect.update()
logging.debug('Updates: %s', str(updates))
except NvrError:
logging.error('Problem connecting to Unifi Protect. Reconnecting...')
break
logging.debug('unifiprotect: %s', unifiprotect.__dict__)
logging.debug('ws_session %s', unifiprotect.ws_session.__dict__)
breakpoint()
#active_ws = await unifiprotect.check_ws()
#if not active_ws:
# logging.error('Websocket unactive. Reconnecting...')
if allow_watchdog and not DEBUG: if allow_watchdog and not DEBUG:
feed_watchdog() feed_watchdog()
await asyncio.sleep(1) await asyncio.sleep(1)
await session.close() await session.close()
@ -93,7 +112,7 @@ def init():
for _, doorbell in settings.DOORBELLS.items(): for _, doorbell in settings.DOORBELLS.items():
GPIO.setup(doorbell['gpio'], GPIO.OUT) GPIO.setup(doorbell['gpio'], GPIO.OUT)
pulse_relay(doorbell['gpio']) #pulse_relay(doorbell['gpio'])
time.sleep(1) time.sleep(1)
logging.info('GPIO initialized') logging.info('GPIO initialized')