From 9b510b5eb70afb8836a557aeea323be1d1ec4f90 Mon Sep 17 00:00:00 2001 From: Angel-Padilla Date: Mon, 25 May 2026 14:31:27 -0600 Subject: [PATCH] feature: allow DeviceControl.wifi_connect to accept None as password This allows the package to call the `nmcli` util without passing a password as parameter, currently passing `None` would create the following command `sudo nmcli (--wait)? device wifi connect password` which is an invalid command as it is missing the password argument, with this change the command will be `sudo nmcli (--wait)? device wifi connect ` allowing connection with open networks and networks already known by `nmcli` --- nmcli/_device.py | 6 ++++-- tests/test_device.py | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/nmcli/_device.py b/nmcli/_device.py index ea60321..79216ba 100644 --- a/nmcli/_device.py +++ b/nmcli/_device.py @@ -182,11 +182,13 @@ def wifi(self, ifname: str = None, rescan: bool = None) -> List[DeviceWifi]: def wifi_connect(self, ssid: str, - password: str, + password: str | None, ifname: str = None, wait: int = None) -> None: cmd = add_wait_option_if_needed( - wait) + ['device', 'wifi', 'connect', ssid, 'password', password] + wait) + ['device', 'wifi', 'connect', ssid] + if password is not None: + cmd += ['password', password] if ifname is not None: cmd += ['ifname', ifname] r = self._syscmd.nmcli(cmd) diff --git a/tests/test_device.py b/tests/test_device.py index 18ff45c..2b1576d 100644 --- a/tests/test_device.py +++ b/tests/test_device.py @@ -309,6 +309,24 @@ def test_wifi_connect(): '--wait', '10', 'device', 'wifi', 'connect', ssid, 'password', password] +def test_wifi_connect_without_password(): + s = DummySystemCommand() + device = DeviceControl(s) + ssid = 'Open AP' + ifname = 'wlan0' + + device.wifi_connect(ssid, None) + assert s.passed_parameters == ['device', 'wifi', 'connect', ssid] + + device.wifi_connect(ssid, None, ifname) + assert s.passed_parameters == [ + 'device', 'wifi', 'connect', ssid, 'ifname', ifname] + + device.wifi_connect(ssid, None, wait=10) + assert s.passed_parameters == [ + '--wait', '10', 'device', 'wifi', 'connect', ssid] + + def test_wifi_connect_when_connection_activate_failed(): s = DummySystemCommand( '''Error: Connection activation failed: (7) Secrets were required, but not provided.