Skip to content

Commit 8cfabc3

Browse files
committed
Add type annotations for fona_3g.py
1 parent 9b8fb8b commit 8cfabc3

File tree

1 file changed

+48
-24
lines changed

1 file changed

+48
-24
lines changed

adafruit_fona/fona_3g.py

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@
2424
from micropython import const
2525
from .adafruit_fona import FONA, REPLY_OK
2626

27+
try:
28+
from typing import Optional, Tuple, Union, Literal
29+
from busio import UART
30+
from digitalio import DigitalInOut
31+
except ImportError:
32+
pass
33+
2734
__version__ = "0.0.0-auto.0"
2835
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FONA.git"
2936

@@ -32,18 +39,24 @@
3239

3340
class FONA3G(FONA):
3441
"""FONA 3G module interface.
35-
:param ~busio.uart UART: FONA UART connection.
36-
:param ~digialio RST: FONA RST pin.
37-
:param ~digialio RI: Optional FONA Ring Interrupt (RI) pin.
38-
:param bool debug: Enable debugging output.
3942
43+
:param ~busio.uart uart: FONA UART connection.
44+
:param ~DigitalInOut rst: FONA RST pin.
45+
:param ~DigitalInOut ri: Optional FONA Ring Interrupt (RI) pin.
46+
:param bool debug: Enable debugging output.
4047
"""
4148

42-
def __init__(self, uart, rst, ri=None, debug=False):
49+
def __init__(
50+
self,
51+
uart: UART,
52+
rst: DigitalInOut,
53+
ri: Optional[DigitalInOut] = None,
54+
debug: bool = False,
55+
) -> None:
4356
uart.baudrate = 4800
4457
super().__init__(uart, rst, ri, debug)
4558

46-
def set_baudrate(self, baudrate):
59+
def set_baudrate(self, baudrate: int) -> bool:
4760
"""Sets the FONA's UART baudrate."""
4861
if not self._send_check_reply(
4962
b"AT+IPREX=" + str(baudrate).encode(), reply=REPLY_OK
@@ -52,14 +65,14 @@ def set_baudrate(self, baudrate):
5265
return True
5366

5467
@property
55-
def gps(self):
68+
def gps(self) -> bool:
5669
"""Module's GPS status."""
5770
if not self._send_check_reply(b"AT+CGPS?", reply=b"+CGPS: 1,1"):
5871
return False
5972
return True
6073

6174
@gps.setter
62-
def gps(self, gps_on=False):
75+
def gps(self, gps_on: bool = False) -> bool:
6376
# check if GPS is already enabled
6477
if not self._send_parse_reply(b"AT+CGPS?", b"+CGPS: "):
6578
return False
@@ -77,25 +90,30 @@ def gps(self, gps_on=False):
7790
return True
7891

7992
@property
80-
def ue_system_info(self):
93+
def ue_system_info(self) -> bool:
8194
"""UE System status."""
8295
self._send_parse_reply(b"AT+CPSI?\r\n", b"+CPSI: ")
8396
if not self._buf == "GSM" or self._buf == "WCDMA": # 5.15
8497
return False
8598
return True
8699

87100
@property
88-
def local_ip(self):
101+
def local_ip(self) -> Optional[str]:
89102
"""Module's local IP address, None if not set."""
90103
if not self._send_parse_reply(b"AT+IPADDR", b"+IPADDR:"):
91104
return None
92105
return self._buf
93106

94107
# pylint: disable=too-many-return-statements
95-
def set_gprs(self, apn=None, enable=True):
108+
def set_gprs(
109+
self,
110+
apn: Optional[Tuple[str, Optional[str], Optional[str]]] = None,
111+
enable: bool = True,
112+
) -> bool:
96113
"""Configures and brings up GPRS.
97-
:param bool enable: Enables or disables GPRS.
98114
115+
:param tuple apn: APN configuration settings
116+
:param bool enable: Enables or disables GPRS.
99117
"""
100118
if enable:
101119
if not self._send_check_reply(b"AT+CGATT=1", reply=REPLY_OK, timeout=10000):
@@ -142,24 +160,25 @@ def set_gprs(self, apn=None, enable=True):
142160
### Socket API (TCP, UDP) ###
143161

144162
@property
145-
def tx_timeout(self):
163+
def tx_timeout(self) -> bool:
146164
"""CIPSEND timeout, in milliseconds."""
147165
self._read_line()
148166
if not self._send_parse_reply(b"AT+CIPTIMEOUT?", b"+CIPTIMEOUT:", idx=2):
149167
return False
150168
return True
151169

152170
@tx_timeout.setter
153-
def tx_timeout(self, timeout):
171+
def tx_timeout(self, timeout: int) -> bool:
154172
self._read_line()
155173
if not self._send_check_reply(
156174
b"AT+CIPTIMEOUT=" + str(timeout).encode(), reply=REPLY_OK
157175
):
158176
return False
159177
return True
160178

161-
def get_host_by_name(self, hostname):
179+
def get_host_by_name(self, hostname: str) -> Union[str, Literal[False]]:
162180
"""Converts a hostname to a 4-byte IP address.
181+
163182
:param str hostname: Domain name.
164183
"""
165184
self._read_line()
@@ -175,7 +194,7 @@ def get_host_by_name(self, hostname):
175194
return False
176195
return self._buf
177196

178-
def get_socket(self):
197+
def get_socket(self) -> int:
179198
"""Returns an unused socket."""
180199
if self._debug:
181200
print("*** Get socket")
@@ -198,14 +217,16 @@ def get_socket(self):
198217
print("Allocated socket #%d" % socket)
199218
return socket
200219

201-
def socket_connect(self, sock_num, dest, port, conn_mode=0):
220+
def socket_connect(
221+
self, sock_num: int, dest: str, port: int, conn_mode: int = 0
222+
) -> bool:
202223
"""Connects to a destination IP address or hostname.
203224
By default, we use conn_mode TCP_MODE but we may also use UDP_MODE.
225+
204226
:param int sock_num: Desired socket number
205227
:param str dest: Destination dest address.
206228
:param int port: Destination dest port.
207229
:param int conn_mode: Connection mode (TCP/UDP)
208-
209230
"""
210231
if self._debug:
211232
print(
@@ -236,8 +257,11 @@ def socket_connect(self, sock_num, dest, port, conn_mode=0):
236257
return False
237258
return True
238259

239-
def remote_ip(self, sock_num):
240-
"""Returns the IP address of the remote connection."""
260+
def remote_ip(self, sock_num: int) -> str:
261+
"""Returns the IP address of the remote connection.
262+
263+
:param int sock_num: Desired socket number
264+
"""
241265
self._read_line()
242266
assert (
243267
sock_num < FONA_MAX_SOCKETS
@@ -254,12 +278,12 @@ def remote_ip(self, sock_num):
254278
self._read_line() # eat the rest of '+CIPOPEN' responses
255279
return ip_addr
256280

257-
def socket_write(self, sock_num, buffer, timeout=120000):
281+
def socket_write(self, sock_num: int, buffer: bytes, timeout: int = 120000) -> bool:
258282
"""Writes len(buffer) bytes to the socket.
283+
259284
:param int sock_num: Desired socket number to write to.
260285
:param bytes buffer: Bytes to write to socket.
261286
:param int timeout: Socket write timeout, in milliseconds. Defaults to 120000ms.
262-
263287
"""
264288
self._read_line()
265289
assert (
@@ -295,10 +319,10 @@ def socket_write(self, sock_num, buffer, timeout=120000):
295319
return False
296320
return True
297321

298-
def socket_status(self, sock_num):
322+
def socket_status(self, sock_num: int) -> bool:
299323
"""Returns socket status, True if connected. False otherwise.
300-
:param int sock_num: Desired socket number.
301324
325+
:param int sock_num: Desired socket number.
302326
"""
303327
if not self._send_parse_reply(b"AT+CIPCLOSE?", b"+CIPCLOSE:", idx=sock_num):
304328
return False

0 commit comments

Comments
 (0)