From f19ede77bf730a782004aad69b61d121d3d07248 Mon Sep 17 00:00:00 2001
From: Anton Sarukhanov <code@ant.sr>
Date: Mon, 11 Jun 2018 23:28:15 -0400
Subject: [PATCH] Implement battery level. Fixes #1.

---
 README.md              | 4 ++--
 turntouch/turntouch.py | 9 +++++++++
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/README.md b/README.md
index 681b1e6..9b43adf 100644
--- a/README.md
+++ b/README.md
@@ -39,8 +39,8 @@ import turntouch
 # Connect to a device by MAC address
 tt = turntouch.TurnTouch('c0:ff:ee:c0:ff:ee')
 
-# Read the device nickname
-print(tt.name)
+# Read the device nickname and battery percentage
+print("Name: {}\nBattery: {}".format(tt.name, tt.battery))
 
 # Update the device nickname (max. 32 characters)
 tt.name = 'Living Room Remote'
diff --git a/turntouch/turntouch.py b/turntouch/turntouch.py
index b22412b..2e1b1f3 100644
--- a/turntouch/turntouch.py
+++ b/turntouch/turntouch.py
@@ -283,6 +283,15 @@ class TurnTouch(btle.Peripheral):
         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()
+        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):
         """Listen for button press events indefinitely."""
         self.listen(only_one=False)
-- 
GitLab