Skip to content

Commit 11c5467

Browse files
committed
Add forget all option for removing connections
1 parent ab7ac63 commit 11c5467

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,16 @@ Check whether your device is connected to a Wi-Fi hotspot and whether there is i
6565

6666
### http://your-device:9090/v1/forget
6767

68-
Disconnect from the access point you earlier connected to with this app and forget the connection so it will not automatically reconnect on next launch of your device.
69-
#### GET
68+
Disconnect from an access point and forget the connection so it will not automatically reconnect on next launch of your device.
69+
70+
When passing `"all_networks": false` this endpoint will only touch Wi-Fi connections set up using this app. If you pass `"all_networks": true` it will remove all Wi-Fi connections from the device. This is useful if you have set up a Wi-Fi connection with another app and need to clear out connections to allow Py-WiFi-Connect to manage connections.
71+
72+
#### POST
73+
````
74+
{
75+
"all_networks": false
76+
}
77+
````
7078

7179
#### Response status 202
7280
Requests are returned immediately and then the process is executed. Otherwise users would be disconnected before they were able to receive the returned response.

src/common/wifi.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ def connect(conn_type=config.type_hotspot,
110110

111111
# Connect
112112
NetworkManager.NetworkManager.ActivateConnection(conn, dev, "/")
113-
logger.info("Activated connection.")
114113

115114
# Wait for ADDRCONF(NETDEV_CHANGE): wlan0: link becomes ready
116115
logger.info("Waiting for connection to become active...")
@@ -122,7 +121,7 @@ def connect(conn_type=config.type_hotspot,
122121
break
123122

124123
if dev.State == NetworkManager.NM_DEVICE_STATE_ACTIVATED:
125-
logger.info("Connection is live.")
124+
logger.info("Connected.")
126125
return True
127126
# If the current attempt is not already a hotspot attempt
128127
elif conn_type != config.type_hotspot:
@@ -141,16 +140,26 @@ def connect(conn_type=config.type_hotspot,
141140
raise WifiConnectionFailed
142141

143142

144-
def forget(create_new_hotspot=False):
143+
def forget(create_new_hotspot=False, all_networks=False):
145144
# Find and delete the hotspot connection
146145
try:
147146
connections = NetworkManager.Settings.ListConnections()
148-
connections = dict([(x.GetSettings()['connection']['id'], x)
149-
for x in connections])
150147

151-
if config.ap_name in connections:
152-
conn = connections[config.ap_name]
153-
conn.Delete()
148+
if all_networks:
149+
for connection in connections:
150+
if connection.GetSettings()["connection"]["type"] \
151+
== "802-11-wireless":
152+
# Delete the identified connection
153+
network_id = connection.GetSettings()["connection"]["id"]
154+
connection.Delete()
155+
logger.info(f"Deleted connection: {network_id}")
156+
else:
157+
connection_ids = \
158+
dict([(x.GetSettings()['connection']['id'], x)
159+
for x in connections])
160+
if config.ap_name in connection_ids:
161+
connection_ids[config.ap_name].Delete()
162+
logger.info(f"Deleted connection: {config.ap_name}")
154163

155164
if create_new_hotspot:
156165
refresh_networks()

src/resources/wifi_routes.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,26 @@ def get(self):
4343

4444

4545
class wifi_forget(Resource):
46-
def get(self):
46+
def post(self):
4747
# If the device is not connected to a wifi network
4848
if not check_wifi_status():
4949
return {'message': 'Device is already disconnected.'}, 409
5050

51+
# Check the all_networks boolean is valid
52+
if (not request.get_json() or
53+
'all_networks' not in request.get_json()
54+
or type(request.get_json()['all_networks']) is not bool):
55+
return {'message': "all_networks boolean missing or is not "
56+
"a boolean."}, 202
57+
5158
# Use threading so the response can be returned before the user is
5259
# disconnected.
5360
wifi_forget_thread = threading.Thread(target=forget,
5461
kwargs={'create_new_hotspot':
55-
True})
62+
True,
63+
'all_networks':
64+
request.get_json()
65+
['all_networks']})
5666

5767
wifi_forget_thread.start()
5868

0 commit comments

Comments
 (0)