NetworkManager: Set current connection of device as metered

I really hope that this isn't the best answer: it seems convoluted in the simple case, and even more so if allowing for a binary SSID. Anyways, here goes:

Get device's current connection

nmcli -t -f GENERAL.CONNECTION --mode tabular device show $DEVICE | head -n1

-t is required as there is a space appended at the end otherwise (!?).

Show current metered status

nmcli -f connection.metered connection show $CONNECTION

Where $CONNECTION is the string returned by the previous command.

Change metered status

The valid statuses are yes, no, and unknown. unknown is the default, which will do the guessing based on things like DHCP option ANDROID_METERED (reference).

Example: set $CONNECTION to be metered:

nmcli connection modify $CONNECTION connection.metered yes

Allowing for binary SSIDs

To do this "right" (allowing for 32 arbitrary octets in the SSID), you'll need to use the device's GENERAL.CON-PATH:

nmcli -t -f GENERAL.CON-PATH --mode tabular device show DEVICE | tail -n1

This will return a string like: /org/freedesktop/NetworkManager/ActiveConnection/39

Then use this path to get connection's GENERAL.CON-PATH. NB: this has a different value (a device's CON-PATH == connection's GENERAL.DBUS-PATH)

nmcli -t -f GENERAL.CON-PATH -m tabular connection show apath /org/freedesktop/NetworkManager/ActiveConnection/39

This will return a string like: /org/freedesktop/NetworkManager/Settings/5 (note no ActiveConnection). This can be then used to modify the connection:

nmcli connection modify /org/freedesktop/NetworkManager/Settings/<NUMBER> connection.metered <VALUE>