I was able to find the connection.
bpy.context.selected_objects[0].animation_data.drivers[1].data_path
find the name of the variable or change the type
bpy.context.selected_objects[0].animation_data.drivers[1].driver.variables[0].name
bpy.context.selected_objects[0].animation_data.drivers[1].driver.variables[0].type
which blender say it’s DriverVariable.type DriverVariable.name
but I can’t figure out how to change the name of the target just under those parameters, for the name of the object controling the variable, it says DriverTarget.id, but I can’t find the correct function to change it.
Here is a code snippet. In the code belo speaker is a speaker object. channel is the name of a custom prop on the speaker and also used as a variable name.
var = driver.variables.get(channel)
if var is None:
var = driver.variables.new()
var.type = "SINGLE_PROP"
var.name = channel
target = var.targets[0]
target.id_type = "SPEAKER"
target.id = speaker.id_data
target.data_path = '["%s"]' % channel