2021-09-17 00:47:33 +02:00
|
|
|
import logging
|
2021-07-24 23:49:23 +02:00
|
|
|
import time
|
2021-05-19 18:43:44 +02:00
|
|
|
from typing import Callable, Optional
|
|
|
|
|
|
|
|
from pushbullet import Listener as _Listener
|
|
|
|
|
|
|
|
|
|
|
|
class Listener(_Listener):
|
|
|
|
"""
|
|
|
|
Extends the Pushbullet Listener object by adding ``on_open`` and ``on_close`` handlers.
|
|
|
|
"""
|
|
|
|
def __init__(self,
|
|
|
|
*args,
|
|
|
|
on_open: Optional[Callable[[], None]] = None,
|
|
|
|
on_close: Optional[Callable[[], None]] = None,
|
|
|
|
**kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self._on_open_hndl = on_open
|
|
|
|
self._on_close_hndl = on_close
|
2021-09-17 00:47:33 +02:00
|
|
|
self.logger = logging.getLogger(__name__)
|
2021-05-19 18:43:44 +02:00
|
|
|
|
|
|
|
def _on_open(self):
|
|
|
|
def callback(*_):
|
|
|
|
self.connected = True
|
|
|
|
self.last_update = time.time()
|
|
|
|
if self._on_open_hndl:
|
|
|
|
self._on_open_hndl()
|
|
|
|
|
|
|
|
return callback
|
|
|
|
|
|
|
|
def _on_close(self):
|
|
|
|
def callback(*_):
|
|
|
|
self.connected = False
|
|
|
|
if self._on_close_hndl:
|
2021-07-25 01:17:25 +02:00
|
|
|
try:
|
|
|
|
self._on_close_hndl()
|
2021-09-17 00:47:33 +02:00
|
|
|
except Exception as e:
|
|
|
|
self.logger.warning(f'Pushbullet listener close error: {e}')
|
2021-05-19 18:43:44 +02:00
|
|
|
|
|
|
|
return callback
|
|
|
|
|
|
|
|
|
|
|
|
# vim:sw=4:ts=4:et:
|