IFACE = "wlx08bd43beb941"
AP_NAME = "DJ_2.4G"

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
gi.require_version('NetworkManager', '1.0')
from gi.repository import NetworkManager
gi.require_version('NMClient', '1.0')
from gi.repository import NMClient
gi.require_version('NMA', '1.0')
from gi.repository import NMA

def connect(client, conn, path, error, user_data):
    print(conn.get_state())
    print(error)
    Gtk.main_quit()

def response(dialog, response):
    if response == Gtk.ResponseType.OK:
        print('OK')
        conn, dev, ap = dialog.get_connection()
        print(conn)
        print(dev)
        print(ap)
        client.add_and_activate_connection(conn, dev, None, connect, None)
    elif response == Gtk.ResponseType.CANCEL:
        print('CANCEL')
        Gtk.main_quit()
    elif response == Gtk.ResponseType.DELETE_EVENT:
        print('CLOSE')
        Gtk.main_quit()
    else:
        print(response)
        Gtk.main_quit()

def find_ap(device, ssid):
    for ap in device.get_access_points():
        if ap.get_ssid() == ssid:
            return ap
    return None

client = NMClient.Client()

device = client.get_device_by_iface(IFACE)
ap = find_ap(device, AP_NAME)
print(ap.get_ssid(), ap.get_bssid(), ap)

connection = NetworkManager.Connection()

s_con = NetworkManager.SettingConnection()
s_con.set_property('uuid', NetworkManager.utils_uuid_generate())
connection.add_setting (s_con);

s_wifi = NetworkManager.SettingWireless()
#s_wifi.set_property('ssid', ap.get_property('ssid'))
connection.add_setting (s_wifi);

dialog = NMA.WifiDialog.new(
    client, connection, device, ap, False
)
dialog.connect("response", response)
dialog.show()
Gtk.main()
