diff --git a/README.md b/README.md index 681b1e65984dc8e4881eaa291fe465adfa4b5f3d..9b43adf1085df7e9e97df3fb35608f2cf48061e5 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 b22412b8cc037061f4088ddb3d94e88e6f71678d..2e1b1f3452ec3a16c4621139e2661aa422e99f1d 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)