12
12
* Author(s): Jim Bennett, Elena Horton
13
13
"""
14
14
15
+ try :
16
+ from typing import Any , Callable , Mapping , Union
17
+ except ImportError :
18
+ pass
19
+
15
20
import json
16
21
import adafruit_logging as logging
17
22
from .iot_error import IoTError
18
23
from .iot_mqtt import IoTMQTT , IoTMQTTCallback , IoTResponse
19
24
20
25
21
- def _validate_keys (connection_string_parts ) :
26
+ def _validate_keys (connection_string_parts : Mapping ) -> None :
22
27
"""Raise ValueError if incorrect combination of keys"""
23
28
host_name = connection_string_parts .get (HOST_NAME )
24
29
shared_access_key_name = connection_string_parts .get (SHARED_ACCESS_KEY_NAME )
@@ -67,7 +72,7 @@ def connection_status_change(self, connected: bool) -> None:
67
72
self ._on_connection_status_changed (connected )
68
73
69
74
# pylint: disable=W0613, R0201
70
- def direct_method_invoked (self , method_name : str , payload ) -> IoTResponse :
75
+ def direct_method_invoked (self , method_name : str , payload : str ) -> IoTResponse :
71
76
"""Called when a direct method is invoked
72
77
:param str method_name: The name of the method that was invoked
73
78
:param str payload: The payload with the message
@@ -91,7 +96,10 @@ def cloud_to_device_message_received(self, body: str, properties: dict) -> None:
91
96
self ._on_cloud_to_device_message_received (body , properties )
92
97
93
98
def device_twin_desired_updated (
94
- self , desired_property_name : str , desired_property_value , desired_version : int
99
+ self ,
100
+ desired_property_name : str ,
101
+ desired_property_value : Any ,
102
+ desired_version : int ,
95
103
) -> None :
96
104
"""Called when the device twin desired properties are updated
97
105
:param str desired_property_name: The name of the desired property that was updated
@@ -107,7 +115,7 @@ def device_twin_desired_updated(
107
115
def device_twin_reported_updated (
108
116
self ,
109
117
reported_property_name : str ,
110
- reported_property_value ,
118
+ reported_property_value : Any ,
111
119
reported_version : int ,
112
120
) -> None :
113
121
"""Called when the device twin reported values are updated
@@ -175,21 +183,23 @@ def __init__(
175
183
self ._mqtt = None
176
184
177
185
@property
178
- def on_connection_status_changed (self ):
186
+ def on_connection_status_changed (self ) -> Callable :
179
187
"""A callback method that is called when the connection status is changed. This method should have the following signature:
180
188
def connection_status_changed(connected: bool) -> None
181
189
"""
182
190
return self ._on_connection_status_changed
183
191
184
192
@on_connection_status_changed .setter
185
- def on_connection_status_changed (self , new_on_connection_status_changed ):
193
+ def on_connection_status_changed (
194
+ self , new_on_connection_status_changed : Callable
195
+ ) -> None :
186
196
"""A callback method that is called when the connection status is changed. This method should have the following signature:
187
197
def connection_status_changed(connected: bool) -> None
188
198
"""
189
199
self ._on_connection_status_changed = new_on_connection_status_changed
190
200
191
201
@property
192
- def on_direct_method_invoked (self ):
202
+ def on_direct_method_invoked (self ) -> Callable :
193
203
"""A callback method that is called when a direct method is invoked. This method should have the following signature:
194
204
def direct_method_invoked(method_name: str, payload: str) -> IoTResponse:
195
205
@@ -202,7 +212,7 @@ def direct_method_invoked(method_name: str, payload: str) -> IoTResponse:
202
212
return self ._on_direct_method_invoked
203
213
204
214
@on_direct_method_invoked .setter
205
- def on_direct_method_invoked (self , new_on_direct_method_invoked ) :
215
+ def on_direct_method_invoked (self , new_on_direct_method_invoked : Callable ) -> None :
206
216
"""A callback method that is called when a direct method is invoked. This method should have the following signature:
207
217
def direct_method_invoked(method_name: str, payload: str) -> IoTResponse:
208
218
@@ -215,16 +225,16 @@ def direct_method_invoked(method_name: str, payload: str) -> IoTResponse:
215
225
self ._on_direct_method_invoked = new_on_direct_method_invoked
216
226
217
227
@property
218
- def on_cloud_to_device_message_received (self ):
228
+ def on_cloud_to_device_message_received (self ) -> Callable :
219
229
"""A callback method that is called when a cloud to device message is received. This method should have the following signature:
220
230
def cloud_to_device_message_received(body: str, properties: dict) -> None:
221
231
"""
222
232
return self ._on_cloud_to_device_message_received
223
233
224
234
@on_cloud_to_device_message_received .setter
225
235
def on_cloud_to_device_message_received (
226
- self , new_on_cloud_to_device_message_received
227
- ):
236
+ self , new_on_cloud_to_device_message_received : Callable
237
+ ) -> None :
228
238
"""A callback method that is called when a cloud to device message is received. This method should have the following signature:
229
239
def cloud_to_device_message_received(body: str, properties: dict) -> None:
230
240
"""
@@ -233,15 +243,17 @@ def cloud_to_device_message_received(body: str, properties: dict) -> None:
233
243
)
234
244
235
245
@property
236
- def on_device_twin_desired_updated (self ):
246
+ def on_device_twin_desired_updated (self ) -> Callable :
237
247
"""A callback method that is called when the desired properties of the devices device twin are updated.
238
248
This method should have the following signature:
239
249
def device_twin_desired_updated(desired_property_name: str, desired_property_value, desired_version: int) -> None:
240
250
"""
241
251
return self ._on_device_twin_desired_updated
242
252
243
253
@on_device_twin_desired_updated .setter
244
- def on_device_twin_desired_updated (self , new_on_device_twin_desired_updated ):
254
+ def on_device_twin_desired_updated (
255
+ self , new_on_device_twin_desired_updated : Callable
256
+ ) -> None :
245
257
"""A callback method that is called when the desired properties of the devices device twin are updated.
246
258
This method should have the following signature:
247
259
def device_twin_desired_updated(desired_property_name: str, desired_property_value, desired_version: int) -> None:
@@ -252,15 +264,17 @@ def device_twin_desired_updated(desired_property_name: str, desired_property_val
252
264
self ._mqtt .subscribe_to_twins ()
253
265
254
266
@property
255
- def on_device_twin_reported_updated (self ):
267
+ def on_device_twin_reported_updated (self ) -> Callable :
256
268
"""A callback method that is called when the reported properties of the devices device twin are updated.
257
269
This method should have the following signature:
258
270
def device_twin_reported_updated(reported_property_name: str, reported_property_value, reported_version: int) -> None:
259
271
"""
260
272
return self ._on_device_twin_reported_updated
261
273
262
274
@on_device_twin_reported_updated .setter
263
- def on_device_twin_reported_updated (self , new_on_device_twin_reported_updated ):
275
+ def on_device_twin_reported_updated (
276
+ self , new_on_device_twin_reported_updated : Callable
277
+ ) -> None :
264
278
"""A callback method that is called when the reported properties of the devices device twin are updated.
265
279
This method should have the following signature:
266
280
def device_twin_reported_updated(reported_property_name: str, reported_property_value, reported_version: int) -> None:
@@ -327,7 +341,9 @@ def is_connected(self) -> bool:
327
341
328
342
return False
329
343
330
- def send_device_to_cloud_message (self , message , system_properties = None ) -> None :
344
+ def send_device_to_cloud_message (
345
+ self , message : Union [str , dict ], system_properties : dict = None
346
+ ) -> None :
331
347
"""Send a device to cloud message from this device to Azure IoT Hub
332
348
:param message: The message data as a JSON string or a dictionary
333
349
:param system_properties: System properties to send with the message
@@ -339,7 +355,7 @@ def send_device_to_cloud_message(self, message, system_properties=None) -> None:
339
355
340
356
self ._mqtt .send_device_to_cloud_message (message , system_properties )
341
357
342
- def update_twin (self , patch ) -> None :
358
+ def update_twin (self , patch : Union [ str , dict ] ) -> None :
343
359
"""Updates the reported properties in the devices device twin
344
360
:param patch: The JSON patch to apply to the device twin reported properties
345
361
"""
0 commit comments