Skip to content

Commit 69b6e97

Browse files
authored
Merge pull request #199 from anecdata/connect
Improve native wifi API compatibility
2 parents a48d516 + 42dc7f5 commit 69b6e97

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

adafruit_esp32spi/adafruit_esp32spi.py

+28-11
Original file line numberDiff line numberDiff line change
@@ -372,12 +372,12 @@ def MAC_address(self): # pylint: disable=invalid-name
372372
@property
373373
def MAC_address_actual(self): # pylint: disable=invalid-name
374374
"""A bytearray containing the actual MAC address of the ESP32"""
375-
if self._debug:
376-
print("MAC address")
377-
resp = self._send_command_get_response(_GET_MACADDR_CMD, [b"\xFF"])
378-
new_resp = bytearray(resp[0])
379-
new_resp = reversed(new_resp)
380-
return new_resp
375+
return bytearray(reversed(self.MAC_address))
376+
377+
@property
378+
def mac_address(self):
379+
"""A bytes containing the actual MAC address of the ESP32"""
380+
return bytes(self.MAC_address_actual)
381381

382382
def start_scan_networks(self):
383383
"""Begin a scan of visible access points. Follow up with a call
@@ -545,14 +545,19 @@ def ip_address(self):
545545
return self.network_data["ip_addr"]
546546

547547
@property
548-
def is_connected(self):
548+
def connected(self):
549549
"""Whether the ESP32 is connected to an access point"""
550550
try:
551551
return self.status == WL_CONNECTED
552552
except OSError:
553553
self.reset()
554554
return False
555555

556+
@property
557+
def is_connected(self):
558+
"""Whether the ESP32 is connected to an access point"""
559+
return self.connected
560+
556561
@property
557562
def ap_listening(self):
558563
"""Returns if the ESP32 is in access point mode and is listening for connections"""
@@ -568,10 +573,17 @@ def disconnect(self):
568573
if resp[0][0] != 1:
569574
raise OSError("Failed to disconnect")
570575

571-
def connect(self, secrets):
572-
"""Connect to an access point using a secrets dictionary
573-
that contains a 'ssid' and 'password' entry"""
574-
self.connect_AP(secrets["ssid"], secrets["password"])
576+
def connect(self, ssid, password=None, timeout=10):
577+
"""Connect to an access point with given name and password.
578+
579+
**Deprecated functionality:** If the first argument (``ssid``) is a ``dict``,
580+
assume it is a dictionary with entries for keys ``"ssid"`` and, optionally, ``"password"``.
581+
This mimics the previous signature for ``connect()``.
582+
This upward compatbility will be removed in a future release.
583+
"""
584+
if isinstance(ssid, dict): # secrets
585+
ssid, password = ssid["ssid"], ssid.get("password")
586+
self.connect_AP(ssid, password, timeout_s=timeout)
575587

576588
def connect_AP(self, ssid, password, timeout_s=10): # pylint: disable=invalid-name
577589
"""Connect to an access point with given name and password.
@@ -647,6 +659,11 @@ def create_AP(
647659
raise ConnectionError("Failed to create AP", ssid)
648660
raise OSError("Unknown error 0x%02x" % stat)
649661

662+
@property
663+
def ipv4_address(self):
664+
"""IP address of the station when connected to an access point."""
665+
return self.pretty_ip(self.ip_address)
666+
650667
def pretty_ip(self, ip): # pylint: disable=no-self-use, invalid-name
651668
"""Converts a bytearray IP address to a dotted-quad string for printing"""
652669
return "%d.%d.%d.%d" % (ip[0], ip[1], ip[2], ip[3])

0 commit comments

Comments
 (0)