Skip to content
Snippets Groups Projects
Commit 27814c1c authored by Anton Sarukhanov's avatar Anton Sarukhanov
Browse files

Handle more bluepy exceptions.

parent f4f6ec5b
No related branches found
No related tags found
No related merge requests found
...@@ -9,7 +9,7 @@ def read(filename): ...@@ -9,7 +9,7 @@ def read(filename):
setup( setup(
name='TurnTouch', name='TurnTouch',
version='0.3', version='0.4',
url='https://github.com/antsar/python-turntouch', url='https://github.com/antsar/python-turntouch',
author='Anton Sarukhanov', author='Anton Sarukhanov',
author_email='code@ant.sr', author_email='code@ant.sr',
......
...@@ -265,8 +265,12 @@ class TurnTouch(btle.Peripheral): ...@@ -265,8 +265,12 @@ class TurnTouch(btle.Peripheral):
@property @property
def name(self) -> str: def name(self) -> str:
"""Read the nickname of this remote.""" """Read the nickname of this remote."""
name_bytes = self.getCharacteristics( try:
uuid=self.DEVICE_NAME_CHARACTERISTIC_UUID)[0].read() 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( logger.debug("Read name of device {address}: '{name}'".format(
address=self.addr, name=name_bytes)) address=self.addr, name=name_bytes))
return name_bytes.decode('utf-8').rstrip('\0') return name_bytes.decode('utf-8').rstrip('\0')
...@@ -280,15 +284,23 @@ class TurnTouch(btle.Peripheral): ...@@ -280,15 +284,23 @@ class TurnTouch(btle.Peripheral):
name_characteristic = self.getCharacteristics( name_characteristic = self.getCharacteristics(
uuid=self.DEVICE_NAME_CHARACTERISTIC_UUID)[0] uuid=self.DEVICE_NAME_CHARACTERISTIC_UUID)[0]
name_bytes = name.encode('utf-8').ljust(self.DEVICE_NAME_LENGTH, b'\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( logger.debug("Set name for device {address} to '{name}'".format(
address=self.addr, name=name_bytes)) address=self.addr, name=name_bytes))
@property @property
def battery(self) -> int: def battery(self) -> int:
"""Read the battery level (percentage) of this remote.""" """Read the battery level (percentage) of this remote."""
battery_bytes = self.getCharacteristics( try:
uuid=self.BATTERY_LEVEL_CHARACTERISTIC_UUID)[0].read() 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( logger.debug("Read device {address} battery level: '{battery}'".format(
address=self.addr, battery=battery_bytes)) address=self.addr, battery=battery_bytes))
return int.from_bytes(battery_bytes, byteorder='big') return int.from_bytes(battery_bytes, byteorder='big')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment