From 7b0d84dab332bd52a83324e34c3de4c27574ad7d Mon Sep 17 00:00:00 2001 From: Anton Sarukhanov <code@ant.sr> Date: Thu, 14 Jun 2018 00:08:23 -0400 Subject: [PATCH] Add more exception handling. --- setup.py | 2 +- turntouch/turntouch.py | 26 ++++++++++++++++---------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/setup.py b/setup.py index c6bff8f..2d3bb61 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,7 @@ def read(filename): setup( name='TurnTouch', - version='0.4', + version='0.4.1', url='https://github.com/antsar/python-turntouch', author='Anton Sarukhanov', author_email='code@ant.sr', diff --git a/turntouch/turntouch.py b/turntouch/turntouch.py index ae62f0b..a7fcd63 100644 --- a/turntouch/turntouch.py +++ b/turntouch/turntouch.py @@ -327,16 +327,22 @@ class TurnTouch(btle.Peripheral): def _enable_notifications(self, enabled=True): """Tell the remote to start sending button press notifications.""" - notification_handle = self.getCharacteristics( - uuid=self.BUTTON_STATUS_CHARACTERISTIC_UUID)[0].getHandle() - notification_enable_handle = notification_handle + 1 - logger.debug("{action} notifications for device {address}...".format( - action="Enabling" if enabled else "Disabling", address=self.addr)) - self.writeCharacteristic(notification_enable_handle, - bytes([0x01 if enabled else 0x00, 0x00]), - withResponse=True) - logger.debug("Notifications {action} for device {address}.".format( - action="enabled" if enabled else "disabled", address=self.addr)) + try: + notification_handle = self.getCharacteristics( + uuid=self.BUTTON_STATUS_CHARACTERISTIC_UUID)[0].getHandle() + notification_enable_handle = notification_handle + 1 + logger.debug("{action} notifications for device {address}..." + .format(action="Enabling" if enabled else "Disabling", + address=self.addr)) + self.writeCharacteristic(notification_enable_handle, + bytes([0x01 if enabled else 0x00, 0x00]), + withResponse=True) + logger.debug("Notifications {action} for device {address}." + .format(action="enabled" if enabled else "disabled", + address=self.addr)) + except btle.BTLEException: + raise TurnTouchException("Failed to enable notifications for" + "device {addr}".format(addr=self.addr)) class NotificationDelegate(btle.DefaultDelegate): """Handle callbacks for notifications from the device. -- GitLab