Improve Wi-Fi recovery and reconnect handling

This commit is contained in:
2026-04-25 15:00:45 -04:00
parent 18cc9b6bab
commit 94952c7136
4 changed files with 99 additions and 12 deletions

View File

@@ -92,6 +92,23 @@ def restart_adapter(interface_name: str) -> None:
run_command(["netsh", "interface", "set", "interface", interface_name, "enable"])
def set_interface_enabled(interface_name: str, enabled: bool = True) -> None:
state = "enable" if enabled else "disable"
run_command(["netsh", "interface", "set", "interface", interface_name, state])
def set_wlan_autoconfig(interface_name: str, enabled: bool = True) -> None:
state = "yes" if enabled else "no"
run_command(["netsh", "wlan", "set", "autoconfig", f"enabled={state}", f"interface={interface_name}"])
def request_wifi_scan(interface_name: str | None = None) -> None:
command = ["netsh", "wlan", "show", "networks"]
if interface_name:
command.append(f"interface={interface_name}")
run_command(command)
def disconnect_wifi(interface_name: str | None = None) -> None:
command = ["netsh", "wlan", "disconnect"]
if interface_name:
@@ -106,13 +123,9 @@ def connect_wifi(
ssid: str | None = None,
auto_create_open_profile: bool = False,
) -> None:
command = ["netsh", "wlan", "connect", f"name={profile_name}"]
if ssid:
command.append(f"ssid={ssid}")
if interface_name:
command.append(f"interface={interface_name}")
commands = _build_connect_commands(profile_name, interface_name, ssid)
try:
run_command(command)
_run_first_successful_command(commands)
except WifiCommandError as exc:
if auto_create_open_profile and _is_missing_profile_error(str(exc), profile_name):
create_open_wifi_profile(
@@ -120,7 +133,7 @@ def connect_wifi(
ssid=ssid or profile_name,
interface_name=interface_name,
)
run_command(command)
_run_first_successful_command(commands)
return
if _is_missing_profile_error(str(exc), profile_name):
available_profiles = list_wifi_profiles(interface_name)
@@ -133,6 +146,40 @@ def connect_wifi(
raise
def _build_connect_commands(
profile_name: str,
interface_name: str | None,
ssid: str | None,
) -> list[list[str]]:
variants: list[list[str]] = []
def add_variant(include_ssid: bool, include_interface: bool) -> None:
command = ["netsh", "wlan", "connect", f"name={profile_name}"]
if include_ssid and ssid:
command.append(f"ssid={ssid}")
if include_interface and interface_name:
command.append(f"interface={interface_name}")
if command not in variants:
variants.append(command)
add_variant(include_ssid=True, include_interface=True)
add_variant(include_ssid=False, include_interface=True)
add_variant(include_ssid=True, include_interface=False)
add_variant(include_ssid=False, include_interface=False)
return variants
def _run_first_successful_command(commands: list[list[str]]) -> None:
errors: list[str] = []
for command in commands:
try:
run_command(command)
return
except WifiCommandError as exc:
errors.append(f"{' '.join(command)} -> {exc}")
raise WifiCommandError("; ".join(errors))
def list_wifi_profiles(interface_name: str | None = None) -> list[str]:
command = ["netsh", "wlan", "show", "profiles"]
if interface_name: