Faced the same case. If someone is looking for a complete solution here is a sample for adding a PostgreSQL:
settings = QSettings()
settings.setValue('PostgreSQL/connections/your_connection_name/host', 'host_name')
settings.setValue('PostgreSQL/connections/your_connection_name/database', 'database_name')
settings.setValue('PostgreSQL/connections/your_connection_name/username', 'user_name')
settings.setValue('PostgreSQL/connections/your_connection_name/password', 'password_name')
settings.setValue('PostgreSQL/connections/your_connection_name/port', port_number)
settings.setValue('PostgreSQL/connections/your_connection_name/allowGeometrylessTables', 'false')
settings.setValue('PostgreSQL/connections/your_connection_name/authcfg', '')
settings.setValue('PostgreSQL/connections/your_connection_name/dontResolveType', 'false')
settings.setValue('PostgreSQL/connections/your_connection_name/estimatedMetadata', 'false')
settings.setValue('PostgreSQL/connections/your_connection_name/geometryColumnsOnly', 'false')
settings.setValue('PostgreSQL/connections/your_connection_name/projectsInDatabase', 'false')
settings.setValue('PostgreSQL/connections/your_connection_name/publicOnly', 'false')
settings.setValue('PostgreSQL/connections/your_connection_name/savePassword', 'true')
settings.setValue('PostgreSQL/connections/your_connection_name/saveUsername', 'true')
settings.setValue('PostgreSQL/connections/your_connection_name/service', '')
settings.setValue('PostgreSQL/connections/your_connection_name/sslmode', 'SslAllow')
iface.mainWindow().findChildren(QWidget, 'Browser')[0].refresh() # if connection is needed to be displayed immediately in browser
Params after port are from my connection setting and not set by default.