diff --git a/bumble/gatt.py b/bumble/gatt.py index 6b740ce2..067f31db 100644 --- a/bumble/gatt.py +++ b/bumble/gatt.py @@ -326,6 +326,15 @@ class Characteristic(Attribute): f"Characteristic.Properties::from_string() error:\nExpected a string containing any of the keys, separated by , or |: {enum_list_str}\nGot: {properties_str}" ) + def __str__(self): + # NOTE: we override this method to offer a consistent result between python + # versions: the value returned by IntFlag.__str__() changed in version 11. + return '|'.join( + flag.name + for flag in Characteristic.Properties + if self.value & flag.value and flag.name is not None + ) + # For backwards compatibility these are defined here # For new code, please use Characteristic.Properties.X BROADCAST = Properties.BROADCAST @@ -365,7 +374,7 @@ class Characteristic(Attribute): f'Characteristic(handle=0x{self.handle:04X}, ' f'end=0x{self.end_group_handle:04X}, ' f'uuid={self.uuid}, ' - f'{self.properties.name})' + f'{self.properties})' ) @@ -393,7 +402,7 @@ class CharacteristicDeclaration(Attribute): f'CharacteristicDeclaration(handle=0x{self.handle:04X}, ' f'value_handle=0x{self.value_handle:04X}, ' f'uuid={self.characteristic.uuid}, ' - f'{self.characteristic.properties.name})' + f'{self.characteristic.properties})' ) diff --git a/tests/gatt_test.py b/tests/gatt_test.py index d33df98f..dd0277ea 100644 --- a/tests/gatt_test.py +++ b/tests/gatt_test.py @@ -803,14 +803,15 @@ async def test_mtu_exchange(): # ----------------------------------------------------------------------------- def test_char_property_to_string(): # single - assert Characteristic.Properties(0x01).name == "BROADCAST" - assert Characteristic.Properties.BROADCAST.name == "BROADCAST" + assert str(Characteristic.Properties(0x01)) == "BROADCAST" + assert str(Characteristic.Properties.BROADCAST) == "BROADCAST" # double - assert Characteristic.Properties(0x03).name == "BROADCAST|READ" + assert str(Characteristic.Properties(0x03)) == "BROADCAST|READ" assert ( - Characteristic.Properties.BROADCAST | Characteristic.Properties.READ - ).name == "BROADCAST|READ" + str(Characteristic.Properties.BROADCAST | Characteristic.Properties.READ) + == "BROADCAST|READ" + ) # -----------------------------------------------------------------------------