1212* Author(s): Jim Bennett, Elena Horton
1313"""
1414
15+ try :
16+ from typing import Any , Callable , Mapping , Union
17+ except ImportError :
18+ pass
19+
1520import json
1621import adafruit_logging as logging
1722from .iot_error import IoTError
1823from .iot_mqtt import IoTMQTT , IoTMQTTCallback , IoTResponse
1924
2025
21- def _validate_keys (connection_string_parts ) :
26+ def _validate_keys (connection_string_parts : Mapping ) -> None :
2227 """Raise ValueError if incorrect combination of keys"""
2328 host_name = connection_string_parts .get (HOST_NAME )
2429 shared_access_key_name = connection_string_parts .get (SHARED_ACCESS_KEY_NAME )
@@ -67,7 +72,7 @@ def connection_status_change(self, connected: bool) -> None:
6772 self ._on_connection_status_changed (connected )
6873
6974 # 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 :
7176 """Called when a direct method is invoked
7277 :param str method_name: The name of the method that was invoked
7378 :param str payload: The payload with the message
@@ -91,7 +96,10 @@ def cloud_to_device_message_received(self, body: str, properties: dict) -> None:
9196 self ._on_cloud_to_device_message_received (body , properties )
9297
9398 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 ,
95103 ) -> None :
96104 """Called when the device twin desired properties are updated
97105 :param str desired_property_name: The name of the desired property that was updated
@@ -107,7 +115,7 @@ def device_twin_desired_updated(
107115 def device_twin_reported_updated (
108116 self ,
109117 reported_property_name : str ,
110- reported_property_value ,
118+ reported_property_value : Any ,
111119 reported_version : int ,
112120 ) -> None :
113121 """Called when the device twin reported values are updated
@@ -175,21 +183,23 @@ def __init__(
175183 self ._mqtt = None
176184
177185 @property
178- def on_connection_status_changed (self ):
186+ def on_connection_status_changed (self ) -> Callable :
179187 """A callback method that is called when the connection status is changed. This method should have the following signature:
180188 def connection_status_changed(connected: bool) -> None
181189 """
182190 return self ._on_connection_status_changed
183191
184192 @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 :
186196 """A callback method that is called when the connection status is changed. This method should have the following signature:
187197 def connection_status_changed(connected: bool) -> None
188198 """
189199 self ._on_connection_status_changed = new_on_connection_status_changed
190200
191201 @property
192- def on_direct_method_invoked (self ):
202+ def on_direct_method_invoked (self ) -> Callable :
193203 """A callback method that is called when a direct method is invoked. This method should have the following signature:
194204 def direct_method_invoked(method_name: str, payload: str) -> IoTResponse:
195205
@@ -202,7 +212,7 @@ def direct_method_invoked(method_name: str, payload: str) -> IoTResponse:
202212 return self ._on_direct_method_invoked
203213
204214 @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 :
206216 """A callback method that is called when a direct method is invoked. This method should have the following signature:
207217 def direct_method_invoked(method_name: str, payload: str) -> IoTResponse:
208218
@@ -215,16 +225,16 @@ def direct_method_invoked(method_name: str, payload: str) -> IoTResponse:
215225 self ._on_direct_method_invoked = new_on_direct_method_invoked
216226
217227 @property
218- def on_cloud_to_device_message_received (self ):
228+ def on_cloud_to_device_message_received (self ) -> Callable :
219229 """A callback method that is called when a cloud to device message is received. This method should have the following signature:
220230 def cloud_to_device_message_received(body: str, properties: dict) -> None:
221231 """
222232 return self ._on_cloud_to_device_message_received
223233
224234 @on_cloud_to_device_message_received .setter
225235 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 :
228238 """A callback method that is called when a cloud to device message is received. This method should have the following signature:
229239 def cloud_to_device_message_received(body: str, properties: dict) -> None:
230240 """
@@ -233,15 +243,17 @@ def cloud_to_device_message_received(body: str, properties: dict) -> None:
233243 )
234244
235245 @property
236- def on_device_twin_desired_updated (self ):
246+ def on_device_twin_desired_updated (self ) -> Callable :
237247 """A callback method that is called when the desired properties of the devices device twin are updated.
238248 This method should have the following signature:
239249 def device_twin_desired_updated(desired_property_name: str, desired_property_value, desired_version: int) -> None:
240250 """
241251 return self ._on_device_twin_desired_updated
242252
243253 @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 :
245257 """A callback method that is called when the desired properties of the devices device twin are updated.
246258 This method should have the following signature:
247259 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
252264 self ._mqtt .subscribe_to_twins ()
253265
254266 @property
255- def on_device_twin_reported_updated (self ):
267+ def on_device_twin_reported_updated (self ) -> Callable :
256268 """A callback method that is called when the reported properties of the devices device twin are updated.
257269 This method should have the following signature:
258270 def device_twin_reported_updated(reported_property_name: str, reported_property_value, reported_version: int) -> None:
259271 """
260272 return self ._on_device_twin_reported_updated
261273
262274 @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 :
264278 """A callback method that is called when the reported properties of the devices device twin are updated.
265279 This method should have the following signature:
266280 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:
327341
328342 return False
329343
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 :
331347 """Send a device to cloud message from this device to Azure IoT Hub
332348 :param message: The message data as a JSON string or a dictionary
333349 :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:
339355
340356 self ._mqtt .send_device_to_cloud_message (message , system_properties )
341357
342- def update_twin (self , patch ) -> None :
358+ def update_twin (self , patch : Union [ str , dict ] ) -> None :
343359 """Updates the reported properties in the devices device twin
344360 :param patch: The JSON patch to apply to the device twin reported properties
345361 """
0 commit comments