Log to console when the doorbell is ringing

This commit is contained in:
Tanner Collin 2021-10-15 14:31:17 -06:00
parent d5b5a2ae2e
commit 9e49c728ed

47
main.py
View File

@ -1,14 +1,47 @@
import os
import sys
import asyncio
import aiohttp
from aiohttp import ClientSession, CookieJar
import settings import settings
from pyunifiprotect import cli from pyunifiprotect.unifi_protect_server import UpvServer
if __name__ == '__main__': import logging
protect = cli._get_server( logger = logging.getLogger(__name__)
settings.UFP_USERNAME,
settings.UFP_PASSWORD, 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_ADDRESS,
settings.UFP_PORT, 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()