Skip to content

Commit d8f4ad2

Browse files
committed
Added an enum-like class for the WiFi connection type and refactored the code
1 parent 7386534 commit d8f4ad2

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

adafruit_esp32spi/adafruit_esp32spi_wifimanager.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,31 @@
3434
from adafruit_esp32spi import adafruit_esp32spi
3535
import adafruit_esp32spi.adafruit_esp32spi_requests as requests
3636

37+
class WiFiConnType: # pylint: disable=too-few-public-methods
38+
"""An enum-like class representing the different types of WiFi connections
39+
that can be made. The values can be referenced like ``WiFiConnType.normal``.
40+
Possible values are
41+
- ``ThermocoupleType.normal``
42+
- ``ThermocoupleType.enterprise``
43+
"""
44+
# pylint: disable=invalid-name
45+
normal = 1
46+
enterprise = 2
47+
3748
class ESPSPI_WiFiManager:
3849
"""
3950
A class to help manage the Wifi connection
4051
"""
41-
def __init__(self, esp, secrets, status_pixel=None, attempts=2, con_type=1):
52+
def __init__(self, esp, secrets, status_pixel=None, attempts=2, wificonntype=WiFiConnType.normal):
4253
"""
4354
:param ESP_SPIcontrol esp: The ESP object we are using
4455
:param dict secrets: The WiFi and Adafruit IO secrets dict (See examples)
4556
:param status_pixel: (Optional) The pixel device - A NeoPixel or DotStar (default=None)
4657
:type status_pixel: NeoPixel or DotStar
4758
:param int attempts: (Optional) Failed attempts before resetting the ESP32 (default=2)
48-
:param int con_type: (Optional) Type of WiFi connection to make: normal=1, WPA2 Enterprise=2
59+
:param const con_type: (Optional) Type of WiFi connection: normal=1, WPA2 Enterprise=2
60+
:param ~adafruit_esp32spi_wifimanager.WiFiConnType wificonntype: The type of WiFi \
61+
connection to make. The default is "normal".
4962
"""
5063
# Read the settings
5164
self._esp = esp
@@ -57,7 +70,7 @@ def __init__(self, esp, secrets, status_pixel=None, attempts=2, con_type=1):
5770
self.ent_user = secrets['ent_user']
5871
self.ent_passwd = secrets['ent_passwd']
5972
self.attempts = attempts
60-
self.con_type = con_type
73+
self.wificonntype = wificonntype
6174
requests.set_interface(self._esp)
6275
self.statuspix = status_pixel
6376
self.pixel_status(0)
@@ -82,7 +95,7 @@ def connect(self):
8295
for access_pt in self._esp.scan_networks():
8396
print("\t%s\t\tRSSI: %d" % (str(access_pt['ssid'], 'utf-8'), access_pt['rssi']))
8497
failure_count = 0
85-
if self.con_type == 1:
98+
if self.wificonntype == WiFiConnType.normal:
8699
while not self._esp.is_connected:
87100
try:
88101
if self.debug:
@@ -98,7 +111,7 @@ def connect(self):
98111
failure_count = 0
99112
self.reset()
100113
continue
101-
elif self.con_type == 2:
114+
elif self.wificonntype == WiFiConnType.enterprise:
102115
self._esp.wifi_set_network(bytes(self.ent_ssid, 'utf-8'))
103116
self._esp.wifi_set_entidentity(bytes(self.ent_ident, 'utf-8'))
104117
self._esp.wifi_set_entusername(bytes(self.ent_user, 'utf-8'))

0 commit comments

Comments
 (0)