Skip to content

Commit

Permalink
Add disconnected_callback
Browse files Browse the repository at this point in the history
  • Loading branch information
abmantis authored and newAM committed Aug 24, 2023
1 parent c8fd84b commit b6eb2fa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
5 changes: 4 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@
pygments_style = "sphinx"
todo_include_todos = True
nitpicky = True
nitpick_ignore = [("py:class", "bleak.backends.device.BLEDevice")]
nitpick_ignore = [
("py:class", "bleak.backends.device.BLEDevice"),
("py:class", "bleak.BleakClient"),
]

# HTML Options
html_theme = "sphinx_rtd_theme"
Expand Down
16 changes: 14 additions & 2 deletions idasen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ class IdasenDesk:
mac: Bluetooth MAC address of the desk, or an instance of a BLEDevice.
exit_on_fail: If set to True, failing to connect will call ``sys.exit(1)``,
otherwise the exception will be raised.
disconnected_callback:
Callback that will be scheduled in the event loop when the client is
disconnected. The callable must take one argument, which will be
this client object.
Note:
There is no locking to prevent you from running multiple movement
Expand All @@ -84,9 +88,17 @@ class IdasenDesk:
#: Number of times to retry upon failure to connect.
RETRY_COUNT: int = 3

def __init__(self, mac: Union[BLEDevice, str], exit_on_fail: bool = False):
def __init__(
self,
mac: Union[BLEDevice, str],
exit_on_fail: bool = False,
disconnected_callback: Optional[Callable[[BleakClient], None]] = None,
):
self._exit_on_fail = exit_on_fail
self._client = BleakClient(mac)
self._client = BleakClient(
address_or_ble_device=mac,
disconnected_callback=disconnected_callback,
)
self._mac = mac.address if isinstance(mac, BLEDevice) else mac
self._logger = _DeskLoggingAdapter(
logger=logging.getLogger(__name__), extra={"mac": self.mac}
Expand Down

0 comments on commit b6eb2fa

Please sign in to comment.