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

Implement battery level.

Fixes #1.
parent 66210139
No related branches found
No related tags found
No related merge requests found
...@@ -39,8 +39,8 @@ import turntouch ...@@ -39,8 +39,8 @@ import turntouch
# Connect to a device by MAC address # Connect to a device by MAC address
tt = turntouch.TurnTouch('c0:ff:ee:c0:ff:ee') tt = turntouch.TurnTouch('c0:ff:ee:c0:ff:ee')
# Read the device nickname # Read the device nickname and battery percentage
print(tt.name) print("Name: {}\nBattery: {}".format(tt.name, tt.battery))
# Update the device nickname (max. 32 characters) # Update the device nickname (max. 32 characters)
tt.name = 'Living Room Remote' tt.name = 'Living Room Remote'
......
...@@ -283,6 +283,15 @@ class TurnTouch(btle.Peripheral): ...@@ -283,6 +283,15 @@ class TurnTouch(btle.Peripheral):
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
def battery(self) -> int:
"""Read the battery level (percentage) of this remote."""
battery_bytes = self.getCharacteristics(
uuid=self.BATTERY_LEVEL_CHARACTERISTIC_UUID)[0].read()
logger.debug("Read device {address} battery level: '{battery}'".format(
address=self.addr, battery=battery_bytes))
return int.from_bytes(battery_bytes, byteorder='big')
def listen_forever(self): def listen_forever(self):
"""Listen for button press events indefinitely.""" """Listen for button press events indefinitely."""
self.listen(only_one=False) self.listen(only_one=False)
......
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