Coverage for src / bluetooth_sig / core / registration.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-03-18 11:17 +0000

1"""Custom characteristic and service registration manager. 

2 

3Provides runtime registration of custom characteristic and service classes 

4into the SIG registries. Stateless — writes to global registries. 

5""" 

6 

7from __future__ import annotations 

8 

9from typing import Any 

10 

11from ..gatt.characteristics.base import BaseCharacteristic 

12from ..gatt.characteristics.registry import CharacteristicRegistry 

13from ..gatt.services.base import BaseGattService 

14from ..gatt.services.registry import GattServiceRegistry 

15from ..gatt.uuid_registry import uuid_registry 

16from ..types import ( 

17 CharacteristicInfo, 

18 ServiceInfo, 

19) 

20 

21 

22class RegistrationManager: 

23 """Handles runtime registration of custom characteristics and services. 

24 

25 All registrations write to the global CharacteristicRegistry, GattServiceRegistry, 

26 and uuid_registry singletons. 

27 """ 

28 

29 @staticmethod 

30 def register_custom_characteristic_class( 

31 uuid_or_name: str, 

32 cls: type[BaseCharacteristic[Any]], 

33 info: CharacteristicInfo | None = None, 

34 override: bool = False, 

35 ) -> None: 

36 """Register a custom characteristic class at runtime. 

37 

38 Args: 

39 uuid_or_name: The characteristic UUID or name 

40 cls: The characteristic class to register 

41 info: Optional CharacteristicInfo with metadata (name, unit, value_type) 

42 override: Whether to override existing registrations 

43 

44 Raises: 

45 TypeError: If cls does not inherit from BaseCharacteristic 

46 ValueError: If UUID conflicts with existing registration and override=False 

47 

48 """ 

49 CharacteristicRegistry.register_characteristic_class(uuid_or_name, cls, override) 

50 

51 if info: 

52 uuid_registry.register_characteristic( 

53 uuid=info.uuid, 

54 name=info.name or cls.__name__, 

55 identifier=info.id, 

56 unit=info.unit, 

57 python_type=info.python_type, 

58 override=override, 

59 ) 

60 

61 @staticmethod 

62 def register_custom_service_class( 

63 uuid_or_name: str, 

64 cls: type[BaseGattService], 

65 info: ServiceInfo | None = None, 

66 override: bool = False, 

67 ) -> None: 

68 """Register a custom service class at runtime. 

69 

70 Args: 

71 uuid_or_name: The service UUID or name 

72 cls: The service class to register 

73 info: Optional ServiceInfo with metadata (name) 

74 override: Whether to override existing registrations 

75 

76 Raises: 

77 TypeError: If cls does not inherit from BaseGattService 

78 ValueError: If UUID conflicts with existing registration and override=False 

79 

80 """ 

81 GattServiceRegistry.register_service_class(uuid_or_name, cls, override) 

82 

83 if info: 

84 uuid_registry.register_service( 

85 uuid=info.uuid, 

86 name=info.name or cls.__name__, 

87 identifier=info.id, 

88 override=override, 

89 )