2021-10-15 20:31:17 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import asyncio
|
|
|
|
import aiohttp
|
|
|
|
from aiohttp import ClientSession, CookieJar
|
|
|
|
|
2021-10-15 19:47:23 +00:00
|
|
|
import settings
|
|
|
|
|
2021-10-15 20:31:17 +00:00
|
|
|
from pyunifiprotect.unifi_protect_server import UpvServer
|
2021-10-15 19:47:23 +00:00
|
|
|
|
2021-10-15 20:31:17 +00:00
|
|
|
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,
|
2021-10-15 19:47:23 +00:00
|
|
|
settings.UFP_ADDRESS,
|
|
|
|
settings.UFP_PORT,
|
2021-10-15 20:31:17 +00:00
|
|
|
settings.UFP_USERNAME,
|
|
|
|
settings.UFP_PASSWORD,
|
2021-10-15 19:47:23 +00:00
|
|
|
)
|
|
|
|
|
2021-10-15 20:31:17 +00:00
|
|
|
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()
|