From 9e49c728ed175c06b90611cc0372ff15b27fe5b8 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Fri, 15 Oct 2021 14:31:17 -0600 Subject: [PATCH] Log to console when the doorbell is ringing --- main.py | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/main.py b/main.py index c837328..3cac9ac 100644 --- a/main.py +++ b/main.py @@ -1,14 +1,47 @@ +import os +import sys +import asyncio +import aiohttp +from aiohttp import ClientSession, CookieJar + import settings -from pyunifiprotect import cli +from pyunifiprotect.unifi_protect_server import UpvServer -if __name__ == '__main__': - protect = cli._get_server( - settings.UFP_USERNAME, - settings.UFP_PASSWORD, +import logging +logger = logging.getLogger(__name__) + +def subscriber(updated): + logger.debug('Subscription: updated=%s', updated) + + for _, data in updated.items(): + if data['event_type'] == 'ring' and data['event_ring_on']: + logger.info('%s is ringing!', data['name']) + +async def ws_listener(): + session = ClientSession(cookie_jar=CookieJar(unsafe=True)) + + unifiprotect = UpvServer( + session, settings.UFP_ADDRESS, settings.UFP_PORT, - settings.UFP_SSL_VERIFY + settings.UFP_USERNAME, + settings.UFP_PASSWORD, ) - cli._listen_to_ws(protect) + await unifiprotect.check_unifi_os() + await unifiprotect.update() + + unsub = unifiprotect.subscribe_websocket(subscriber) + + for i in range(15000): + await asyncio.sleep(1) + + await session.close() + unsub() + +if __name__ == '__main__': + logging.basicConfig(level=logging.INFO) + loop = asyncio.get_event_loop() + loop.run_until_complete(ws_listener()) + loop.close()