From 8349d77b4ecd9f313146cb61197579915b2b687b Mon Sep 17 00:00:00 2001 From: anecdata <16617689+anecdata@users.noreply.github.com> Date: Tue, 30 Apr 2024 14:07:23 -0500 Subject: [PATCH 1/6] Fully deprecate `secrets` and improve native wifi API compatibility. --- adafruit_esp32spi/adafruit_esp32spi.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/adafruit_esp32spi/adafruit_esp32spi.py b/adafruit_esp32spi/adafruit_esp32spi.py index 4c3fc5f..dde0784 100644 --- a/adafruit_esp32spi/adafruit_esp32spi.py +++ b/adafruit_esp32spi/adafruit_esp32spi.py @@ -545,7 +545,7 @@ def ip_address(self): return self.network_data["ip_addr"] @property - def is_connected(self): + def connected(self): """Whether the ESP32 is connected to an access point""" try: return self.status == WL_CONNECTED @@ -553,6 +553,11 @@ def is_connected(self): self.reset() return False + @property + def is_connected(self): + """Whether the ESP32 is connected to an access point""" + return self.connected + @property def ap_listening(self): """Returns if the ESP32 is in access point mode and is listening for connections""" @@ -568,10 +573,9 @@ def disconnect(self): if resp[0][0] != 1: raise OSError("Failed to disconnect") - def connect(self, secrets): - """Connect to an access point using a secrets dictionary - that contains a 'ssid' and 'password' entry""" - self.connect_AP(secrets["ssid"], secrets["password"]) + def connect(self, ssid, password, timeout=10): + """Connect to an access point with given name and password.""" + self.connect_AP(ssid, password, timeout_s=timeout) def connect_AP(self, ssid, password, timeout_s=10): # pylint: disable=invalid-name """Connect to an access point with given name and password. @@ -647,6 +651,11 @@ def create_AP( raise ConnectionError("Failed to create AP", ssid) raise OSError("Unknown error 0x%02x" % stat) + @property + def ipv4_address(self): + """IP address of the station when connected to an access point.""" + return self.pretty_ip(self.ip_address) + def pretty_ip(self, ip): # pylint: disable=no-self-use, invalid-name """Converts a bytearray IP address to a dotted-quad string for printing""" return "%d.%d.%d.%d" % (ip[0], ip[1], ip[2], ip[3]) From a589e81ea4c64d92ac7401b20b5ffc250f2f38b6 Mon Sep 17 00:00:00 2001 From: anecdata <16617689+anecdata@users.noreply.github.com> Date: Thu, 2 May 2024 12:42:33 -0500 Subject: [PATCH 2/6] ad `mac_address` and fix `connect` per discussion. `connect` with `secrets` dict, `ssid, password`, and `ssid`-only (open) all work as before. --- adafruit_esp32spi/adafruit_esp32spi.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/adafruit_esp32spi/adafruit_esp32spi.py b/adafruit_esp32spi/adafruit_esp32spi.py index dde0784..895454f 100644 --- a/adafruit_esp32spi/adafruit_esp32spi.py +++ b/adafruit_esp32spi/adafruit_esp32spi.py @@ -372,12 +372,12 @@ def MAC_address(self): # pylint: disable=invalid-name @property def MAC_address_actual(self): # pylint: disable=invalid-name """A bytearray containing the actual MAC address of the ESP32""" - if self._debug: - print("MAC address") - resp = self._send_command_get_response(_GET_MACADDR_CMD, [b"\xFF"]) - new_resp = bytearray(resp[0]) - new_resp = reversed(new_resp) - return new_resp + return bytearray(reversed(self.MAC_address)) + + @property + def mac_address(self): + """A bytes containing the actual MAC address of the ESP32""" + return bytes(reversed(self.MAC_address)) def start_scan_networks(self): """Begin a scan of visible access points. Follow up with a call @@ -573,8 +573,10 @@ def disconnect(self): if resp[0][0] != 1: raise OSError("Failed to disconnect") - def connect(self, ssid, password, timeout=10): + def connect(self, ssid, password=None, timeout=10): """Connect to an access point with given name and password.""" + if isinstance(ssid, dict): # secrets + ssid, password = ssid["ssid"], ssid["password"] self.connect_AP(ssid, password, timeout_s=timeout) def connect_AP(self, ssid, password, timeout_s=10): # pylint: disable=invalid-name From cfb61f5fe7e6b4ce3e455f3dc6e5ec7933f0fa5d Mon Sep 17 00:00:00 2001 From: anecdata <16617689+anecdata@users.noreply.github.com> Date: Thu, 2 May 2024 12:47:41 -0500 Subject: [PATCH 3/6] Update adafruit_esp32spi/adafruit_esp32spi.py Co-authored-by: Justin Myers --- adafruit_esp32spi/adafruit_esp32spi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_esp32spi/adafruit_esp32spi.py b/adafruit_esp32spi/adafruit_esp32spi.py index 895454f..552c1bd 100644 --- a/adafruit_esp32spi/adafruit_esp32spi.py +++ b/adafruit_esp32spi/adafruit_esp32spi.py @@ -377,7 +377,7 @@ def MAC_address_actual(self): # pylint: disable=invalid-name @property def mac_address(self): """A bytes containing the actual MAC address of the ESP32""" - return bytes(reversed(self.MAC_address)) + return bytes(self.MAC_address_actual) def start_scan_networks(self): """Begin a scan of visible access points. Follow up with a call From e69c95014e11c60760593d58931701352e489f57 Mon Sep 17 00:00:00 2001 From: anecdata <16617689+anecdata@users.noreply.github.com> Date: Thu, 2 May 2024 14:17:51 -0500 Subject: [PATCH 4/6] Update adafruit_esp32spi/adafruit_esp32spi.py Co-authored-by: Justin Myers --- adafruit_esp32spi/adafruit_esp32spi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_esp32spi/adafruit_esp32spi.py b/adafruit_esp32spi/adafruit_esp32spi.py index 552c1bd..e1219d5 100644 --- a/adafruit_esp32spi/adafruit_esp32spi.py +++ b/adafruit_esp32spi/adafruit_esp32spi.py @@ -576,7 +576,7 @@ def disconnect(self): def connect(self, ssid, password=None, timeout=10): """Connect to an access point with given name and password.""" if isinstance(ssid, dict): # secrets - ssid, password = ssid["ssid"], ssid["password"] + ssid, password = ssid["ssid"], ssid.get("password") self.connect_AP(ssid, password, timeout_s=timeout) def connect_AP(self, ssid, password, timeout_s=10): # pylint: disable=invalid-name From 5a640445f4f0616382d45f62c4e5e7a368efec82 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 2 May 2024 22:03:06 -0400 Subject: [PATCH 5/6] add documentation for `.connect(secrets_dict)`. --- adafruit_esp32spi/adafruit_esp32spi.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/adafruit_esp32spi/adafruit_esp32spi.py b/adafruit_esp32spi/adafruit_esp32spi.py index e1219d5..fd5b579 100644 --- a/adafruit_esp32spi/adafruit_esp32spi.py +++ b/adafruit_esp32spi/adafruit_esp32spi.py @@ -574,7 +574,13 @@ def disconnect(self): raise OSError("Failed to disconnect") def connect(self, ssid, password=None, timeout=10): - """Connect to an access point with given name and password.""" + """Connect to an access point with given name and password. + + **Deprecated functionality:** If the first argument (``ssid``) is a ``dict``, + assume it is a dictionary with entries for keys ``"ssid"`` and, optionally, ``"password"``. + This mimics the previous signature for ``connect()``. + This upward compatbility will be removed in a future release. + """ if isinstance(ssid, dict): # secrets ssid, password = ssid["ssid"], ssid.get("password") self.connect_AP(ssid, password, timeout_s=timeout) From 42dc7f5546ffeb0923fb025856c46b2f42d61cb7 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 2 May 2024 22:06:50 -0400 Subject: [PATCH 6/6] remove extraneous whitespace --- adafruit_esp32spi/adafruit_esp32spi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_esp32spi/adafruit_esp32spi.py b/adafruit_esp32spi/adafruit_esp32spi.py index fd5b579..a124323 100644 --- a/adafruit_esp32spi/adafruit_esp32spi.py +++ b/adafruit_esp32spi/adafruit_esp32spi.py @@ -575,7 +575,7 @@ def disconnect(self): def connect(self, ssid, password=None, timeout=10): """Connect to an access point with given name and password. - + **Deprecated functionality:** If the first argument (``ssid``) is a ``dict``, assume it is a dictionary with entries for keys ``"ssid"`` and, optionally, ``"password"``. This mimics the previous signature for ``connect()``.