Let Ethernet be switched off and on

Wi-Fi had a switch and wired did not, which left no way to take the cable down
without reaching for nmcli -- and this machine has two interfaces on one subnet,
so turning one off is a genuinely useful thing to be able to do.

Both halves are needed to turn it off. Disconnecting alone survives the session
but not a carrier event or a reboot, because NetworkManager brings an
autoconnecting device straight back; the switch sets the device's autoconnect
alongside it. The device property rather than the connection profile, so a
toggle here does not quietly rewrite a saved connection somebody expects to come
up at boot.

The first version built a trap door. It decided whether the switch was usable
from hasLink, which reads false while a device is merely disconnected even
though NetworkManager still reports the carrier as on -- so turning Ethernet off
made the switch disable itself, blame the cable, and offer no way back. A wired
device that exists can always be asked to come up; if there is really no cable
the attempt fails and says so, which is the honest failure. The word "off" is
used where the old text guessed "no cable", because nothing available here can
tell those apart.

Verified as a round trip against the real device, including that off stays off
through eight seconds rather than reconnecting a moment later, and that the
mechanism tested is the one the code uses -- the first test drove the connection
profile while the code drives the device, which are different things.

Claude-Session: https://claude.ai/code/session_01BRvzt4H8XXLPVH5MyYdk9L
This commit is contained in:
Gabriel Brown
2026-08-20 12:13:49 -04:00
parent 4cbab01ae2
commit c148bae4ac
3 changed files with 74 additions and 3 deletions
@@ -45,6 +45,40 @@ Singleton {
return fallback;
}
// Whether the wired connection is on, as a person means it: carrying
// traffic, and set to come back by itself.
readonly property bool wiredOn: root.wiredDevice !== null
&& root.wiredDevice.connected
// Deliberately NOT gated on hasLink. That property reads false while the
// device is merely disconnected -- NetworkManager still reports the carrier
// as on -- so using it to decide whether the switch works built a trap
// door: turning Ethernet off made the switch disable itself, claim "no
// cable", and leave no way to turn it back on. A wired device that exists
// can always be asked to come up; if there is genuinely no cable, the
// attempt fails and says so, which is the honest failure.
readonly property bool wiredAvailable: root.wiredDevice !== null
// Turning wired networking off means both halves. Disconnecting alone lasts
// about a second: NetworkManager sees a managed device with autoconnect set
// and immediately brings it back, so the switch would flip itself on again
// and read as broken.
function setWired(enabled: bool): void {
const device = root.wiredDevice;
if (!device)
return;
device.autoconnect = enabled;
if (enabled) {
// With autoconnect restored NetworkManager will usually bring it up
// on its own; asking directly makes it immediate rather than
// whenever the daemon next looks.
if (device.network)
device.network.connect();
} else {
device.disconnect();
}
}
readonly property var adapter: Bluetooth.defaultAdapter
readonly property bool wifiEnabled: Networking.wifiEnabled