10
10
11
11
"""
12
12
13
+ from __future__ import annotations
14
+
13
15
import json
14
16
from . import Attribute
15
17
from . import Characteristic
16
18
19
+ try :
20
+ from typing import Optional , Any , Type , TYPE_CHECKING
21
+
22
+ if TYPE_CHECKING :
23
+ from circuitpython_typing import ReadableBuffer
24
+ from adafruit_ble .uuid import UUID
25
+ from adafruit_ble .services import Service
26
+
27
+ except ImportError :
28
+ pass
29
+
17
30
__version__ = "0.0.0-auto.0"
18
31
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BLE.git"
19
32
@@ -24,12 +37,12 @@ class JSONCharacteristic(Characteristic):
24
37
def __init__ (
25
38
self ,
26
39
* ,
27
- uuid = None ,
28
- properties = Characteristic .READ ,
29
- read_perm = Attribute .OPEN ,
30
- write_perm = Attribute .OPEN ,
31
- initial_value = None ,
32
- ):
40
+ uuid : Optional [ UUID ] = None ,
41
+ properties : int = Characteristic .READ ,
42
+ read_perm : int = Attribute .OPEN ,
43
+ write_perm : int = Attribute .OPEN ,
44
+ initial_value : Optional [ ReadableBuffer ] = None ,
45
+ ) -> None :
33
46
super ().__init__ (
34
47
uuid = uuid ,
35
48
properties = properties ,
@@ -41,19 +54,21 @@ def __init__(
41
54
)
42
55
43
56
@staticmethod
44
- def pack (value ) :
57
+ def pack (value : Any ) -> bytes :
45
58
"""Converts a JSON serializable python value into a utf-8 encoded JSON string."""
46
59
return json .dumps (value ).encode ("utf-8" )
47
60
48
61
@staticmethod
49
- def unpack (value ) :
62
+ def unpack (value : ReadableBuffer ) -> Any :
50
63
"""Converts a utf-8 encoded JSON string into a python value."""
51
64
return json .loads (str (value , "utf-8" ))
52
65
53
- def __get__ (self , obj , cls = None ):
66
+ def __get__ (
67
+ self , obj : Optional [Service ], cls : Optional [Type [Service ]] = None
68
+ ) -> Any :
54
69
if obj is None :
55
70
return self
56
71
return self .unpack (super ().__get__ (obj , cls ))
57
72
58
- def __set__ (self , obj , value ) :
73
+ def __set__ (self , obj : Service , value : Any ) -> None :
59
74
super ().__set__ (obj , self .pack (value ))
0 commit comments