bumble 0.0.159__py3-none-any.whl → 0.0.161__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
bumble/gatt.py CHANGED
@@ -283,8 +283,7 @@ class IncludedServiceDeclaration(Attribute):
283
283
  f'IncludedServiceDefinition(handle=0x{self.handle:04X}, '
284
284
  f'group_starting_handle=0x{self.service.handle:04X}, '
285
285
  f'group_ending_handle=0x{self.service.end_group_handle:04X}, '
286
- f'uuid={self.service.uuid}, '
287
- f'{self.service.properties!s})'
286
+ f'uuid={self.service.uuid})'
288
287
  )
289
288
 
290
289
 
@@ -309,31 +308,33 @@ class Characteristic(Attribute):
309
308
  AUTHENTICATED_SIGNED_WRITES = 0x40
310
309
  EXTENDED_PROPERTIES = 0x80
311
310
 
312
- @staticmethod
313
- def from_string(properties_str: str) -> Characteristic.Properties:
314
- property_names: List[str] = []
315
- for property in Characteristic.Properties:
316
- if property.name is None:
317
- raise TypeError()
318
- property_names.append(property.name)
319
-
320
- def string_to_property(property_string) -> Characteristic.Properties:
321
- for property in zip(Characteristic.Properties, property_names):
322
- if property_string == property[1]:
323
- return property[0]
324
- raise TypeError(f"Unable to convert {property_string} to Property")
325
-
311
+ @classmethod
312
+ def from_string(cls, properties_str: str) -> Characteristic.Properties:
326
313
  try:
327
314
  return functools.reduce(
328
- lambda x, y: x | string_to_property(y),
329
- properties_str.split(","),
315
+ lambda x, y: x | cls[y],
316
+ properties_str.replace("|", ",").split(","),
330
317
  Characteristic.Properties(0),
331
318
  )
332
- except TypeError:
319
+ except (TypeError, KeyError):
320
+ # The check for `p.name is not None` here is needed because for InFlag
321
+ # enums, the .name property can be None, when the enum value is 0,
322
+ # so the type hint for .name is Optional[str].
323
+ enum_list: List[str] = [p.name for p in cls if p.name is not None]
324
+ enum_list_str = ",".join(enum_list)
333
325
  raise TypeError(
334
- f"Characteristic.Properties::from_string() error:\nExpected a string containing any of the keys, separated by commas: {','.join(property_names)}\nGot: {properties_str}"
326
+ f"Characteristic.Properties::from_string() error:\nExpected a string containing any of the keys, separated by , or |: {enum_list_str}\nGot: {properties_str}"
335
327
  )
336
328
 
329
+ def __str__(self):
330
+ # NOTE: we override this method to offer a consistent result between python
331
+ # versions: the value returned by IntFlag.__str__() changed in version 11.
332
+ return '|'.join(
333
+ flag.name
334
+ for flag in Characteristic.Properties
335
+ if self.value & flag.value and flag.name is not None
336
+ )
337
+
337
338
  # For backwards compatibility these are defined here
338
339
  # For new code, please use Characteristic.Properties.X
339
340
  BROADCAST = Properties.BROADCAST
@@ -373,7 +374,7 @@ class Characteristic(Attribute):
373
374
  f'Characteristic(handle=0x{self.handle:04X}, '
374
375
  f'end=0x{self.end_group_handle:04X}, '
375
376
  f'uuid={self.uuid}, '
376
- f'{self.properties!s})'
377
+ f'{self.properties})'
377
378
  )
378
379
 
379
380
 
@@ -401,7 +402,7 @@ class CharacteristicDeclaration(Attribute):
401
402
  f'CharacteristicDeclaration(handle=0x{self.handle:04X}, '
402
403
  f'value_handle=0x{self.value_handle:04X}, '
403
404
  f'uuid={self.characteristic.uuid}, '
404
- f'{self.characteristic.properties!s})'
405
+ f'{self.characteristic.properties})'
405
406
  )
406
407
 
407
408