13
13
14
14
from ..attributes import Attribute
15
15
16
+ try :
17
+ from typing import Optional , Type , Union , Tuple , Iterable
18
+ from circuitpython_typing import ReadableBuffer
19
+ from adafruit_ble .uuid import UUID
20
+ from adafruit_ble .services import Service
21
+ except ImportError :
22
+ pass
23
+
16
24
__version__ = "0.0.0-auto.0"
17
25
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BLE.git"
18
26
@@ -75,14 +83,14 @@ class Characteristic:
75
83
def __init__ (
76
84
self ,
77
85
* ,
78
- uuid = None ,
79
- properties = 0 ,
80
- read_perm = Attribute .OPEN ,
81
- write_perm = Attribute .OPEN ,
82
- max_length = None ,
83
- fixed_length = False ,
84
- initial_value = None
85
- ):
86
+ uuid : Optional [ UUID ] = None ,
87
+ properties : int = 0 ,
88
+ read_perm : int = Attribute .OPEN ,
89
+ write_perm : int = Attribute .OPEN ,
90
+ max_length : Optional [ int ] = None ,
91
+ fixed_length : bool = False ,
92
+ initial_value : Optional [ ReadableBuffer ] = None
93
+ ) -> None :
86
94
self .field_name = None # Set by Service during basic binding
87
95
88
96
if uuid :
@@ -94,7 +102,9 @@ def __init__(
94
102
self .fixed_length = fixed_length
95
103
self .initial_value = initial_value
96
104
97
- def _ensure_bound (self , service , initial_value = None ):
105
+ def _ensure_bound (
106
+ self , service : Service , initial_value : Optional [bytes ] = None
107
+ ) -> None :
98
108
"""Binds the characteristic to the local Service or remote Characteristic object given."""
99
109
if self .field_name in service .bleio_characteristics :
100
110
return
@@ -110,7 +120,9 @@ def _ensure_bound(self, service, initial_value=None):
110
120
111
121
service .bleio_characteristics [self .field_name ] = bleio_characteristic
112
122
113
- def __bind_locally (self , service , initial_value ):
123
+ def __bind_locally (
124
+ self , service : Service , initial_value : Optional [bytes ]
125
+ ) -> _bleio .Characteristic :
114
126
if initial_value is None :
115
127
initial_value = self .initial_value
116
128
if initial_value is None and self .max_length :
@@ -132,7 +144,9 @@ def __bind_locally(self, service, initial_value):
132
144
write_perm = self .write_perm ,
133
145
)
134
146
135
- def __get__ (self , service , cls = None ):
147
+ def __get__ (
148
+ self , service : Optional [Service ], cls : Optional [Type [Service ]] = None
149
+ ) -> ReadableBuffer :
136
150
# CircuitPython doesn't invoke descriptor protocol on obj's class,
137
151
# but CPython does. In the CPython case, pretend that it doesn't.
138
152
if service is None :
@@ -141,7 +155,7 @@ def __get__(self, service, cls=None):
141
155
bleio_characteristic = service .bleio_characteristics [self .field_name ]
142
156
return bleio_characteristic .value
143
157
144
- def __set__ (self , service , value ) :
158
+ def __set__ (self , service : Service , value : ReadableBuffer ) -> None :
145
159
self ._ensure_bound (service , value )
146
160
if value is None :
147
161
value = b""
@@ -159,14 +173,14 @@ class ComplexCharacteristic:
159
173
def __init__ (
160
174
self ,
161
175
* ,
162
- uuid = None ,
163
- properties = 0 ,
164
- read_perm = Attribute .OPEN ,
165
- write_perm = Attribute .OPEN ,
166
- max_length = 20 ,
167
- fixed_length = False ,
168
- initial_value = None
169
- ):
176
+ uuid : Optional [ UUID ] = None ,
177
+ properties : int = 0 ,
178
+ read_perm : int = Attribute .OPEN ,
179
+ write_perm : int = Attribute .OPEN ,
180
+ max_length : int = 20 ,
181
+ fixed_length : bool = False ,
182
+ initial_value : Optional [ ReadableBuffer ] = None
183
+ ) -> None :
170
184
self .field_name = None # Set by Service during basic binding
171
185
172
186
if uuid :
@@ -178,7 +192,7 @@ def __init__(
178
192
self .fixed_length = fixed_length
179
193
self .initial_value = initial_value
180
194
181
- def bind (self , service ) :
195
+ def bind (self , service : Service ) -> _bleio . Characteristic :
182
196
"""Binds the characteristic to the local Service or remote Characteristic object given."""
183
197
if service .remote :
184
198
for characteristic in service .bleio_service .characteristics :
@@ -195,7 +209,9 @@ def bind(self, service):
195
209
write_perm = self .write_perm ,
196
210
)
197
211
198
- def __get__ (self , service , cls = None ):
212
+ def __get__ (
213
+ self , service : Optional [Service ], cls : Optional [Type [Service ]] = None
214
+ ) -> Characteristic :
199
215
if service is None :
200
216
return self
201
217
bound_object = self .bind (service )
@@ -220,12 +236,12 @@ def __init__(
220
236
self ,
221
237
struct_format ,
222
238
* ,
223
- uuid = None ,
224
- properties = 0 ,
225
- read_perm = Attribute .OPEN ,
226
- write_perm = Attribute .OPEN ,
227
- initial_value = None
228
- ):
239
+ uuid : Optional [ UUID ] = None ,
240
+ properties : int = 0 ,
241
+ read_perm : int = Attribute .OPEN ,
242
+ write_perm : int = Attribute .OPEN ,
243
+ initial_value : Optional [ ReadableBuffer ] = None
244
+ ) -> None :
229
245
self ._struct_format = struct_format
230
246
self ._expected_size = struct .calcsize (struct_format )
231
247
if initial_value is not None :
@@ -240,14 +256,16 @@ def __init__(
240
256
write_perm = write_perm ,
241
257
)
242
258
243
- def __get__ (self , obj , cls = None ):
259
+ def __get__ (
260
+ self , obj : Optional [Service ], cls : Optional [Type [Service ]] = None
261
+ ) -> Optional [Union [Tuple , "StructCharacteristic" ]]:
244
262
if obj is None :
245
263
return self
246
264
raw_data = super ().__get__ (obj , cls )
247
265
if len (raw_data ) < self ._expected_size :
248
266
return None
249
267
return struct .unpack (self ._struct_format , raw_data )
250
268
251
- def __set__ (self , obj , value ) :
269
+ def __set__ (self , obj : Service , value : Iterable ) -> None :
252
270
encoded = struct .pack (self ._struct_format , * value )
253
271
super ().__set__ (obj , encoded )
0 commit comments