From 27814c1cbb0e1ee98ccc2e1296263e66c665258f Mon Sep 17 00:00:00 2001
From: Anton Sarukhanov <code@ant.sr>
Date: Wed, 13 Jun 2018 23:44:53 -0400
Subject: [PATCH] Handle more bluepy exceptions.

---
 setup.py               |  2 +-
 turntouch/turntouch.py | 22 +++++++++++++++++-----
 2 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/setup.py b/setup.py
index c0dbe08..c6bff8f 100644
--- a/setup.py
+++ b/setup.py
@@ -9,7 +9,7 @@ def read(filename):
 
 setup(
     name='TurnTouch',
-    version='0.3',
+    version='0.4',
     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 8030cc0..ae62f0b 100644
--- a/turntouch/turntouch.py
+++ b/turntouch/turntouch.py
@@ -265,8 +265,12 @@ class TurnTouch(btle.Peripheral):
     @property
     def name(self) -> str:
         """Read the nickname of this remote."""
-        name_bytes = self.getCharacteristics(
-            uuid=self.DEVICE_NAME_CHARACTERISTIC_UUID)[0].read()
+        try:
+            name_bytes = self.getCharacteristics(
+                uuid=self.DEVICE_NAME_CHARACTERISTIC_UUID)[0].read()
+        except btle.BTLEException:
+            raise TurnTouchException("Failed to read name of device {addr}"
+                                     .format(addr=self.addr))
         logger.debug("Read name of device {address}: '{name}'".format(
             address=self.addr, name=name_bytes))
         return name_bytes.decode('utf-8').rstrip('\0')
@@ -280,15 +284,23 @@ class TurnTouch(btle.Peripheral):
         name_characteristic = self.getCharacteristics(
             uuid=self.DEVICE_NAME_CHARACTERISTIC_UUID)[0]
         name_bytes = name.encode('utf-8').ljust(self.DEVICE_NAME_LENGTH, b'\0')
-        name_characteristic.write(name_bytes, withResponse=True)
+        try:
+            name_characteristic.write(name_bytes, withResponse=True)
+        except btle.BTLEException:
+            raise TurnTouchException("Failed to set name of device {addr}"
+                                     .format(addr=self.addr))
         logger.debug("Set name for device {address} to '{name}'".format(
             address=self.addr, name=name_bytes))
 
     @property
     def battery(self) -> int:
         """Read the battery level (percentage) of this remote."""
-        battery_bytes = self.getCharacteristics(
-            uuid=self.BATTERY_LEVEL_CHARACTERISTIC_UUID)[0].read()
+        try:
+            battery_bytes = self.getCharacteristics(
+                uuid=self.BATTERY_LEVEL_CHARACTERISTIC_UUID)[0].read()
+        except btle.BTLEException:
+            raise TurnTouchException("Failed to read battery of device {addr}"
+                                     .format(addr=self.addr))
         logger.debug("Read device {address} battery level: '{battery}'".format(
             address=self.addr, battery=battery_bytes))
         return int.from_bytes(battery_bytes, byteorder='big')
-- 
GitLab