33
33
from adafruit_portalbase .network import NetworkBase
34
34
from adafruit_portalbase .wifi_esp32s2 import WiFi
35
35
36
+ try :
37
+ from typing import Optional , Union , Callable
38
+ from adafruit_dotstar import DotStar
39
+ except ImportError :
40
+ pass
41
+
36
42
__version__ = "0.0.0-auto.0"
37
43
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FunHouse.git"
38
44
@@ -54,18 +60,18 @@ class Network(NetworkBase):
54
60
def __init__ (
55
61
self ,
56
62
* ,
57
- status_dotstar = None ,
58
- extract_values = True ,
59
- debug = False ,
60
- ):
63
+ status_dotstar : Optional [ DotStar ] = None ,
64
+ extract_values : bool = True ,
65
+ debug : bool = False ,
66
+ ) -> None :
61
67
super ().__init__ (
62
68
WiFi (status_led = status_dotstar ),
63
69
extract_values = extract_values ,
64
70
debug = debug ,
65
71
)
66
72
self ._mqtt_client = None
67
73
68
- def init_io_mqtt (self ):
74
+ def init_io_mqtt (self ) -> IO_MQTT :
69
75
"""Initialize MQTT for Adafruit IO"""
70
76
try :
71
77
aio_username = self ._secrets ["aio_username" ]
@@ -80,12 +86,12 @@ def init_io_mqtt(self):
80
86
# pylint: disable=too-many-arguments
81
87
def init_mqtt (
82
88
self ,
83
- broker ,
84
- port = 8883 ,
85
- username = None ,
86
- password = None ,
87
- use_io = False ,
88
- ):
89
+ broker : str ,
90
+ port : int = 8883 ,
91
+ username : str = None ,
92
+ password : str = None ,
93
+ use_io : bool = False ,
94
+ ) -> Union [ MQTT . MQTT , IO_MQTT ] :
89
95
"""Initialize MQTT"""
90
96
self .connect ()
91
97
self ._mqtt_client = MQTT .MQTT (
@@ -103,12 +109,14 @@ def init_mqtt(
103
109
104
110
# pylint: enable=too-many-arguments
105
111
106
- def _get_mqtt_client (self ):
112
+ def _get_mqtt_client (self ) -> Union [ MQTT . MQTT , IO_MQTT ] :
107
113
if self ._mqtt_client is not None :
108
114
return self ._mqtt_client
109
115
raise RuntimeError ("Please initialize MQTT before using" )
110
116
111
- def mqtt_loop (self , * args , suppress_mqtt_errors = True , ** kwargs ):
117
+ def mqtt_loop (
118
+ self , * args : int , suppress_mqtt_errors : bool = True , ** kwargs : int
119
+ ) -> None :
112
120
"""Run the MQTT Loop"""
113
121
self ._get_mqtt_client ()
114
122
if suppress_mqtt_errors :
@@ -123,7 +131,12 @@ def mqtt_loop(self, *args, suppress_mqtt_errors=True, **kwargs):
123
131
if self ._mqtt_client is not None :
124
132
self ._mqtt_client .loop (* args , ** kwargs )
125
133
126
- def mqtt_publish (self , * args , suppress_mqtt_errors = True , ** kwargs ):
134
+ def mqtt_publish (
135
+ self ,
136
+ * args : Union [str , int , float ],
137
+ suppress_mqtt_errors : bool = True ,
138
+ ** kwargs : Union [str , int , float ]
139
+ ) -> None :
127
140
"""Publish to MQTT"""
128
141
self ._get_mqtt_client ()
129
142
if suppress_mqtt_errors :
@@ -136,14 +149,16 @@ def mqtt_publish(self, *args, suppress_mqtt_errors=True, **kwargs):
136
149
if self ._mqtt_client is not None :
137
150
self ._mqtt_client .publish (* args , ** kwargs )
138
151
139
- def mqtt_connect (self , * args , ** kwargs ):
152
+ def mqtt_connect (
153
+ self , * args : Union [bool , str , int ], ** kwargs : Union [bool , str , int ]
154
+ ) -> None :
140
155
"""Connect to MQTT"""
141
156
self ._get_mqtt_client ()
142
157
if self ._mqtt_client is not None :
143
158
self ._mqtt_client .connect (* args , ** kwargs )
144
159
145
160
@property
146
- def on_mqtt_connect (self ):
161
+ def on_mqtt_connect (self ) -> Optional [ Callable ] :
147
162
"""
148
163
Get or Set the MQTT Connect Handler
149
164
@@ -153,12 +168,12 @@ def on_mqtt_connect(self):
153
168
return None
154
169
155
170
@on_mqtt_connect .setter
156
- def on_mqtt_connect (self , value ) :
171
+ def on_mqtt_connect (self , value : Callable ) -> None :
157
172
self ._get_mqtt_client ()
158
173
self ._mqtt_client .on_connect = value
159
174
160
175
@property
161
- def on_mqtt_disconnect (self ):
176
+ def on_mqtt_disconnect (self ) -> Optional [ Callable ] :
162
177
"""
163
178
Get or Set the MQTT Disconnect Handler
164
179
@@ -168,11 +183,11 @@ def on_mqtt_disconnect(self):
168
183
return None
169
184
170
185
@on_mqtt_disconnect .setter
171
- def on_mqtt_disconnect (self , value ) :
186
+ def on_mqtt_disconnect (self , value : Callable ) -> None :
172
187
self ._get_mqtt_client ().on_disconnect = value
173
188
174
189
@property
175
- def on_mqtt_subscribe (self ):
190
+ def on_mqtt_subscribe (self ) -> Optional [ Callable ] :
176
191
"""
177
192
Get or Set the MQTT Subscribe Handler
178
193
@@ -182,11 +197,11 @@ def on_mqtt_subscribe(self):
182
197
return None
183
198
184
199
@on_mqtt_subscribe .setter
185
- def on_mqtt_subscribe (self , value ) :
200
+ def on_mqtt_subscribe (self , value : Callable ) -> None :
186
201
self ._get_mqtt_client ().on_subscribe = value
187
202
188
203
@property
189
- def on_mqtt_unsubscribe (self ):
204
+ def on_mqtt_unsubscribe (self ) -> Optional [ Callable ] :
190
205
"""
191
206
Get or Set the MQTT Unsubscribe Handler
192
207
@@ -196,11 +211,11 @@ def on_mqtt_unsubscribe(self):
196
211
return None
197
212
198
213
@on_mqtt_unsubscribe .setter
199
- def on_mqtt_unsubscribe (self , value ) :
214
+ def on_mqtt_unsubscribe (self , value : Callable ) -> None :
200
215
self ._get_mqtt_client ().on_unsubscribe = value
201
216
202
217
@property
203
- def on_mqtt_message (self ):
218
+ def on_mqtt_message (self ) -> Optional [ Callable ] :
204
219
"""
205
220
Get or Set the MQTT Message Handler
206
221
@@ -210,17 +225,17 @@ def on_mqtt_message(self):
210
225
return None
211
226
212
227
@on_mqtt_message .setter
213
- def on_mqtt_message (self , value ) :
228
+ def on_mqtt_message (self , value : Callable ) -> None :
214
229
self ._get_mqtt_client ().on_message = value
215
230
216
231
@property
217
- def enabled (self ):
232
+ def enabled (self ) -> bool :
218
233
"""
219
234
Get or Set whether the WiFi is enabled
220
235
221
236
"""
222
237
return self ._wifi .enabled
223
238
224
239
@enabled .setter
225
- def enabled (self , value ) :
240
+ def enabled (self , value : bool ) -> None :
226
241
self ._wifi .enabled = bool (value )
0 commit comments